[b758a2]: / code / pretrainSimple.lua

Download this file

179 lines (140 with data), 4.5 kB

  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
require 'torch';
require 'nn';
require 'optim';
cudaFlag = true
if cudaFlag then
require 'cutorch';
require 'cunn';
end
c = os.clock()
t = os.time()
-- parameters
local learningRate = 0.05
local learningRateDecay = 0.0005
local weightdecay = 0.0000
local momentum = 0.9
local maxIteration = 5
local p = 0.25
local batchSize = 200
local folder = '/home/andrew/mitosis/data/mitosis-train-large'
dofile("data.lua")
local classes, classList, imagePaths = getImagePaths(folder)
dofile('/home/andrew/mitosis/models/model2.lua')
net = model2()
if cudaFlag then
net = net:cuda()
end
dofile('/home/andrew/mitosis/code/autoencoder.lua')
autoencoder = convnet2autoencoder(net)
--[
autoencoder:insert(nn.Dropout(p),1)
if cudaFlag then
autoencoder = autoencoder:cuda()
end
criterion = nn.MSECriterion()
if cudaFlag then
criterion = criterion:cuda()
end
-- compute size of each batch
batchSizes, numBatches = getBatchSizes(classes, classList, batchSize)
-- shuffle the images
classList = shuffleImages(classList, classes)
-- train
print("# StochasticGradient: training")
autoencoder:training()
subNet = nn.Sequential()
subNet:insert(autoencoder:get(1),1)
-- count the number of convolutional layers
numConvLayers = 0
for i = 1, net:size() do
if torch.typename(net:get(i)) == 'nn.SpatialConvolution' then
numConvLayers = numConvLayers + 1
end
end
errors = {}
for j = 1, numConvLayers do
c0 = os.clock()
t0 = os.time()
--[
subNet:insert(autoencoder:get(3*(j-1)+2),3*(j-1)+2)
subNet:insert(autoencoder:get(3*(j-1)+3),3*(j-1)+3)
subNet:insert(autoencoder:get(3*(j-1)+4),3*(j-1)+4)
subNet:insert(autoencoder:get(autoencoder:size()-3*(j-1)-2),3*(j-1)+5)
subNet:insert(autoencoder:get(autoencoder:size()-3*(j-1)-1),3*(j-1)+6)
subNet:insert(autoencoder:get(autoencoder:size()-3*(j-1)),3*(j-1)+7)
params, gradParams = subNet:getParameters()
optimState = {}
optimState.learningRate = learningRate
optimState.learningRateDecay = learningRateDecay
optimState.weightDecay = weightDecay
optimState.momentum = momentum
--]]
for epoch = 1, maxIteration do
c1 = os.clock()
t1 = os.time()
local currentError = 0
local sampleSum = {}
for i = 1, #classes do
sampleSum[i] = 0
end
for i = 1, numBatches do
c2 = os.clock()
t2 = os.time()
-- split classList into batches
local sampleList = {}
for j=1,#classes do
sampleList[j] = classList[j][{{sampleSum[j] + 1, sampleSum[j] + batchSizes[j][i]}}]
sampleSum[j] = sampleSum[j] + batchSizes[j][i]
end
local dataset = getSample(classes, sampleList, imagePaths)
if cudaFlag then
dataset.data = dataset.data:cuda()
end
dataset.label = dataset.data
local input = dataset.data
local target = dataset.label
function feval(params)
gradParams:zero()
local outputs = subNet:forward(input)
local loss = criterion:forward(outputs, target)
local dloss_doutputs = criterion:backward(outputs, target)
subNet:backward(input, dloss_doutputs)
return loss, gradParams
end
_, fs = optim.sgd(feval, params, optimState)
print('Layer = ' .. j .. ' of ' .. numConvLayers)
print('Epoch = ' .. epoch .. ' of ' .. maxIteration)
print('Batch = ' .. i .. ' of ' .. numBatches)
for k=1,#errors do
print('Final Error for Layer ' .. k .. ' = ' .. errors[k])
end
print('Error = ' .. fs[1])
print('CPU batch time = ' .. os.clock()-c2 .. ' seconds')
print('Actual batch time (rounded) = ' .. os.time()-t2 .. ' seconds')
if epochClock then
print('CPU epoch time = ' .. epochClock .. ' seconds')
print('Actual epoch time (rounded) = ' .. epochTime .. ' seconds')
end
if layerClock then
print('CPU layer time = ' .. layerClock .. ' seconds')
print('Actual layer time (rounded) = ' ..layerTime .. ' seconds')
end
print('Total CPU time so far = ' .. os.clock()-c .. ' seconds')
print('Total actual time so far (rounded) = ' .. os.time()-t .. ' seconds')
print('')
end
epochClock = os.clock()-c1
epochTime = os.time()-t1
end
errors[j] = fs[1]
layerClock = os.clock()-c0
layerTime = os.time()-t0
end
--autoencoder = torch.load('/home/andrew/mitosis/data/nets/pretrain.t7')
-- get CNN from autoencoder
net = autoencoder2convnet(autoencoder,net)
torch.save('/home/andrew/mitosis/data/nets/model2-pretrained-greedylayerwise2.t7',net)
totalClock = os.clock()-c
totalTime = os.time()-t
print('Total CPU time = ' .. totalClock .. ' seconds')
print('Total actual time (rounded) ' .. totalTime .. ' seconds')