'function' object has no attribute 'cuda'

Hello.

I’m trying to tweak things from the lecture little bit, and now working on having the Resnet without the pre-trained weights.
So after copying and pasting all the Resnet-related codes from master library, I attempted the following:

arch = resnet152
learn = ConvLearner.from_model_data(resnet152, data)

As you can see, I put .from_model_data instead of .pretrained, and got this error.


AttributeError Traceback (most recent call last)
in
----> 1 learn = ConvLearner.from_model_data(resnet152, data)

~\fastai\courses\dl1\fastai\learner.py in from_model_data(cls, m, data, **kwargs)
42 @classmethod
43 def from_model_data(cls, m, data, **kwargs):
—> 44 self = cls(data, BasicModel(to_gpu(m)), **kwargs)
45 self.unfreeze()
46 return self

~\fastai\courses\dl1\fastai\core.py in to_gpu(x, *args, **kwargs)
88 def to_gpu(x, *args, **kwargs):
89 ‘’‘puts pytorch variable to gpu, if cuda is available and USE_GPU is set to true. ‘’’
—> 90 return x.cuda(*args, **kwargs) if USE_GPU else x
91
92 def noop(*args, **kwargs): return

AttributeError: ‘function’ object has no attribute ‘cuda’

Does anyone know how to deal with this issue? Seems like the ‘function’ and ‘cuda’ problem, but my CUDA is working just fine and I don’t know where that ‘function’ comes from.

Any help is appreciated.

I encountered this problem today as well. I think the problem is that you have not instantiated a class instance, which seems to be required for from_model_data. In order to access the .cuda() function, which is inherited from torch.nn.Module, you must create a new instance of the resnet152 class, i.e.:

arch = resnet152()
learn = ConvLearner.from_model_data(arch, data)

It looks like you also create arch, but then didn’t use it when creating your ConvLearner. I’m still a bit confused after looking through the source code, but it seems to be something about how ConvLearner is intialized differently depending on whether it calls pretrained or from_model_data.