-
Notifications
You must be signed in to change notification settings - Fork 104
/
script_training.py
158 lines (141 loc) · 5.58 KB
/
script_training.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
import os
import utils
import torch
import torch.nn as nn
from torchvision import transforms
from torch.utils.data import DataLoader
import numpy as np
import data
import scipy.io as sio
from options.training_options import TrainOptions
import utils
import time
from models import AutoEncoderCov3D, AutoEncoderCov3DMem
from models import EntropyLossEncap
###
opt_parser = TrainOptions()
opt = opt_parser.parse(is_print=True)
use_cuda = opt.UseCUDA
device = torch.device("cuda" if use_cuda else "cpu")
###
utils.seed(opt.Seed)
if(opt.IsDeter):
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
######
model_setting = utils.get_model_setting(opt)
print('Setting: %s' % (model_setting))
############
batch_size_in = opt.BatchSize
learning_rate = opt.LR
max_epoch_num = opt.EpochNum
chnum_in_ = opt.ImgChnNum # channel number of the input images
framenum_in_ = opt.FrameNum # num of frames in a video clip
mem_dim_in = opt.MemDim
entropy_loss_weight = opt.EntropyLossWeight
sparse_shrink_thres = opt.ShrinkThres
img_crop_size = 0
print('bs=%d, lr=%f, entrloss=%f, shr=%f, memdim=%d' % (batch_size_in, learning_rate, entropy_loss_weight, sparse_shrink_thres, mem_dim_in))
############
## data path
data_root = opt.DataRoot + opt.Dataset + '/'
tr_data_frame_dir = data_root + 'Train/'
tr_data_idx_dir = data_root + 'Train_idx/'
############ model saving dir path
saving_root = opt.ModelRoot
saving_model_path = os.path.join(saving_root, 'model_' + model_setting + '/')
utils.mkdir(saving_model_path)
### tblog
if(opt.IsTbLog):
log_path = os.path.join(saving_root, 'log_'+model_setting + '/')
utils.mkdir(log_path)
tb_logger = utils.Logger(log_path)
##
if(chnum_in_==1):
norm_mean = [0.5]
norm_std = [0.5]
elif(chnum_in_==3):
norm_mean = (0.5, 0.5, 0.5)
norm_std = (0.5, 0.5, 0.5)
frame_trans = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(norm_mean, norm_std)
])
unorm_trans = utils.UnNormalize(mean=norm_mean, std=norm_std)
###### data
video_dataset = data.VideoDataset(tr_data_idx_dir, tr_data_frame_dir, transform=frame_trans)
tr_data_loader = DataLoader(video_dataset,
batch_size=batch_size_in,
shuffle=True,
num_workers=opt.NumWorker
)
###### model
if(opt.ModelName=='MemAE'):
model = AutoEncoderCov3DMem(chnum_in_, mem_dim_in, shrink_thres=sparse_shrink_thres)
else:
model = []
print('Wrong model name.')
model.apply(utils.weights_init)
#########
device = torch.device("cuda" if use_cuda else "cpu")
model.to(device)
tr_recon_loss_func = nn.MSELoss().to(device)
tr_entropy_loss_func = EntropyLossEncap().to(device)
tr_optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
##
data_loader_len = len(tr_data_loader)
textlog_interval = opt.TextLogInterval
snap_save_interval = opt.SnapInterval
save_check_interval = opt.SaveCheckInterval
tb_img_log_interval = opt.TBImgLogInterval
global_ite_idx = 0 # for logging
for epoch_idx in range(0, max_epoch_num):
for batch_idx, (item, frames) in enumerate(tr_data_loader):
frames = frames.to(device)
if (opt.ModelName == 'MemAE'):
recon_res = model(frames)
recon_frames = recon_res['output']
att_w = recon_res['att']
loss = tr_recon_loss_func(recon_frames, frames)
recon_loss_val = loss.item()
entropy_loss = tr_entropy_loss_func(att_w)
entropy_loss_val = entropy_loss.item()
loss = loss + entropy_loss_weight * entropy_loss
loss_val = loss.item()
##
tr_optimizer.zero_grad()
loss.backward()
tr_optimizer.step()
##
## TB log val
if(opt.IsTbLog):
tb_info = {
'loss': loss_val,
'recon_loss': recon_loss_val,
'entropy_loss': entropy_loss_val
}
for tag, value in tb_info.items():
tb_logger.scalar_summary(tag, value, global_ite_idx)
# TB log img
if( (global_ite_idx % tb_img_log_interval)==0 ):
frames_vis = utils.vframes2imgs(unorm_trans(frames.data), step=5, batch_idx=0)
frames_vis = np.concatenate(frames_vis, axis=-1)
frames_vis = frames_vis[None, :, :] * np.ones(3, dtype=int)[:, None, None]
frames_recon_vis = utils.vframes2imgs(unorm_trans(recon_frames.data), step=5, batch_idx=0)
frames_recon_vis = np.concatenate(frames_recon_vis, axis=-1)
frames_recon_vis = frames_recon_vis[None, :, :] * np.ones(3, dtype=int)[:, None, None]
tb_info = {
'x': frames_vis,
'x_rec': frames_recon_vis
}
for tag, imgs in tb_info.items():
tb_logger.image_summary(tag, imgs, global_ite_idx)
##
if((batch_idx % textlog_interval)==0):
print('[%s, epoch %d/%d, bt %d/%d] loss=%f, rc_losss=%f, ent_loss=%f' % (model_setting, epoch_idx, max_epoch_num, batch_idx, data_loader_len, loss_val, recon_loss_val, entropy_loss_val) )
if((global_ite_idx % snap_save_interval)==0):
torch.save(model.state_dict(), '%s/%s_snap.pt' % (saving_model_path, model_setting) )
global_ite_idx += 1
if((epoch_idx % save_check_interval)==0):
torch.save(model.state_dict(), '%s/%s_epoch_%04d.pt' % (saving_model_path, model_setting, epoch_idx) )
torch.save(model.state_dict(), '%s/%s_epoch_%04d_final.pt' % (saving_model_path, model_setting, epoch_idx) )