-
Notifications
You must be signed in to change notification settings - Fork 12
/
neural_decoder.py
293 lines (217 loc) · 10.3 KB
/
neural_decoder.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import numpy as np
import matplotlib.pyplot as plt
import os
from networks import *
import utils
import torch
torch.cuda.current_device()
import torch.optim as optim
from torch.optim import lr_scheduler
import torch.nn as nn
# Decide which device we want to run on
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class Decoder():
def __init__(self, args, dataloaders):
self.dataloaders = dataloaders
self.net_G = define_G(args).to(device)
# Learning rate and Beta1 for Adam optimizers
self.lr = args.lr
# define optimizers
self.optimizer_G = optim.Adam(
self.net_G.parameters(), lr=self.lr, betas=(0.9, 0.999))
# define lr schedulers
self.exp_lr_scheduler_G = lr_scheduler.StepLR(
self.optimizer_G, step_size=args.scheduler_step_size, gamma=0.1)
# define some other vars to record the training states
self.running_acc = []
self.epoch_acc = 0
self.best_val_acc = 0.0
self.best_epoch_id = 0
self.epoch_to_start = 0
self.max_num_epochs = args.max_num_epochs
self.G_pred = None
self.batch = None
self.G_loss = None
self.is_training = False
self.batch_id = 0
self.epoch_id = 0
self.checkpoint_dir = args.checkpoint_dir
self.vis_dir = args.vis_dir
# define loss functions
self._loss = nn.MSELoss()
# buffers to save training/val accuracy
self.VAL_ACC = np.array([], np.float32)
if os.path.exists(os.path.join(self.checkpoint_dir, 'val_acc.npy')):
self.VAL_ACC = np.load(os.path.join(self.checkpoint_dir, 'val_acc.npy'))
self.TRAIN_ACC = np.array([], np.float32)
if os.path.exists(os.path.join(self.checkpoint_dir, 'train_acc.npy')):
self.TRAIN_ACC = np.load(os.path.join(self.checkpoint_dir, 'train_acc.npy'))
# check and create model dir
if os.path.exists(self.checkpoint_dir) is False:
os.mkdir(self.checkpoint_dir)
if os.path.exists(self.vis_dir) is False:
os.mkdir(self.vis_dir)
def _load_checkpoint(self):
if os.path.exists(os.path.join(self.checkpoint_dir, 'last_ckpt.pt')):
print('loading last checkpoint...')
# load the entire checkpoint
checkpoint = torch.load(os.path.join(self.checkpoint_dir, 'last_ckpt.pt'))
# update net_G states
self.net_G.load_state_dict(checkpoint['model_G_state_dict'])
self.optimizer_G.load_state_dict(checkpoint['optimizer_G_state_dict'])
self.exp_lr_scheduler_G.load_state_dict(
checkpoint['exp_lr_scheduler_G_state_dict'])
self.net_G.to(device)
# update some other states
self.epoch_to_start = checkpoint['epoch_id'] + 1
self.best_val_acc = checkpoint['best_val_acc']
self.best_epoch_id = checkpoint['best_epoch_id']
print('Epoch_to_start = %d, Historical_best_acc = %.4f (at epoch %d)' %
(self.epoch_to_start, self.best_val_acc, self.best_epoch_id))
print()
else:
print('training from scratch...')
def _save_checkpoint(self, ckpt_name):
torch.save({
'epoch_id': self.epoch_id,
'best_val_acc': self.best_val_acc,
'best_epoch_id': self.best_epoch_id,
'model_G_state_dict': self.net_G.state_dict(),
'optimizer_G_state_dict': self.optimizer_G.state_dict(),
'exp_lr_scheduler_G_state_dict': self.exp_lr_scheduler_G.state_dict()
}, os.path.join(self.checkpoint_dir, ckpt_name))
def _update_lr_schedulers(self):
self.exp_lr_scheduler_G.step()
def _compute_acc(self):
target = self.batch['dmap'].to(device).detach()
img = self.G_pred.detach()
psnr = utils.cpt_batch_psnr(img, target, PIXEL_MAX=1.0)
return psnr
def _collect_running_batch_states(self):
self.running_acc.append(self._compute_acc().item())
m = len(self.dataloaders['train'])
if self.is_training is False:
m = len(self.dataloaders['val'])
if np.mod(self.batch_id, 10) == 1:
print('Is_training: %s. [%d,%d][%d,%d], G_loss: %.5f, running_acc: %.5f'
% (self.is_training, self.epoch_id, self.max_num_epochs-1, self.batch_id, m,
self.G_loss.item(), np.mean(self.running_acc)))
def _visualize_batch_and_prediction(self):
if np.mod(self.batch_id, 100) == 1:
vis_input = utils.make_numpy_grid(self.batch['stereogram'])
vis_pred = utils.make_numpy_grid(self.G_pred)
vis_gt = utils.make_numpy_grid(self.batch['dmap'])
vis = np.concatenate([vis_input, vis_pred, vis_gt], axis=0)
vis = np.clip(vis, a_min=0.0, a_max=1.0)
file_name = os.path.join(
self.vis_dir, 'istrain_'+str(self.is_training)+'_'+
str(self.epoch_id)+'_'+str(self.batch_id)+'.jpg')
plt.imsave(file_name, vis)
def _collect_epoch_states(self):
self.epoch_acc = np.mean(self.running_acc)
print('Is_training: %s. Epoch %d / %d, epoch_acc= %.5f' %
(self.is_training, self.epoch_id, self.max_num_epochs-1, self.epoch_acc))
print()
def _update_checkpoints(self):
# save current model
self._save_checkpoint(ckpt_name='last_ckpt.pt')
print('Lastest model updated. Epoch_acc=%.4f, Historical_best_acc=%.4f (at epoch %d)'
% (self.epoch_acc, self.best_val_acc, self.best_epoch_id))
print()
# update the best model (based on eval acc)
if self.epoch_acc > self.best_val_acc:
self.best_val_acc = self.epoch_acc
self.best_epoch_id = self.epoch_id
self._save_checkpoint(ckpt_name='best_ckpt.pt')
print('*' * 10 + 'Best model updated!')
print()
def _update_training_acc_curve(self):
# update train acc curve
self.TRAIN_ACC = np.append(self.TRAIN_ACC, [self.epoch_acc])
np.save(os.path.join(self.checkpoint_dir, 'train_acc.npy'), self.TRAIN_ACC)
def _update_val_acc_curve(self):
# update val acc curve
self.VAL_ACC = np.append(self.VAL_ACC, [self.epoch_acc])
np.save(os.path.join(self.checkpoint_dir, 'val_acc.npy'), self.VAL_ACC)
def _clear_cache(self):
self.running_acc = []
def _forward_pass(self, batch):
self.batch = batch
img_in = batch['stereogram'].to(device)
self.G_pred =self.net_G(img_in)
def _backward_G(self):
gt = self.batch['dmap'].to(device)
self.G_loss = self._loss(self.G_pred, gt)
self.G_loss.backward()
def train_models(self):
self._load_checkpoint()
# loop over the dataset multiple times
for self.epoch_id in range(self.epoch_to_start, self.max_num_epochs):
################## train #################
##########################################
self._clear_cache()
self.is_training = True
self.net_G.train() # Set model to training mode
# Iterate over data.
for self.batch_id, batch in enumerate(self.dataloaders['train'], 0):
self._forward_pass(batch)
# update G
self.optimizer_G.zero_grad()
self._backward_G()
self.optimizer_G.step()
self._collect_running_batch_states()
self._visualize_batch_and_prediction()
self._collect_epoch_states()
self._update_training_acc_curve()
self._update_lr_schedulers()
################## Eval ##################
##########################################
print('Begin evaluation...')
self._clear_cache()
self.is_training = False
self.net_G.eval()
# Iterate over data.
for self.batch_id, batch in enumerate(self.dataloaders['val'], 0):
with torch.no_grad():
self._forward_pass(batch)
self._collect_running_batch_states()
self._visualize_batch_and_prediction()
self._collect_epoch_states()
########### Update_Checkpoints ###########
##########################################
self._update_val_acc_curve()
self._update_checkpoints()
class Classifier(Decoder):
def __init__(self, args, dataloaders):
super(Classifier, self).__init__(args, dataloaders)
if args.dataset not in ['mnist', 'shapenet']:
raise NotImplementedError(
'Wrong dataset name %s (classification mode only supports [mnist] and [shapenet] dataset)'
% args.dataset)
self._loss = nn.CrossEntropyLoss()
self.prediction_mode = args.prediction_mode
def _compute_acc(self):
target = self.batch['label'].to(device).detach()
predicted = self.G_pred.detach()
cls_acc = utils.cpt_batch_classification_acc(predicted, target)
return cls_acc
def _forward_pass(self, batch):
self.batch = batch
if self.prediction_mode == 'stereogram2label':
# predict label from autostereogram
img_in = batch['stereogram'].to(device)
elif self.prediction_mode == 'depth2label':
# predict label from depthmap (upperbound model)
img_in = batch['dmap'].to(device)
img_in = img_in.repeat((1, 3, 1, 1))
else:
raise NotImplementedError(
'Wrong prediction mode %s (choose one from [stereogram2label] or [depth2label])'
% self.prediction_mode)
self.G_pred =self.net_G(img_in)
def _backward_G(self):
target = self.batch['label'].to(device).long()
self.G_loss = self._loss(self.G_pred, target)
self.G_loss.backward()
def _visualize_batch_and_prediction(self):
pass