Hans Pinckaers — Public notes

A collection of short public notes exported from my private tiddlywiki. Find an overview of all notes here. Feedback? You can reach me at mail@hanspinckaers.com.

Use fast.ai transforms with regular PyTorch

hanspinckaers updated 2 days ago created: 02 March 2021
Deep learning Public

I wanted to use the data augmentations pipeline of fast.ai in some other repository which didn't use fast.ai. Here are examples to use the aug_transforms.

Apply on a batch:

from fastai.vision.all import *

for batch, labels in train_loader:

fastai_transforms_train = aug_transforms(mult=3, size=768)
fast_batch = TensorImage(batch).float()
for tfm in fastai_transforms_train: fast_batch = tfm(fast_batch, split_idx=0)

Apply on a single image:

def fastai_transforms(self, img):
    img = TensorImage(img).permute(2, 0, 1)[None].float() / 255
    for tfm in self.fastai_transforms_train: img = tfm(img, split_idx=0)
    img = img[0].permute(1, 2, 0) * 255
    img = PIL.Image.fromarray(img.byte().numpy())
    return img