Pytorch – mat1 and mat2 shapes cannot be multiplied (3328×13 and 9216×4096)

Question:

I’m a beginner to pytorch and I’m trying to extract activations from 7 layers for the pre-trained alexnet model. Getting this error when trying to extract activations from the fc6 layer:

image of the error

Here is my code for looping over my array of images and attempting to extract the activations.

    conv1 = alexnet.features[0:2](img_tensor)
    conv2 = alexnet.features[2:5](conv1)
    conv3 = alexnet.features[5:7](conv2)
    conv4 = alexnet.features[7:9](conv3)
    conv5 = alexnet.features[9:12](conv4)
    fc6 = alexnet.classifier[0:2](conv5)
    fc7 = alexnet.classifier[0:2](fc6)

I am applying the following transform to each image

transform = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225]
    )
])

From what I understand I need to resize the conv5 layer before multiplying it with the alexnet classifier but I’m not too sure what an effective way to do that is.

Appreciate any help with resolving this issue.

Thanks!

Asked By: Amit Verma

||

Answers:

Yes, you need to flatten it. You can do it easily with:
conv5 = conv5.flatten(1)

Although I don’t know why you were applying 2 layers by 2 layers. I guess you were just learning. You could apply all at a time you know.

Edit:
I detected you have another mistake, here conv5 = alexnet.features[9:12](conv4) it should be conv5 = alexnet.features[9:](conv4) remember that the right is exclusive. And fc7 = alexnet.classifier[0:2](fc6) I guess you mean fc7 = alexnet.classifier[2:4](fc6).