def test_compute_gt(self):
        level = 3
        ds = voc.build_dataset('test/data/VOC2007', im_input_size=(512, 512))

        anchors = self.generate_anchors(config.AnchorsConfig(), 512)
        im, (l, bbs) = next(iter(ds.take(1)))

        gt_reg, gt_labels = utils.anchors.anchor_targets_bbox(
            anchors, tf.expand_dims(im, 0), tf.expand_dims(bbs, 0),
            tf.expand_dims(l, 0), len(voc.IDX_2_LABEL))

        nearest_anchors = anchors[gt_reg[0, :, -1] == 1].numpy()
        im_random = unnormalize_image(im)
        im_random = visualizer.draw_boxes(im_random, nearest_anchors)
        im_random = visualizer.draw_boxes(im_random, bbs, colors=[0, 0, 255])

        for label in l:
            print(voc.IDX_2_LABEL[int(label)])

        plt.imshow(im_random)
        plt.show(block=True)

        print('GT shapes:', gt_labels.shape, gt_reg.shape)
        print('Found any overlapping anchor?', np.any(gt_labels[:, :,
                                                                -1] == 1.))
    def plot_single(self, aug_fn):
        image, (labels, boxes) = next(iter(self.ds.take(1)))
        aug_image, (aug_labels, aug_boxes) = aug_fn(image, (labels, boxes))

        image = data.preprocess.unnormalize_image(image)
        aug_image = data.preprocess.unnormalize_image(aug_image)

        labels = [self.classes[l] for l in labels.numpy().tolist()]
        aug_labels = [self.classes[l] for l in aug_labels.numpy().tolist()]

        colors = visualizer.colors_per_labels(labels)
        aug_colors = visualizer.colors_per_labels(aug_labels)

        image = visualizer.draw_boxes(image, boxes, labels, colors=colors)
        aug_image = visualizer.draw_boxes(aug_image,
                                          aug_boxes,
                                          aug_labels,
                                          colors=aug_colors)

        plt.subplot(121)
        plt.title('No Image augmentation')
        plt.imshow(image)
        plt.axis('off')

        plt.subplot(122)
        plt.title('Image augmentation')
        plt.imshow(aug_image)
        plt.axis('off')

        plt.savefig('aug.png')
        plt.show(block=True)
    def test_regress_boxes(self):
        print('Regress anchors test')

        ds = voc.build_dataset('test/data/VOC2007', im_input_size=(512, 512))

        anchors = self.generate_anchors(config.AnchorsConfig(), 512)
        im, (l, bbs) = next(iter(ds.take(1)))

        gt_reg, gt_labels = utils.anchors.anchor_targets_bbox(
            anchors, tf.expand_dims(im, 0), tf.expand_dims(bbs, 0),
            tf.expand_dims(l, 0), len(voc.IDX_2_LABEL))

        near_mask = gt_reg[0, :, -1] == 1
        nearest_regressors = tf.expand_dims(
            tf.boolean_mask(gt_reg[0], near_mask)[:, :-1], 0)
        nearest_anchors = tf.expand_dims(anchors[near_mask], 0)

        # apply regression to boxes
        regressed_boxes = utils.bndbox.regress_bndboxes(
            nearest_anchors, nearest_regressors)

        im_random = unnormalize_image(im)
        im_random = visualizer.draw_boxes(im_random, regressed_boxes[0])

        plt.imshow(im_random)
        plt.show(block=True)
    def test_keras_pretrained(self):
        # Load efficientdet pretrained on VOC2007
        model = EfficientDet(D=0,
                             num_classes=20,
                             weights='D0-VOC',
                             score_threshold=.4)
        print('Done loading...')
        image = io.load_image('imgs/cat-dog.jpg', model.config.input_size)
        n_image = normalize_image(image)
        n_image = tf.expand_dims(n_image, axis=0)

        classes = voc.IDX_2_LABEL

        boxes, labels, scores = model(n_image, training=False)
        labels = [classes[l] for l in labels[0]]

        colors = visualizer.colors_per_labels(labels)
        im = visualizer.draw_boxes(image,
                                   boxes[0],
                                   labels,
                                   scores[0],
                                   colors=colors)

        plt.imshow(im)
        plt.axis('off')
        plt.savefig('test.png')

        plt.show(block=True)
    def test_tile_anchors(self):
        level = 3
        feature_size = 512
        im_random = np.zeros((512, 512, 3))

        boxes = self.generate_anchors(config.AnchorsConfig(),
                                      im_random.shape[0])

        im_random = visualizer.draw_boxes(im_random, boxes)

        plt.imshow(im_random)
        plt.show(block=True)
Exemple #6
0
image = load_image('sample.jpg', image_size)
image.shape


# In[ ]:


n_image = preprocess.normalize_image(image)
n_image = tf.expand_dims(n_image, 0)


# In[ ]:


predictions = model(n_image, training=False)
boxes, labels, scores = predictions
labels = [IDX_2_LABEL[o] for o in labels[0]]


# In[7]:


colors = visualizer.colors_per_labels(labels)
visualizer.draw_boxes(image, 
                      boxes=boxes[0], 
                      labels=labels, 
                      scores=scores[0], 
                      colors=colors)