Example #1
0
    def testTransformBBoxesToTopDown(self):
        bs = 5
        nbboxes = 2
        cbboxes = np.zeros(shape=(bs, nbboxes, 4))
        # Ensures ymin/xmin/ymax/ymin.
        cbboxes[:, :, :2] = 100. * np.random.normal(size=(bs, nbboxes, 2))
        cbboxes[:, :, 2:4] = cbboxes[:, :, :2] + 10. * np.random.uniform(
            size=(bs, nbboxes, 2))

        image_bboxes = summary.TransformBBoxesToTopDown(cbboxes)
        self.assertAllEqual(cbboxes.shape, image_bboxes.shape)
    def _EvaluateIfNecessary(self, name):
        """Create a top down image summary, if not already created."""
        if self._summary is not None:
            return

        tf.logging.info('Generating top down summary.')
        ret = tf.Summary()

        transform = self._top_down_transform

        for batch_idx, batch_sample in enumerate(self._sampler.samples):
            batch_size = batch_sample.labels.shape[0]
            visualization_labels = batch_sample.visualization_labels
            predicted_bboxes = batch_sample.predicted_bboxes
            visualization_weights = batch_sample.visualization_weights
            points_xyz = batch_sample.points_xyz
            points_padding = batch_sample.points_padding
            gt_bboxes_2d = batch_sample.gt_bboxes_2d
            gt_bboxes_2d_weights = batch_sample.gt_bboxes_2d_weights
            labels = batch_sample.labels
            difficulties = batch_sample.difficulties
            source_ids = batch_sample.source_ids

            # Create base images for entire batch that we will update.
            images = np.zeros(
                [batch_size, self._image_height, self._image_width, 3],
                dtype=np.uint8)

            # Draw lasers first, so that bboxes can be on top.
            self._DrawLasers(images, points_xyz, points_padding, transform)

            # Draw ground-truth bboxes.
            gt_bboxes_2d = np.where(
                np.expand_dims(gt_bboxes_2d_weights > 0, -1), gt_bboxes_2d,
                np.zeros_like(gt_bboxes_2d))
            transformed_gt_bboxes_2d = summary.TransformBBoxesToTopDown(
                gt_bboxes_2d, transform)

            summary.DrawBBoxesOnImages(images,
                                       transformed_gt_bboxes_2d,
                                       gt_bboxes_2d_weights,
                                       labels,
                                       self._class_id_to_name,
                                       groundtruth=True)

            # Draw predicted bboxes.
            predicted_bboxes = np.where(
                np.expand_dims(visualization_weights > 0, -1),
                predicted_bboxes, np.zeros_like(predicted_bboxes))
            transformed_predicted_bboxes = summary.TransformBBoxesToTopDown(
                predicted_bboxes, transform)

            summary.DrawBBoxesOnImages(images,
                                       transformed_predicted_bboxes,
                                       visualization_weights,
                                       visualization_labels,
                                       self._class_id_to_name,
                                       groundtruth=False)

            # Draw the difficulties on the image.
            self.DrawDifficulty(images, transformed_gt_bboxes_2d,
                                gt_bboxes_2d_weights, difficulties)

            for idx in range(batch_size):
                source_id = source_ids[idx]

                def AnnotateImage(fig, axes, source_id=source_id):
                    """Add source_id to image."""
                    del fig
                    # Draw in top middle of image.
                    text = axes.text(500,
                                     15,
                                     source_id,
                                     fontsize=16,
                                     color='blue',
                                     fontweight='bold',
                                     horizontalalignment='center')
                    text.set_path_effects([
                        path_effects.Stroke(linewidth=3,
                                            foreground='lightblue'),
                        path_effects.Normal()
                    ])

                image_summary = plot.Image(name='{}/{}/{}'.format(
                    name, batch_idx, idx),
                                           aspect='equal',
                                           figsize=self._figsize,
                                           image=images[idx, ...],
                                           setter=AnnotateImage)
                ret.value.extend(image_summary.value)

        tf.logging.info('Done generating top down summary.')
        self._summary = ret