Skip to content

Instantly share code, notes, and snippets.

@ferrine
Last active May 23, 2018 16:01
Show Gist options
  • Save ferrine/30982fb376df84284a893d26713db8bd to your computer and use it in GitHub Desktop.
Save ferrine/30982fb376df84284a893d26713db8bd to your computer and use it in GitHub Desktop.
Random batch subimages numpy
def random_subimages_idx(n, d1, d2, h, w):
hs = np.random.randint(d1-h, size=n)
ws = np.random.randint(d2-w, size=n)
grid = np.meshgrid(np.arange(h), np.arange(w), indexing='ij')
i = np.tile(np.arange(n)[:, None, None], (h, w))
xs = grid[0]+hs[:, None, None]
ys = grid[1]+ws[:, None, None]
return i, xs, ys
def random_subimages(images, h, w):
return images[random_subimages_idx(*images.shape, h, w)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment