Loading pretrained vgg16 and replacing classifier

Hello everyone.

I load a pretrained vgg16 and written some python script to replace the classifier layer and serialize it in a pt file.

"import torch
import torchvision.models as models
import torchvision.transforms as transforms
from PIL import Image
import torch.nn as nn

class Identity(nn.Module):
def init(self):
super(Identity, self).init()

def forward(self, x):
    return x

model = models.vgg16(weights=“VGG16_Weights.IMAGENET1K_V1”)
model.classifier = Identity()
model.eval()
torch.jit.save(torch.jit.script(model), “vgg16_features.pt”)"

The pretrained vgg16 file weighs around 500mo, but the file obtained after applying the script weighs 58mo. I have the impression that the weights of the pre-trained vgg16 were not set during serialization. Am I wrong?

This would be expected, since the .classifier module contains the majority of the trainable parameters:

model = models.vgg16()

nb_params = 0
for param in model.classifier.parameters():
    nb_params += param.nelement()

print("{} parameters".format(nb_params))
print("{:.3f} MB".format(nb_params * 4 / 1024**2))
# 471.660 MB

I see. Thank you very much sir