[ SOLVED ] Get prediction from the ImageNet model without creating a databunch

Hi, instead of fine-tuning a pretrained model (like resnet34 that was trained on the 1000 classes of ImageNet), I would like to use the pretrained model without modification in order to get predictions through model.predict(img).

Note: I’ve already posted this question into another thread but I think it deserves its own one.

If it is easy to get the weights of a pretrained model with model = models.resnet34(pretrained=True) for example, how to get the labels of the 1000 ImageNet classes and their structure (for example: 0 is car, 1 is plant, etc.).

The fastai v1 way for that is creating a (Image)databunch which gives the labels from the stored data (images here) thanks to the data block API, and then build a learner with create_cnn(data, model) for example but I do not want to download the 138 GB of Imagenet training dataset :wink:

Any advice?


@pierreguillou Is this what you’re looking for: https://discuss.pytorch.org/t/pretrained-resnet-constant-output/2760

Thanks @jithinrocs. I rewrote the Pytorch code with the fastai v1 one in the following notebook.
Just one thing is missing: how can I export the model through model.export() (see tutorial on inference) as my model does not have a databunch?

1 Like

@pierreguillou Check this from the Pytorch tutorial: https://pytorch.org/tutorials/beginner/saving_loading_models.html#

Thanks Jithin. This Pytorch code is working but I was looking for the fastai code in order to export/load my model… and I found it :slight_smile:

Indeed, I updated my notebook with the following code from fastai.

Note: there is only one point to improve. In order to create the DataBunch data, I used single_from_classes which is now obsolete. If someone could give me the code to use instead, we could close this thread.

# Get transforms
tfms = [ [], [crop_pad()] ]         # tfms = get_transforms() is possible too 

# Get databunch with the ImageNet classes
data = ImageDataBunch.single_from_classes(path, classes, tfms=tfms, size=224).normalize(imagenet_stats)

# Get and save learner
learn = Learner(data, model)

# Load learner
learn = load_learner(path)

# Get prediction
cat, indice, preds = learn.predict(img)

Eh, I think you might have found the one application where this function is still useful. I’ll leave the deprecation warning as I don’t want people to think it’s the right way to do inference (learn.export is) but we won’t remove the function.


Thanks Sylvain for leaving single_from_classes (although deprecated) as this solves the problem of creating a DataBunch (and then, a learner) from a list of classes but without having any data (the regular fastai v1 way is more to recover the labels from the data we have).

(I edited the title of the thread as [SOLVED]).