-
Notifications
You must be signed in to change notification settings - Fork 0
/
CudaBuildFeatureMapForTrain.lua
306 lines (280 loc) · 12.4 KB
/
CudaBuildFeatureMapForTrain.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
require 'nn'
require 'optim'
require 'image'
require 'torch'
require 'xlua'
require 'cutorch'
require 'cunn'
require 'cudnn'
----------------------------------------------------------------------
-- parse command-line options
--
dname,fname = sys.fpath()
cmd = torch.CmdLine()
cmd:text()
cmd:text('Training')
cmd:text()
cmd:text('Options:')
cmd:option('-save', fname:gsub('.lua',''), 'subdirectory to save/log experiments in')
cmd:option('-network', '', 'reload pretrained network')
cmd:option('-full', false, 'use full dataset (50,000 samples)')
cmd:option('-visualize', false, 'visualize input data and weights during training')
cmd:option('-seed', 1, 'fixed input seed for repeatable experiments')
cmd:option('-optimization', 'SGD', 'optimization method: SGD | ASGD | CG | LBFGS')
cmd:option('-learningRate', 1e-2, 'learning rate at t=0')
cmd:option('-batchSize', 50, 'mini-batch size (1 = pure stochastic)')
cmd:option('-weightDecay', 0, 'weight decay (SGD only)')
cmd:option('-momentum', 0.95, 'momentum (SGD only)')
cmd:option('-t0', 1, 'start averaging at t0 (ASGD only), in nb of epochs')
cmd:option('-maxIter', 5, 'maximum nb of iterations for CG and LBFGS')
cmd:option('-threads', 2, 'nb of threads to use')
cmd:text()
opt = cmd:parse(arg)
-- fix seed
torch.manualSeed(opt.seed)
-- threads
torch.setnumthreads(opt.threads)
print('<torch> set nb of threads to ' .. opt.threads)
----------------------------------------------------------------------
cnn_model = torch.load('/opt/zbstudio/myprograms/CNN3/Max_NN.net')
print(cnn_model)
--print(model.modules[2]:get(1).bias)
--print(model.modules[2].output)
--print(model.modules[1].output)
----------------------------------------------------------------------
-- define model to train
-- on the 4-class classification problem
--
classes = {1, 2, 3, 4}
dir_test = '/opt/zbstudio/myprograms/Data/Fold1/Train'
seg_dir_test = '/opt/zbstudio/myprograms/Data/Fold1/Train/Label'
local channels = {'y','u' ,'v'}
-- get/create dataset
--
twidth = 32
theigh = 32
pwidth = 100 -- width of a patch centered a pixel
pheigh = 100 -- heigh of a patch centered a pixel
iwidth = 4200 --2250 -- width of input image
iheigh = 3200 --1750 -- heigh of input image
nimage = 2 -- the number of input
tsize_scale1 = 13072652
tsize_scale2 = 3270790
tsize_scale3 = 796488
trsize = tsize_scale1
NUM_FEATURE_MAP = 200
-- load files from folder for train
files = {}
seg_files = {}
-- Go over all files in directory. We use an iterator, paths.files().
for file in paths.files(dir_test) do
-- We only load files that match the extension
if file:find('jpg' .. '$') or file:find('png' .. '$') then
-- and insert the ones we care about in our table
table.insert(files, paths.concat(dir_test,file))
table.insert(seg_files, paths.concat(seg_dir_test,file))
end
end
-- Check files
if #files == 0 then
error('given directory doesnt contain any files of type: ')
end
--normalize image by the way inserting it into the fixed image
function copyMemmoryTensor(destTensor, targetTensor)
targetTensor[{{},{1,destTensor:size(2)},{1, destTensor:size(3)}}] = destTensor
return targetTensor
end
-- convert input with yuv to 10 Y channels, 3 U channels, 3 V channels
function convert3to16channels(input)
local outTensor = torch.Tensor(16, input:size()[2], input:size()[3])
for i=1, 3 do
--outTensor[{{i*4-3},{},{}}] = input[i]
outTensor[{{i*3-2},{},{}}] = input[i]
outTensor[{{i*3-1},{},{}}] = input[i]
outTensor[{{i*3},{},{}}] = input[i]
end
for i= 10, 16 do
outTensor[{{i},{},{}}] = input[1]
end
return outTensor
end
-- Go over the file list:
images = {}
seg_images = {}
for i,file in ipairs(files) do
-- load each image
table.insert(images, image.load(file))
print(file)
end
for i,file in ipairs(seg_files) do
-- load each image
table.insert(seg_images, image.load(file))
print(file)
end
-- load dataset
inputData = {
data_scale1 = torch.Tensor(tsize_scale1, 2),
data_scale2 = torch.Tensor(tsize_scale2, 2),
link = torch.IntTensor(trsize):cuda(),
pixs_pos = torch.IntTensor(trsize, 3, 2),
size = function() return trsize end
}
----------------------------------------------------------------------
-- preprocess/normalize train/test sets
--
-- convert input with yuv to 10 Y channels, 3 U channels, 3 V channels
function convert3to16channels(input)
local outTensor = torch.Tensor(16, input:size()[2], input:size()[3])
for i=1, 3 do
--outTensor[{{i*4-3},{},{}}] = input[i]
outTensor[{{i*3-2},{},{}}] = input[i]
outTensor[{{i*3-1},{},{}}] = input[i]
outTensor[{{i*3},{},{}}] = input[i]
end
for i= 10, 16 do
outTensor[{{i},{},{}}] = input[1]
end
return outTensor
end
function getPatch(image_in, i, j)
return image.crop(image_in, j-1-pwidth/2, i-1-pheigh/2,j-1+pwidth/2,i-1+pheigh/2)
end
-- training function
function build_feature(dataset, count_scale1, count_scale2)
local ft_maps_scale1 = torch.Tensor(iheigh, iwidth):fill(0)
local ft_maps_scale2 = torch.Tensor(iheigh/2, iwidth/2):fill(0)
local vt_maps_scale1 = torch.Tensor(count_scale1, NUM_FEATURE_MAP)
local vt_maps_scale2 = torch.Tensor(count_scale2, NUM_FEATURE_MAP)
for t = 1, count_scale1 do
-- get new sample
local input_scale1 = torch.Tensor(16, pwidth, pheigh)
local input_scale2 = torch.Tensor(16, pwidth, pheigh)
local output_scale1 = torch.Tensor(NUM_FEATURE_MAP)
local output_scale2 = torch.Tensor(NUM_FEATURE_MAP)
input_scale1 = convert3to16channels(getPatch(scale_yuvs_train1[dataset.link[t]],dataset.data_scale1[t][1],dataset.data_scale1[t][2]))
output1 = cnn_model.modules[2]:get(1):forward(input_scale1:cuda())
output3 = cnn_model.modules[2]:get(3):forward(output1:cuda())
output4 = cnn_model.modules[2]:get(4):forward(output3:cuda())
output5 = cnn_model.modules[2]:get(5):forward(output4:cuda())
output7 = cnn_model.modules[2]:get(7):forward(output5:cuda())
output8 = cnn_model.modules[2]:get(8):forward(output7:cuda())
output9 = cnn_model.modules[2]:get(9):forward(output8:cuda())
output11 = cnn_model.modules[2]:get(11):forward(output9:cuda())
output12 = cnn_model.modules[2]:get(12):forward(output11:cuda())
output13 = cnn_model.modules[2]:get(13):forward(output12:cuda())
output15 = cnn_model.modules[2]:get(15):forward(output13:cuda())
output16 = cnn_model.modules[2]:get(16):forward(output15:cuda())
output_scale1 = cnn_model.modules[2]:get(17):forward(output16:cuda())
vt_maps_scale1[t] = output_scale1:double()
ft_maps_scale1[dataset.data_scale1[t][1]][dataset.data_scale1[t][2]] = t
if (t<count_scale2) then
input_scale2 = convert3to16channels(getPatch(scale_yuvs_train2[dataset.link[t]],dataset.data_scale2[t][1],dataset.data_scale2[t][2]))
output1 = cnn_model.modules[2]:get(1):forward(input_scale2:cuda())
output3 = cnn_model.modules[2]:get(3):forward(output1:cuda())
output4 = cnn_model.modules[2]:get(4):forward(output3:cuda())
output5 = cnn_model.modules[2]:get(5):forward(output4:cuda())
output7 = cnn_model.modules[2]:get(7):forward(output5:cuda())
output8 = cnn_model.modules[2]:get(8):forward(output7:cuda())
output9 = cnn_model.modules[2]:get(9):forward(output8:cuda())
output11 = cnn_model.modules[2]:get(11):forward(output9:cuda())
output12 = cnn_model.modules[2]:get(12):forward(output11:cuda())
output13 = cnn_model.modules[2]:get(13):forward(output12:cuda())
output15 = cnn_model.modules[2]:get(15):forward(output13:cuda())
output16 = cnn_model.modules[2]:get(16):forward(output15:cuda())
output_scale2 = cnn_model.modules[2]:get(17):forward(output16:cuda())
vt_maps_scale2[t] = output_scale2:double()
ft_maps_scale2[dataset.data_scale2[t][1]][dataset.data_scale2[t][2]] = t
end
print(t)
end
upsample_model2 = nn.SpatialUpSamplingNearest(2)
local ft_maps_scale2_upsampling = upsample_model2:forward(ft_maps_scale2:reshape(1, ft_maps_scale2:size()[1], ft_maps_scale2:size()[2]))
torch.save(paths.concat(opt.save, dataset.link[1]) .. 'maps1', ft_maps_scale1)
torch.save(paths.concat(opt.save, dataset.link[1]) .. 'maps2', ft_maps_scale2_upsampling:reshape(ft_maps_scale2_upsampling:size()[2],ft_maps_scale2_upsampling:size()[3]))
torch.save(paths.concat(opt.save, dataset.link[1]) .. 'list1', vt_maps_scale1)
torch.save(paths.concat(opt.save, dataset.link[1]) .. 'list2', vt_maps_scale2)
end
print 'preprocessing data (color space + normalization)'
collectgarbage()
-- preprocess trainSet
mean_scale1 = torch.load('/opt/zbstudio/myprograms/ReviseArchitecture/CNN3/mean1')
std_scale1 = torch.load('/opt/zbstudio/myprograms/ReviseArchitecture/CNN3/std1')
mean_scale2 = torch.load('/opt/zbstudio/myprograms/ReviseArchitecture/CNN3/mean2')
std_scale2 = torch.load('/opt/zbstudio/myprograms/ReviseArchitecture/CNN3/std2')
scale_yuvs_train1 = torch.Tensor(#images, 3,iheigh,iwidth):fill(0)
scale_yuvs_train2 = torch.Tensor(#images, 3,iheigh/2,iwidth/2):fill(0)
temp_scale_yuv1 = scale_yuvs_train1:clone()
temp_scale_yuv2 = scale_yuvs_train2:clone()
for i = 1,#images do
-- rgb -> yuv
local rgb = images[i]
local yuv = image.rgb2yuv(rgb)
--create 3 scaled tempatory images with the fixed size for all
temp_scale_yuv1[i] = image.scale(yuv, iwidth, iheigh)
temp_scale_yuv2[i] = image.scale(yuv, iwidth/2, iheigh/2)
-- transform into 3 scales --
scale_yuvs_train1[i] = copyMemmoryTensor(image.scale(yuv, yuv:size()[3], yuv:size()[2]), scale_yuvs_train1[i])
scale_yuvs_train2[i] = copyMemmoryTensor(image.scale(yuv, yuv:size()[3]*0.5, yuv:size()[2]*0.5), scale_yuvs_train2[i])
end
for i,channel in ipairs(channels) do
-- normalize each channel globally:
scale_yuvs_train1[{ {},i,{},{} }]:add(-mean_scale1[i])
scale_yuvs_train1[{ {},i,{},{} }]:div(std_scale1[i])
scale_yuvs_train2[{ {},i,{},{} }]:add(-mean_scale2[i])
scale_yuvs_train2[{ {},i,{},{} }]:div(std_scale2[i])
end
-- create dataset
for i = 5,#seg_images do
count_scale1 = 1
count_scale2 = 1
local scale1_seg_image = seg_images[i]
local scale2_seg_image = image.scale(seg_images[i], seg_images[i]:size()[3]/2, seg_images[i]:size()[2]/2)
for h=pheigh/2+1,(scale1_seg_image:size()[2]-pheigh/2 ) do
for w=pwidth/2+1,(scale1_seg_image:size()[3]-pwidth/2 ) do
-- extract label per pixel for scale1
if (math.floor(scale1_seg_image[1][h][w]*4+0.5)>=1) and (count_scale1<=trsize) then
inputData.data_scale1[count_scale1][1]=h
inputData.data_scale1[count_scale1][2]=w
inputData.link[count_scale1] = i
count_scale1 = count_scale1 + 1
end
-- extract label per pixel for scale2
if (h<=(scale2_seg_image:size()[2]-pheigh/2)) and (w<=(scale2_seg_image:size()[3]-pwidth/2))
and (math.floor(scale2_seg_image[1][h][w]*4+0.5)>=1) and (count_scale2<=trsize) then
inputData.data_scale2[count_scale2][1]=h
inputData.data_scale2[count_scale2][2]=w
count_scale2 = count_scale2 + 1
end
end
end
print(inputData.link[1] .. '.jpg')
image.save(paths.concat(opt.save, inputData.link[1]) .. '.jpg', scale_yuvs_train1[inputData.link[1]])
build_feature(inputData, count_scale1 - 1, count_scale2 - 1)
end
for i = 1,1 do
count_scale1 = 1
count_scale2 = 1
local scale1_seg_image = seg_images[i]
local scale2_seg_image = image.scale(seg_images[i], seg_images[i]:size()[3]/2, seg_images[i]:size()[2]/2)
for h=pheigh/2+1,(scale1_seg_image:size()[2]-pheigh/2 ) do
for w=pwidth/2+1,(scale1_seg_image:size()[3]-pwidth/2 ) do
-- extract label per pixel for scale1
if (math.floor(scale1_seg_image[1][h][w]*4+0.5)>=1) and (count_scale1<=trsize) then
inputData.data_scale1[count_scale1][1]=h
inputData.data_scale1[count_scale1][2]=w
inputData.link[count_scale1] = i
count_scale1 = count_scale1 + 1
end
-- extract label per pixel for scale2
if (h<=(scale2_seg_image:size()[2]-pheigh/2)) and (w<=(scale2_seg_image:size()[3]-pwidth/2))
and (math.floor(scale2_seg_image[1][h][w]*4+0.5)>=1) and (count_scale2<=trsize) then
inputData.data_scale2[count_scale2][1]=h
inputData.data_scale2[count_scale2][2]=w
count_scale2 = count_scale2 + 1
end
end
end
print(inputData.link[1] .. '.jpg')
image.save(paths.concat(opt.save, inputData.link[1]) .. '.jpg', scale_yuvs_train1[inputData.link[1]])
build_feature(inputData, count_scale1 - 1, count_scale2 - 1)
end