@@ -119,11 +119,11 @@ def nms(dets, thresh):
119
119
if scene_ids :
120
120
scene_ids_curr = set (scene_ids_curr ).intersection (scene_ids )
121
121
122
- # mode = 'render_train'
123
- mode = 'test'
122
+ mode = 'render_train'
123
+ # mode = 'test'
124
124
125
125
base_path = join (dp ['base_path' ], 'LCHF' )
126
- train_from_radius = 1000
126
+ train_from_radius = 500
127
127
if mode == 'render_train' :
128
128
start_time = time .time ()
129
129
visual = True
@@ -160,7 +160,7 @@ def nms(dets, thresh):
160
160
# Sample views
161
161
views , views_level = view_sampler .sample_views (min_n_views , radius ,
162
162
azimuth_range , elev_range ,
163
- tilt_range = (0 , 2 * math .pi ), tilt_step = 0.1 * math .pi )
163
+ tilt_range = (- math . pi / 2 , math .pi / 2 ), tilt_step = 0.2 * math .pi )
164
164
print ('Sampled views: ' + str (len (views )))
165
165
166
166
# Render the object model from all the views
@@ -210,41 +210,37 @@ def nms(dets, thresh):
210
210
cols = depth .shape [1 ]
211
211
# have read rgb, depth, pose, obj_bb, obj_id, bbox, mask here
212
212
213
- # 5 box
214
- for i in range (5 ):
215
- j = (i - (i % 2 ))/ 2
216
-
217
- # offset, width, height, depth
218
- offset1 = [int (i % 2 * cols / 2 ), int (j * rows / 2 ), int (cols / 2 ), int (rows / 2 )]
219
- if i == 4 :
220
- offset1 = [int (cols / 4 ), int (rows / 4 ), int (cols / 2 ), int (rows / 2 ), t [2 ]]
221
-
222
- rgb1 = rgb [offset1 [1 ]:(offset1 [1 ] + offset1 [3 ]), offset1 [0 ]:(offset1 [0 ] + offset1 [2 ]), :]
223
- depth1 = depth [offset1 [1 ]:(offset1 [1 ] + offset1 [3 ]), offset1 [0 ]:(offset1 [0 ] + offset1 [2 ])]
224
-
225
- visualized = False
226
- if visualized :
227
- rgb_ = np .copy (rgb )
228
- cv2 .rectangle (rgb_ , (offset1 [0 ], offset1 [1 ]),
229
- (offset1 [0 ] + offset1 [2 ], offset1 [1 ] + offset1 [3 ]), (0 , 0 , 255 ), 1 )
230
- cv2 .imshow ('rgb' , rgb_ )
231
- cv2 .imshow ('rgb1' , rgb1 )
232
- cv2 .waitKey (0 )
233
-
234
- LCHF_linemod_feat = cxxLCHF_pybind .Linemod_feature (rgb1 , depth1 )
235
- if LCHF_linemod_feat .constructEmbedding (): # extract template OK
236
- LCHF_linemod_feat .constructResponse () # extract response map for simi func
237
- else :
238
- # print('points not enough')
239
- continue # no enough points for template extraction, pass
240
-
241
- LCHF_linemod_feats .append (LCHF_linemod_feat ) # record feature
242
-
243
- LCHF_info = cxxLCHF_pybind .Info ()
244
- LCHF_info .rpy = (rotationMatrixToEulerAngles (R )).astype (np .float32 ) # make sure consistent
245
- LCHF_info .t = (np .array (offset1 )).astype (np .float32 )
246
- LCHF_info .id = str (obj_id )
247
- LCHF_infos .append (LCHF_info ) # record info
213
+ # 5x5 cm patch, stride 5, assume 1pix = 1mm in around 500mm depth
214
+ stride = 10
215
+ for row in range (0 , rows - 50 , stride ):
216
+ for col in range (0 , cols - 50 , stride ):
217
+ offset1 = [col , row , 50 , 50 ]
218
+ rgb1 = rgb [offset1 [1 ]:(offset1 [1 ] + offset1 [3 ]), offset1 [0 ]:(offset1 [0 ] + offset1 [2 ]), :]
219
+ depth1 = depth [offset1 [1 ]:(offset1 [1 ] + offset1 [3 ]), offset1 [0 ]:(offset1 [0 ] + offset1 [2 ])]
220
+
221
+ visualized = False
222
+ if visualized :
223
+ rgb_ = np .copy (rgb )
224
+ cv2 .rectangle (rgb_ , (offset1 [0 ], offset1 [1 ]),
225
+ (offset1 [0 ] + offset1 [2 ], offset1 [1 ] + offset1 [3 ]), (0 , 0 , 255 ), 1 )
226
+ cv2 .imshow ('rgb' , rgb_ )
227
+ cv2 .imshow ('rgb1' , rgb1 )
228
+ cv2 .waitKey (0 )
229
+
230
+ LCHF_linemod_feat = cxxLCHF_pybind .Linemod_feature (rgb1 , depth1 )
231
+ if LCHF_linemod_feat .constructEmbedding (): # extract template OK
232
+ LCHF_linemod_feat .constructResponse () # extract response map for simi func
233
+ else :
234
+ # print('points not enough')
235
+ continue # no enough points for template extraction, pass
236
+
237
+ LCHF_linemod_feats .append (LCHF_linemod_feat ) # record feature
238
+
239
+ LCHF_info = cxxLCHF_pybind .Info ()
240
+ LCHF_info .rpy = (rotationMatrixToEulerAngles (R )).astype (np .float32 ) # make sure consistent
241
+ LCHF_info .t = (np .array (offset1 )).astype (np .float32 )
242
+ LCHF_info .id = str (obj_id )
243
+ LCHF_infos .append (LCHF_info ) # record info
248
244
249
245
del rgb , depth , mask
250
246
@@ -258,7 +254,6 @@ def nms(dets, thresh):
258
254
forest = cxxLCHF_pybind .lchf_model_train (LCHF_linemod_feats , LCHF_infos )
259
255
cxxLCHF_pybind .lchf_model_saveForest (forest , base_path )
260
256
261
-
262
257
elapsed_time = time .time () - start_time
263
258
print ('train time: {}\n ' .format (elapsed_time ))
264
259
@@ -307,7 +302,7 @@ def nms(dets, thresh):
307
302
308
303
rows = depth .shape [0 ]
309
304
cols = depth .shape [1 ]
310
- stride = 3
305
+ stride = 5
311
306
312
307
# should be max_bbox * render_depth/max_scene_depth
313
308
width = 50 # bigger is OK, top left corner should align obj
@@ -345,47 +340,66 @@ def nms(dets, thresh):
345
340
start_time = time .time ()
346
341
print ('forest predict time: {}' .format (elapsed_time ))
347
342
348
- # voting isn't working well, and
349
- # should meanshift the leaf first
350
- num_x_bins = int (cols / 20 )
351
- num_y_bins = int (rows / 20 )
343
+ steps = 10
344
+ num_x_bins = int (cols / steps )
345
+ num_y_bins = int (rows / steps )
352
346
num_angle_bins = 10
353
347
348
+ print ('x_bins: {}, y_bins: {}' .format (num_x_bins , num_y_bins ))
349
+
354
350
votes = np .zeros (shape = (num_x_bins , num_y_bins , num_angle_bins , num_angle_bins , num_angle_bins ),
355
351
dtype = np .float32 )
356
352
353
+ voted_ids = {}
354
+
357
355
for scene_i in range (len (leaf_of_trees_of_scene )):
358
356
trees_of_scene = leaf_of_trees_of_scene [scene_i ]
359
357
roi = rois [scene_i ]
358
+
360
359
for tree_i in range (len (trees_of_scene )):
361
360
leaf_i = trees_of_scene [tree_i ]
362
- leaf_map = leaf_feats_map [tree_i ]
363
- predicted_ids = leaf_map [leaf_i ]
364
- for id_ in predicted_ids :
365
- info = LCHF_infos [id_ ]
366
- offset = info .t
367
- offset_x = offset [0 ] * train_from_radius / roi [4 ]
368
- offset_y = offset [1 ] * train_from_radius / roi [4 ]
369
-
370
- x = int ((roi [0 ] - offset_x ) / 20 )
371
- y = int ((roi [1 ] - offset_y ) / 20 )
372
- theta0 = int (info .rpy [0 ] / 2 / 3.14 * num_angle_bins )
373
- theta1 = int (info .rpy [1 ] / 2 / 3.14 * num_angle_bins )
374
- theta2 = int (info .rpy [2 ] / 2 / 3.14 * num_angle_bins )
375
-
376
- # votes[x-1:x+1, y-1:y+1, theta0-1:theta0+1, theta1-1:theta1+1, theta2-1:theta2+1] \
377
- # += 1.0/len(predicted_ids)/len(trees_of_scene)
378
- votes [x , y , theta0 , theta1 , theta2 ] \
379
- += 1.0 / len (predicted_ids )/ len (trees_of_scene )
361
+
362
+ # if leaf_i has predicted
363
+ if (tree_i , leaf_i ) in voted_ids :
364
+ votes += voted_ids [(tree_i , leaf_i )]
365
+ else :
366
+ # leaf_i votes
367
+ votes_local = np .zeros (
368
+ shape = (num_x_bins , num_y_bins , num_angle_bins , num_angle_bins , num_angle_bins ),
369
+ dtype = np .float32 )
370
+
371
+ leaf_map = leaf_feats_map [tree_i ]
372
+ predicted_ids = leaf_map [leaf_i ]
373
+ for id_ in predicted_ids :
374
+ info = LCHF_infos [id_ ]
375
+ offset = info .t
376
+ offset_x = offset [0 ] * train_from_radius / roi [4 ]
377
+ offset_y = offset [1 ] * train_from_radius / roi [4 ]
378
+
379
+ x = int ((roi [0 ] - offset_x ) / steps )
380
+ y = int ((roi [1 ] - offset_y ) / steps )
381
+ theta0 = int (info .rpy [0 ] / 2 / 3.14 * num_angle_bins )
382
+ theta1 = int (info .rpy [1 ] / 2 / 3.14 * num_angle_bins )
383
+ theta2 = int (info .rpy [2 ] / 2 / 3.14 * num_angle_bins )
384
+
385
+ # votes[x-1:x+1, y-1:y+1, theta0-1:theta0+1, theta1-1:theta1+1, theta2-1:theta2+1] \
386
+ # += 1.0/len(predicted_ids)/len(trees_of_scene)
387
+ votes_local [x , y , theta0 , theta1 , theta2 ] \
388
+ += 1.0 / len (predicted_ids ) / len (trees_of_scene )
389
+ votes += votes_local
390
+
391
+ # cache
392
+ voted_ids [(tree_i , leaf_i )] = votes_local
380
393
381
394
votes_sort_idx = np .dstack (np .unravel_index (np .argsort (votes .ravel ()), votes .shape ))
382
395
383
- top10 = 100
396
+ top10 = 10
397
+ if top10 > votes_sort_idx .shape [1 ]:
398
+ top10 = votes_sort_idx .shape [1 ]
399
+
400
+ print ('top {}' .format (top10 ))
384
401
for i in range (1 , top10 ):
385
- if 19 > votes_sort_idx [0 , - i , 0 ] > 1 and 19 > votes_sort_idx [0 , - i , 1 ] > 1 :
386
- cv2 .circle (rgb , (votes_sort_idx [0 , - i , 1 ]* 20 , votes_sort_idx [0 , - i , 0 ]* 20 ), 2 , (0 , 0 , 255 ), - 1 )
387
- print ('votes_sort_idx: {}, votes: {}' .format (votes_sort_idx [0 , - i , :],
388
- votes [tuple (votes_sort_idx [0 , - i , :])]))
402
+ cv2 .circle (rgb , (votes_sort_idx [0 , - i , 0 ]* steps , votes_sort_idx [0 , - i , 1 ]* steps ), 4 , (0 , 255 - i * 2 , 0 ), - 1 )
389
403
390
404
elapsed_time = time .time () - start_time
391
405
print ('voting time: {}' .format (elapsed_time ))
0 commit comments