in tensorflow_graphics/projects/points_to_3Dobjects/utils/evaluator.py [0:0]
def add_detections(self, sample, detections):
"""Add detections to evaluation.
Args:
sample: the ground truth information
detections: the predicted detections
Returns:
dict of intermediate results.
"""
result_dict = {'iou_mean': -1, 'iou_min': -1, 'collisions': 0,
'collision_intersection': 0, 'collision_iou': 0}
num_boxes = sample['num_boxes'].numpy()
for _, metric in self.metrics.items():
if isinstance(metric, ShapeAccuracyMetric):
labels = sample['shapes']
weights = tf.math.sign(labels + 1) # -1 is mapped to zero, else 1
metric.update(labels, detections['shapes_logits'], weights)
elif isinstance(metric, BoxIoUMetric):
scene_id = str(sample['scene_filename'].numpy(), 'utf-8')
# Get ground truth boxes
labeled_boxes = tf.gather(
sample['groundtruth_boxes'], axis=1, indices=[1, 0, 3, 2]) * 256.0
if metric.threed:
rotations_y = tf.concat([tf_utils.euler_from_rotation_matrix(
tf.reshape(detections['rotations_3d'][i], [3, 3]),
1) for i in range(num_boxes)], axis=0)
rotations_y = tf.reshape(rotations_y, [-1, 1])
labeled_boxes = tf.concat([sample['translations_3d'],
sample['sizes_3d'],
rotations_y], axis=1)
# Get predicted boxes
predicted_boxes = detections['detection_boxes']
if metric.threed:
rotations_y = tf.concat([tf_utils.euler_from_rotation_matrix(
tf.reshape(detections['rotations_3d'][i], [3, 3]),
1) for i in range(num_boxes)], axis=0)
rotations_y = tf.reshape(rotations_y, [-1, 1])
predicted_boxes = tf.concat([detections['translations_3d'],
detections['sizes_3d'],
rotations_y], axis=1)
labeled_classes = tf.cast(sample['groundtruth_valid_classes'], tf.int64)
predicted_classes = tf.cast(detections['detection_classes'], tf.int64)
confidences = detections['detection_scores']
metric.update(scene_id, labeled_boxes, labeled_classes, predicted_boxes,
predicted_classes, confidences)
elif isinstance(metric, IoUMetric):
classes = sample['classes']
mesh_names = sample['mesh_names']
labeled_sdfs = []
for i in range(num_boxes):
class_id = str(classes[i].numpy()).zfill(8)
model_name = str(mesh_names[i].numpy(), 'utf-8')
path_prefix = os.path.join(self.shapenet_dir, class_id, model_name)
file_sdf = os.path.join(path_prefix, 'model_normalized_sdf.npy')
with gfile.Open(file_sdf, 'rb') as f:
labeled_sdfs.append(tf.expand_dims(np.load(f).astype(np.float32),
0))
labeled_sdfs = tf.concat(labeled_sdfs, axis=0)
labeled_classes = tf.cast(sample['groundtruth_valid_classes'], tf.int64)
labeled_permutation = np.argsort(labeled_classes)
labeled_sdfs = labeled_sdfs.numpy()[labeled_permutation]
labeled_classes = labeled_classes.numpy()[labeled_permutation]
labeled_rotations_3d = sample['rotations_3d'].numpy()
labeled_rotations_3d = labeled_rotations_3d[labeled_permutation]
labeled_translations_3d = sample['translations_3d'].numpy()
labeled_translations_3d = labeled_translations_3d[labeled_permutation]
labeled_sizes_3d = sample['sizes_3d'].numpy()[labeled_permutation]
labeled_poses = (labeled_rotations_3d, labeled_translations_3d,
labeled_sizes_3d)
# Predictions
predicted_classes = tf.cast(detections['detection_classes'], tf.int64)
predicted_permutation = np.argsort(predicted_classes)
predicted_classes = predicted_classes.numpy()[predicted_permutation]
predicted_sdfs = \
detections['predicted_sdfs'].numpy()[predicted_permutation]
predicted_rotations_3d = \
detections['rotations_3d'].numpy()[predicted_permutation]
predicted_translations_3d = \
detections['translations_3d'].numpy()[predicted_permutation]
predicted_sizes_3d = \
detections['sizes_3d'].numpy()[predicted_permutation]
predicted_poses = (predicted_rotations_3d, predicted_translations_3d,
predicted_sizes_3d)
full_oracle = False
if full_oracle:
predicted_sdfs = detections['groundtruth_sdfs'].numpy()
predicted_sdfs = predicted_sdfs[labeled_permutation]
predicted_classes = labeled_classes
predicted_poses = labeled_poses
print('----------------------------')
print(predicted_sdfs.shape)
print(predicted_classes.shape)
print(predicted_poses[0].shape)
print(predicted_poses[1].shape)
print(predicted_poses[2].shape)
pose_oracle = False
if pose_oracle:
predicted_sdfs = detections['predicted_sdfs'].numpy()
predicted_sdfs = predicted_sdfs[predicted_permutation]
predicted_poses = (labeled_rotations_3d, labeled_translations_3d,
labeled_sizes_3d)
class_oracle = True
if class_oracle:
predicted_classes *= 0
labeled_classes *= 0
iou_mean, iou_min = metric.update(
labeled_sdfs, labeled_classes, labeled_poses, predicted_sdfs,
predicted_classes, predicted_poses, sample['dot'])
result_dict['iou_mean'] = iou_mean
result_dict['iou_min'] = iou_min
elif isinstance(metric, CollisionMetric):
labeled_sdfs = detections['groundtruth_sdfs']
labeled_classes = tf.cast(sample['groundtruth_valid_classes'], tf.int64)
labeled_poses = (sample['rotations_3d'],
sample['translations_3d'],
sample['sizes_3d'])
predicted_classes = tf.cast(detections['detection_classes'], tf.int64)
predicted_sdfs = detections['predicted_sdfs']
predicted_poses = (detections['rotations_3d'],
detections['translations_3d'],
detections['sizes_3d'])
full_oracle = False
if full_oracle:
predicted_sdfs = detections['groundtruth_sdfs'].numpy()
predicted_classes = labeled_classes
predicted_poses = labeled_poses
num_collisions, intersection, iou = metric.update(
labeled_sdfs, labeled_classes, labeled_poses,
predicted_sdfs, predicted_classes, predicted_poses)
result_dict['collisions'] = num_collisions
result_dict['collision_intersection'] = intersection
result_dict['collision_iou'] = iou
return result_dict