-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtrain.py
140 lines (109 loc) · 6.14 KB
/
train.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
import math
from torch.autograd import Variable
import torch.nn.functional as F
import torch
running_loss_final = 0
def cross_entropy2d(input, target, weight=None, size_average=True):
n, c, h, w = input.size()
input = input.transpose(1, 2).transpose(2, 3).contiguous()
input = input[target.view(n, h, w, 1).repeat(1, 1, 1, c) >= 0] # 262144 #input = 2*256*256*2
input = input.view(-1, c)
mask = target >= 0
target = target[mask]
loss = F.cross_entropy(input, target, weight=weight, size_average=False)
if size_average:
loss /= mask.data.sum()
return loss
class Trainer(object):
def __init__(self, cuda, model_depth, model_baseline, model_ladder, optimizer_depth, optimizer_baseline, optimizer_ladder,
train_loader, max_iter, snapshot, outpath, sshow, size_average=False):
self.cuda = cuda
self.model_depth = model_depth
self.model_baseline = model_baseline
self.model_ladder = model_ladder
self.optim_depth = optimizer_depth
self.optim_baseline = optimizer_baseline
self.optim_ladder = optimizer_ladder
self.train_loader = train_loader
self.epoch = 0
self.iteration = 0
self.max_iter = max_iter
self.snapshot = snapshot
self.outpath = outpath
self.sshow = sshow
self.size_average = size_average
def train_epoch(self):
for batch_idx, (img, mask, depth) in enumerate(self.train_loader):
iteration = batch_idx + self.epoch * len(self.train_loader)
if self.iteration != 0 and (iteration - 1) != self.iteration:
continue # for resuming
self.iteration = iteration
if self.iteration >= self.max_iter:
break
if self.cuda:
img, mask, depth = img.cuda(), mask.cuda(), depth.cuda()
img, mask, depth = Variable(img), Variable(mask), Variable(depth)
n, c, h, w = img.size() # batch_size, channels, height, weight
self.optim_depth.zero_grad()
self.optim_baseline.zero_grad()
self.optim_ladder.zero_grad()
global running_loss_final
# depth = torch.unsqueeze(depth, 1)
depth = depth.view(n, 1, h, w).repeat(1, c, 1, 1)
d2, d3, d4 = self.model_depth(depth)
h2, h3, h4, h5 = self.model_baseline(img)
predict_stage2_mask, predict_stage3_mask, predict_stage4_mask = self.model_ladder(h2, h3, h4, h5, d2, d3, d4)
# predict_stage4_mask = self.model_ladder(h2, h3, h4, h5, d2, d3, d4)
# loss_stage2_mask = cross_entropy2d(predict_stage2_mask, mask, size_average=False)
# loss_stage3_mask = cross_entropy2d(predict_stage3_mask, mask, size_average=False)
loss_stage4_mask = cross_entropy2d(predict_stage4_mask, mask, size_average=False)
# loss_all = loss_stage2_mask + loss_stage3_mask + loss_stage4_mask
loss_all = loss_stage4_mask # Supervised only on the stage4
running_loss_final += loss_all.item()
if iteration % self.sshow == (self.sshow - 1):
print('\n [%3d, %6d, RGB-D Net loss: %.3f]' % (
self.epoch + 1, iteration + 1, running_loss_final / (n * self.sshow)))
running_loss_final = 0.0
if iteration <= 200000:
if iteration % self.snapshot == (self.snapshot - 1):
savename_depth = ('%s/depth_snapshot_iter_%d.pth' % (self.outpath, iteration + 1))
torch.save(self.model_depth.state_dict(), savename_depth)
print('save: (snapshot: %d)' % (iteration + 1))
savename_baseline = ('%s/baseline_snapshot_iter_%d.pth' % (self.outpath, iteration + 1))
torch.save(self.model_baseline.state_dict(), savename_baseline)
print('save: (snapshot: %d)' % (iteration + 1))
savename_ladder = ('%s/ladder_snapshot_iter_%d.pth' % (self.outpath, iteration + 1))
torch.save(self.model_ladder.state_dict(), savename_ladder)
print('save: (snapshot: %d)' % (iteration + 1))
else:
if iteration % 10000 == (10000 - 1):
savename_depth = ('%s/depth_snapshot_iter_%d.pth' % (self.outpath, iteration + 1))
torch.save(self.model_depth.state_dict(), savename_depth)
print('save: (snapshot: %d)' % (iteration + 1))
savename_baseline = ('%s/baseline_snapshot_iter_%d.pth' % (self.outpath, iteration + 1))
torch.save(self.model_baseline.state_dict(), savename_baseline)
print('save: (snapshot: %d)' % (iteration + 1))
savename_ladder = ('%s/ladder_snapshot_iter_%d.pth' % (self.outpath, iteration + 1))
torch.save(self.model_ladder.state_dict(), savename_ladder)
print('save: (snapshot: %d)' % (iteration + 1))
if (iteration + 1) == self.max_iter:
savename_depth = ('%s/depth_snapshot_iter_%d.pth' % (self.outpath, iteration + 1))
torch.save(self.model_depth.state_dict(), savename_depth)
print('save: (snapshot: %d)' % (iteration + 1))
savename_baseline = ('%s/baseline_snapshot_iter_%d.pth' % (self.outpath, iteration + 1))
torch.save(self.model_baseline.state_dict(), savename_baseline)
print('save: (snapshot: %d)' % (iteration + 1))
savename_ladder = ('%s/ladder_snapshot_iter_%d.pth' % (self.outpath, iteration + 1))
torch.save(self.model_ladder.state_dict(), savename_ladder)
print('save: (snapshot: %d)' % (iteration + 1))
loss_all.backward()
self.optim_depth.step()
self.optim_baseline.step()
self.optim_ladder.step()
def train(self):
max_epoch = int(math.ceil(1. * self.max_iter / len(self.train_loader)))
for epoch in range(max_epoch):
self.epoch = epoch
self.train_epoch()
if self.iteration >= self.max_iter:
break