Exemplo n.º 1
0
def evaluate_review(env, image, nms_params, review):
    model = env.best.model.to(env.device)
    encoder = env.encoder.to(env.device)

    scale = env.args.scale

    detections = evaluate.evaluate_image(model,
                                         image,
                                         encoder,
                                         device=env.device,
                                         nms_params=nms_params).detections

    if detections._size == 0:
        return make_detections(env, [])

    review = tensors_to(review, device=env.device)
    ious = box.iou_matrix(detections.bbox, review.bbox * scale)

    ious[ious < nms_params.nms].fill_(-1)
    scores = ious.mul(detections.confidence.unsqueeze(1))

    review_inds = scores.max(0).values.argsort(descending=True)

    detections = table_list(detections)
    for i in review_inds.tolist():
        score, ind = scores[:, i].max(0)

        if score > 0:
            detections[ind].match = i

        scores[ind].fill_(0)

    return make_detections(env, detections)
Exemplo n.º 2
0
def encode(target, anchor_boxes, params):
    n = anchor_boxes.size(0)
    m = target.bbox.size(0)

    if m == 0:
        return struct(location=target.bbox.new_zeros(n, 4),
                      classification=target.bbox.new_zeros(n,
                                                           dtype=torch.long))

    ious = box.iou_matrix(box.point_form(anchor_boxes), target.bbox)

    if params.top_anchors > 0:
        top_ious, inds = ious.topk(params.top_anchors, dim=0)
        ious = ious.scatter(0, inds, top_ious * 2)

    max_ious, max_ids = ious.max(1)

    class_target = encode_classes(target.label,
                                  max_ious,
                                  max_ids,
                                  match_thresholds=params.match_thresholds)

    location = target.bbox[max_ids]
    if params.location_loss == "l1":
        location = encode_boxes(location, anchor_boxes)

    return struct(location=location, classification=class_target)
Exemplo n.º 3
0
def match_positives(detections, target):
    assert detections.label.dim() == 1 and target.label.dim() == 1
    n, m = detections._size, target._size

    if m == 0 or n == 0:
        return const(torch.FloatTensor(n).zero_())

    ious = box.iou_matrix(detections.bbox, target.bbox)
    return lambda threshold: _match_positives(
        detections.label, target.label, ious, threshold=threshold)
Exemplo n.º 4
0
def match_targets(target1, target2, threshold=0.5):
    ious = box.iou_matrix(target1.bbox, target2.bbox)
    matches = []

    if ious.size(0) == 0 or ious.size(1) == 0:
        return []

    value, loc = take_max_2d(ious)
    while value > threshold:
        matches.append(struct(match=loc, iou=value))
        value, loc = take_max_2d(ious)

    return matches
Exemplo n.º 5
0
def match_boxes(prediction, target, threshold=0.5, eps=1e-7):
    n = prediction.label.size(0)
    matches = []

    ious = box.iou_matrix(prediction.bbox, target.bbox)

    for i, p in enumerate(prediction._sequence()):
        match = None
        if ious.size(1) > 0:
            iou, j = max_1d(ious[i])

            label = target.label[j]
            matches_box = iou > threshold

            if matches_box:
                ious[:,
                     j] = 0  # mark target overlaps to 0 so they won't be selected twice

                if p.label == label:
                    match = (j, iou)

        matches.append(p._extend(match=match))
    return matches