-
Notifications
You must be signed in to change notification settings - Fork 85
/
main-mnist.lua
executable file
·161 lines (129 loc) · 5.53 KB
/
main-mnist.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
unpack = unpack or table.unpack
require 'nn'
require 'cunn'
require 'paths'
require 'torch'
require 'cutorch'
require 'image'
require 'stn'
require 'BilinearSamplerBHWD'
require 'optim'
require 'ConvLSTM'
require 'display_flow'
--torch.setdefaulttensortype('torch.FloatTensor')
local function main()
cutorch.setDevice(1)
paths.dofile('opts-mnist.lua')
paths.dofile('data-mnist.lua')
paths.dofile('model.lua')
datasetSeq = getdataSeq_mnist(opt.dataFile) -- we sample nSeq consecutive frames
print ('Loaded ' .. datasetSeq:size() .. ' images')
print('==> training model')
torch.manualSeed(opt.seed)
-- init LSTM parameters to small values, uniformly distributed
local lstm_params, lstm_grads = model.modules[2].modules[2].modules[1].module:getParameters()
lstm_params:uniform(-0.08,0.08)
-- init LSTM biases to (forget_bias, other_bias)
model.modules[2].modules[2].modules[1].module:initBias(0,0)
-- call LSTM forget to reset the memory
model.modules[2].modules[2].modules[1].module:forget()
-- useful to display optical flow
local optical_flow = model.modules[2].modules[2].modules[3].modules[7].output
parameters, grads = model:getParameters()
print('Number of parameters ' .. parameters:nElement())
print('Number of grads ' .. grads:nElement())
local eta0 = 1e-6
local eta = opt.eta
local err = 0
local iter = 0
local epoch = 0
rmspropconf = {learningRate = eta}
model:training()
for t = 1,opt.maxIter do
--------------------------------------------------------------------
-- progress
iter = iter+1
--------------------------------------------------------------------
-- define eval closure
local feval = function()
local f = 0
model:zeroGradParameters()
inputTable = {}
target = torch.Tensor()--= torch.Tensor(opt.transf,opt.memorySizeH, opt.memorySizeW)
sample = datasetSeq[t]
data = sample[1]
for i = 1,data:size(1)-1 do
table.insert(inputTable, data[i]:cuda())
end
target:resizeAs(data[1]):copy(data[data:size(1)])
target = target:cuda()
-- estimate f and gradients
output = model:updateOutput(inputTable)
gradtarget = gradloss:updateOutput(target):clone()
gradoutput = gradloss:updateOutput(output)
f = f + criterion:updateOutput(gradoutput,gradtarget)
-- gradients
local gradErrOutput = criterion:updateGradInput(gradoutput,gradtarget)
local gradErrGrad = gradloss:updateGradInput(output,gradErrOutput)
model:updateGradInput(inputTable,gradErrGrad)
model:accGradParameters(inputTable, gradErrGrad)
grads:clamp(-opt.gradClip,opt.gradClip)
return f, grads
end
if math.fmod(t,20000) == 0 then
epoch = epoch + 1
eta = opt.eta*math.pow(0.5,epoch/50)
rmspropconf.learningRate = eta
end
_,fs = optim.rmsprop(feval, parameters, rmspropconf)
err = err + fs[1]
model:forget()
--------------------------------------------------------------------
-- compute statistics / report error
if math.fmod(t , opt.nSeq) == 1 then
print('==> iteration = ' .. t .. ', average loss = ' .. err/(opt.nSeq) .. ' lr '..eta ) -- err/opt.statInterval)
err = 0
if opt.save and math.fmod(t , opt.nSeq*1000) == 1 and t>1 then
-- clean model before saving to save space
-- model:forget()
-- cleanupModel(model)
torch.save(opt.dir .. '/model_' .. t .. '.bin', model)
torch.save(opt.dir .. '/rmspropconf_' .. t .. '.bin', rmspropconf)
end
if opt.display then
_im1_ = image.display{image=inputTable[#inputTable-4]:squeeze(),win = _im1_, legend = 't-4'}
_im2_ = image.display{image=inputTable[#inputTable-3]:squeeze(),win = _im2_, legend = 't-3'}
_im3_ = image.display{image=inputTable[#inputTable-2]:squeeze(),win = _im3_, legend = 't-2'}
_im4_ = image.display{image=inputTable[#inputTable-1]:squeeze(),win = _im4_, legend = 't-1'}
_im5_ = image.display{image=inputTable[#inputTable]:squeeze(),win = _im5_, legend = 't'}
_im6_ = image.display{image=target:squeeze(),win = _im6_, legend = 'Target'}
_im7_ = image.display{image=output:squeeze(),win = _im7_, legend = 'Output'}
local imflow = flow2colour(optical_flow)
_im8_ = image.display{image=imflow,win=_im8_,legend='Flow'}
print (' ==== Displaying weights ==== ')
-- get weights
eweight = model.modules[1].module.modules[1].modules[1].modules[1].weight
dweight = model.modules[5].modules[2].modules[1].weight
dweight_cpu = dweight:view(opt.nFilters[2], opt.kernelSize, opt.kernelSize)
eweight_cpu = eweight:view(opt.nFilters[2], opt.kernelSize, opt.kernelSize)
-- render filters
dd = image.toDisplayTensor{input=dweight_cpu,
padding=2,
nrow=math.floor(math.sqrt(opt.nFilters[2])),
symmetric=true}
de = image.toDisplayTensor{input=eweight_cpu,
padding=2,
nrow=math.floor(math.sqrt(opt.nFilters[2])),
symmetric=true}
-- live display
if opt.display then
_win1_ = image.display{image=dd, win=_win1_, legend='Decoder filters', zoom=8}
_win2_ = image.display{image=de, win=_win2_, legend='Encoder filters', zoom=8}
end
end
end
end
print ('Training done')
collectgarbage()
end
main()