Can't load Inception V3 in Fastai V1

I’m trying to load Inception V3 in fastai V1 (1.0.41) like so:

from fastai.train import ShowGraph
from fastai.vision import (
    accuracy,
    create_cnn,
    models,
)
from fastai.vision.data import (
    ImageDataBunch,
    ClassificationInterpretation,
)
from fastai.vision.transform import get_transforms
import matplotlib.pyplot as plt
import torch
import torchvision.models as torch_models

data = ImageDataBunch.from_folder(
    './data/processed/images_sample',
    size=299,
    valid_pct=0.1,
    bs=128,
    ds_tfms=get_transforms(),
)

learn = create_cnn(
    data,
    torch_models.inception_v3,
    metrics=accuracy,
    callback_fns=ShowGraph,
)

I then get the following error:

RuntimeError: std::exception

The error seems specific to fastai as I can load the model fine using only PyTorch.

1 Like

I have not tried to get it to work myself, so no real answers on that, but I can try to explain why your approach won’t work:

create_cnn takes a given architecture, cuts of the final layer(s) (head) and attaches a custom fastai head (concat pooling, multiple fc layers instead of one). For this it uses the methods create_body and create_head.

The create_body method actually disassembles the model and puts it back together using a nn.Sequential module. This actually assumes a certain way the base model has been coded and works with the ResNets and some other archs. But it basically looses (some of?) the original models .forward() methods and replaces them with those the sequential module auto-creates. So if there is a lot of special stuff going on in the forwards and/or the model is not coded the way fastai assumes (i.e. nn.ReLU as a layer, not F.ReLU() in the forward), the resulting model after create_cnn will be different from the original you put in and therefore simply may not work anymore.

So I think if it is possible to get it to work with fastai, it will be by using the model “directly” and not through create_cnn. It might work by creating a databunch separately and then using the Learner itself ( learn = Learner(data, model, metrics) ) (Not tested, just a pointer…)

hi did you get any work around for this…

You can load the model with a costume number of outputs like this:

model = torch_models.inception_v3()
nf = data.c # number of output classes in your data
model.fc = nn.Linear(in_features=2048, out_features=nf) # replace fc layer

Then take a look at the code for create_cnn to find how to create the Learner.

1 Like

m doing this already but not sure where the error coming in from. Message is very vague ,hard to understand…

Hi, I can do:

model = torchvision.models.inception_v3(pretrained=True)
model.fc = nn.Linear(in_features=2048, out_features=data_299.c) # replace fc layer
learn = Learner(data_299, model)

But learn.summary() fails with:

AttributeError: ‘NoneType’ object has no attribute ‘shape’

fit_one_cycle also fails. Have you been able to successfully create and train a learner with this model?