Example #1
0
            example_np, flow_color_np = sess.run([example, flow_color])
            img_id_np = i  #example_np['filename']
            image_np = example_np['image']
            gt_boxes_np = example_np['groundtruth_boxes']
            gt_classes_np = example_np['groundtruth_classes']
            gt_masks_np = example_np['groundtruth_instance_masks']
            height, width = image_np.shape[:2]
            num_instances_np = gt_masks_np.shape[0]
            image_np = np.squeeze(image_np)
            depth_np = example_np['groundtruth_depth']
            gt_motions_np = example_np['groundtruth_instance_motions']

            composed_flow_color_np, flow_error_np = visualize_flow(
                depth_np,
                gt_motions_np,
                np.ones([gt_motions_np.shape[0]]),
                example_np['groundtruth_camera_motion'],
                example_np['camera_intrinsics'],
                masks=gt_masks_np,
                groundtruth_flow=example_np['groundtruth_flow'])

            # motion gt summary
            gt_rot = np.reshape(gt_motions_np[:, :9], [-1, 3, 3])
            gt_trans = np.reshape(gt_motions_np[:, 9:12], [-1, 3])
            print(gt_rot.shape)
            mean_rot_angle = np.mean(np.degrees(_rotation_angle(gt_rot)))
            mean_trans = np.mean(np.linalg.norm(gt_trans))

            print(
                'image_id: {}, instances: {}, shape: {}, rot(deg): {}, trans: {}'
                .format(img_id_np, num_instances_np, image_np.shape,
                        mean_rot_angle, mean_trans))
Example #2
0
def visualize_detection_results(result_dict,
                                tag,
                                global_step,
                                categories,
                                summary_dir='',
                                export_dir='',
                                agnostic_mode=False,
                                show_groundtruth=False,
                                min_score_thresh=.5,
                                max_num_predictions=20,
                                sess=None):
    """Visualizes detection results and writes visualizations to image summaries.
    This function visualizes an image with its detected bounding boxes and writes
    to image summaries which can be viewed on tensorboard.  It optionally also
    writes images to a directory. In the case of missing entry in the label map,
    unknown class name in the visualization is shown as "N/A".
    Args:
      result_dict: a dictionary holding groundtruth and detection
        data corresponding to each image being evaluated.  The following keys
        are required:
          'original_image': a numpy array representing the image with shape
            [1, height, width, 3]
          'detection_boxes': a numpy array of shape [N, 4]
          'detection_scores': a numpy array of shape [N]
          'detection_classes': a numpy array of shape [N]
        The following keys are optional:
          'groundtruth_boxes': a numpy array of shape [N, 4]
          'groundtruth_keypoints': a numpy array of shape [N, num_keypoints, 2]
        Detections are assumed to be provided in decreasing order of score and for
        display, and we assume that scores are probabilities between 0 and 1.
      tag: tensorboard tag (string) to associate with image.
      global_step: global step at which the visualization are generated.
      categories: a list of dictionaries representing all possible categories.
        Each dict in this list has the following keys:
            'id': (required) an integer id uniquely identifying this category
            'name': (required) string representing category name
              e.g., 'cat', 'dog', 'pizza'
            'supercategory': (optional) string representing the supercategory
              e.g., 'animal', 'vehicle', 'food', etc
      summary_dir: the output directory to which the image summaries are written.
      export_dir: the output directory to which images are written.  If this is
        empty (default), then images are not exported.
      agnostic_mode: boolean (default: False) controlling whether to evaluate in
        class-agnostic mode or not.
      show_groundtruth: boolean (default: False) controlling whether to show
        groundtruth boxes in addition to detected boxes
      min_score_thresh: minimum score threshold for a box to be visualized
      max_num_predictions: maximum number of detections to visualize
    Raises:
      ValueError: if result_dict does not contain the expected keys (i.e.,
        'original_image', 'detection_boxes', 'detection_scores',
        'detection_classes')
    """
    if not set([
            'original_image', 'detection_boxes', 'detection_scores',
            'detection_classes'
    ]).issubset(set(result_dict.keys())):
        raise ValueError('result_dict does not contain all expected keys.')
    if show_groundtruth and 'groundtruth_boxes' not in result_dict:
        raise ValueError(
            'If show_groundtruth is enabled, result_dict must contain '
            'groundtruth_boxes.')
    logging.info('Creating detection visualizations.')
    category_index = label_map_util.create_category_index(categories)

    image = np.squeeze(result_dict['original_image'], axis=0)
    detection_boxes = result_dict['detection_boxes']
    detection_scores = result_dict['detection_scores']
    detection_classes = np.int32((result_dict['detection_classes']))
    detection_keypoints = result_dict.get('detection_keypoints', None)
    detection_masks = result_dict.get('detection_masks', None)
    detection_motions = result_dict.get('detection_motions', None)

    # Plot groundtruth underneath detections
    if show_groundtruth:
        groundtruth_boxes = result_dict['groundtruth_boxes']
        groundtruth_keypoints = result_dict.get('groundtruth_keypoints', None)
        vis_utils.visualize_boxes_and_labels_on_image_array(
            image,
            groundtruth_boxes,
            None,
            None,
            category_index,
            keypoints=groundtruth_keypoints,
            use_normalized_coordinates=False,
            max_boxes_to_draw=None)
    vis_utils.visualize_boxes_and_labels_on_image_array(
        image,
        detection_boxes,
        detection_classes,
        detection_scores,
        category_index,
        instance_masks=detection_masks,
        keypoints=detection_keypoints,
        use_normalized_coordinates=False,
        max_boxes_to_draw=max_num_predictions,
        min_score_thresh=min_score_thresh,
        agnostic_mode=agnostic_mode)

    if export_dir:
        export_path = os.path.join(export_dir, 'export-{}.png'.format(tag))
        vis_utils.save_image_array_as_png(image, export_path)

    summary_value = [
        tf.Summary.Value(
            tag=tag,
            image=tf.Summary.Image(encoded_image_string=vis_utils.
                                   encode_image_array_as_png_str(image)))
    ]

    camera_motion = result_dict.get(
        'camera_motion', result_dict.get('groundtruth_camera_motion'))
    if detection_motions is not None and camera_motion is not None:
        with sess.as_default():
            flow_image, flow_error_image = vis_utils.visualize_flow(
                result_dict['depth'],
                detection_motions,
                detection_scores,
                camera_motion,
                result_dict['camera_intrinsics'],
                masks=detection_masks,
                groundtruth_flow=result_dict.get('groundtruth_flow'),
                boxes=detection_boxes,
                min_score_thresh=min_score_thresh)
            summary_value.append(
                tf.Summary.Value(tag=tag + '_flow',
                                 image=tf.Summary.Image(
                                     encoded_image_string=vis_utils.
                                     encode_image_array_as_png_str(flow_image *
                                                                   255))))
            if flow_error_image is not None:
                summary_value.append(
                    tf.Summary.Value(
                        tag=tag + '_flow_error',
                        image=tf.Summary.Image(
                            encoded_image_string=vis_utils.
                            encode_image_array_as_png_str(flow_error_image *
                                                          255))))

    summary = tf.Summary(value=summary_value)
    summary_writer = tf.summary.FileWriter(summary_dir)
    summary_writer.add_summary(summary, global_step)
    summary_writer.close()

    logging.info('Detection visualizations written to summary with tag %s.',
                 tag)