-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_3dnormal_scratch.lua
executable file
·215 lines (172 loc) · 6.48 KB
/
train_3dnormal_scratch.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
209
210
211
212
213
214
215
require 'torch'
require 'nngraph'
require 'cunn'
require 'optim'
require 'image'
-- require 'datasets.scaled_3d'
require 'pl'
require 'paths'
image_utils = require 'image'
ok, disp = pcall(require, 'display')
if not ok then print('display not found. unable to plot') end
local sanitize = require('sanitize')
----------------------------------------------------------------------
-- parse command-line options
-- TODO: put your path for saving models in "save"
opt = lapp [[
-s,--save (default "./SAVEDMODEL_S") subdirectory to save logs
--saveFreq (default 1) save every saveFreq epochs
-n,--network (default "") reload pretrained network
-r,--learningRate (default 0.01) learning rate
-b,--batchSize (default 10) batch size
-m,--momentum (default 0.9) momentum term of adam
-t,--threads (default 2) number of threads
-g,--gpu (default 3) gpu to run on (default cpu)
--scale (default 512) scale of images to train on
--epochSize (default 2000) number of samples per epoch
--forceDonkeys (default 0)
--nDonkeys (default 2) number of data loading threads
--weightDecay (default 0.0005) weight decay
--classnum (default 40)
--classification (default 1)
]]
if opt.gpu < 0 or opt.gpu > 8 then opt.gpu = false end
print(opt)
opt.loadSize = opt.scale
-- TODO: setup the output size
opt.labelSize = 32
opt.manualSeed = torch.random(1, 10000)
print("Seed: " .. opt.manualSeed)
torch.manualSeed(opt.manualSeed)
-- threads
torch.setnumthreads(opt.threads)
print('<torch> set nb of threads to ' .. torch.getnumthreads())
if opt.gpu then
cutorch.setDevice(opt.gpu + 1)
print('<gpu> using device ' .. opt.gpu)
torch.setdefaulttensortype('torch.CudaTensor')
else
torch.setdefaulttensortype('torch.FloatTensor')
end
opt.geometry = { 3, opt.scale, opt.scale }
opt.outDim = opt.classnum
local function weights_init(m)
local name = torch.type(m)
if name:find('Convolution') then
m.weight:normal(0.0, 0.01)
m.bias:fill(0)
elseif name:find('BatchNormalization') then
if m.weight then m.weight:normal(1.0, 0.02) end
if m.bias then m.bias:fill(0) end
end
end
if opt.network == '' then
---------------------------------------------------------------------
-- TODO: write your own networks, let's name it as model_FCN for the sake of simplicity
-- hint: you might need to add large padding in conv1 (perhaps around 100ish? )
-- hint2: use ReArrange instead of Reshape or View
model_FCN = nn.Sequential()
model_FCN:add(nn.SpatialConvolution(3, 96, 11, 11, 4, 4, 68, 68))
--model_FCN:add(nn.SpatialConvolution(3, 96, 11, 11, 4, 68))
model_FCN:add(nn.SpatialBatchNormalization(96))
model_FCN:add(nn.ReLU())
model_FCN:add(nn.SpatialMaxPooling(3,3,2,2))
model_FCN:add(nn.SpatialConvolution(96, 256, 5, 5, 1, 1, 2, 2))
model_FCN:add(nn.SpatialBatchNormalization(256))
model_FCN:add(nn.ReLU())
model_FCN:add(nn.SpatialMaxPooling(3,3,2,2))
model_FCN:add(nn.SpatialConvolution(256, 384, 3, 3, 1, 1, 1, 1))
model_FCN:add(nn.SpatialBatchNormalization(384))
model_FCN:add(nn.ReLU())
model_FCN:add(nn.SpatialConvolution(384, 384, 3, 3, 1, 1, 1, 1))
model_FCN:add(nn.SpatialBatchNormalization(384))
model_FCN:add(nn.ReLU())
model_FCN:add(nn.SpatialConvolution(384, 256, 3, 3, 1, 1, 1, 1))
model_FCN:add(nn.SpatialBatchNormalization(256))
model_FCN:add(nn.ReLU())
model_FCN:add(nn.SpatialMaxPooling(3,3,2,2))
model_FCN:add(nn.SpatialConvolution(256, 1024, 6, 6, 1, 1, 1, 1))
model_FCN:add(nn.SpatialBatchNormalization(1024))
model_FCN:add(nn.ReLU())
model_FCN:add(nn.SpatialFullConvolution(1024, 512, 4, 4, 2, 2, 1, 1))
model_FCN:add(nn.SpatialBatchNormalization(512))
model_FCN:add(nn.ReLU())
model_FCN:add(nn.SpatialConvolution(512, 40, 3, 3, 1, 1, 1, 1))
model_FCN:add(nn.Transpose({2,3},{3,4}))
model_FCN:add(nn.View(-1, 40))
model_FCN:add(nn.LogSoftMax())
-- pre-existing code
model_FCN:apply(weights_init)
else
print('<trainer> reloading previously trained network: ' .. opt.network)
tmp = torch.load(opt.network)
model_FCN = tmp.FCN
end
-- print networks
print('fcn network:')
print(model_FCN)
-- TODO: loss function
criterion = nn.ClassNLLCriterion()
print('done with loss function')
-- TODO: convert model and loss function to cuda
print("Migrating model to GPU...")
model_FCN:cuda()
criterion:cuda()
-- TODO: retrieve parameters and gradients
print('getting params')
params_FCN, gradParams_FCN = model_FCN:getParameters()
print('done with getting params')
-- TODO: setup dataset, use data.lua
print('calling data.lua')
paths.dofile('data.lua')
print('done with data.lua')
-- TODO: setup training functions, use fcn_train_cls.lua
print('calling fcn_train_cls.lua')
fcn = paths.dofile('fcn_train_cls.lua')
print('done with fcn_train_cls.lua')
local optimState = {
learningRate = opt.learningRate,
learningRateDecay = 0.0,
momentum = opt.momentum,
dampening = 0.0,
weightDecay = opt.weightDecay
}
local function train()
print('\n<trainer> on training set:')
print("<trainer> online epoch # " .. epoch .. ' [batchSize = ' .. opt.batchSize .. ' lr = ' .. optimState.learningRate .. ', momentum = ' .. optimState.momentum .. ']')
model_FCN:training()
batchNumber = 0
for i = 1, opt.epochSize do
donkeys:addjob(function()
return makeData_cls(trainLoader:sample(opt.batchSize))
end,
fcn.train)
end
donkeys:synchronize()
cutorch.synchronize()
end
epoch = 1
-- training loop
while true do
-- train/test
train()
if epoch % opt.saveFreq == 0 then
local filename = paths.concat(opt.save, string.format('fcn_%d.net', epoch))
os.execute('mkdir -p ' .. sys.dirname(filename))
if paths.filep(filename) then
os.execute('mv ' .. filename .. ' ' .. filename .. '.old')
end
print('<trainer> saving network to ' .. filename)
torch.save(filename, { FCN = sanitize(model_FCN), opt = opt })
end
epoch = epoch + 1
-- plot errors
if opt.plot and epoch and epoch % 1 == 0 then
torch.setdefaulttensortype('torch.FloatTensor')
if opt.gpu then
torch.setdefaulttensortype('torch.CudaTensor')
else
torch.setdefaulttensortype('torch.FloatTensor')
end
end
end