So the thing is I am trying to do something with VGG19 pretrained model and initial 8 convolution layers are frozen.
My image size is 3 x 400 x 400
and I do not want to resize as it may affect the performance. I am getting error again and again that the matrices can not be multiplied, so is there a way to pass 3 x 400 x 400
image through vgg19?
weights = VGG19_Weights.DEFAULT
vgg19_layer = vgg19(weights = weights)
for i in range(16):
for param in vgg19_layer.features[i].parameters():
param.requires_grad = False
This is how I am using vgg19_layer
.
self.vgg19 = vgg19_layer
This is how I am currently sending the input.
x = torch.randn((3, 400, 400))
model(x)
Output
RuntimeError: mat1 and mat2 shapes cannot be multiplied (512x49 and 25088x4096)
The problem is that the shape of your input tensor is incorrect. The model expects a shape [B, 3, 400, 400] with B being the batch size. If you send a single image at a time to your model, then you need to use a tensor of shape [1, 3, 400, 400] and not [3, 400, 400]. You can achieve this with
torch.unsqueeze()
. At the output of the network, you can eliminate this dimension withtorch.squeeze()
.Also note that instead of explicitly setting
require_grad
tofalse
, you can simply put the model in evaluation mode (model.eval()
) and also decorate your inference function withtorch.no_grad()
. This will immediately deactivate any gradient calculations within the function.To further clarify, the model expects a tensor of shape [B, 3, 400, 400] where B represents the batch size, 3 represents the RGB color channels, and 400x400 is the image resolution. If you are sending a single image, it needs to be reshaped to [1, 3, 400, 400] for the model to process it correctly. The
torch.unsqueeze()
function can be used to add an additional dimension required for batch processing. Similarly,torch.squeeze()
can be used to remove this extra dimension after the image has been processed.Finally, when performing inference, it's not necessary to compute gradients. You can stop PyTorch from calculating gradients by using
torch.no_grad()
. This can save memory and speed up your code. Also, remember to set your model to evaluation mode usingmodel.eval()
before running inference. This will set all the layers in your model to evaluation mode, which is essential for some types of layers like dropout and batch normalization which behave differently during training and evaluation.