-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss_model.py
273 lines (250 loc) · 13.8 KB
/
rss_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.optim import Adam
from rss_lstm.seq2seq_recon_grid import Seq2Seq
from torch.utils.data import DataLoader
from torch.nn import functional as F
import torchvision.transforms as transforms
import pandas as pd
import random
import math
from lstm import loss_utils
import wandb
import metric
#import visualize
#import data
config = dict(epochs = 50,
clip = 0.0,
s = 10,
num_channels = 15,
num_kernels = 64,
kernel_size = (3, 3),
padding = (1, 1),
activation = "elu",
frame_size = (41, 161),
num_layers=3,
bias = True,
w_init = True,
batch_size = 32,
lr = 0.0005,
hidden_size = 128,
weight_decay = 0.0,
num_timesteps = 4,
threshold = 0.5,
threshold_decay = 0.95,
sampling_step1 = 15,
sampling_step2 = 30,
attention_hidden_dims = 4,
future_interval = 1,
forecasting = True,
pred_thresold = 0.3,
iou_threshold = 0.1,
alpha = 0.75
)
# Use GPU if available
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class CustomDataset(torch.utils.data.Dataset):
def __init__(self, data, labels, output_frames, grid_labels):
self.data = data
self.labels = labels
self.output_frames = output_frames
self.grid_labels = grid_labels
def __len__(self):
return len(self.labels)
def __getitem__(self, idx):
sample = self.data[idx]
label = self.labels[idx]
output_frame = self.output_frames[idx]
grid_label = self.grid_labels[idx]
return sample, label, output_frame, grid_label
def train_epoch(model, optimizer, train_loader, epoch, config):
training_loss = 0.
accs = []
batch_count = 0
examples_count = 0
model.train()
for i, (inputs, labels, target_output_frames, grid_labels) in enumerate(train_loader, 1):
true_positive = 0
true_negative = 0
false_positive = 0
false_negative = 0
inputs, labels, target_output_frames, grid_labels = inputs.float().to(device), labels.float().to(device), target_output_frames.float().to(device), grid_labels.to(device)
#inputs, labels, target_output_frames = inputs.float().to(device), labels.float().to(device), target_output_frames.float().to(device)
#RSS
# prob_mask = torch.rand(labels.shape[0], config.num_timesteps - 1)
# prob_mask1 = torch.rand(labels.shape[0], 1)
# prob_mask, prob_mask1 = prob_mask.to(device), prob_mask1.to(device)
# if epoch < config.sampling_step1:
# prob_mask = (prob_mask < config.threshold).float()
# prob_mask1 = (prob_mask1 < config.threshold).float()
# elif epoch < config.sampling_step2:
# prob_mask = (prob_mask < (1 - config.threshold * (config.threshold_decay ** (epoch - config.sampling_step1 + 1)))).float()
# prob_mask1 = (prob_mask1 < (1 - config.threshold * (config.threshold_decay ** (epoch - config.sampling_step1 + 1)))).float()
# else:
# prob_mask = (prob_mask < 1.0).float()
# prob_mask1 = (prob_mask1 < 1.0).float()
# prob_mask = prob_mask.unsqueeze(1).unsqueeze(3).unsqueeze(4).expand(-1, config.num_channels, -1, config.frame_size[0], config.frame_size[1])
# prob_mask1 = prob_mask1.unsqueeze(1).unsqueeze(3).unsqueeze(4).expand(-1, config.num_channels, -1, config.frame_size[0], config.frame_size[1])
outputs, output_frames, pred_grid_labels = model(inputs.float())
#outputs, output_frames = model(inputs.float(), prediction = True)
log_probs = torch.sigmoid(outputs)
labels = labels.reshape(labels.shape[0], 1)
loss_func = torch.nn.BCELoss(reduction='none')
bce_loss = loss_func(log_probs, labels)
bce_loss = bce_loss/torch.max(bce_loss)
label_loss = torch.sum(bce_loss) / labels.shape[0]
recon_loss = F.mse_loss(output_frames.float(), target_output_frames.float())
recon_loss = recon_loss/config.batch_size
#y_pred = torch.sigmoid(pred_grid_labels)
#grid_loss = sigmoid_focal_loss(y_pred.float(), grid_labels.float(), alpha=0.25, gamma=2, reduction='sum') / grid_labels.size(0)
grid_loss = metric.focal_loss(pred_grid_labels.float(), grid_labels.float(), alpha = config.alpha)
loss = label_loss + recon_loss + grid_loss
optimizer.zero_grad()
loss.backward()
if config.clip > 0:
#torch.nn.utils.clip_grad_norm_(model.parameters(), config.clip)
torch.nn.utils.clip_grad_value_(model.parameters(), config.clip)
optimizer.step()
examples_count += len(inputs)
if ((i + 1) % 10) == 0:
wandb.log({"epoch": epoch, "loss": loss}, step = examples_count)
true_positive = torch.sum((log_probs > 0.5) * (labels == 1))
true_negative = torch.sum((log_probs <= 0.5) * (labels == 0))
false_positive = torch.sum((log_probs > 0.5) * (labels == 0))
false_negative = torch.sum((log_probs <= 0.5) * (labels == 1))
training_loss += loss.cpu().data.numpy()
acc = (true_positive + true_negative)/(true_negative + true_positive + false_positive + false_negative)
accs.append(acc.cpu().numpy())
return training_loss / len(train_loader), np.mean(accs)
def plot_grid_labels(label, save_path=None):
plt.imshow(label, interpolation='none', vmin=0., vmax=1., cmap='gray')
if save_path is not None:
plt.savefig(save_path)
else:
plt.show()
def eval_epoch(model, test_loader, epoch, config):
test_loss = 0.
all_predicted_labels = []
all_labels = []
true_positive = 0
true_negative = 0
false_positive = 0
false_negative = 0
pred_grid_labels = []
true_grid_labels = []
with torch.no_grad():
for i, (inputs, labels, target_output_frames, grid_labels) in enumerate(test_loader, 1):
#example_data, example_target = example_data.float().to(device), example_target.float().to(device)
inputs, labels, target_output_frames, grid_labels = inputs.float().to(device), labels.float().to(device), target_output_frames.to(device), grid_labels.to(device)
# if i == 1:
# inputs[0] = example_data[0]
# labels[0] = 1
# target_output_frames[0] = example_target[0]
outputs, output_frames, pred_grid = model(inputs.float())
log_probs = torch.sigmoid(outputs)
labels = labels.reshape(labels.shape[0], 1)
loss_func = torch.nn.BCELoss(reduction='none')
bce_loss = loss_func(log_probs, labels)
bce_loss = bce_loss/torch.max(bce_loss)
label_loss = torch.sum(bce_loss) / labels.shape[0]
# if epoch == 49 and i == 1:
# for j in range(labels.shape[0]):
# if labels[j] == 1:
# print(j)
# target_array= target_output_frames.clone().detach().cpu().numpy()
# predicted_array = output_frames.clone().detach().cpu().numpy()
# target_arrayReshaped_tplus1 = target_array[j,0].reshape(target_array[j,0].shape[0], -1)
# predicted_arrayReshaped_tplus1 = predicted_array[j,0].reshape(predicted_array[j,0].shape[0], -1)
# target_arrayReshaped_tplus2 = target_array[j,1].reshape(target_array[j,1].shape[0], -1)
# predicted_arrayReshaped_tplus2 = predicted_array[j,1].reshape(predicted_array[j,1].shape[0], -1)
# np.savetxt('predicted_array_tplus1.txt', predicted_arrayReshaped_tplus1)
# np.savetxt('target_array_tplus1.txt', target_arrayReshaped_tplus1)
# np.savetxt('predicted_array_tplus2.txt', predicted_arrayReshaped_tplus2)
# np.savetxt('target_array_tplus2.txt', target_arrayReshaped_tplus2)
# break
#torch.onnx.export(model, inputs, "model.onnx")
#wandb.save("model.onnx")
recon_loss = F.mse_loss(output_frames.float(), target_output_frames.float())
recon_loss = recon_loss/config.batch_size
#y_pred = torch.sigmoid(pred_grid)
#grid_loss = sigmoid_focal_loss(y_pred.float(), grid_labels.float(), alpha=0.25, gamma=2, reduction='sum') / grid_labels.size(0)
grid_loss = metric.focal_loss(pred_grid.float(), grid_labels.float(), alpha = config.alpha)
loss = label_loss + recon_loss + grid_loss
true_positive += torch.sum((log_probs > 0.5) * (labels == 1))
true_negative += torch.sum((log_probs <= 0.5) * (labels == 0))
false_positive += torch.sum((log_probs > 0.5) * (labels == 0))
false_negative += torch.sum((log_probs <= 0.5) * (labels == 1))
test_loss += loss.cpu().data.numpy()
all_predicted_labels.append(outputs.detach().cpu().numpy().argmax(axis=1))
all_labels.append(labels.cpu().numpy())
y_pred = torch.sigmoid(pred_grid)
if epoch == 49 and i == 1:
for k in range(grid_labels.shape[0]):
if torch.any(grid_labels[k,0] > 0.1).item():
plot_grid_labels(grid_labels[k,0].detach().cpu().numpy(), save_path=f'visual_lstm_recon/target/grid_labels-{k}.png')
if torch.any(y_pred[k,0] > 0.1).item():
plot_grid_labels(y_pred[k,0].detach().cpu().numpy(), save_path=f'visual_lstm_recon/predicted/pred_grid_labels-{k}.png')
pred_grid_labels.append(y_pred.detach().cpu().numpy())
true_grid_labels.append(grid_labels.detach().cpu().numpy())
pred_grid_labels = np.concatenate(pred_grid_labels, axis=0).squeeze()
true_grid_labels = np.concatenate(true_grid_labels, axis=0).squeeze()
stat1 = metric.bbiou(pred_grid_labels, true_grid_labels, iou_threshold=0.1, pred_threshold = config.pred_thresold)
all_predicted_labels = np.concatenate(all_predicted_labels, axis=0)
all_labels = np.concatenate(all_labels, axis=0)
# accuracy
acc = (true_positive + true_negative)/(true_negative + true_positive + false_positive + false_negative)
stat = {'TP': true_positive, 'TN': true_negative, 'FP': false_positive, 'FN': false_negative}
print(stat)
print("Spatial metrics:\n")
print(stat1)
precision = true_positive / (true_positive + false_positive)
recall = true_positive / (true_positive + false_negative)
f1 = 2 * true_positive / (2 * true_positive + false_positive + false_negative)
return test_loss / len(test_loader.dataset), acc, precision, recall, f1
def run_model(model, train_loader, test_loader, optimizer, config):
wandb.watch(model, log="all", log_freq=10)
epochs = config.epochs
for epoch in range(epochs):
train_loss, train_acc = train_epoch(model, optimizer, train_loader, epoch, config)
test_loss, test_acc, precision, recall, f1 = eval_epoch(model, test_loader, epoch, config)
loss_dict = {'train_loss': train_loss, 'test_loss': test_loss}
wandb.log(loss_dict)
print(
'Epoch: {} \tTraining Loss: {:.4f} \tTraining Accuracy: {:.4f}\tTest Loss: {:.4f}\tTest Accuracy: {:.4f}'.format(
epoch, train_loss, train_acc, test_loss, test_acc))
print('Precision: {:.4f} \tRecall: {:.4f}\tF1: {:.4f}'.format(precision, recall, f1))
def make(config):
train_npz = np.load('ncep_WP_EP_new_2_binary.npz')
data1, target = train_npz['data'], train_npz['target']
#data1 = data[:,:3]
#data1 = np.concatenate((data[:, :3], data[:, 6:7]), axis=1)
#data1 = data.load_data(correct = True)
mean = np.mean(data1, axis=(0, 2, 3))
std = np.std(data1, axis=(0, 2, 3))
transform = transforms.Normalize(mean, std)
#data, target = torch.from_numpy(data), torch.from_numpy(target)
data1 = torch.from_numpy(data1)
data1 = transform(data1)
train_data, train_target, train_output_frame, train_grid_labels, test_data, test_target, test_output_frame, test_grid_labels = loss_utils.load_data_tc(data1, config)
trainset = CustomDataset(train_data, train_target, train_output_frame, train_grid_labels)
train_loader = torch.utils.data.DataLoader(trainset, batch_size=config.batch_size)
testset = CustomDataset(test_data, test_target, test_output_frame, test_grid_labels)
test_loader = torch.utils.data.DataLoader(testset, batch_size=config.batch_size)
model = Seq2Seq(num_channels=config.num_channels, num_kernels=config.num_kernels,
kernel_size=config.kernel_size, padding=config.padding, activation=config.activation,
frame_size=config.frame_size, num_layers=config.num_layers, num_timesteps = config.num_timesteps,
future_interval = config.future_interval, bias = config.bias, w_init = config.w_init, hidden_size = config.hidden_size, forecasting = config.forecasting)
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=config.lr, weight_decay = config.weight_decay)
model = model.to(device)
return model, train_loader, test_loader, optimizer
def model_pipeline(hyperparameters):
with wandb.init(project="tc_forecast_prediction_spatialRecon_new_TC", config=hyperparameters):
config = wandb.config
random.seed(config.s)
model, train_loader, test_loader, optimizer = make(config)
run_model(model, train_loader, test_loader, optimizer, config)
return model
if __name__=='__main__':
model = model_pipeline(config)