-
Notifications
You must be signed in to change notification settings - Fork 19
/
train_online_dream.py
229 lines (176 loc) · 7.23 KB
/
train_online_dream.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
# Package Includes
from __future__ import division
import argparse
import os
import socket
import timeit
from datetime import datetime
from tensorboardX import SummaryWriter
import numpy as np
# PyTorch includes
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import transforms
from torch.utils.data import DataLoader
import torch.nn.functional as F
# Custom includes
from dataloaders import DAVIS_dataloader as db
from dataloaders.DAVIS_dataloader import im_normalize
from dataloaders import custom_transforms as tr
import cv2
import scipy.misc as sm
from network.joint_pred_seg import FramePredDecoder,FramePredEncoder,SegEncoder,JointSegDecoder,STCNN
from mypath import Path
main_arg_parser = argparse.ArgumentParser(description="parser for train frame predict")
main_arg_parser.add_argument("--frame_nums", type=int, default=4,
help="input frame nums")
args = main_arg_parser.parse_args()
db_root_dir = Path.db_root_dir()
save_dir = Path.save_root_dir()
if not os.path.exists(save_dir):
os.makedirs(os.path.join(save_dir))
parentModelName = 'STCNN_frame_'+str(args.frame_nums)
save_model_dir = os.path.join(save_dir, parentModelName)
save_online_model_dir = os.path.join(save_dir, parentModelName)
if not os.path.exists(save_online_model_dir):
os.makedirs(os.path.join(save_online_model_dir))
vis_res = 1 # Visualize the results?
nEpochs = 6
snapshot = nEpochs # Store a model every snapshot epochs
parentEpoch = 10
# Parameters in p are used for the name of the model
trainBatch = 1 # Number of Images in each mini-batch
seed = 0
seg_lr = 1e-4
wd = 5e-4
resume = 0
# Select which GPU, -1 if CPU
gpu_id = 0
device = torch.device("cuda:"+str(gpu_id) if torch.cuda.is_available() else "cpu")
lp_function = nn.MSELoss().to(device)
criterion = nn.BCELoss().to(device)
seg_criterion = nn.BCEWithLogitsLoss().to(device)
# Define augmentation transformations as a composition
composed_transforms = transforms.Compose([tr.RandomHorizontalFlip(),
tr.ScaleNRotate(rots=(-30, 30), scales=(.75, 1.25))
])
fname = 'val'
with open(os.path.join(db_root_dir, 'ImageSets/2016/', fname + '.txt')) as f:
seqnames = f.readlines()
for i in range(len(seqnames)):
seq_name = seqnames[i].strip()
seg_enc = SegEncoder()
pred_enc = FramePredEncoder(frame_nums=args.frame_nums)
pred_dec = FramePredDecoder()
j_seg_dec = JointSegDecoder()
net = STCNN(pred_enc, seg_enc, pred_dec, j_seg_dec)
if resume != 0:
net.load_state_dict(
torch.load(os.path.join(save_online_model_dir, seq_name+ '_epoch-' + str(resume - 1) + '.pth'),
map_location=lambda storage, loc: storage))
else:
net.load_state_dict(torch.load(os.path.join(save_model_dir, parentModelName+'_epoch-'+str(parentEpoch-1)+'.pth'),
map_location=lambda storage, loc: storage))
# Logging into Tensorboard
log_dir = os.path.join(save_dir, 'runs', datetime.now().strftime('%b%d_%H-%M-%S') + '_' + socket.gethostname()+'-'+seq_name)
writer = SummaryWriter(log_dir=log_dir)
net.to(device) # PyTorch 0.4.0 style
# Use the following optimizer
optimizer = optim.SGD([
{'params': [param for name, param in net.seg_encoder.named_parameters()], 'lr': seg_lr},
{'params': [param for name, param in net.seg_decoder.named_parameters()], 'lr': seg_lr},
], weight_decay=wd, momentum=0.9)
# fix the pred network
for param in net.pred_encoder.parameters():
param.requires_grad = False
for param in net.pred_decoder.parameters():
param.requires_grad = False
# Training dataset and its iterator
db_train = db.DAVIS_First_Frame_Dataset(train=True,inputRes=None, db_root_dir=db_root_dir, transform=composed_transforms,
seq_name=seq_name,frame_nums=args.frame_nums)
trainloader = DataLoader(db_train, batch_size=trainBatch, shuffle=True, num_workers=4)
# Testing dataset and its iterator
db_test = db.DAVIS_Online_Dataset(train=False, db_root_dir=db_root_dir, transform=None, seq_name=seq_name,
frame_nums=args.frame_nums)
testloader = DataLoader(db_test, batch_size=1, shuffle=False, num_workers=4)
num_img_tr = len(trainloader)
num_img_ts = len(testloader)
aveGrad = 0
print("Start of Online Training, sequence: " + seq_name)
start_time = timeit.default_timer()
# Main Training and Testing Loop
for epoch in range(resume, nEpochs):
# One training epoch
running_loss_tr = 0
for ii, sample_batched in enumerate(trainloader):
seqs, frames, gts, pred_gts = sample_batched['images'], sample_batched['frame'], sample_batched['seg_gt'], \
sample_batched['pred_gt']
# Forward-Backward of the mini-batch
frames.requires_grad_()
seqs, frames, pred_gts, gts = seqs.to(device), frames.to(device), pred_gts.to(device)\
, gts.to(device)
pred_gts = F.upsample(pred_gts, size=(100, 178), mode='bilinear', align_corners=True)
pred_gts = pred_gts.detach()
seg_res, pred = net.forward(seqs, frames)
optimizer.zero_grad()
seg_loss = seg_criterion(seg_res[-1], gts)
seg_loss.backward()
optimizer.step()
stop_time = timeit.default_timer()
# Print stuff
print('[Epoch: %d]' % (epoch+1))
print('seg_Loss: %f' % (seg_loss))
writer.add_scalar('data/total_loss_epoch', seg_loss.item(), epoch)
if (epoch % snapshot) == snapshot - 1 and epoch != 0:
torch.save(net.state_dict(), os.path.join(save_online_model_dir, seq_name + '_epoch-'+str(epoch) + '.pth'))
print('Online training time: ' + str(stop_time - start_time))
# Testing Phase
if vis_res:
import matplotlib.pyplot as plt
plt.close("all")
plt.ion()
f, ax_arr = plt.subplots(1, 4)
save_dir_res = os.path.join(save_dir, parentModelName+'_Results', seq_name)
if not os.path.exists(save_dir_res):
os.makedirs(save_dir_res)
print('Testing Network')
with torch.no_grad(): # PyTorch 0.4.0 style
# Main Testing Loop
for ii, sample_batched in enumerate(testloader):
seqs, frames, gts, pred_gts, fname = sample_batched['images'], sample_batched['frame'], sample_batched['seg_gt'], \
sample_batched['pred_gt'],sample_batched['fname']
seqs, frames, gts, pred_gts = seqs.to(device), frames.to(device), gts.to(device), pred_gts.to(device)
if ii == 0:
mask_ = gts.cpu().numpy()[0, 0, :, :]
else:
pred[pred > 0.4] = 1
pred[pred <= 0.4] = 0
mask_ = pred
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (80, 80))
mask__ = cv2.dilate(mask_, kernel)
outputs, pred = net.forward(seqs, frames)
for jj in range(int(seqs.size()[0])):
pred = np.transpose(outputs[-1].cpu().data.numpy()[jj, :, :, :], (1, 2, 0))
pred = 1 / (1 + np.exp(-pred))
pred = np.squeeze(pred)
if np.sum(mask__ > 0) > 50:
pred = pred * mask__
# Save the result, attention to the index jj
sm.imsave(os.path.join(save_dir_res, os.path.basename(fname[jj]) + '.png'), pred)
if vis_res:
img_ = np.transpose(frames.cpu().numpy()[jj, :, :, :], (1, 2, 0))
gt_ = np.transpose(gts.cpu().numpy()[jj, :, :, :], (1, 2, 0))
gt_ = np.squeeze(gt_)
# Plot the particular example
ax_arr[0].cla()
ax_arr[1].cla()
ax_arr[2].cla()
ax_arr[0].set_title('Input Image')
ax_arr[1].set_title('Ground Truth')
ax_arr[2].set_title('Detection')
ax_arr[0].imshow(im_normalize(img_))
ax_arr[1].imshow(gt_)
ax_arr[2].imshow(im_normalize(pred))
plt.pause(0.001)
writer.close()