Ejemplo n.º 1
0
def RandomImagePatches(iterable, imagecols, pshape, npatches):
    """
    samples >> RandomImagePatches(imagecols, shape, npatches)

    Extract patches at random locations from images.

    >>> import numpy as np
    >>> np.random.seed(0)    # just to ensure stable doctest
    >>> img = np.reshape(np.arange(30), (5, 6))
    >>> samples = [(img, 0)]
    >>> getpatches = RandomImagePatches(0, (2, 3), 3)
    >>> for (p, l) in samples >> getpatches:
    ...     print(p.tolist(), l)
    [[7, 8, 9], [13, 14, 15]] 0
    [[8, 9, 10], [14, 15, 16]] 0
    [[8, 9, 10], [14, 15, 16]] 0

    :param iterable iterable: Samples with images
    :param int|tuple imagecols: Indices of sample columns that contain
      images, where patches are extracted from.
      Images must be numpy arrays of shape h,w,c or h,w
    :param tuple shape: Shape of patch (h,w)
    :param int npatches: Number of patches to extract (per image)
    :return: Iterator over samples where images are replaced by patches.
    :rtype: generator
    """
    imagecols = as_tuple(imagecols)
    for sample in iterable:
        image = sample[imagecols[0]]
        hi, wi = image.shape[:2]
        hs2, ws2 = pshape[0] // 2 + 1, pshape[1] // 2 + 1
        rr = np.random.randint(hs2, hi - hs2, npatches)
        cc = np.random.randint(ws2, wi - ws2, npatches)
        for r, c in zip(rr, cc):
            yield ut.col_map(sample, imagecols, ni.extract_patch, pshape, r, c)
Ejemplo n.º 2
0
def RegularImagePatches(iterable, imagecols, pshape, stride):
    """
    samples >> RegularImagePatches(imagecols, shape, stride)

    Extract patches in a regular grid from images.

    >>> import numpy as np
    >>> img = np.reshape(np.arange(12), (3, 4))
    >>> samples = [(img, 0)]
    >>> getpatches = RegularImagePatches(0, (2, 2), 2)
    >>> for p in samples >> getpatches:
    ...     print(p)
    (array([[0, 1],
           [4, 5]]), 0)
    (array([[2, 3],
           [6, 7]]), 0)

    :param iterable iterable: Samples with images
    :param int|tuple imagecols: Indices of sample columns that contain
      images, where patches are extracted from.
      Images must be numpy arrays of shape h,w,c or h,w
    :param tuple shape: Shape of patch (h,w)
    :param int stride: Step size of grid patches are extracted from
    :return: Iterator over samples where images are replaced by patches.
    :rtype: generator
    """
    colset = as_set(imagecols)
    for sample in iterable:
        patch_iters = ut.col_map(sample, imagecols, ni.patch_iter, pshape,
                                 stride)
        while True:
            patched = []
            for i, p in enumerate(patch_iters):
                try:
                    patched.append(next(p) if i in colset else p)
                except StopIteration:
                    pass
            if len(patched) == len(sample):
                yield tuple(patched)
            else:
                break  # one or all patch iterators are depleted
def test_col_map():
    sample = (1, 2, 3)
    add_n = lambda x, n: x + n
    assert util.col_map(sample, 1, add_n, 10) == (1, 12, 3)
    assert util.col_map(sample, (0, 2), add_n, 10) == (11, 2, 13)