Пример #1
0
def test_non_maximum_supression():

    default_boxes = get_default_boxes([(9, 9, 32), (3, 3, 32)],
                                      [(1, 1 / 3, 3), (1, 1 / 3, 3)])
    n_boxes = len(default_boxes)

    print(default_boxes)

    class_mapping = dict(cat=1, dog=2, cow=3, table=4)

    confidences = np.array([(1, 0, 0, 0)] * n_boxes, dtype=np.float32)
    # default box #6 should be associated with class `dog`
    confidences[5] = (0.0, 0.05, 0.9, 0.05)
    # default box #100 should be associated with class `cow`
    confidences[100] = (0.0, 0.05, 0.0, 0.95)
    # default box #256 should be associated with class `cat`
    # and box #253 and #259 should NOT since they
    # have lower confidence and these boxes have big overlap
    confidences[253] = (0.0, 0.75, 0, 0.25)
    confidences[256] = (0.0, 0.9, 0.1, 0.0)
    confidences[259] = (0.0, 0.8, 0.1, 0.1)
    # default box #200 should be associated with class `cat`
    confidences[200] = (0.0, 0.70, 0, 0.30)

    offsets = np.zeros((n_boxes, 4), dtype=np.float32)
    offsets[100] = (0.5, 0, np.log(0.5), np.log(0.1))

    image = np.zeros((300, 300, 3))
    nms_threshold = 0.5
    filename = "foo"
    max_boxes = 10

    annotated_image = non_maximum_supression(confidences,
                                             offsets,
                                             default_boxes,
                                             class_mapping,
                                             image,
                                             nms_threshold,
                                             filename,
                                             max_boxes,
                                             clip=False)

    bboxes = annotated_image.bboxes

    assert isinstance(annotated_image, AnnotatedImage)

    print(bboxes.boundboxes)
    assert len(bboxes) == 4
    assert set(bboxes.classnames) == {"cat", "dog", "cow"}
    assert np.allclose(bboxes.loc["dog"], default_boxes.iloc[5])
    assert np.allclose(bboxes.loc["cat"], default_boxes.iloc[[256, 200]])

    cow_box = default_boxes.centerboxes.iloc[100]
    cow_box.x_center += 0.5 * cow_box.width
    cow_box.width *= 0.5
    cow_box.height *= 0.1
    cow_box = BoundBoxArray.from_centerboxes([cow_box.as_matrix()])
    assert np.allclose(bboxes.loc["cow"], cow_box)

    assert annotated_image.filename == filename
Пример #2
0
def test_offsets():

    original = BoundBoxArray.from_boundboxes([(0, 0, 0.5, 0.5),
                                              (0.3, 0.4, 0.6, 0.9)])
    default_boxes = BoundBoxArray.from_boundboxes([(0, 0, 0.5, 0.4),
                                                   (0.2, 0.4, 0.6, 0.9)])

    # get offsets
    offsets = calculate_offsets(default_boxes, original)
    offsets = BoundBoxArray.from_centerboxes(offsets)

    # apply them to get original bboxes
    recovered = apply_offsets(default_boxes, offsets)
    recovered = BoundBoxArray.from_centerboxes(recovered)

    assert np.allclose(original, recovered)
Пример #3
0
def test_boundbox_creation(boundboxes, centerboxes, boxes, classnames):

    from_boundboxes = BoundBoxArray.from_boundboxes(boundboxes, classnames)

    assert from_boundboxes.shape == (2, 8)
    assert (from_boundboxes.index == classnames).all()

    from_centerboxes = BoundBoxArray.from_centerboxes(centerboxes, classnames)

    assert from_boundboxes.equals(from_centerboxes)

    from_boxes = BoundBoxArray.from_boxes(boxes, classnames)

    assert from_boundboxes.equals(from_boxes)
    assert from_centerboxes.equals(from_boxes)
Пример #4
0
def get_default_boxes(out_shapes, box_ratios):
    """Returns BoundBoxArray of default boxes
    
    Args:
        out_shapes: a list of tuples with output shapes
        box_ration: a list of box aspect ratios
    
    Returns:
    """

    default_boxes = []
    n_outs = len(out_shapes)
    scales = (box_scale(n_out + 1, n_outs) for n_out in range(n_outs))

    layer_params = zip(out_shapes, scales, box_ratios)
    for out_shape, scale, layer_box_ratios in layer_params:
        height, width = height_and_width(out_shape)
        layer_boxes = [[[
            get_default_box(i, j, scale, box_ratio, width, height)
            for box_ratio in layer_box_ratios
        ] for i in range(height)] for j in range(width)]
        default_boxes.append(layer_boxes)

    return BoundBoxArray.from_centerboxes(flatten_list(default_boxes)).clip()