10

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.

4

3 回答 3

2

这可能是一个边缘情况,其中某些批次的批次大小等于 1。确保没有一个批次 = 1(主要是最后一批)

于 2018-03-29T01:54:23.983 回答
2

只要您的一批数据包含单个元素,就会发生此错误。

解决方案 1:在 learn.load('resnet34_test') 之后调用 learn.predict()

解决方案 2:从训练集中删除 1 个数据点。

Pytorch 问题

Fastai论坛问题描述

于 2018-05-02T17:06:55.817 回答
-2

在训练中,如果您在训练集批次中有 1 个数据,您将收到此错误。

如果您使用模型来预测输出,请确保设置

learner.eval()
于 2018-10-12T12:57:30.127 回答