-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test_Model.py
140 lines (110 loc) · 5.02 KB
/
Test_Model.py
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
138
139
140
import math
import os
import os
from pandas import DataFrame
from scipy.stats import pearsonr, stats, spearmanr
import seaborn as sns
import torch
from tqdm import tqdm
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import Set_seed
from MHG_Dataset import Dataset
import Config as config
transform_test = None
def pearson(x, y):
return pearsonr(x, y)
def test_npys(dataset):
model = torch.load(config.predict_model).to(config.device)
model.eval()
wind_error = 0
RMW_error = 0
pressure_error = 0
wind_bias_sum = 0
RMW_bias_sum = 0
pressure_bias_sum = 0
wind_RMSE_sum = 0
RMW_RMSE_sum = 0
pressure_RMSE_sum = 0
item = 0
wind_output_list = []
wind_label_list = []
RMW_output_list = []
RMW_label_list = []
wind_MAE_list_mine = []
RMW_MAE_list_mine = []
with torch.no_grad():
for batch, data in enumerate(tqdm(dataset)):
inputs = data["npy"]
inputs = inputs.to(config.device)
diff_inputs = data["diff_npy"]
diff_inputs = diff_inputs.to(config.device)
wind_label = data["wind"].to(config.device)
RMW_label = data["RMW"].to(config.device)
pressure_label = data["pressure"].to(config.device)
pre = data['pre'].to(config.device)
pre = pre.to(torch.float32)
pre = pre.reshape(pre.shape[0], 1)
t = data['occur_t'].to(config.device)
t = t.to(torch.float32)
t = t.reshape(t.shape[0], 1)
lat = data['lat'].to(config.device)
lat = lat.to(torch.float32)
lat = lat.reshape(lat.shape[0], 1)
lon = data['lon'].to(config.device)
lon = lon.to(torch.float32)
lon = lon.reshape(lon.shape[0], 1)
wind, RMW = model(inputs, diff_inputs, pre, lat, lon, t)
wind = wind[:, 0]
RMW = RMW[:, 0]
# Comparison error between label and estimated result after denormalization.
wind_label_re = wind_label.cpu().detach().numpy() * (170 - 19) + 19
wind_re = wind.cpu().detach().numpy() * (170 - 19) + 19
wind_error = wind_error + np.sum(np.abs(wind_re - wind_label_re))
wind_bias_sum = wind_bias_sum + np.sum(wind_re - wind_label_re)
wind_RMSE_sum = wind_RMSE_sum + np.sum((wind_re - wind_label_re) * (wind_re - wind_label_re))
RMW_label_re = RMW_label.cpu().detach().numpy() * (200 - 5) + 5
RMW_re = RMW.cpu().detach().numpy() * (200 - 5) + 5
RMW_error = RMW_error + np.sum(np.abs(RMW_re - RMW_label_re))
RMW_bias_sum = RMW_bias_sum + np.sum(RMW_re - RMW_label_re)
RMW_RMSE_sum = RMW_RMSE_sum + np.sum((RMW_re - RMW_label_re) * (RMW_re - RMW_label_re))
item += 1
for i in range(0, len(wind_re)):
wind_output_list.append(wind_re[i])
wind_label_list.append(wind_label_re[i])
RMW_output_list.append(RMW_re[i])
RMW_label_list.append(RMW_label_re[i])
wind_MAE_list_mine.append(np.abs(wind_re - wind_label_re)[i])
RMW_MAE_list_mine.append(np.abs(RMW_re - RMW_label_re)[i])
print("wind_label={}, estimatioon result:intensity={}".format(wind_label_re[i], wind_re[i]))
print("RMW_label={}, estimatioon result:RMW={}".format(RMW_label_re[i], RMW_re[i]))
return wind_error, RMW_error, \
wind_bias_sum, wind_RMSE_sum, RMW_bias_sum, RMW_RMSE_sum, \
wind_label_list, wind_output_list, RMW_label_list, RMW_output_list, \
wind_MAE_list_mine, RMW_MAE_list_mine
if __name__ == '__main__':
test_transform = None
test_dataset = Dataset(config.predict_npy_path156, config.predict_npy_path_diff, test_transform, config.data_format)
test_dataloader = torch.utils.data.DataLoader(
test_dataset, batch_size=config.batch_size,
shuffle=True, num_workers=config.num_workers,
)
wind_error, RMW_error, \
wind_bias_sum, wind_RMSE_sum, RMW_bias_sum, RMW_RMSE_sum, \
wind_label_list, wind_output_list, RMW_label_list, RMW_output_list, \
wind_MAE_list_mine, RMW_MAE_list_mine = test_npys(test_dataloader)
test_num = len(wind_label_list)
wind_error = wind_error / test_num
RMW_error = RMW_error / test_num
wind_bias = wind_bias_sum / test_num
wind_RMSE = math.sqrt(wind_RMSE_sum / test_num)
RMW_bias = RMW_bias_sum / test_num
RMW_RMSE = math.sqrt(RMW_RMSE_sum / test_num)
print("Average error of intensity estimation={}".format(wind_error))
print("Average error of RMW estimation={}".format(RMW_error))
print("bias of intensity={}".format(wind_bias))
print("bias of RMW={}".format(RMW_bias))
print("RMSE of intensity estimation={}".format(wind_RMSE))
print("RMSE of RMW estimation={}".format(RMW_RMSE))