-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_predictions.py
More file actions
137 lines (114 loc) · 5.09 KB
/
plot_predictions.py
File metadata and controls
137 lines (114 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import torch
from utils import *
import meshio
def save_prediction_results(data, mesh, model, model_dir, save_path=None):
"""
Takes las input data and trained model to plot prediction results
:param las_dir: directory of las data
:param has_dir: directory of has data
:param model: initialized model
:param model_dir: directory of trained model
:param save_path: directory to save the plot
"""
device = torch.device('cpu')
checkpoint_load(model, model_dir)
model = model.to(device)
model.eval()
(data_l, data_h) = data
data_l = data_l.to(device)
data_h = data_h.to(device)
has = model(data_l, data_h)
prediction = has.detach().numpy()
low_res = data_l.x.detach().numpy()
high_res = data_h.x.detach().numpy()
u_has_star = [prediction[:, 0], prediction[:, 1]]
p_has_star = prediction[:, 2]
u_has = [high_res[:, 0], high_res[:, 1]]
p_has = high_res[:, 2]
u_las = [low_res[:, 0], low_res[:, 1]]
p_las = low_res[:, 2]
# np.savez(save_path, u_has_star=u_has_star, p_has_star=p_has_star, u_has=u_has, p_has=p_has, u_las=u_las, p_las=p_las)
# plot contour map of u_has_star and u_has based on mesh
fig, ax = plt.subplots(1, 1, figsize=(20, 10))
ax.set_title("u_has_star")
ax.set_xlabel("x")
ax.set_ylabel("y")
# reconstruct cells using edges
u_has_star_absolute = np.sqrt(u_has_star[0] ** 2 + u_has_star[1] ** 2)
points = mesh.points
cells = mesh.get_cells_type("triangle")
triang = tri.Triangulation(points[:, 0], points[:, 1], cells)
contour = ax.tricontourf(triang, u_has_star_absolute, 100, cmap="jet")
fig.colorbar(contour, ax=ax, label="absolute velocity - prediction")
# save figure
if save_path is not None:
fig.savefig(os.path.join(save_path, "prediction_u.png"))
# plot contour map of u_has based on mesh alongside
fig, ax = plt.subplots(1, 1, figsize=(20, 10))
ax.set_title("u_has")
ax.set_xlabel("x")
ax.set_ylabel("y")
# reconstruct cells using edges
u_has_absolute = np.sqrt(u_has[0] ** 2 + u_has[1] ** 2)
points = mesh.points
contour = ax.tricontourf(triang, u_has_absolute, 100, cmap="jet")
fig.colorbar(contour, ax=ax, label="absolute velocity - ground truth")
# save figure
if save_path is not None:
fig.savefig(os.path.join(save_path, "ground_truth_u.png"))
# plot quiver map of u_has_star and u_has based on mesh
fig, ax = plt.subplots(1, 1, figsize=(20, 10))
ax.set_title("u_has_star")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.quiver(points[:, 0], points[:, 1], u_has_star[0], u_has_star[1], cmap='jet', scale=100)
# save figure
if save_path is not None:
fig.savefig(os.path.join(save_path, "prediction_quiver_u.png"))
# plot quiver map of u_has based on mesh alongside
fig, ax = plt.subplots(1, 1, figsize=(20, 10))
ax.set_title("u_has")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.quiver(points[:, 0], points[:, 1], u_has[0], u_has[1], cmap='jet', scale=100)
# save figure
if save_path is not None:
fig.savefig(os.path.join(save_path, "ground_truth_quiver_u.png"))
# plt.show()
# plot pressure map of p_has_star and p_has based on mesh
fig, ax = plt.subplots(1, 1, figsize=(20, 10))
ax.set_title("p_has_star")
ax.set_xlabel("x")
ax.set_ylabel("y")
contour = ax.tricontourf(triang, p_has_star, 100, cmap="jet")
fig.colorbar(contour, ax=ax, label="pressure - prediction")
# save figure
if save_path is not None:
fig.savefig(os.path.join(save_path, "prediction_p.png"))
# plot pressure map of p_has based on mesh alongside
fig, ax = plt.subplots(1, 1, figsize=(20, 10))
ax.set_title("p_has")
ax.set_xlabel("x")
ax.set_ylabel("y")
contour = ax.tricontourf(triang, p_has, 100, cmap="jet")
fig.colorbar(contour, ax=ax, label="pressure - ground truth")
# save figure
if save_path is not None:
fig.savefig(os.path.join(save_path, "ground_truth_p.png"))
plt.show()
if __name__ == "__main__":
train_config = load_yaml("config/train_config.yaml")
model = initialize_model(in_channel=train_config["in_channel"], out_channel=train_config["out_channel"], type=train_config["model_name"], layers=None, num_filters=None)
dataset = initialize_dataset(dataset="MegaFlow2D", split_scheme=train_config["split_scheme"], dir=train_config["data_dir"], transform=train_config["transform"], split_ratio=train_config["split_ratio"], pre_transform=None)
data_name, data = dataset.get_eval(203800)
str1, str2, str3 = data_name.split("_")
mesh_name = str1 + "_" + str2
print(data_name)
mesh = meshio.read("D:/Work/research/train/plot_mesh_structure/{}.msh".format(mesh_name))
save_path = "D:/Work/research/train/prediction/plots/prediction_result_{}_model_020".format(data_name)
os.makedirs(save_path, exist_ok=True)
save_prediction_results(data, mesh, model, model_dir="D:/Work/research/train/checkpoints/2023-04-04_15-27/checkpoint-000020.pth", save_path=save_path)