-
Notifications
You must be signed in to change notification settings - Fork 7
/
trainNetworkUniform32.lua
202 lines (142 loc) · 5.45 KB
/
trainNetworkUniform32.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
require('nn')
require('cudnn')
require('cutorch')
require('cunn')
require 'image'
require 'torch'
require 'optim'
require 'socket'
require 'lfs'
local hsp = require 'hsp'
math.randomseed(os.time())
dofile('networks/networkUniform32.lua')
dofile('utils/uniformShapenetDataLoader.lua')
local networkParameters = {}
local trainingParams = {}
local arg = arg
networkParameters, trainingParams = dofile(arg[2])
cutorch.setDevice(arg[1])
local startIteration = 1
local net
local optimState = {}
if not trainingParams.resumeIteration then
if (networkParameters.useColor) then
net = uniformNetwork.generateNewNetwork(3,networkParameters.bottleNeckSize):cuda()
else
net = uniformNetwork.generateNewNetwork(1,networkParameters.bottleNeckSize):cuda()
end
else
net, optimState = uniformNetwork.loadSnapshot(trainingParams.outputFolder .. "/snapshot/", trainingParams.resumeIteration, true)
startIteration = optimState.iter + 1
net:training()
end
valLogger = optim.Logger(trainingParams.outputFolder .. 'training_valLoss_' .. startIteration .. '.log')
valLogger:setNames{'Iteration', 'L', 'time'}
trainLogger = optim.Logger(trainingParams.outputFolder .. 'training_trainLoss_' .. startIteration .. '.log')
trainLogger:setNames{'Iteration', 'L', 'time'}
local optimParams = trainingParams.config
-- set up the data set
uniform3DShapeNetDataSet:setResolution(trainingParams.resolutionStr)
if networkParameters.useColor then
uniform3DShapeNetDataSet:setColor()
end
if networkParameters.hard then
uniform3DShapeNetDataSet:setHard()
else
uniform3DShapeNetDataSet:setSoft()
end
uniform3DShapeNetDataSet:setPath(trainingParams.datasetFolder)
for c=1,#trainingParams.valClasses do
uniform3DShapeNetDataSet:addValDataFromClass(trainingParams.valClasses[c])
end
for c=1,#trainingParams.trainClasses do
uniform3DShapeNetDataSet:addTrainDataFromClass(trainingParams.trainClasses[c])
end
uniform3DShapeNetDataSet:shuffleTrainDataFileNames()
-- generate snapshot folder
local snapshotFolderExists = lfs.attributes(trainingParams.outputFolder .. "/snapshot/",'modification')
if (snapshotFolderExists == nil) then
lfs.mkdir(trainingParams.outputFolder .. "/snapshot/")
end
-- sliding window training loss
local trainingLossBufferSize = 200
local trainingLossBuffer = torch.Tensor(trainingLossBufferSize):zero()
local trainingLossBufferPointer = 1
local loss = 0
local totalTrainingTime = 0
cutorch.synchronize()
local start = socket.gettime()
-- loss layers
local bceCrit = nn.BCECriterion():cuda()
-- allocate structures for the input to the network
local batchInput
if (networkParameters.useColor) then
batchInput = torch.Tensor(trainingParams.batchSize, 3, 128, 128):cuda()
else
batchInput = torch.Tensor(trainingParams.batchSize, 1, 128, 128):cuda()
end
local batchVoxels = torch.Tensor(trainingParams.batchSize, 1, 32, 32, 32):cuda()
local params, gradParams = net:getParameters()
for i = startIteration, 1000000 do
local function evalGradients(params)
gradParams:zero()
-- run first stage
local result = net:forward(batchInput)
loss = bceCrit:forward(result, batchVoxels)
local gradLosss = bceCrit:backward(result, batchVoxels)
net:backward(batchInput, gradLosss)
return loss, gradParams
end
-- load a new batch
for b=1,trainingParams.batchSize do
local obs, voxels, modelName = uniform3DShapeNetDataSet:getNextTrainExample(false, true)
batchInput[{b,{},{},{}}] = obs:cuda()
batchVoxels[{b,{},{},{},{}}] = voxels:cuda()
end
-- do the optimization step
optim.adam(evalGradients, params, optimParams, optimState)
trainingLossBuffer[trainingLossBufferPointer] = loss
trainingLossBufferPointer = trainingLossBufferPointer + 1
if (trainingLossBufferPointer > trainingLossBufferSize) then
trainingLossBufferPointer = 1
end
-- output loss every 5 iterations
if i % 5 == 0 then
local slidingWindowLoss = torch.sum(trainingLossBuffer)/math.min(trainingLossBufferSize,i-startIteration+1)
print("iteration: " .. i .. ", sliding window training loss : ")
print(slidingWindowLoss)
local curEnd = socket.gettime()
trainLogger:add{i,slidingWindowLoss, curEnd - start}
end
if (i % trainingParams.validationInterval == 0) then
uniformNetwork.saveSnapshot(i, trainingParams.outputFolder .. "/snapshot/", net, optimState)
net:evaluate()
local valBatchSize = 1
local valLoss = 0
local numValidatedElements = 0
for j=1,#uniform3DShapeNetDataSet.valDataFiles do
if ((j-1)%trainingParams.validationSubsampling == 0) then
local obs, voxels, modelName = uniform3DShapeNetDataSet:getNextValExample(false, false)
if networkParameters.useColor then
input = obs:cuda():view(1,3,128,128)
else
input = obs:cuda():view(1,1,128,128)
end
voxels = voxels:cuda()
print("Running validation model " .. j .. " : " .. modelName)
-- run first stage
result = net:forward(input)
loss = bceCrit:forward(result, voxels)
--hsp.saveMeshAsObj(result[{1,1,{},{},{}}]:double(),0.25, trainingParams.outputFolder .. "/objs/" .. modelName .. ".obj")
valLoss = valLoss + loss
numValidatedElements = numValidatedElements + 1
else
uniform3DShapeNetDataSet:skipNextValExample()
end
end
valLoss = valLoss / numValidatedElements
local curEnd = socket.gettime()
valLogger:add{i, valLoss, curEnd - start}
net:training()
end
end