How to extract item_tfms from a learner object?

I need to extract. the item transformation (e.g. creating PIL object and resizing) and batch transformation (e.g. normalizing) from a learner and apply them to a PIL image or simply a path.

So I create basic data loader and then a learner like this:

dls = ImageDataLoaders.from_name_func(
    path, imgs, valid_pct=0.2, seed=42,
    label_func=is_cat, item_tfms=Resize(224))
learn = cnn_learner(dls, resnet34, metrics=error_rate)

Now it seems I can get the batch transformation applied to the image as follows (is there a better way?):


But how can I get the item transformation item_tfms from a learner object to apply it on an input path?

This post exposes the API but you want after_item :slight_smile: the Type transforms aren’t quite as clean so you’d still want PILImage I think

Speeding Up fastai2 Inference - And A Few Things Learned

Thanks @muellerzr for the input, the thing is I cannot build the transforms manually like in your case, i.e.

type_tfms = [PILImage.create]
item_tfms = [Resize(224), ToTensor()]
type_pipe = Pipeline(type_tfms)
item_pipe = Pipeline(item_tfms)
norm = Normalize.from_stats(*imagenet_stats)
i2f = IntToFloatTensor()

In my case I have a learner object as input and I need to find out what are all transforms applied by this learner to a given image or path.

You can look at the after_item and after_batch, this is more or less the exact pipeline. Anything that’s a Lighting or Affine transform isn’t applied to the validation set.

Also, if you want an input path to the image that’s specifically the type transforms

1 Like

Oh after_item is the way to go! I realized it accepts an PILImage instance (I was trying with a path)

1 Like