-
Notifications
You must be signed in to change notification settings - Fork 93
/
train_pano_joint.lua
208 lines (165 loc) · 7.16 KB
/
train_pano_joint.lua
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
-- train script
require 'sys'
require 'image'
local matio = require 'matio'
sampleSize = opt.batchSize
numberOfPasses = opt.numPasses
function getBatch_val(data, sampsize, count)
-- select batch
inputMat = torch.zeros(sampsize, data.inp:size(2), data.inp:size(3), data.inp:size(4))
gtMat = torch.zeros(sampsize, data.gt:size(2), data.gt:size(3), data.gt:size(4))
gt2Mat = torch.zeros(sampsize, data.gt2:size(2), data.gt2:size(3), data.gt2:size(4))
for i = 1, sampsize do
inputMat[{{i},{},{},{}}] = data.inp[{{count},{},{},{}}]
gtMat[{{i},{},{}, {}}] = data.gt[{{count},{},{}, {}}]
gt2Mat[{{i},{},{}, {}}] = data.gt2[{{count},{},{}, {}}]
count = count + 1
end
return inputMat, gtMat, gt2Mat, count
end
function getBatch(data, sampsize, count, idx)
-- select batch
inputMat = torch.zeros(sampsize, data.inp:size(2), data.inp:size(3), data.inp:size(4))
gtMat = torch.zeros(sampsize, data.gt:size(2), data.gt:size(3), data.gt:size(4))
gtMsk = torch.zeros(sampsize, data.gt:size(2), data.gt:size(3), data.gt:size(4))
gt2Mat = torch.zeros(sampsize, data.gt2:size(2), data.gt2:size(3), data.gt2:size(4))
gt2Msk = torch.zeros(sampsize, data.gt2:size(2), data.gt2:size(3), data.gt2:size(4))
for i = 1, sampsize do
data_inp = data.inp[{{idx[count]},{},{},{}}]
data_gt = data.gt[{{idx[count]},{},{}, {}}]
data_gt2 = data.gt2[{{idx[count]},{},{}, {}}]
-- data augmentation
torch.seed() -- randomization
-- flip
local f_prob = torch.rand(1)
if f_prob[1]>0.5 then
data_inp = image.hflip(torch.reshape(data_inp, data.inp:size(2), data.inp:size(3), data.inp:size(4)))
data_gt = image.hflip(torch.reshape(data_gt, data.gt:size(2), data.gt:size(3), data.gt:size(4)))
data_gt2 = image.hflip(torch.reshape(data_gt2, data.gt2:size(2), data.gt2:size(3), data.gt2:size(4)))
data_inp = torch.reshape(data_inp, 1, data.inp:size(2), data.inp:size(3), data.inp:size(4))
data_gt = torch.reshape(data_gt, 1, data.gt:size(2), data.gt:size(3), data.gt:size(4))
data_gt2 = torch.reshape(data_gt2, 1, data.gt2:size(2), data.gt2:size(3), data.gt2:size(4))
end
-- gamma
torch.seed()
local g_prob = torch.add(torch.mul(torch.rand(1),1.5), 0.5)
data_inp = torch.pow(data_inp, g_prob[1])
-- rotate
torch.seed()
local r_prob = torch.add(torch.round(torch.mul(torch.rand(1),data.gt:size(4)-2)), 1)
data_inp = torch.cat(data_inp[{{},{},{},{r_prob[1]+1,data.inp:size(4)}}], data_inp[{{},{},{},{1,r_prob[1]}}], 4)
data_gt = torch.cat(data_gt[{{},{},{},{r_prob[1]+1,data.gt:size(4)}}], data_gt[{{},{},{},{1,r_prob[1]}}], 4)
data_gt2 = torch.cat(data_gt2[{{},{},{},{r_prob[1]+1,data.gt2:size(4)}}], data_gt2[{{},{},{},{1,r_prob[1]}}], 4)
msk = data_gt:gt(0)
msk2 = data_gt2:gt(0)
inputMat[{{i},{},{},{}}] = data_inp
gtMat[{{i},{},{}, {}}] = data_gt
gtMsk[{{i},{},{}, {}}] = msk
gt2Mat[{{i},{},{}, {}}] = data_gt2
gt2Msk[{{i},{},{}, {}}] = msk2
count = count + 1
if count > tr_size then
count = 1
idx = torch.randperm(tr_size)
end
end
return inputMat, gtMat, gtMsk, gt2Mat, gt2Msk, count, idx
end
function getValLoss()
local valnumberOfPasses = torch.floor(pano_val.inp:size(1)/1)
local loss = 0
local valcount = 1
--local out
for i=1, valnumberOfPasses do
--------------------- get mini-batch -----------------------
inputMat, gtMat, gt2Mat, valcount = getBatch_val(pano_val, 1, valcount)
------------------------------------------------------------
-- forward
inputMat = inputMat:cuda()
gtMat = gtMat:cuda()
gt2Mat = gt2Mat:cuda()
--print('forward')
output = model.core:forward(inputMat)
--print(model.criterion:forward(output[1], gtMat))
--print(model.criterion_2:forward(output[2], gt2Mat))
loss = model.criterion:forward(output[1], gtMat) + model.criterion_2:forward(output[2], gt2Mat)+loss
output = nil
collectgarbage()
end
loss = loss / valnumberOfPasses
return loss
end
-- do fwd/bwd and return loss, grad_params
function feval(x)
if x ~= params then
params:copy(x)
end
grad_params:zero()
local loss = 0
-- add for loop to increase mini-batch size
for i=1, numberOfPasses do
--------------------- get mini-batch -----------------------
--inputMat, gtMat, gtMask = getBatch_rand(pano_tr, sampleSize)
inputMat, gtMat, gtMsk, gt2Mat, gt2Msk, count, idx = getBatch(pano_tr, sampleSize, count, idx)
------------------------------------------------------------
-- forward
inputMat = inputMat:cuda()
gtMat = gtMat:cuda()
gt2Mat = gt2Mat:cuda()
output = model.core:forward(inputMat)
--print(model.criterion:forward(output[1], gtMat))
--print(model.criterion_2:forward(output[2], gt2Mat))
loss = model.criterion:forward(output[1], gtMat) + model.criterion_2:forward(output[2], gt2Mat)+ loss
-- backward
loss_d_1 = model.criterion:backward(output[1], gtMat)
loss_d_2 = model.criterion_2:backward(output[2], gt2Mat)
gtMsk = torch.mul(gtMsk, 4)
gtMsk = gtMsk:cuda()
gtMsk_w = torch.cmul(loss_d_1, gtMsk)
loss_d_1 = torch.add(gtMsk_w, loss_d_1)
gt2Msk = torch.mul(gt2Msk, 4)
gt2Msk = gt2Msk:cuda()
gt2Msk_w = torch.cmul(loss_d_2, gt2Msk)
loss_d_2 = torch.add(gt2Msk_w, loss_d_2)
model.core:backward(inputMat, {loss_d_1, loss_d_2})
output = nil
loss_d_1 = nil
loss_d_2 = nil
gtMsk_w = nil
gt2Msk_w = nil
collectgarbage()
end
--print(loss_gt)
grad_params:div(numberOfPasses)
-- clip gradient element-wise
grad_params:clamp(-10, 10)
return loss/numberOfPasses, grad_params
end
losses = {}
vallosses = {}
local optim_state = {opt.lr, opt.epsilon}
local iterations = 8000
local minValLoss = 1/0
count = 1
idx = torch.randperm(pano_tr.inp:size(1))
for i = 1, iterations do
model.core:training()
local _, loss = optim.adam(feval, params, optim_state)
--local _, loss = optim.rmsprop(feval, params, optim_state)
print(string.format("update param, loss = %6.8f, gradnorm = %6.4e", loss[1], grad_params:clone():norm()))
if i % 20 == 0 then
print(string.format("iteration %4d, loss = %6.8f, gradnorm = %6.4e", i, loss[1], grad_params:norm()))
model.core:evaluate()
valLoss, output = getValLoss()
vallosses[#vallosses + 1] = valLoss
print(string.format("validation loss = %6.8f", valLoss))
if minValLoss > valLoss then
minValLoss = valLoss
params_save = params:clone()
nn.utils.recursiveType(params_save, 'torch.DoubleTensor')
torch.save("./model/panofull_lay.t7", params_save:double())
print("------- Model Saved --------")
end
losses[#losses + 1] = loss[1]
end
end