Skip to content

Instantly share code, notes, and snippets.

@ferrine
Last active June 22, 2018 11:01
Show Gist options
  • Save ferrine/45f764511c327ca76da707cce9284028 to your computer and use it in GitHub Desktop.
Save ferrine/45f764511c327ca76da707cce9284028 to your computer and use it in GitHub Desktop.
sample swap for image (batch, cannel, height, width*2)
def sample_swap(shape):
b, c, h, w = shape
left = np.arange(0, w//2)
right = np.arange(w//2, w)
flip = np.random.uniform(size=(b, 1)) > .5
b = np.arange(b)[:, None, None, None]
c = np.arange(c)[None, :, None, None]
h = np.arange(h)[None, None, :, None]
w = np.concatenate([left+flip*(w//2), right-flip*(w//2)], -1)
w = w[:, None, None, :]
return np.broadcast_arrays(b, c, h, w)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment