Error when loading model

I get an error when loading my model (last line of code below):
from fastai.imports import *
from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *
PATH = “/media/Snaps/”
sz=224
arch=resnet34
data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz),bs=8)
learn = ConvLearner.pretrained(arch, data, precompute=False)
learn.load(‘break_direction’)

error is:
RuntimeError: While copying the parameter named 16.weight, whose dimensions in the model are torch.Size([3, 512]) and whose dimensions in the checkpoint are torch.Size([2, 512]).

Rest of the code is:
trn_tfms, val_tfms = tfms_from_model(arch,sz) # get transformations
im = val_tfms(open_image(’/media/train/test/snaps87687_1.png’))
learn.precompute=False # We’ll pass in a raw image, not activations
preds = learn.predict_array(im[None])
np.argmax(preds) # preds are log probabilities of classes

I’m trying to re-use my model to predict a specific image.

It looks like the saved weights are not for the same architecture as the one you are trying to load it into.

Coming back on this a month later.
I solved my problem above, indeed I was probably using the wrong model against my data set.

Now I don’t have any error messages anymore, but my code below returns these lines
(‘test/snaps172653_-1.png’, ‘down’, 0.7188118)
(‘test/snaps172653_-1.png’, ‘down’, 0.7188118)
(‘test/snaps172653_-1.png’, ‘down’, 0.7188118)
(‘test/snaps172653_-1.png’, ‘up’, 0.28118828)
(‘test/snaps172653_-1.png’, ‘down’, 0.7188118)
(‘test/snaps172653_-1.png’, ‘up’, 0.28118828)
(‘test/snaps172653_-1.png’, ‘up’, 0.28118828)
(‘test/snaps172653_-1.png’, ‘down’, 0.7188118)
(‘test/snaps172653_-1.png’, ‘up’, 0.28118828)
(‘test/snaps172653_-1.png’, ‘down’, 0.7188118)
(‘test/snaps172653_-1.png’, ‘down’, 0.7188118)
(‘test/snaps172653_-1.png’, ‘up’, 0.28118828)

So basically the same file name 12 times, which is weird. I have 12 files in my test folder (which sits at the same level as my train and valid folders).

from fastai.imports import *
from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *
PATH = "/media/hugues/M.2 windows/Hugues/Snaps/"
sz=224
arch=resnet34
data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz), test_name='test')
learn = ConvLearner.pretrained(arch, data, precompute=False)
learn.load('up_down_2minslippage_scaled_enlarged_skipped50k')

trn_tfms, val_tfms = tfms_from_model(arch,sz) # get transformations
learn.precompute=False # We'll pass in a raw image, not activations
preds = learn.predict_array(im[None])
preds

log_preds = learn.predict(is_test=True)
preds = np.argmax(log_preds, axis=1)
itemIndex = 0
for maxIndex in preds:
    print((data.test_ds.fnames[itemIndex], 
             data.classes[maxIndex], 
             np.exp(log_preds[itemIndex][maxIndex])))
itemIndex = itemIndex + 1

Is my code correct ?

Well if it works then it is good enough :slight_smile: