@@ -295,3 +295,64 @@ def __call__(self, results):
295
295
results [self .item ] = torch .from_numpy (seq )
296
296
297
297
return results
298
+
299
+
300
+ @PIPELINES .register_module ()
301
+ class Generate3DHeatmapTarget :
302
+ """Generate the target 3d heatmap.
303
+
304
+ Required keys: 'joints_3d', 'joints_3d_visible', 'ann_info'.
305
+ Modified keys: 'target', and 'target_weight'.
306
+
307
+ Args:
308
+ sigma: Sigma of heatmap gaussian.
309
+ joint_indices (list): Indices of joints used for heatmap generation.
310
+ If None (default) is given, all joints will be used.
311
+ """
312
+
313
+ def __init__ (self , sigma = 2 , joint_indices = None ):
314
+ self .sigma = sigma
315
+ self .joint_indices = joint_indices
316
+
317
+ def __call__ (self , results ):
318
+ """Generate the target heatmap."""
319
+ joints_3d = results ['joints_3d' ]
320
+ joints_3d_visible = results ['joints_3d_visible' ]
321
+ cfg = results ['ann_info' ]
322
+ image_size = cfg ['image_size' ]
323
+ W , H , D = cfg ['heatmap_size' ]
324
+ heatmap3d_depth_bound = cfg ['heatmap3d_depth_bound' ]
325
+ joint_weights = cfg ['joint_weights' ]
326
+ use_different_joint_weights = cfg ['use_different_joint_weights' ]
327
+
328
+ if self .joint_indices is not None :
329
+ joints_3d = joints_3d [self .joint_indices , ...]
330
+ joints_3d_visible = joints_3d_visible [self .joint_indices , ...]
331
+ joint_weights = joint_weights [self .joint_indices , ...]
332
+
333
+ mu_x = joints_3d [:, 0 ] * W / image_size [0 ]
334
+ mu_y = joints_3d [:, 1 ] * H / image_size [1 ]
335
+ mu_z = (joints_3d [:, 2 ] / heatmap3d_depth_bound + 0.5 ) * D
336
+
337
+ target_weight = joints_3d_visible [:, 0 ]
338
+ target_weight = target_weight * (mu_z >= 0 ) * (mu_z < D )
339
+ if use_different_joint_weights :
340
+ target_weight = target_weight * joint_weights
341
+ target_weight = target_weight [:, None ]
342
+
343
+ x , y , z = np .arange (W ), np .arange (H ), np .arange (D )
344
+ zz , yy , xx = np .meshgrid (z , y , x )
345
+ xx = xx [None , ...].astype (np .float32 )
346
+ yy = yy [None , ...].astype (np .float32 )
347
+ zz = zz [None , ...].astype (np .float32 )
348
+
349
+ mu_x = mu_x [..., None , None , None ]
350
+ mu_y = mu_y [..., None , None , None ]
351
+ mu_z = mu_z [..., None , None , None ]
352
+
353
+ target = np .exp (- ((xx - mu_x )** 2 + (yy - mu_y )** 2 + (zz - mu_z )** 2 ) /
354
+ (2 * self .sigma ** 2 ))
355
+
356
+ results ['target' ] = target
357
+ results ['target_weight' ] = target_weight
358
+ return results
0 commit comments