Last active
May 23, 2018 16:01
-
-
Save ferrine/30982fb376df84284a893d26713db8bd to your computer and use it in GitHub Desktop.
Random batch subimages numpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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