An error occured when use pretrained resnet50 encode images

i just want to get the encode of images use pretrained resnet50, Here is my code

from torchvision.models.resnet import resnet50
import torch.nn as nn
import torch as t
from utils import preprocess

def resnet():
    net = resnet50(pretrained=True)
    # delete fc layer
    del net.fc
    for param in net.parameters():
        param.requires_grad = False

    # print(net)
    return net

model = resnet()

img = preprocess(img_name)

# img(tensor) -> [1, 3, 299, 299]
encode_img = model(img)

when i run above code, it give me an error like this.

Exception has occurred: AttributeError
‘ResNet’ object has no attribute ‘fc’

how could i solve this problem, if anyone can tell me. I will be very grateful

If you delete net.fc, the forward pass will still try to call it and will thus raise this error.
To “disable” this layer, just replace it with nn.Identity().

thank you very much, this is a new kownledge for me :smiley: