def benchmark_region_loss():
    """
    CommandLine:
        python ~/code/netharn/netharn/models/yolo2/light_region_loss.py benchmark_region_loss --profile

    Benchmark:
        >>> benchmark_region_loss()
    """
    from netharn.models.yolo2.light_yolo import Yolo
    torch.random.manual_seed(0)
    network = Yolo(num_classes=2, conf_thresh=4e-2)
    self = light_region_loss.RegionLoss(num_classes=network.num_classes,
                                        anchors=network.anchors)
    Win, Hin = 96, 96
    # true boxes for each item in the batch
    # each box encodes class, center, width, and height
    # coordinates are normalized in the range 0 to 1
    # items in each batch are padded with dummy boxes with class_id=-1
    target = torch.FloatTensor([
        # boxes for batch item 1
        [[0, 0.50, 0.50, 1.00, 1.00],
         [1, 0.32, 0.42, 0.22, 0.12]],
        # boxes for batch item 2 (it has no objects, note the pad!)
        [[-1, 0, 0, 0, 0],
         [-1, 0, 0, 0, 0]],
    ])
    im_data = torch.randn(len(target), 3, Hin, Win)
    output = network.forward(im_data)
    import ubelt
    for timer in ubelt.Timerit(250, bestof=10, label='time'):
        with timer:
            loss = float(self.forward(output, target))
    print('loss = {!r}'.format(loss))
def test_tensor_brambox_compat_ground_truth_loss():
    """
    Check that brambox and tensor boxes produce the same ground truth encoding
    """
    network = Yolo(num_classes=2, conf_thresh=4e-2)
    self = RegionLoss(num_classes=network.num_classes, anchors=network.anchors)

    # Win, Hin = 96, 96
    Win, Hin = 416, 416
    target_brambox, target_tensor = demodata_targets(input_size=(Win, Hin))

    # construct dummy data and run it through the network
    im_data = torch.randn(len(target_brambox), 3, Hin, Win)
    output = network.forward(im_data)
    print('')

    # Brambox and Tensor formats should produce the same loss
    loss_brambox = self(output, target_brambox).data.cpu().numpy()
    loss_tensor = self(output, target_tensor).data.cpu().numpy()
    print('loss_brambox = {!r}'.format(loss_brambox))
    print('loss_tensor = {!r}'.format(loss_tensor))
    assert np.all(np.isclose(loss_brambox, loss_tensor))

    # Brambox and Tensor formats should produce the same loss
    loss_brambox = self(output, target_brambox, seen=9999).data.cpu().numpy()
    loss_tensor = self(output, target_tensor, seen=9999).data.cpu().numpy()
    print('loss_brambox = {!r}'.format(loss_brambox))
    print('loss_tensor = {!r}'.format(loss_tensor))
    assert np.all(np.isclose(loss_brambox, loss_tensor))
def test_tensor_brambox_compat_ground_truth_encoding():
    """
    Check that brambox and tensor boxes produce the same ground truth encoding
    """

    network = Yolo(num_classes=2, conf_thresh=4e-2)
    self = RegionLoss(num_classes=network.num_classes, anchors=network.anchors)

    Win, Hin = 96, 96
    target_brambox, target_tensor = demodata_targets(input_size=(Win, Hin))

    # convert network input size (e.g. 416x416) to output size (e.g. 13x13)
    nW, nH = (Win // self.reduction, Hin // self.reduction)

    pred_boxes = torch.rand(90, 4)
    pred_confs = torch.rand(90)

    _ret_ten = self._build_targets_tensor(pred_boxes, pred_confs,
                                          target_tensor, nH, nW)
    _ret_box = self._build_targets_brambox(pred_boxes, pred_confs,
                                           target_brambox, nH, nW)

    keys = ['coord_mask', 'conf_mask', 'cls_mask', 'tcoord', 'tconf', 'tcls']

    errors = []
    for key, item1, item2 in zip(keys, _ret_box, _ret_ten):
        bad_flags = ~np.isclose(item1, item2)
        if np.any(bad_flags):
            bad_idxs = np.where(bad_flags)
            msg = 'key={} did not agree at indices {}. values1={} values2={}'.format(
                key, bad_idxs, item1[bad_flags], item2[bad_flags])
            errors.append(msg)

    if errors:
        raise AssertionError('\n---\n'.join(errors))
Beispiel #4
0
def compare_loss_speed():
    """
    python ~/code/netharn/netharn/models/yolo2/light_region_loss.py compare_loss_speed

    Example:
        >>> compare_loss_speed()
    """
    from netharn.models.yolo2.light_yolo import Yolo
    import netharn.models.yolo2.light_region_loss
    import lightnet.network
    import ubelt as ub
    torch.random.manual_seed(0)
    network = Yolo(num_classes=2, conf_thresh=4e-2)

    self1 = netharn.models.yolo2.light_region_loss.RegionLoss(
        num_classes=network.num_classes, anchors=network.anchors)
    self2 = lightnet.network.RegionLoss(num_classes=network.num_classes,
                                        anchors=network.anchors)

    # Win, Hin = 416, 416
    Win, Hin = 96, 96

    # ----- More targets -----
    rng = util.ensure_rng(0)
    import netharn as nh

    bsize = 4
    # Make a random semi-realistic set of groundtruth items
    n_targets = [rng.randint(0, 10) for _ in range(bsize)]
    target_list = [
        torch.FloatTensor(
            np.hstack([
                rng.randint(0, network.num_classes, nT)[:, None],
                util.Boxes.random(nT, scale=1.0, rng=rng).data
            ])) for nT in n_targets
    ]
    target = nh.data.collate.padded_collate(target_list)

    im_data = torch.randn(len(target), 3, Hin, Win)
    output = network.forward(im_data)

    self1.iou_mode = 'c'
    for timer in ub.Timerit(100, bestof=10, label='cython_ious'):
        with timer:
            loss_cy = float(self1(output, target))

    self1.iou_mode = 'py'
    for timer in ub.Timerit(100, bestof=10, label='python_ious'):
        with timer:
            loss_py = float(self1(output, target))

    for timer in ub.Timerit(100, bestof=10, label='original'):
        with timer:
            loss_orig = float(self2(output, target))

    print('loss_cy   = {!r}'.format(loss_cy))
    print('loss_py   = {!r}'.format(loss_py))
    print('loss_orig = {!r}'.format(loss_orig))
Beispiel #5
0
def profile_loss_speed():
    """
    python ~/code/netharn/netharn/models/yolo2/light_region_loss.py profile_loss_speed --profile

    Benchmark:
        >>> profile_loss_speed()
    """
    from netharn.models.yolo2.light_yolo import Yolo
    import netharn.models.yolo2.light_region_loss
    import lightnet.network
    import netharn as nh

    rng = util.ensure_rng(0)
    torch.random.manual_seed(0)
    network = Yolo(num_classes=2, conf_thresh=4e-2)

    self1 = netharn.models.yolo2.light_region_loss.RegionLoss(
        num_classes=network.num_classes, anchors=network.anchors)
    self2 = lightnet.network.RegionLoss(num_classes=network.num_classes,
                                        anchors=network.anchors)

    bsize = 8
    # Make a random semi-realistic set of groundtruth items
    n_targets = [rng.randint(0, 20) for _ in range(bsize)]
    target_list = [
        torch.FloatTensor(
            np.hstack([
                rng.randint(0, network.num_classes, nT)[:, None],
                util.Boxes.random(nT, scale=1.0, rng=rng).data
            ])) for nT in n_targets
    ]
    target = nh.data.collate.padded_collate(target_list)

    Win, Hin = 416, 416
    im_data = torch.randn(len(target), 3, Hin, Win)
    output = network.forward(im_data)

    loss1 = float(self1(output, target))
    loss2 = float(self2(output, target))
    print('loss1 = {!r}'.format(loss1))
    print('loss2 = {!r}'.format(loss2))