Tensor Item Transforms

Adds support for resizing and cropping TensorImage and TensorMask as an item_tfm

Implementation Patches and Methods

Augmentations patched: RandomCrop, CropPad, Resize, RandomResizedCrop, & RatioResize.

Note

These are all patched extensions of the fastai.vision.augment item transforms.

For details on how they work, see the fastai documentation.

RandomCrop


source

encodes

 encodes
          (x:Union[fastai.torch_core.TensorImage,fastai.torch_core.TensorM
          ask])

Extends RandomCrop to TensorImage & TensorMask

_,axs = plt.subplots(1,3,figsize=(12,4))
f = RandomCrop(200)
for ax in axs: f(img).show(ctx=ax)

On the validation set, a center crop is always taken.

_,axs = plt.subplots(1,3,figsize=(12,4))
for ax in axs: f(img, split_idx=1).show(ctx=ax)

CropPad


source

encodes

 encodes
          (x:Union[fastai.torch_core.TensorImage,fastai.torch_core.TensorM
          ask])

Extends CropPad to TensorImage & TensorMask

_,axs = plt.subplots(1,3,figsize=(12,4))
for ax,mode in zip(axs.flatten(), [PadMode.Zeros, PadMode.Border, PadMode.Reflection]):
    img.crop_pad((600,700), pad_mode=mode).show(ctx=ax, title=mode)

Resize


source

encodes

 encodes
          (x:Union[fastai.torch_core.TensorImage,fastai.torch_core.TensorM
          ask])

Extends Resize to TensorImage & TensorMask

_,axs = plt.subplots(1,3,figsize=(12,4))
for ax,method in zip(axs.flatten(), [ResizeMethod.Squish, ResizeMethod.Pad, ResizeMethod.Crop]):
    rsz = Resize(256, method=method)
    rsz(img, split_idx=0).show(ctx=ax, title=method)

On the validation set, the crop is always a center crop (on the dimension that’s cropped).

_,axs = plt.subplots(1,3,figsize=(12,4))
for ax,method in zip(axs.flatten(), [ResizeMethod.Squish, ResizeMethod.Pad, ResizeMethod.Crop]):
    rsz(img, split_idx=1).show(ctx=ax, title=method)

RandomResizedCrop


source

encodes

 encodes
          (x:Union[fastai.torch_core.TensorImage,fastai.torch_core.TensorM
          ask])

Extends RandomResizedCrop to TensorImage & TensorMask

crop = RandomResizedCrop(256)
_,axs = plt.subplots(3,3,figsize=(9,9))
for ax in axs.flatten():
    crop(img).show(ctx=ax)

Squish is used on the validation set, removing val_xtra proportion of each side first.

_,axs = plt.subplots(1,3,figsize=(12,4))
for ax in axs.flatten():
    cropped = crop(img, split_idx=1).show(ctx=ax)

RatioResize


source

encodes

 encodes
          (x:Union[fastai.torch_core.TensorImage,fastai.torch_core.TensorM
          ask])

Extends RatioResize to TensorImage & TensorMask

RatioResize(256)(img).show()
<AxesSubplot:>