-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_organizer_new.lua
429 lines (360 loc) · 16.6 KB
/
data_organizer_new.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
-- normalize the pos to [0,1]
require 'json'
require 'image'
require 'colormap'
require 'data_utils_new'
torch.setdefaulttensortype('torch.FloatTensor')
-- read json file
-- keyword
-- filename, class=image, annotations
-- for annotations: keyword
-- class = LeftClasperPoint, RightClasperPoint HeadPoint, ShaftPoint, TrackedPoint, EndPoint.
-- id = tool1, tool2
-- x y
local function readtoolLabelFile(label_file_tab)
local file_num = #label_file_tab
local multi_seq_anno_tab = {}
for seq_idx=1, file_num do
local jsonFilePath = label_file_tab[seq_idx]
local json_tab = json.load(jsonFilePath)
local frame_num = #json_tab
local anno_tab = {}
local anno_frame_num = 0
-- frame
for i=1, frame_num do
local frame_name = json_tab[i].filename
-- print('old frame ' .. frame_name)
-- point to new file location
frame_name = point2newFileLocation(frame_name, '/Users/xiaofeidu/mData', '/home/xiaofei/public_datasets')
frame_name = changeFrameFormat(frame_name, 'img_%06d_raw.png')
-- print('new frame ' .. frame_name)
local annotations = json_tab[i].annotations
if #annotations ~= 0 then
anno_frame_num = anno_frame_num + 1
anno_tab[anno_frame_num] = {}
anno_tab[anno_frame_num].filename = frame_name
local tool_ids = {}
-- reformat annotations: using joint class as key
local frame_anno = {}
for j=1, #annotations do
local joint_anno = annotations[j]
if frame_anno[joint_anno.class] == nil then
frame_anno[joint_anno.class] = {}
end
table.insert(frame_anno[joint_anno.class], { id = joint_anno.id,
x = joint_anno.x,
y = joint_anno.y
}
)
tool_ids[joint_anno.id] = true
end
anno_tab[anno_frame_num].annotations = frame_anno
anno_tab[anno_frame_num].jointNum = #annotations
local tool_num = 0
for __, __ in pairs(tool_ids) do
tool_num = tool_num + 1
end
anno_tab[anno_frame_num].toolNum = tool_num
end
end
-- normalize the location
for i=1, #anno_tab do
local frame_name = anno_tab[i].filename
local frame = image.load(frame_name, 3, 'byte')
local frame_width = frame:size(3)
local frame_height = frame:size(2)
local norm_frame_anno = normalizeToolPos01(frame_width, frame_height, anno_tab[i].annotations)
anno_tab[i].annotations = norm_frame_anno
end
table.insert(multi_seq_anno_tab, anno_tab)
end
return multi_seq_anno_tab
end
local function readOldToolLabelFile(label_file_tab)
local file_num = #label_file_tab
local multi_seq_anno_tab = {}
for seq_idx=1, file_num do
local jsonFilePath = label_file_tab[seq_idx]
local json_tab = json.load(jsonFilePath)
local frame_num = #json_tab
local anno_tab = {}
local anno_frame_num = 0
-- frame
for i=1, frame_num do
local frame_name = json_tab[i].filename
-- print('old frame ' .. frame_name)
-- point to new file location
frame_name = point2newFileLocation(frame_name, '/Users/xiaofeidu/mData', '/home/xiaofei/public_datasets')
frame_name = changeFrameFormat(frame_name, 'img_%06d_raw.png')
-- print('new frame ' .. frame_name)
local annotations = json_tab[i].annotations
if #annotations ~= 0 then
anno_frame_num = anno_frame_num + 1
anno_tab[anno_frame_num] = {}
anno_tab[anno_frame_num].filename = frame_name
local tool_ids = {}
-- reformat annotations: using joint class as key
local frame_anno = {}
for j=1, #annotations do
local joint_anno = annotations[j]
if frame_anno[joint_anno.class] == nil then
frame_anno[joint_anno.class] = {}
end
if joint_anno.id == 'tool1' then
table.insert(frame_anno[joint_anno.class], { id = joint_anno.id,
x = joint_anno.x,
y = joint_anno.y
}
)
tool_ids[joint_anno.id] = true
end
end
anno_tab[anno_frame_num].annotations = frame_anno
anno_tab[anno_frame_num].jointNum = #annotations
local tool_num = 0
for __, __ in pairs(tool_ids) do
tool_num = tool_num + 1
end
anno_tab[anno_frame_num].toolNum = tool_num
end
end
-- normalize the location
for i=1, #anno_tab do
local frame_name = anno_tab[i].filename
local frame = image.load(frame_name, 3, 'byte')
local frame_width = frame:size(3)
local frame_height = frame:size(2)
local norm_frame_anno = normalizeToolPos01(frame_width, frame_height, anno_tab[i].annotations)
anno_tab[i].annotations = norm_frame_anno
end
table.insert(multi_seq_anno_tab, anno_tab)
end
return multi_seq_anno_tab
end
local function readNewToolLabelFile(label_file_tab)
local file_num = #label_file_tab
local multi_seq_anno_tab = {}
for seq_idx=1, file_num do
local jsonFilePath = label_file_tab[seq_idx]
local json_tab = json.load(jsonFilePath)
local frame_num = #json_tab
local anno_tab = {}
local anno_frame_num = 0
-- frame
for i=1, frame_num do
local frame_name = json_tab[i].filename
-- print('old frame ' .. frame_name)
-- point to new file location
frame_name = point2newFileLocation(frame_name, '/Users/xiaofeidu/mData', '/home/xiaofei/public_datasets')
frame_name = changeFrameFormat(frame_name, 'img_%06d_raw.png')
-- print('new frame ' .. frame_name)
local annotations = json_tab[i].annotations
if #annotations ~= 0 then
anno_frame_num = anno_frame_num + 1
anno_tab[anno_frame_num] = {}
anno_tab[anno_frame_num].filename = frame_name
local tool_ids = {}
-- reformat annotations: using joint class as key
local frame_anno = {}
for j=1, #annotations do
local joint_anno = annotations[j]
if frame_anno[joint_anno.class] == nil then
frame_anno[joint_anno.class] = {}
end
if joint_anno.id == 'tool2' then
table.insert(frame_anno[joint_anno.class], { id = joint_anno.id,
x = joint_anno.x,
y = joint_anno.y
}
)
tool_ids[joint_anno.id] = true
end
end
anno_tab[anno_frame_num].annotations = frame_anno
anno_tab[anno_frame_num].jointNum = #annotations
local tool_num = 0
for __, __ in pairs(tool_ids) do
tool_num = tool_num + 1
end
anno_tab[anno_frame_num].toolNum = tool_num
end
end
-- normalize the location
for i=1, #anno_tab do
local frame_name = anno_tab[i].filename
local frame = image.load(frame_name, 3, 'byte')
local frame_width = frame:size(3)
local frame_height = frame:size(2)
local norm_frame_anno = normalizeToolPos01(frame_width, frame_height, anno_tab[i].annotations)
anno_tab[i].annotations = norm_frame_anno
end
table.insert(multi_seq_anno_tab, anno_tab)
end
return multi_seq_anno_tab
end
-- seperate the data into train and validation set for single sequence
local function sepTrainingData(anno_tab, train_percentage)
train_percentage = train_percentage or 0.8
local anno_frame_num = #anno_tab
assert(anno_frame_num >= 1)
local train_anno_tab = {}
local val_anno_tab = {}
local train_anno_frame_num = math.max(math.floor(train_percentage * anno_frame_num), 1)
for i=1, train_anno_frame_num do
table.insert(train_anno_tab, anno_tab[i])
end
for i=train_anno_frame_num+1, anno_frame_num do
table.insert(val_anno_tab, anno_tab[i])
end
return train_anno_tab, val_anno_tab
end
-- seperate the data into train and validation set for multiple sequence (internal sequence 80% : 20%)
local function internalSepTrainingData(multi_seq_anno_tab, train_percentage)
train_percentage = train_percentage or 0.8
local seq_num = #multi_seq_anno_tab
local train_anno_tab = {}
local val_anno_tab = {}
for seq_idx=1, seq_num do
local anno_tab = multi_seq_anno_tab[seq_idx]
local anno_frame_num = #anno_tab
assert(anno_frame_num >= 1)
local train_anno_frame_num = math.max(math.floor(train_percentage * anno_frame_num), 1)
for i=1, train_anno_frame_num do
table.insert(train_anno_tab, anno_tab[i])
end
for i=train_anno_frame_num+1, anno_frame_num do
table.insert(val_anno_tab, anno_tab[i])
end
print(train_anno_frame_num, anno_frame_num - train_anno_frame_num)
end
return train_anno_tab, val_anno_tab
end
local function internalRandomSepTrainingData(multi_seq_anno_tab, train_percentage)
train_percentage = train_percentage or 0.8
local seq_num = #multi_seq_anno_tab
local train_anno_tab = {}
local val_anno_tab = {}
for seq_idx=1, seq_num do
local anno_tab = multi_seq_anno_tab[seq_idx]
local anno_frame_num = #anno_tab
assert(anno_frame_num >= 1)
local perm = torch.randperm(anno_frame_num)
local train_anno_frame_num = math.max(math.floor(train_percentage * anno_frame_num), 1)
for i=1, train_anno_frame_num do
table.insert(train_anno_tab, anno_tab[perm[i]])
end
for i=train_anno_frame_num+1, anno_frame_num do
table.insert(val_anno_tab, anno_tab[perm[i]])
end
print(train_anno_frame_num, anno_frame_num - train_anno_frame_num)
end
return train_anno_tab, val_anno_tab
end
-- seq_info_tab = {{seqDir=, frameFormat=, startFrame=, endFrame=}}
local function genTestData(seq_info_tab)
local seq_num = #seq_info_tab
local anno_tab = {}
for seq_idx=1, seq_num do
local seq_info = seq_info_tab[seq_idx]
local frame_num = seq_info.endFrame - seq_info.startFrame + 1
for frame_idx=1, frame_num do
local frame_anno = {}
frame_anno.filename = paths.concat(seq_info.seqDir, string.format(seq_info.frameFormat, frame_idx))
frame_anno.annotations = nil
frame_anno.jointNum = nil
frame_anno.toolNum = nil
table.insert(anno_tab, frame_anno)
end
end
return anno_tab
end
local joint_names = {'LeftClasperPoint', 'RightClasperPoint',
'HeadPoint', 'ShaftPoint',
'TrackedPoint', 'EndPoint' } -- joint number = 6
-- train dataset
--local trainBaseDir = '/home/xiaofei/public_datasets/MICCAI_tool/Tracking_Robotic_Training/tool_label'
--local json_files = {}
--for seq_idx=1, 4 do
---- local json_file_path = paths.concat(trainBaseDir, 'endo' .. seq_idx .. '_labels.json') -- original label
-- local json_file_path = paths.concat(trainBaseDir, 'train' .. seq_idx .. '_labels.json') -- improved label (head)
-- table.insert(json_files, json_file_path)
--end
--local anno_tab = readtoolLabelFile(json_files)
--local train_anno_tab, val_anno_tab = internalSepTrainingData(anno_tab)
--print(#train_anno_tab)
--print(#val_anno_tab)
--torch.save(paths.concat(trainBaseDir, 'train_endo_toolpos_head.t7'), train_anno_tab)
--torch.save(paths.concat(trainBaseDir, 'val_endo_toolpos_head.t7'), val_anno_tab)
--print('===========================================================================')
-- ---------------------------------------------------------------------------------------------------
-- in vivo
local invivoBaseDir = '/home/xiaofei/public_datasets/MICCAI_tool/Test_data/tool_label'
local invivo_json_files = {}
for seq_idx = 1, 3 do
local json_file_path = paths.concat(invivoBaseDir, 'invivo' .. seq_idx .. '_labels.json')
table.insert(invivo_json_files, json_file_path)
end
--local anno_tab = readtoolLabelFile(invivo_json_files)
--local train_anno_tab, val_anno_tab = internalRandomSepTrainingData(anno_tab, 0.8)
--print(#train_anno_tab, #val_anno_tab)
--torch.save(paths.concat(invivoBaseDir, 'train_invivo2_toolpos.t7'), train_anno_tab)
--torch.save(paths.concat(invivoBaseDir, 'val_invivo2_toolpos.t7'), val_anno_tab)
local exvivoBaseDir = '/home/xiaofei/public_datasets/MICCAI_tool/Tracking_Robotic_Testing/tool_label'
local exvivo_json_files = {}
for seq_dix = 1, 6 do
local json_file_path = paths.concat(exvivoBaseDir, 'test' .. seq_dix .. '_labels_head.json')
table.insert(exvivo_json_files, json_file_path)
end
--local anno_tab = readtoolLabelFile(exvivo_json_files)
--local test_anno_tab = internalSepTrainingData(anno_tab, 1)
--print(#test_anno_tab)
--print(paths.concat(exvivoBaseDir, 'test_endo_toolpos_head.t7'))
--torch.save(paths.concat(exvivoBaseDir, 'test_endo_toolpos_head.t7'), test_anno_tab)
-- ----------------------------------------------------------------------------------------------------
-- seq new instrument and old instrument
local new_exvivoBaseDir = '/home/xiaofei/public_datasets/MICCAI_tool/Tracking_Robotic_Testing/tool_label'
local pure_old_exvivo_json_files = {}
for seq_dix = 1, 4 do
local json_file_path = paths.concat(exvivoBaseDir, 'test' .. seq_dix .. '_labels_head.json')
table.insert(pure_old_exvivo_json_files, json_file_path)
end
local pure_old_anno_tab = readtoolLabelFile(pure_old_exvivo_json_files)
local pure_old_test_anno_tab = internalSepTrainingData(pure_old_anno_tab, 1)
print('seq 1-4 old data num: ' .. #pure_old_test_anno_tab)
local new_exvivo_json_files = {}
for seq_dix = 5, 6 do
local json_file_path = paths.concat(exvivoBaseDir, 'test' .. seq_dix .. '_labels_head.json')
table.insert(new_exvivo_json_files, json_file_path)
end
local old_anno_tab = readOldToolLabelFile(new_exvivo_json_files)
local new_anno_tab = readNewToolLabelFile(new_exvivo_json_files)
local old_test_anno_tab = internalSepTrainingData(old_anno_tab, 1)
local new_test_anno_tab = internalSepTrainingData(new_anno_tab, 1)
print('seq 5-6 old data num: ' .. #old_test_anno_tab)
print('seq 5-6 new data num: ' .. #new_test_anno_tab)
local all_old_test_anno_tab = {}
for i=1, #pure_old_test_anno_tab do
table.insert(all_old_test_anno_tab, pure_old_test_anno_tab[i])
end
for i=1, #old_test_anno_tab do
table.insert(all_old_test_anno_tab, old_test_anno_tab[i])
end
print('seq 1-6 old data num: ' .. #all_old_test_anno_tab)
print(paths.concat(exvivoBaseDir, 'test_old_endo_toolpos_head.t7'))
torch.save(paths.concat(exvivoBaseDir, 'test_old_endo_toolpos_head.t7'), all_old_test_anno_tab)
print(paths.concat(exvivoBaseDir, 'test_new_endo_toolpos_head.t7'))
torch.save(paths.concat(exvivoBaseDir, 'test_new_endo_toolpos_head.t7'), new_test_anno_tab)
-- ---------------------------------------------------------------------------------------
-- -- icl data
--local iclBaseDir = '/home/xiaofei/public_datasets/MICCAI_tool/Test_data/tool_label'
--local icl_json_files = {}
--for seq_idx = 2,3 do
-- local json_file_path = paths.concat(iclBaseDir, 'icl_data' .. seq_idx .. '_labels.json')
-- table.insert(icl_json_files, json_file_path)
--end
--
--local anno_tab = readtoolLabelFile(icl_json_files)
--local train_anno_tab, val_anno_tab = internalSepTrainingData(anno_tab)
--print(#train_anno_tab, #val_anno_tab)
--torch.save(paths.concat(iclBaseDir, 'train_icl_toolpos.t7'), train_anno_tab)
--torch.save(paths.concat(iclBaseDir, 'val_icl_toolpos.t7'), val_anno_tab)