Using .In_feature for fetching the input to conv2d layer inin FCN_ResNet101 model

username: samin_hamidi
cooked: <p>I have my model  defined this way:<br>
def initialize_model(model_name, num_classes, feature_extract, use_pretrained=True):<br>
# Initialize these variables which will be set in this if statement. Each of these<br>
#   variables is model specific.<br>
model_ft = None<br>
input_size = 0</p>
<pre><code>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
</code></pre>
<p>but I get this error:</p>
<p>AttributeError                            Traceback (most recent call last)<br>
 in <br>
1 # Initialize the model for this run<br>
----&gt; 2 model_ft, input_size = initialize_model(model_name, num_classes, feature_extract, use_pretrained=True)<br>
3<br>
4 # Print the model we just instantiated<br>
5 print(model_ft)</p>
<p> in initialize_model(model_name, num_classes, feature_extract, use_pretrained)<br>
10<br>
11     # Handle the auxilary net<br>
—&gt; 12     num_ftrs = model_ft.aux_classifier[4].in_features<br>
13     model_ft.aux_classifier[4] = nn.Conv2d(num_ftrs, num_classes, kernel_size=(1, 1), stride=(1, 1))<br>
14</p>
<p>~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in <strong>getattr</strong>(self, name)<br>
537                 return modules[name]<br>
538         raise AttributeError("’{}’ object has no attribute ‘{}’".format(<br>
–&gt; 539             type(self).<strong>name</strong>, name))<br>
540<br>
541     def <strong>setattr</strong>(self, name, value):</p>
<p>AttributeError: ‘Conv2d’ object has no attribute ‘in_features’</p>
<p>The reason I opted using in-features was that when running this part of the code:</p>
<p>model_ft, hist = train_model(model_ft, dataloaders_dict, criterion, optimizer_ft, num_epochs=num_epochs, has_aux = True)</p>
<p>I got this error:</p>
<p>~\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)<br>
336                             _pair(0), self.dilation, self.groups)<br>
337         return F.conv2d(input, self.weight, self.bias, self.stride,<br>
–&gt; 338                         self.padding, self.dilation, self.groups)<br>
339<br>
340</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>

username: ptrblck
cooked: <p><code>.in_features</code> defines the input features of e.g. <code>nn.Linear</code>, while <code>nn.Conv2d</code> uses <code>.in_channels</code> to define the number of input channels.<br>
Could you try this attribute and let me know, if this works for you?</p>