How to delete layer in pretrained model?

That was typed on my phone and is a typo. Should be Sequential. When in doubt, just search the docs.

https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html

now getting this error:


TypeError Traceback (most recent call last)
in
43
44 #model_ft = model_ft.to(device)
β€”> 45 model = MyEnsemble(model_ft,model_ft1).to(device)
46
47 criterion = nn.CrossEntropyLoss()

in init(self, modelA, modelB, nb_classes)
25
26
β€”> 27 self.cnnC = nn.Sequential(nn.Conv2d(in_channels=2, out_channels=3, kernel_size = (3, 3), padding = (1, 1)), nn.BatchNorm2d(out_channels=3), nn.ReLU())
28
29 self.final_linear=nn.LazyLinear(10)

TypeError: init() got an unexpected keyword argument β€˜out_channels’

Thanks in advance.

The stacktrace points to the line of code raising the error:

β€”> 27 self.cnnC = nn.Sequential(nn.Conv2d(in_channels=2, out_channels=3, kernel_size = (3, 3), padding = (1, 1)), nn.BatchNorm2d(out_channels=3), nn.ReLU())

and explains what exactly fails:

TypeError: init() got an unexpected keyword argument β€˜out_channels’

The TypeError means that the tagged line of code contains an out_channels argument, which is invalid.
Now you can check where this out_channels argument is used and check the docs of each module.
As you can see, nn.Conv2d expects the out_channels argument, while nn.BatchNorm2d does not and thus raises the error.
I would highly recommend checking the docs for these types of errors and I’m sure @J_Johnson is happy to help you in more complicated situations.

The code provided was intended to have you define in_channels and out_channels as an argument.

For example:

in_channels = 128
out_channels = 128

self.cnnC = nn.Sequential(nn.Conv2d(in_channels, ...

And then the code provided should work.

Because I do not know the dims coming out of your ModelA and ModelB, I cannot tell you what those values should be. You need to print the output size of those models and add the dim=1 values together, since they are concatenated. That should give the correct value to replace 64 in the above code

can you please tell me how can i found out in_channels = ? and out_channels = ? values in my code.

print(x1.size(1), x2.size(1))

Put that in the forward pass of your original code. Then find what printed before the errors.

Thanks its 4096 both x1 and x2.

Now this error comes in execution:

in this code:

model = train_model(model, criterion, optimizer_ft, exp_lr_scheduler, num_epochs=8)

error is:

RuntimeError Traceback (most recent call last)
in
----> 1 model = train_model(model, criterion, optimizer_ft, exp_lr_scheduler, num_epochs=8)

7 frames
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
457 weight, bias, self.stride,
458 _pair(0), self.dilation, self.groups)
β†’ 459 return F.conv2d(input, weight, bias, self.stride,
460 self.padding, self.dilation, self.groups)
461

RuntimeError: Expected 3D (unbatched) or 4D (batched) input to conv2d, but got input of size: [16, 8192]

Is that print before or after your .view() statements? If before, it means your models are giving out flattened tensors. You could either skip the sequentual convolution operation, or reshape them with something like:

x = x.reshape(-1, 512, 4, 4) 

x = cnnC(x)
...

4096 4096

RuntimeError Traceback (most recent call last)
in
----> 1 model = train_model(model, criterion, optimizer_ft, exp_lr_scheduler, num_epochs=8)

7 frames
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
457 weight, bias, self.stride,
458 _pair(0), self.dilation, self.groups)
β†’ 459 return F.conv2d(input, weight, bias, self.stride,
460 self.padding, self.dilation, self.groups)
461

RuntimeError: Given groups=1, weight of size [4096, 4096, 3, 3], expected input[16, 512, 4, 4] to have 4096 channels, but got 512 channels instead

hello dear,

I am Geeting this error:

RuntimeError: Given groups=1, weight of size [512, 16, 3, 3], expected input[16, 512, 4, 4] to have 16 channels, but got 512 channels instead
.

code is:
β€˜β€™β€™
class MyEnsemble(nn.Module):
def init(self, modelA, modelB, nb_classes=4):
super(MyEnsemble, self).init()
self.modelA = modelA
self.modelB = modelB
self.modelA.classifier[6] = nn.Identity()
self.modelB.classifier[6]= nn.Identity()

    self.classifier = nn.MaxPool3d(4096+4096, nb_classes) 

    in_channels = 16
    out_channels = 512

    self.cnnC = nn.Sequential(nn.Conv2d(in_channels, out_channels, kernel_size = (3, 3), padding = (1, 1)), nn.BatchNorm2d(out_channels), nn.ReLU())

    self.final_linear=nn.LazyLinear(10)
def forward(self, x):
    x1 = self.modelA(x.clone())  
    x2 = self.modelB(x)
    print(x1.size(1), x2.size(1))
    x = torch.cat((x1, x2), dim=1)
   
    x = x.reshape(-1, 512, 4, 4) 
    x = self.cnnC(x)

           x = self.cnnC(x)
    x = x.view(x.size(0), -1)
    x = self.final_linear(x)
    
    return x

β€˜β€™β€™

Thanks

self.cnnC is raising this error as in_channels is defined as 16 while you are explicitly reshaping the activation to have 512 channels:

x = x.reshape(-1, 512, 4, 4) 
x = self.cnnC(x)

hello dear,
This is the print model:
β€˜β€™β€™
MyEnsemble(
(modelA): AlexNet(
(features): Sequential(
(0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))
(1): ReLU(inplace=True)
(2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
(3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(4): ReLU(inplace=True)
(5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
(6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(7): ReLU(inplace=True)
(8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(9): ReLU(inplace=True)
(10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(11): ReLU(inplace=True)
(12): Identity()
)
(avgpool): AdaptiveAvgPool2d(output_size=(6, 6))
(classifier): Sequential()
)
(modelB): AlexNet(
(features): Sequential(
(0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))
(1): ReLU(inplace=True)
(2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
(3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(4): ReLU(inplace=True)
(5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
(6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(7): ReLU(inplace=True)
(8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(9): ReLU(inplace=True)
(10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(11): ReLU(inplace=True)
(12): Identity()
)
(avgpool): AdaptiveAvgPool2d(output_size=(6, 6))
(classifier): Sequential()
)
(features): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
(cnnC): Sequential(
(0): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU()
)
(classifier): Linear(in_features=4096, out_features=2, bias=True)
)
β€˜β€™β€™
This is the code:
β€˜β€™β€™
class MyEnsemble(nn.Module):
def init(self, modelA, modelB, nb_classes=2):
super(MyEnsemble, self).init()
self.modelA = modelA
self.modelB = modelB

    self.modelA.features[12] = nn.Identity()
    self.modelB.features[12] = nn.Identity()

    self.features = nn.MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
   

    in_channels = 256
    out_channels = 256
    self.cnnC = nn.Sequential(nn.Conv2d(in_channels, out_channels, kernel_size = (3, 3), padding = (1, 1)), nn.BatchNorm2d(out_channels), nn.ReLU())

    #add a FC layer
    self.classifier = nn.Linear(4096, nb_classes)      


def forward(self, x):
    x1 = self.modelA(x.clone())          x1 = x1.view(x1.size(0), -1)
    x2 = self.modelB(x)
    x2 = x2.view(x2.size(0), -1)
    x = torch.cat((x1, x2), dim=1)
    
    x = self.features(F.relu(x))
    return x

model_ft = models.alexnet(pretrained=True)
model_ft1 = models.alexnet(pretrained=True)

model_ft.classifier=model_ft.classifier[:-7]
model_ft1.classifier=model_ft1.classifier[:-7]

num_ftrs = model_ft.features[12].get_parameter
num_ftrs1 = model_ft1.features[12].get_parameter

model_ft.features[12]= nn.MaxPool2d(num_ftrs, 2)

model_ft1.features[12]= nn.MaxPool2d(num_ftrs1, 2)

model = MyEnsemble(model_ft,model_ft1).to(device)

criterion = nn.CrossEntropyLoss()

model = MyEnsemble(model_ft, model_ft1)

optimizer_ft = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=20, gamma=0.1)

print(model)

β€˜β€™β€™
In this code I have used two alexnet model and then I merge both of them in Maxpool2d layer(in features). I also remoted all layers of classifier.

After this I added maxpool2d layer, 1 conv layer and 1 Fully connected layer.
Please check this code . I am geeting this error:

β†’ 782 return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
783
784

RuntimeError: non-empty 3D or 4D (batch mode) tensor expected for input.

The error is now raised at the self.features = nn.MaxPool2d(...) layer which expects a 4D input while your are concatenating two 2D tensors.

1 Like

Thank you @ptrblck
So please tell me how I can change this code to the correct one.
Thanks.

Yes, this works for one or multiple layers.
Please tell me if I want to remove all linear layers in the AlexNet model (whole fully connected - classifier block). what I do?