TypeError on the Conv2d()


I’m going through the Practical Deep Learning for coders and I’m getting the following error:

Traceback (most recent call last):
  File "/home/onur/Documents/fastai-deep-learning/Model.py", line 16, in <module>
    learn = cnn_learner(data, models.resnet34(), metrics=error_rate)
  File "/home/onur/anaconda3/lib/python3.7/site-packages/fastai/vision/learner.py", line 98, in cnn_learner
    bn_final=bn_final, concat_pool=concat_pool)
  File "/home/onur/anaconda3/lib/python3.7/site-packages/fastai/vision/learner.py", line 84, in create_cnn_model
    body = create_body(base_arch, pretrained, cut)
  File "/home/onur/anaconda3/lib/python3.7/site-packages/fastai/vision/learner.py", line 56, in create_body
    model = arch(pretrained)
  File "/home/onur/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/onur/anaconda3/lib/python3.7/site-packages/torchvision/models/resnet.py", line 216, in forward
    return self._forward_impl(x)
  File "/home/onur/anaconda3/lib/python3.7/site-packages/torchvision/models/resnet.py", line 199, in _forward_impl
    x = self.conv1(x)
  File "/home/onur/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/onur/anaconda3/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 345, in forward
    return self.conv2d_forward(input, self.weight)
  File "/home/onur/anaconda3/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 342, in conv2d_forward
    self.padding, self.dilation, self.groups)
TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not bool

I looked up the problem and saw that other students were having the same issue. However, I couldn’t really make sense of the suggested solutions. I’m very new to this. The following is my code so far:

from fastai import *
from fastai.vision import *
from pathlib import Path

image_path = Path("images")
fnames = get_image_files(image_path)

# The regular expression will be used to get the image names
file_parse = r'/([^/]+)_\d+.jpg$'

# ImageDataBunch.from_name_re extracts the names of the images and assings them as the labels for the images
# Size=224 turns each image to the same size, in this case, 224
data = ImageDataBunch.from_name_re(image_path, fnames, file_parse, ds_tfms=get_transforms(), size=224)

learn = cnn_learner(data, models.resnet34(), metrics=error_rate)

You should just use models.resnet34. Not an instance of it. It expects a function not the object (cnn_learner I mean)

Let me know if that’s confusing at all :slight_smile:

@muellerzr made it clear, if you need more explanation this helped me -> https://www.datacamp.com/courses/object-oriented-programming-in-python

I am not affiliated with them, and I paid for the course. If you did not code before at all clearing out the basics could help. (Just code!)