-
Notifications
You must be signed in to change notification settings - Fork 16
/
dataset_2d.lua
357 lines (302 loc) · 10.6 KB
/
dataset_2d.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
require 'torch'
torch.setdefaulttensortype('torch.FloatTensor')
require 'hdf5'
local ffi = require 'ffi'
local argcheck = require 'argcheck'
paths.dofile('models/alignet.lua')
require 'image'
local dataset = torch.class('dataLoader')
local initcheck = argcheck{
pack=true,
help=[[
A dataset class for hdf5 files]],
{name="dataPath",
type="string",
help="path of the rendered HDF5 data"},
{name="inputDataKey",
type="string",
help="key name of the input data"},
{name="labelDataKey",
type="string",
help="key name of the label (warp) data"},
{name="mean",
type="table",
help="table of mean data values",
opt = true},
{name="std",
type="table",
help="table of std data values",
opt = true},
{name="test",
type="boolean",
default=false,
help="boolean indicating whether test set"},
}
function dataset:__init(...)
-- argcheck
local args = initcheck(...)
print(args)
for k,v in pairs(args) do self[k] = v end
-- construct list of train/test files
if self.test then
self.data_file = paths.concat(self.dataPath,'testfiles.txt')
else
self.data_file = paths.concat(self.dataPath,'trainfiles.txt')
end
if not paths.filep(self.data_file) then
error("the file", self.data_file, "doesnt exist!")
end
self._data_list = readtxtfile(self.data_file)
self._nSamples = #self._data_list
-- constructors
self:set_imsz()
self._dim2 = 2
-- initializing mean, std
if not self.mean then
print('no mean assigned, setting to 0')
local mean = {}
for i=1,self._dim2 do mean[i] = 0 end
self.mean = mean
end
if not self.std then
print('no std assigned, setting to 1')
local std = {}
for i=1,self._dim2 do std[i] = 1 end
self.std = std
end
if self.test then
self.testcounter = 1
self:compute_test_permutations()
end
end
--------------------------------------------------------------
------------------------ MAIN ROUTINES -----------------------
--------------------------------------------------------------
-- main wrapper to randomly sample data -- used during train / test
function dataset:sample(quantity)
local sidx, tidx
if self.test then
sidx, tidx = self:testidx(quantity)
else
sidx, tidx = self:randidx(quantity)
end
-- get the data at these indexes
local data = self:get_data(sidx, tidx)
if not self.test then
local SRng = {torch.normal(1,0.1), torch.normal(1,0.1)}
local TRng = {0, 0}
data[{{},{1},{},{}}] = self:aug_affine_warp(data[{{},{1},{},{}}], {0, 0}, TRng, SRng) -- changed to 0/0 ...
data[{{},{2},{},{}}] = self:aug_affine_warp(data[{{},{2},{},{}}], {0, 0}, TRng, SRng)
end
-- partial data on the fly
data, targetImgs = self:partialData_train(data)
local targetImgs = torch.repeatTensor(targetImgs, 1, 2, 1, 1)
-- get sourceImgs before normalizing by mu/std
local sourceImgs = data[{{},{1},{},{}}]:clone()
if self.test then
sourceImgs = self:addtexture(sourceImgs)
else
sourceImgs = torch.repeatTensor(sourceImgs, 1, 2, 1, 1)
end
-- normalize by mean and std
data = self:muStdNorm(data)
-- create sampling grid to upsample cage
local fieldI = interpField(data:size(1), {self._imsz[1], self._imsz[2]})
collectgarbage()
return data, fieldI, sourceImgs, targetImgs
end
function dataset:testidx(quantity)
local nextcounter = math.min(self.testcounter+ quantity-1,self._nTest)
local sidx = self.sidx[{{self.testcounter, nextcounter}}]
local tidx = self.tidx[{{self.testcounter, nextcounter}}]
if nextcounter == self._nTest then
self.testcounter = 1
else
self.testcounter = nextcounter
end
return sidx, tidx
end
function dataset:randidx(quantity)
-- generate random source/target idxs
local sidx, tidx = torch.Tensor(), torch.Tensor()
while sidx:numel() < quantity do
-- jumble samples for source & target
sidx = torch.cat(sidx,torch.randperm(self._nSamples))
tidx = torch.cat(tidx,torch.randperm(self._nSamples))
-- truncate up to quantity
sidx = sidx[{{1,math.min(sidx:numel(), quantity)}}]
tidx = tidx[{{1,math.min(tidx:numel(), quantity)}}]
end
return sidx, tidx
end
function dataset:compute_test_permutations()
-- compute all permutations of 2 for entire test set (all images source to target, etc)
local sids, tids= {}, {}
local xx,yy = meshgrid(torch.range(1,#self._data_list), torch.range(1,#self._data_list))
local NN = xx:numel()
local sids_ = yy:view(NN,1)
local tids_ = xx:view(NN,1)
-- only keep pairs which are not the same
for i = 1,NN do
if tids_[i][1] ~= sids_[i][1] then
table.insert(sids,sids_[i][1])
table.insert(tids,tids_[i][1])
end
end
self.sidx = torch.Tensor(sids)
self.tidx = torch.Tensor(tids)
self._nTest = self.sidx:numel()
end
function dataset:size()
return self._nTest
end
--------------------------------------------------------------
---------------------- MAIN HELPER ROUTINES ------------------
--------------------------------------------------------------
function dataset:get_data(sids, tids)
local B = sids:numel()
-- allocate data size
local data = torch.zeros(B, self._dim2, self._imsz[1], self._imsz[2])
for i = 1,B do
-- get source sil
local src_datalist = self.s_data_list or self._data_list
local sim = self:get_sil(sids[i], src_datalist)
-- get a target sil
local tgt_datalist = self.t_data_list or self._data_list
local tim = self:get_sil(tids[i], tgt_datalist)
-- drop into tensors for saving
data[{{i},{1},{},{}}] = sim
data[{{i},{2},{},{}}] = tim
end
collectgarbage()
return data
end
function dataset:get_sil(id, data_list)
local HDF5file = paths.concat(self.dataPath, data_list[id])
local myFile = hdf5.open(HDF5file,'r')
local sil = myFile:read(self.inputDataKey):all()
myFile:close()
sil = sil:transpose(1,2)
return sil
end
function dataset:muStdNorm(data)
for i=1,data:size(2) do -- channels
data[{{},{i},{},{}}]:add(-self.mean[i])
data[{{},{i},{},{}}]:div(self.std[i])
end
return data
end
--------------------------------------------------------------
--------------------- DATA ROUTINES --------------------------
--------------------------------------------------------------
function dataset:get_texture(sil_S) -- to-do: update for several diff source
local txtimage_
if self.s_data_list then
local hdf5file = paths.concat(self.dataPath, self.s_data_list[1])
local myFile = hdf5.open(hdf5file,'r')
txtimage_ = myFile:read('/txtr/view1'):all()
txtimage_ = txtimage_:transpose(2,3)
txtimage_ = txtimage_:float()
myFile:close()
else
txtimage_ = image.load('utils/checker.png')
end
local txtimage = torch.repeatTensor(txtimage_,sil_S:size(1),1,1,1)
return txtimage
end
function dataset:addtexture(sil_S)
-- read in checker image and make it size of 2nd slot...
local checkerim = self:get_texture(sil_S)
local tsz = {checkerim:size(3),checkerim:size(4)}
if tsz[1] ~= self._imsz[1] or tsz[2] ~= self._imsz[2] then
checkerim = sil_S:clone()
checkerim = torch.repeatTensor(checkerim, 1,3,1,1)
end
local ch = sil_S:size(2)
local text_ch = checkerim:size(2)
local sourceImgs = sil_S:clone()
sourceImgs = torch.repeatTensor(sourceImgs, 1, text_ch+1, 1, 1)
sourceImgs[{{},{2,text_ch+1},{},{}}] = torch.cmul(sourceImgs[{{},{2,text_ch+1},{},{}}], checkerim)
return sourceImgs
end
function dataset:create_T(B,RRng, TRng, SRng)
local T = torch.CudaTensor(B, 2,3):zero()
local Sx = SRng[1] + (SRng[2]-SRng[1])*torch.rand(B)
local Sy = SRng[1] + (SRng[2]-SRng[1])*torch.rand(B)
local R = RRng[1] + (RRng[2]-RRng[1])*torch.rand(B)
local r11 = torch.cmul(Sx,torch.cos(R))
local r21 = torch.cmul(Sy,torch.sin(R))
local r22 = torch.cmul(Sy,torch.cos(R))
local r12 = torch.cmul(Sx,-torch.sin(R))
local tx = torch.cmul(Sx, TRng[1] + (TRng[2]-TRng[1])*torch.rand(B))
local ty = torch.cmul(Sy, TRng[1] + (TRng[2]-TRng[1])*torch.rand(B))
T[{{},{1},{1}}] = torch.ones(B,1,1):cmul(Sx):cuda()
T[{{},{2},{2}}] = torch.ones(B,1,1):cmul(Sy):cuda()
T[{{},{1},{3}}] = torch.ones(B,1,1):cmul(tx):cuda()
T[{{},{2},{3}}] = torch.ones(B,1,1):cmul(ty):cuda()
T[{{},{1},{1}}] = r11:cuda()
T[{{},{2},{1}}] = r21:cuda()
T[{{},{2},{2}}] = r22:cuda()
T[{{},{1},{2}}] = r12:cuda()
return T
end
-- affine warp images
function dataset:aug_affine_warp(inputs, RRng, TRng, SRng)
-- Sx and Sy are torch tensors that vectors of length batchSize
-- i.e., torch.ones(batchSize) does not perform any augmentation
local B = inputs:size(1)
local csz = inputs:size(inputs:dim())
-- generate affine full warp field
local smallGrid = nn.Sequential()
smallGrid:add(nn.AffineGridGeneratorBHWD(csz,csz))
smallGrid = smallGrid:cuda()
local T = self:create_T(B,RRng, TRng, SRng)
local warpC = smallGrid:forward(T)
warpC = warpC:transpose(4,2):transpose(3,4)
-- warp images with it
local SIL = torch.Tensor(inputs,1,2,1,1):cuda()
local lookup = bilinearSampler()
local SIL_warped = lookup:forward({SIL, warpC})
SIL_warped = SIL_warped:float()
SIL_warped = SIL_warped[{{},{1},{},{}}]
collectgarbage()
return SIL_warped
end
function dataset:partialData_train(inputs)
local B = inputs:size(1)
-- create targetImgs
local targetImgs = inputs[{{},{2},{},{}}]:clone()
local partialTargetImgs = inputs[{{},{2},{},{}}]
-- uniform range of half win size
local pWinSz = {30, 40}
-- vector of window (of hole) sizes
local WIN = ((pWinSz[2] - pWinSz[1])*torch.rand(B,1) + pWinSz[1]):round()
local numHoles = 1
local x,y = meshgrid(torch.range(1,self._imsz[1]), torch.range(1,self._imsz[2]))
for i = 1,B do
local partialImg = partialTargetImgs[{{i},{1},{},{}}]
for j =1,numHoles do
local win = WIN[i][1]
local Y = y[partialImg:squeeze():gt(0)]
local X = x[partialImg:squeeze():gt(0)]
local ii = torch.random(1,Y:size(1))
local XY = {Y[ii], X[ii]}
local xR = torch.Tensor({XY[2] - win, XY[2] + win}):clamp(1,self._imsz[1])
local yR = torch.Tensor({XY[1] - win, XY[1] + win}):clamp(1,self._imsz[2])
partialImg[{{},{},{xR[1],xR[2]},{yR[1],yR[2]}}] = 0
end
end
collectgarbage()
return inputs, targetImgs
end
--------------------------------------------------------------
------------------------ CONSTRUCTORS ------------------------
--------------------------------------------------------------
function dataset:set_imsz()
local HDF5file = paths.concat(self.dataPath, self._data_list[1])
local myFile = hdf5.open(HDF5file,'r')
local imageSize = myFile:read(self.inputDataKey):dataspaceSize()
myFile:close()
self._imsz = imageSize
end
return dataset