Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I re-use trained fastai models?

How do I load pretrained model using fastai implementation over PyTorch? Like in SkLearn I can use pickle to dump a model in file then load and use later. I've use .load() method after declaring learn instance like bellow to load previously saved weights:

arch=resnet34
data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz))
learn = ConvLearner.pretrained(arch, data, precompute=False)
learn.load('resnet34_test')

Then to predict the class of an image:

trn_tfms, val_tfms = tfms_from_model(arch,100)
img = open_image('circle/14.png')
im = val_tfms(img)
preds = learn.predict_array(im[None])
print(np.argmax(preds))

But It gets me the error:

ValueError: Expected more than 1 value per channel when training, got input size [1, 1024]

This code works if I use learn.fit(0.01, 3) instead of learn.load(). What I really want is to avoid the training step In my application.

like image 970
ni8mr Avatar asked Mar 21 '18 04:03

ni8mr


2 Answers

This error occurs whenever a batch of your data contains a single element.

Solution 1: Call learn.predict() after learn.load('resnet34_test')

Solution 2: Remove 1 data point from your training set.

Pytorch issue

Fastai forum issue description

like image 105
zentia Avatar answered Nov 03 '22 13:11

zentia


This could be an edge case where batch size equals 1 for some batch. Make sure none of you batches = 1 (mostly the last batch)

like image 32
Feras Avatar answered Nov 03 '22 14:11

Feras