Skip to content

Instantly share code, notes, and snippets.

@jcreinhold
Created January 3, 2019 15:18
Show Gist options
  • Save jcreinhold/2850d21faf426af06f077375b8a7eafc to your computer and use it in GitHub Desktop.
Save jcreinhold/2850d21faf426af06f077375b8a7eafc to your computer and use it in GitHub Desktop.
transforms for fastai
from functools import singledispatch
@faiv.TfmPixel
@singledispatch
def crop(x, pct, axis:int) -> torch.Tensor:
"""" crop a 3d image along an axis """
s = x.shape
i0, i1 = int(s[axis]*pct[0]), int(s[axis]*pct[1])
return x[np.newaxis,i0:i1,:,:].contiguous() if axis == 0 else \
x[np.newaxis,:,i0:i1,:].contiguous() if axis == 1 else \
x[np.newaxis,:,:,i0:i1].contiguous()
tfms = [crop(pct=(0.20,0.80),axis=2)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment