Exemplo n.º 1
0
class BBoxMeanAP:
    def __init__(self, threshold=0.5):
        self.scores_per_image = []
        self.threshold = threshold
        self.box_coder = FPNSSDBoxCoder()

    def reset(self):
        self.scores_per_image = []

    def update(self, y_pred: Tensor, y_true: Tensor):
        true_ssd_bboxes = y_true[SSD_BBOXES_KEY].detach().cpu()
        pred_ssd_bboxes = y_pred[SSD_BBOXES_KEY].detach().cpu()
        pred_classes = y_pred[SSD_LABELS_KEY].detach().cpu()
        true_classes = y_true[SSD_LABELS_KEY].detach().cpu()

        pred_classes = pred_classes.softmax(dim=2)
        true_classes = one_hot(true_classes, num_classes=pred_classes.size(2))

        for pred_loc, pred_cls, true_loc, true_cls in zip(
                pred_ssd_bboxes, pred_classes, true_ssd_bboxes, true_classes):
            pred_bboxes, _, pred_conf = self.box_coder.decode(
                pred_loc, pred_cls)
            true_bboxes, _, _ = self.box_coder.decode(true_loc, true_cls)

            true_bboxes = change_box_order(true_bboxes, 'xyxy2xywh')
            pred_bboxes = change_box_order(pred_bboxes, 'xyxy2xywh')

            true_bboxes = to_numpy(true_bboxes)
            pred_bboxes = to_numpy(pred_bboxes)
            pred_conf = to_numpy(pred_conf)

            if len(true_bboxes) == 0:
                continue

            if len(pred_bboxes) == 0:
                score = 0
            else:
                score = map_iou(true_bboxes, pred_bboxes, pred_conf)

            self.scores_per_image.append(score)

    def __str__(self):
        return '%.4f' % self.value()

    def value(self):
        if len(self.scores_per_image) == 0:
            return 0
        return np.mean(self.scores_per_image)

    def log_to_tensorboard(self, saver: SummaryWriter, prefix, step):
        if len(self.scores_per_image) > 0:
            saver.add_scalar(prefix + '/value', self.value(), step)
            saver.add_histogram(prefix + '/histogram',
                                np.array(self.scores_per_image), step)
Exemplo n.º 2
0
def test_sdd_box_coder():
    box_coder = FPNSSDBoxCoder()
    #
    boxes = [
        [20, 40, 80, 100],
        [200, 4, 300, 200],
        [100, 100, 160, 200],
        [50, 90, 175, 300],
    ]

    labels = [0, 0, 0, 0]

    loc_targets, cls_targets = box_coder.encode(
        torch.tensor(boxes, dtype=torch.float32),
        torch.tensor(labels, dtype=torch.float32))
    dec_boxes, dec_labels, dec_scores = box_coder.decode(
        loc_targets, cls_targets)
    print(dec_boxes, dec_labels, dec_scores)
Exemplo n.º 3
0
def test_ssd_synthetic():
    label_image = np.zeros((512, 512), dtype=np.uint8)

    # cv2.fillConvexPoly(label_image, np.expand_dims(cv2.boxPoints(((100, 100), (100, 20), 0)), 1).astype(int), (1, 1, 1))
    # cv2.fillConvexPoly(label_image, np.expand_dims(cv2.boxPoints(((200, 100), (100, 20), 45)), 1).astype(int), (2, 2, 2))
    # cv2.fillConvexPoly(label_image, np.expand_dims(cv2.boxPoints(((100, 200), (100, 20), 90)), 1).astype(int), (3, 3, 3))
    # cv2.fillConvexPoly(label_image, np.expand_dims(cv2.boxPoints(((200, 200), (100, 20), 135)), 1).astype(int), (4, 4, 4))

    # cv2.fillConvexPoly(label_image, np.expand_dims(cv2.boxPoints(((100 + 200, 100), (20, 100), 0)), 1).astype(int), (5, 5, 5))
    # cv2.fillConvexPoly(label_image, np.expand_dims(cv2.boxPoints(((200 + 200, 100), (20, 100), 45)), 1).astype(int), (6, 6, 6))
    # cv2.fillConvexPoly(label_image, np.expand_dims(cv2.boxPoints(((100 + 200, 200), (20, 100), 90)), 1).astype(int), (7, 7, 7))
    # cv2.fillConvexPoly(label_image, np.expand_dims(cv2.boxPoints(((200 + 200, 200), (20, 100), 135)), 1).astype(int), (8, 8, 8))

    # cv2.fillConvexPoly(label_image, np.expand_dims(cv2.boxPoints(((100, 100 + 200), (100, 20), 17)), 1).astype(int), (9, 9, 9))
    # cv2.fillConvexPoly(label_image, np.expand_dims(cv2.boxPoints(((200, 100 + 200), (100, 20), 49)), 1).astype(int), (10, 10, 10))
    cv2.fillConvexPoly(
        label_image,
        np.expand_dims(cv2.boxPoints(((100, 200 + 200), (100, 20), 99)),
                       1).astype(int), (11, 11, 11))
    # cv2.fillConvexPoly(label_image, np.expand_dims(cv2.boxPoints(((200, 200 + 200), (100, 20), 165)), 1).astype(int), (12, 12, 12))

    # cv2.fillConvexPoly(label_image, np.expand_dims(np.array([[25, 90], [125, 90], [125, 110], [25, 110]]), 1), (1, 1, 1))
    # cv2.fillConvexPoly(label_image, np.expand_dims(np.array([[10, 400], [70, 400], [70, 420], [10, 420]]), 1), (3, 3, 3))
    # cv2.fillConvexPoly(label_image, np.expand_dims(np.array([[100, 100], [200, 110], [200, 140], [110, 130]]), 1), (4, 4, 4))
    # cv2.fillConvexPoly(label_image, np.expand_dims(np.array([[300, 200], [400, 210], [410, 330], [310, 330]]), 1), (5, 5, 5))

    image = (label2rgb(label_image, bg_label=0) * 255).astype(np.uint8)

    # Test what happens if we rotate
    # image = np.rot90(image).copy()
    # label_image = np.rot90(label_image).copy()

    bboxes = instance_mask_to_bboxes(label_image)
    print(bboxes)

    labels = np.zeros(len(bboxes), dtype=np.intp)

    box_coder = FPNSSDBoxCoder()

    loc_targets, cls_targets, anchors = box_coder.encode(
        torch.from_numpy(bboxes).float(),
        torch.from_numpy(labels),
        return_anchors=True)
    print(loc_targets.shape, cls_targets.shape)

    cls_targets_one_hot = np.eye(2)[cls_targets]
    print(cls_targets_one_hot.shape)

    dec_boxes, dec_labels, dec_scores = box_coder.decode(
        loc_targets, torch.from_numpy(cls_targets_one_hot))
    print(dec_boxes)

    for bbox in dec_boxes.numpy():
        visualize_bbox(image, bbox, (255, 0, 255), thickness=3)

    for bbox in bboxes:
        visualize_bbox(image, bbox, (0, 255, 0), thickness=1)

    for bbox in anchors.numpy():
        visualize_bbox(image, bbox, (255, 255, 255), thickness=1)

    cv2.imshow('overlays', image)
    # cv2.imshow('anchors', anchors)
    cv2.waitKey(-1)