I have my model defined this way:
def initialize_model(model_name, num_classes, feature_extract, use_pretrained=True):
# Initialize these variables which will be set in this if statement. Each of these
# variables is model specific.
model_ft = None
input_size = 0
model_ft = models.segmentation.fcn_resnet101(pretrained=use_pretrained)
set_parameter_requires_grad(model_ft, feature_extract)
# Handle the auxilary net
num_ftrs = model_ft.aux_classifier[4].in_features
model_ft.aux_classifier[4] = nn.Conv2d(num_ftrs, num_classes, kernel_size=(1, 1), stride=(1, 1))
# Handle the primary net
num_ftrs = model_ft.classifier[4].in_features
model_ft.classifier[4] = nn.Conv2d(num_ftrs, num_classes, kernel_size=(1, 1), stride=(1, 1))
input_size = 224 #?
return model_ft, input_size
but I get this error:
AttributeError Traceback (most recent call last)
in
1 # Initialize the model for this run
----> 2 model_ft, input_size = initialize_model(model_name, num_classes, feature_extract, use_pretrained=True)
3
4 # Print the model we just instantiated
5 print(model_ft)
in initialize_model(model_name, num_classes, feature_extract, use_pretrained)
10
11 # Handle the auxilary net
—> 12 num_ftrs = model_ft.aux_classifier[4].in_features
13 model_ft.aux_classifier[4] = nn.Conv2d(num_ftrs, num_classes, kernel_size=(1, 1), stride=(1, 1))
14
~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in getattr(self, name)
537 return modules[name]
538 raise AttributeError("’{}’ object has no attribute ‘{}’".format(
–> 539 type(self).name, name))
540
541 def setattr(self, name, value):
AttributeError: ‘Conv2d’ object has no attribute ‘in_features’
The reason I opted using in-features was that when running this part of the code:
model_ft, hist = train_model(model_ft, dataloaders_dict, criterion, optimizer_ft, num_epochs=num_epochs, has_aux = True)
I got this error:
~\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)
336 _pair(0), self.dilation, self.groups)
337 return F.conv2d(input, self.weight, self.bias, self.stride,
–> 338 self.padding, self.dilation, self.groups)
339
340
RuntimeError: Given groups=1, weight of size 4 256 1 1, expected input[4, 512, 28, 28] to have 256 channels, but got 512 channels instead.
I assuemd I need to get the input features to the layers I am changing their output through in_features. Before, I copied the layer and only changed the number of classes argument.
Could you please help me out if you encountered similar issues or have an idea what is going on in my case? any help is immensely appreciated.