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_compute_gt(self):
        ds = voc.build_dataset('test/data/VOC2007',
                               im_input_size=(512, 512),
                               shuffle=False)
        ds = ds.skip(1).batch(1)

        wrapped_ds = utils.training.wrap_detection_dataset(ds, (512, 512), 20)
        anchors = self.generate_anchors(config.AnchorsConfig(), 512)

        im, (regressors, l) = next(iter(wrapped_ds.take(1)))

        im = unnormalize_image(im[0])
        near_mask = regressors[0, :, -1] == 1
        nearest_regressors = tf.expand_dims(
            tf.boolean_mask(regressors[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 = utils.visualizer.draw_boxes(im,
                                         nearest_anchors[0],
                                         colors=[(255, 255, 0)])
        im = utils.visualizer.draw_boxes(im,
                                         regressed_boxes[0],
                                         colors=[(0, 255, 255)])

        plt.imshow(im)
        plt.axis('off')
        plt.show(block=True)

        print('GT shapes:', l.shape, regressors.shape)
        print('Found any overlapping anchor?',
              tf.reduce_any(tf.equal(l[:, :, -1], 1.)))
    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 test_compute_gt(self):
        classes = ['treecko', 'psyduck', 'greninja', 'solgaleo', 'mewtwo']
        class2idx = {c: i for i, c in enumerate(classes)}
        ds = labelme.build_dataset('test/data/pokemon',
                                   'test/data/pokemon',
                                   class2idx=class2idx,
                                   im_input_size=(512, 512))

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

        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(classes))

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

        plt.imshow(im)
        plt.axis('off')
        plt.show(block=True)

        print('GT shapes:', gt_labels.shape, gt_reg.shape)
        print('Found any overlapping anchor?', np.any(gt_labels[:, :,
                                                                -1] == 1.))