Hi all,

I’m new to the DL field and pytorch.

I want to use pretrained MobileNetV2 but instead of its classifier, add 1x1 convolutional layer+max pooling+convolutional layer+other linear layers (all this in order to reduce the output to less dimensions so I can cluster it later on).

When I tried to run the next code I got “Expected 4-dimensional input for 4-dimensional weight [1280, 1280, 2, 2], but got 2-dimensional input of size [2, 1280] instead”.

Here’s the code:

model = models.mobilenet_v2(pretrained=True)

for param in model.parameters():

param.requires_grad = False

#modify last layer of the net

fc = nn.Sequential(

nn.Conv2d(1280, 1280, kernel_size=(2, 2), stride=(2, 2), bias=False),

nn.MaxPool2d(kernel_size=2, stride=2, padding=0),

nn.Conv2d(1280, 320, kernel_size=(1, 1), stride=(1, 1), bias=False),

nn.Dropout(0.2),

nn.BatchNorm1d(320),

nn.ReLU(),

nn.Dropout(0.3),

nn.Linear(320,NUM_CLASSES),

n.LogSoftmax(dim=1)

)

model.classifier = fc

criterion = nn.NLLLoss()

optimizer = torch.optim.Adam(model.classifier.parameters(), lr=0.0015)

model.to(DEVICE)

summary(model, input_size=(3, 224, 224))

Is there someone who managed to that successfully? Or tell me where am I wrong?

Thanks a lot in advance!