Lesson 7 official topic

How do we retrieve the mean and std vectors for normalization associated with pre-trained models?

I realize that fastai applies the normalization for training and inference automatically, but if I export the model (eg, to iOS), I need to apply the normalization manually to the data prior to feeding it to the model for inference. Do all pre-trained models use the imagenet normalization vectors? How about pre-trained models provided by timm

Thanks!

1 Like

Most use imagenet stats, but not all. You’ll find learner.normalization contains the callback object, and the stats will be in there.

1 Like

Thank you. I must be doing something wrong. I get this: AttributeError: 'XResNet' object has no attribute 'normalization'

from fastai.vision.all import *
path = untar_data(URLs.IMAGENETTE)
dblock = DataBlock(blocks=(ImageBlock(), CategoryBlock()),
                   get_items=get_image_files,
                   get_y=parent_label,
                   item_tfms=Resize(460),
                   batch_tfms=aug_transforms(size=224, min_scale=0.75))
dls = dblock.dataloaders(path, bs=64)
learn = Learner(dls, model, loss_func=CrossEntropyLossFlat(), metrics=accuracy)
learn.normalization

Ah that’s because you didn’t use vision_learner, so you didn’t get normalization added automatically.