I used to train Inception model using older fastai as follows:
from fastai.conv_learner import*
from fastai.transforms import*
from fastai.dataset import*
from fastai.plots import*
import torch
import torchvision.models as TorchModels
import numpy as np
Initial setup
PATH=’/my_path/’
bs=32
sz=299 # image size for Inception-V3
Choose torch model
inceptionModel = TorchModels.inception_v3(pretrained=True)
My dataset
np.random.seed(42)
data = ImageClassifierData.from_paths(PATH, bs=bs, trn_name =‘train’, val_name = ‘valid’, test_name = ‘test’, tfms = tfms_from_model(inceptionModel,sz))
Freeze Inception weights
for param in inceptionModel.parameters():
param.requires_grad = False
Adjust last layer to my dataset
num_ftrs = inceptionModel.AuxLogits.fc.in_features
inceptionModel.AuxLogits.fc = nn.Linear(num_ftrs, len(data.classes))
num_ftrs = inceptionModel.fc.in_features
inceptionModel.fc = nn.Linear(num_ftrs, len(data.classes))
Set model to GPU
inceptionModel = inceptionModel.cuda()
Choose criterion and metrics
loss = nn.CrossEntropy()
metrics = [accuracy]
set optimizer
opt = optim.Adam(inceptionModel.parameters(), lr = 1e-2)
Begin training
vals, ep_vals = fit(inceptionModel, data, n_epochs = 2, crit = loss, opt = opt, metrics = metrics, get_ep_vals = True)
And so on…
Now, on Fastai V1 I tried the steps:
from fastai.vision import *
from fastai.metrics import accuracy
mport torch
import torchvision.models as TorchModels
import numpy as np
Initial setup
path=’/my_path/’
bs=32
sz=299 # image size for Inception-V3
np.random.seed(42)
data = ImageDataBunch.from_folder(path, ds_transformsms(do_flip = False, flip_vert = False, max_rotate = 0.0), size = sz, bs= bs). normalize (imagenet_stats)
Choose torch model
inceptionModel = TorchModels.inception_v3(pretrained=True)
Freeze Inception weights
for param in inceptionModel.parameters():
param.requires_grad = False
Adjust last layer to my dataset
If needed uncomment the line below
inceptionModel.aux_logit = False
num_ftrs =
inceptionModel.AuxLogits.fc.in_features
inceptionModel.AuxLogits.fc = nn.Linear(num_ftrs, len(data.classes))
num_ftrs = inceptionModel.fc.in_features
inceptionModel.fc = nn.Linear(num_ftrs, len(data.classes))
Set model to GPU
inceptionModel = inceptionModel.cuda()
Show samples from dataset
data.show_batch(rows = 3, figsize = (10,7))
Classes
print(data.classes)
At this point, the errors start to pop out. Using
learn = cnn_learner(data, inceptionModel, metrics = accuracy)
In the function create_body(arch:Callable, pretrained:bool=True, cut: Optional)
When forward function from torchvision\models\inception.py produces the error:
def forward(self,x):
if self.transform_input:
—> 77 x_ch0 = torch.unsqueeze(x[:,0] *(0.229/0.5) + (0.485 - 0.5) /0.5
TypeError: ‘bool’ object is not subscriptable
When using:
learn = Learner(data, inceptionModel, metrics= accuracy)
learn.fit_one_cycle(1)
In this case, because Inception has two outputs, the model returns a tuple instead of a tensor so the the loss_bacth in fastai\basic_train.py returns:
fastai\layers.py in call(self, input, target, **kwargs)
def call(self, input:Tensor, target:Tensor, **kwargs)
—>240 input= input.transpose(self.axis,-1).contiguous()
TypeError: ‘tuple’ object has no attribute ‘transpose’
Any ideas besides disabling inceptionModel.aux_logit?
PS.: I have also tried AlexNet. It worked with Learner and fit_one_cycle, but not with interp.plot_top_losses
TypeError: ‘AlexNet’ object does not support indexing
My previous experience with older fastai and torch models says that there be ways to work around with this.