-
Notifications
You must be signed in to change notification settings - Fork 43
/
exp.py
172 lines (140 loc) · 6.37 KB
/
exp.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
import os
import os.path as osp
import json
import torch
import pickle
import logging
import numpy as np
from model import SimVP
from tqdm import tqdm
from API import *
from utils import *
class Exp:
def __init__(self, args):
super(Exp, self).__init__()
self.args = args
self.config = self.args.__dict__
self.device = self._acquire_device()
self._preparation()
print_log(output_namespace(self.args))
self._get_data()
self._select_optimizer()
self._select_criterion()
def _acquire_device(self):
if self.args.use_gpu:
os.environ["CUDA_VISIBLE_DEVICES"] = str(self.args.gpu)
device = torch.device('cuda:{}'.format(0))
print_log('Use GPU: {}'.format(self.args.gpu))
else:
device = torch.device('cpu')
print_log('Use CPU')
return device
def _preparation(self):
# seed
set_seed(self.args.seed)
# log and checkpoint
self.path = osp.join(self.args.res_dir, self.args.ex_name)
check_dir(self.path)
self.checkpoints_path = osp.join(self.path, 'checkpoints')
check_dir(self.checkpoints_path)
sv_param = osp.join(self.path, 'model_param.json')
with open(sv_param, 'w') as file_obj:
json.dump(self.args.__dict__, file_obj)
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
logging.basicConfig(level=logging.INFO, filename=osp.join(self.path, 'log.log'),
filemode='a', format='%(asctime)s - %(message)s')
# prepare data
self._get_data()
# build the model
self._build_model()
def _build_model(self):
args = self.args
self.model = SimVP(tuple(args.in_shape), args.hid_S,
args.hid_T, args.N_S, args.N_T).to(self.device)
def _get_data(self):
config = self.args.__dict__
self.train_loader, self.vali_loader, self.test_loader, self.data_mean, self.data_std = load_data(**config)
self.vali_loader = self.test_loader if self.vali_loader is None else self.vali_loader
def _select_optimizer(self):
self.optimizer = torch.optim.Adam(
self.model.parameters(), lr=self.args.lr)
self.scheduler = torch.optim.lr_scheduler.OneCycleLR(
self.optimizer, max_lr=self.args.lr, steps_per_epoch=len(self.train_loader), epochs=self.args.epochs)
return self.optimizer
def _select_criterion(self):
self.criterion = torch.nn.MSELoss()
def _save(self, name=''):
torch.save(self.model.state_dict(), os.path.join(
self.checkpoints_path, name + '.pth'))
state = self.scheduler.state_dict()
fw = open(os.path.join(self.checkpoints_path, name + '.pkl'), 'wb')
pickle.dump(state, fw)
def train(self, args):
config = args.__dict__
recorder = Recorder(verbose=True)
for epoch in range(config['epochs']):
train_loss = []
self.model.train()
train_pbar = tqdm(self.train_loader)
for batch_x, batch_y in train_pbar:
self.optimizer.zero_grad()
batch_x, batch_y = batch_x.to(self.device), batch_y.to(self.device)
pred_y = self.model(batch_x)
loss = self.criterion(pred_y, batch_y)
train_loss.append(loss.item())
train_pbar.set_description('train loss: {:.4f}'.format(loss.item()))
loss.backward()
self.optimizer.step()
self.scheduler.step()
train_loss = np.average(train_loss)
if epoch % args.log_step == 0:
with torch.no_grad():
vali_loss = self.vali(self.vali_loader)
if epoch % (args.log_step * 100) == 0:
self._save(name=str(epoch))
print_log("Epoch: {0} | Train Loss: {1:.4f} Vali Loss: {2:.4f}\n".format(
epoch + 1, train_loss, vali_loss))
recorder(vali_loss, self.model, self.path)
best_model_path = self.path + '/' + 'checkpoint.pth'
self.model.load_state_dict(torch.load(best_model_path))
return self.model
def vali(self, vali_loader):
self.model.eval()
preds_lst, trues_lst, total_loss = [], [], []
vali_pbar = tqdm(vali_loader)
for i, (batch_x, batch_y) in enumerate(vali_pbar):
if i * batch_x.shape[0] > 1000:
break
batch_x, batch_y = batch_x.to(self.device), batch_y.to(self.device)
pred_y = self.model(batch_x)
list(map(lambda data, lst: lst.append(data.detach().cpu().numpy()), [
pred_y, batch_y], [preds_lst, trues_lst]))
loss = self.criterion(pred_y, batch_y)
vali_pbar.set_description(
'vali loss: {:.4f}'.format(loss.mean().item()))
total_loss.append(loss.mean().item())
total_loss = np.average(total_loss)
preds = np.concatenate(preds_lst, axis=0)
trues = np.concatenate(trues_lst, axis=0)
mse, mae, ssim, psnr = metric(preds, trues, vali_loader.dataset.mean, vali_loader.dataset.std, True)
print_log('vali mse:{:.4f}, mae:{:.4f}, ssim:{:.4f}, psnr:{:.4f}'.format(mse, mae, ssim, psnr))
self.model.train()
return total_loss
def test(self, args):
self.model.eval()
inputs_lst, trues_lst, preds_lst = [], [], []
for batch_x, batch_y in self.test_loader:
pred_y = self.model(batch_x.to(self.device))
list(map(lambda data, lst: lst.append(data.detach().cpu().numpy()), [
batch_x, batch_y, pred_y], [inputs_lst, trues_lst, preds_lst]))
inputs, trues, preds = map(lambda data: np.concatenate(
data, axis=0), [inputs_lst, trues_lst, preds_lst])
folder_path = self.path+'/results/{}/sv/'.format(args.ex_name)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
mse, mae, ssim, psnr = metric(preds, trues, self.test_loader.dataset.mean, self.test_loader.dataset.std, True)
print_log('mse:{:.4f}, mae:{:.4f}, ssim:{:.4f}, psnr:{:.4f}'.format(mse, mae, ssim, psnr))
for np_data in ['inputs', 'trues', 'preds']:
np.save(osp.join(folder_path, np_data + '.npy'), vars()[np_data])
return mse