Transfer learning cnn_learner: TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not bool

I would like to do transfer learning, ie. pretrain on CIFAR100 and train on Imagewoof. What am I doing wrong?

import as vision
from import *

data = ImageDataBunch.from_folder(untar_data(URLs.CIFAR_100, dest='/data'), valid="test")
learn = vision.cnn_learner(data, models.resnet18, metrics=[accuracy], pretrained=False)
learn.fit_one_cycle(10, slice(0.0005, 0.0005 * 30))

data2 = ImageDataBunch.from_folder(untar_data(URLs.IMAGEWOOF, dest='/data'), valid="val")
learn2 = vision.cnn_learner(data2, learn.model, metrics=[accuracy])
learn2.fit_one_cycle(5, slice(0.0014, 0.0014 * 30))

Gives me the error: TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not bool

You cannot pass an architecture explicitly into cnn_learner. If you want to do what you have described, you should instead use a Learner. IE:

learn2 = Learner(data2, learn.model, metrics=[accuracy], loss_func=CrossEntropyLossFlat(), splitter=default_split).freeze()

(I’m not 100% exactly sure on the splitter there, you’d want to look at cnn_learner's source code to verify if that does not work.

1 Like

Hi @muellerzr ,

I tried to reference the previously trained cnn_learner using the Learner and specifying learn.model as you suggest

learn2 = Learner(dls2, learn.model, metrics=accuracy)

Unfortunately as soon as I use the new learner to fine tune I got this runtime error:

RuntimeError                              Traceback (most recent call last)
<ipython-input-30-37b3634da61a> in <module>()
      1 learn4 = Learner(dls2, learn.model, metrics=accuracy)
----> 2 learn4.fine_tune(20)

19 frames
/usr/local/lib/python3.6/dist-packages/fastai2/ in _f(self, *args, **kwargs)
    296         def _f(self, *args, **kwargs):
    297             cls = self.__class__
--> 298             res = getattr(super(TensorBase, self), fn)(*args, **kwargs)
    299             return retain_type(res, self, copy_meta=True)
    300         return _f

RuntimeError: CUDA error: device-side assert triggered

Do you have any advise ? Thanks!!

I figure it out it was my mistake. Reading the book I got the wrong misunderstanding that the Learner automatically adapt the last layer based on the passed dataloaders labels.

I’ve manually changed the last linear layer of the model before passing it to the Learner and everything works now.