Exemplo n.º 1
0
    def random(Boxes, num=1, scale=1.0, format='xywh', rng=None, tensor=False):
        """
        Makes random boxes

        Example:
            >>> # xdoctest: +IGNORE_WHITESPACE
            >>> Boxes.random(3, rng=0, scale=100)
            <Boxes(xywh,
                array([[27, 35, 30, 27],
                       [21, 32, 21, 44],
                       [48, 19, 39, 26]]))>
            >>> Boxes.random(3, rng=0, scale=100, tensor=True)
            <Boxes(xywh,
                tensor([[ 27,  35,  30,  27],
                        [ 21,  32,  21,  44],
                        [ 48,  19,  39,  26]]))>
        """
        from netharn import util
        rng = util.ensure_rng(rng)

        xywh = (rng.rand(num, 4) * scale / 2)
        as_integer = isinstance(scale, int)
        if as_integer:
            xywh = xywh.astype(np.int)
        if tensor:
            if as_integer:
                xywh = torch.LongTensor(xywh)
            else:
                xywh = torch.FloatTensor(xywh)
        boxes = Boxes(xywh, format='xywh').toformat(format, copy=False)
        return boxes
Exemplo n.º 2
0
    def __init__(self, size=4, border=1, n=100, rng=None):
        rng = util.ensure_rng(rng)

        h = w = size

        whiteish = 1 - (np.abs(rng.randn(n, 1, h, w) / 4) % 1)
        blackish = (np.abs(rng.randn(n, 1, h, w) / 4) % 1)

        fw = border
        slices = [slice(None, fw), slice(-fw, None)]

        # class 0 is white block inside a black frame
        data1 = whiteish.copy()
        for sl1, sl2 in it.product(slices, slices):
            data1[..., sl1, :] = blackish[..., sl1, :]
            data1[..., :, sl2] = blackish[..., :, sl2]

        # class 1 is black block inside a white frame
        data2 = blackish.copy()
        for sl1, sl2 in it.product(slices, slices):
            data2[..., sl1, :] = whiteish[..., sl1, :]
            data2[..., :, sl2] = whiteish[..., :, sl2]

        self.data = np.concatenate([data1, data2], axis=0)
        self.labels = np.array(([0] * n) + ([1] * n))
Exemplo n.º 3
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))
Exemplo n.º 4
0
    def __init__(self, needed_std=1.0, std_tol=0.1, max_attempts=10,
                 do_orthonorm=True, rng=None):

        self.rng = util.ensure_rng(rng)

        self.do_orthonorm = do_orthonorm
        self.needed_std = needed_std
        self.std_tol = std_tol
        self.max_attempts = max_attempts
Exemplo n.º 5
0
    def __init__(self, rng=None):
        """
        Spiral 2d data points

        CommandLine:
            python ~/code/netharn/netharn/data/toydata.py ToyData1d --show

        Example:
            >>> dset = ToyData1d()
            >>> data, labels = next(iter(dset.make_loader(batch_size=2000)))
            >>> # xdoctest: +REQUIRES(--show)
            >>> from netharn.util import mplutil
            >>> mplutil.qtensure()  # xdoc: +SKIP
            >>> mplutil.figure(fnum=1, doclf=True)
            >>> cls1 = data[labels == 0]
            >>> cls2 = data[labels == 1]
            >>> from matplotlib import pyplot as plt
            >>> plt.plot(*cls1.T.numpy(), 'rx')
            >>> plt.plot(*cls2.T.numpy(), 'bx')
            >>> mplutil.show_if_requested()
        """
        rng = util.ensure_rng(rng)

        # spiral equation in parameteric form:
        # x(t) = r(t) * cos(t)
        # y(t) = r(t) * sin(t)

        # class 1
        n = 1000
        theta1 = rng.rand(n) * 10
        x1 = theta1 * np.cos(theta1)
        y1 = theta1 * np.sin(theta1)

        theta2 = rng.rand(n) * 10
        x2 = -theta2 * np.cos(theta2)
        y2 = -theta2 * np.sin(theta2)

        data = []
        labels = []

        data.extend(list(zip(x1, y1)))
        labels.extend([0] * n)

        data.extend(list(zip(x2, y2)))
        labels.extend([1] * n)

        data = np.array(data)
        labels = np.array(labels)

        self.data = data
        self.labels = labels
Exemplo n.º 6
0
def svd_orthonormal(shape, rng=None, cache_key=None):
    """
    If cache_key is specified, then the result will be cached, and subsequent
    calls with the same key and shape will return the same result.

    References:
        Orthonorm init code is taked from Lasagne
        https://github.com/Lasagne/Lasagne/blob/master/lasagne/init.py
    """
    rng = util.ensure_rng(rng)

    if len(shape) < 2:
        raise RuntimeError("Only shapes of length 2 or more are supported.")
    flat_shape = (shape[0], np.prod(shape[1:]))

    enabled = False and cache_key is not None
    if enabled:
        rand_sequence = rng.randint(0, 2**16)
        depends = [shape, cache_key, rand_sequence]
        cfgstr = ub.hash_data(depends)
    else:
        cfgstr = ''

    # this process can be expensive, cache it

    # TODO: only cache very large matrices (4096x4096)
    # TODO: only cache very large matrices, not (256,256,3,3)
    cacher = ub.Cacher('svd_orthonormal',
                       appname='netharn',
                       enabled=enabled,
                       cfgstr=cfgstr)
    q = cacher.tryload()
    if q is None:
        # print('Compute orthonormal matrix with shape ' + str(shape))
        a = rng.normal(0.0, 1.0, flat_shape)
        u, _, v = np.linalg.svd(a, full_matrices=False)
        q = u if u.shape == flat_shape else v
        # print(shape, flat_shape)
        q = q.reshape(shape)
        q = q.astype(np.float32)
        cacher.save(q)
    return q
Exemplo n.º 7
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))
Exemplo n.º 8
0
    def random(Boxes,
               num=1,
               scale=1.0,
               format='tlwh',
               rng=None,
               tensor=False,
               anchors=None,
               anchor_std=1.0 / 6):
        """
        Makes random boxes

        Args:
            num (int): number of boxes to generate
            scale (float): size of imgdims
            format (str): format of boxes to be created (e.g. tlbr, xywh)
            anchors (ndarray): normalized width / heights of anchor boxes to
                perterb and randomly place. (must be in range 0-1)

        Example:
            >>> # xdoctest: +IGNORE_WHITESPACE
            >>> Boxes.random(3, rng=0, scale=100)
            <Boxes(tlwh,
                array([[54, 54,  6, 17],
                       [42, 64,  1, 25],
                       [79, 38, 17, 14]]))>
            >>> Boxes.random(3, rng=0, scale=100, tensor=True)
            <Boxes(tlwh,
                tensor([[ 54,  54,   6,  17],
                        [ 42,  64,   1,  25],
                        [ 79,  38,  17,  14]]))>
            >>> anchors = np.array([[.5, .5], [.3, .3]])
            >>> Boxes.random(3, rng=0, scale=100, anchors=anchors)
            <Boxes(tlwh,
                array([[ 2, 13, 51, 51],
                       [32, 51, 32, 36],
                       [36, 28, 23, 26]]))>
        """
        from netharn import util
        rng = util.ensure_rng(rng)
        as_integer = isinstance(scale, int)

        if anchors is None:
            tlbr = rng.rand(num, 4)

            tl_x = np.minimum(tlbr[:, 0], tlbr[:, 2])
            tl_y = np.minimum(tlbr[:, 1], tlbr[:, 3])
            br_x = np.maximum(tlbr[:, 0], tlbr[:, 2])
            br_y = np.maximum(tlbr[:, 1], tlbr[:, 3])

            tlbr[:, 0] = tl_x
            tlbr[:, 1] = tl_y
            tlbr[:, 2] = br_x
            tlbr[:, 3] = br_y
        else:
            anchors = np.asarray(anchors)
            assert np.all(anchors <= 1.0)
            assert np.all(anchors > 0.0)
            anchor_xs = rng.randint(0, len(anchors), size=num)
            base_whs = anchors[anchor_xs]
            rand_whs = np.clip(
                base_whs * np.exp(rng.randn(num, 2) * anchor_std), 0, 1)
            # Allow cxy to vary within the allowed range
            min_cxy = rand_whs / 2
            max_cxy = (1 - min_cxy)
            rel_cxy = rng.rand(num, 2) * .99
            rand_cxwy = rel_cxy * (max_cxy - min_cxy) + min_cxy
            cxywh = np.hstack([rand_cxwy, rand_whs])
            tlbr = Boxes(cxywh, 'cxywh').to_tlbr().data

        tlbr = tlbr * scale
        if as_integer:
            tlbr = tlbr.astype(np.int)
        if tensor:
            if as_integer:
                tlbr = torch.LongTensor(tlbr)
            else:
                tlbr = torch.FloatTensor(tlbr)
        boxes = Boxes(tlbr, format='tlbr').toformat(format, copy=False)
        return boxes
Exemplo n.º 9
0
 def __init__(self, rng=None):
     self.rng = util.ensure_rng(rng)