from fastai.vision.all import *
Creating your own
Transform is way easier than you think. In fact, each time you have passed a label function to the data block API or to
ImageDataLoaders.from_name_func, you have created a
Transform without knowing it. At its base, a
Transform is just a function. Let's show how you can easily add a transform by implementing one that wraps a data augmentation from the albumentations library.
First things first, you will need to install the albumentations library. Uncomment the following cell to do so if needed:
Then it's going to be easier to see the result of the transform on a color image bigger than the mnist one we had before, so let's load something from the PETS dataset.
source = untar_data(URLs.PETS) items = get_image_files(source/"images")
We can still open it with
img = PILImage.create(items) img
We will show how to wrap one transform, but you can as easily wrap any set of transforms you wrapped in a
Compose method. Here let's do some
from albumentations import ShiftScaleRotate
aug = ShiftScaleRotate(p=1) def aug_tfm(img): np_img = np.array(img) aug_img = aug(image=np_img)['image'] return PILImage.create(aug_img)