Hi All,
I am trying to create an image classifier using this [tutorial]. (Transfer Learning for Computer Vision Tutorial — PyTorch Tutorials 1.13.1+cu117 documentation) In my case I am trying to use the EfficientNet
model. I plugged the regent model instead of the resnet
model used in the tutorial. However, I get an error for the code. Please find the code below and the respective error.
#model_conv = torchvision.models.regnet_y_32gf(weights = 'RegNet_Y_32GF_Weights.IMAGENET1K_SWAG_E2E_V1')
model_conv = torchvision.models.efficientnet_b7(weights = 'EfficientNet_B7_Weights.IMAGENET1K_V1')
for param in model_conv.parameters():
param.requires_grad = False
# Parameters of newly constructed modules have requires_grad=True by default
num_ftrs = model_conv.fc.in_features
model_conv.fc = nn.Linear(num_ftrs, 2)
model_conv = model_conv.to(device)
criterion = nn.CrossEntropyLoss()
# Observe that only parameters of final layer are being optimized as
# opposed to before.
optimizer_conv = optim.SGD(model_conv.fc.parameters(), lr=0.001, momentum=0.9)
# Decay LR by a factor of 0.1 every 7 epochs
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_conv, step_size=7, gamma=0.1)
Error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/tmp/ipykernel_23/272062303.py in <module>
6
7 # Parameters of newly constructed modules have requires_grad=True by default
----> 8 num_ftrs = model_conv.fc.in_features
9 model_conv.fc = nn.Linear(num_ftrs, 2)
10
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in __getattr__(self, name)
1264 return modules[name]
1265 raise AttributeError("'{}' object has no attribute '{}'".format(
-> 1266 type(self).__name__, name))
1267
1268 def __setattr__(self, name: str, value: Union[Tensor, 'Module']) -> None:
AttributeError: 'EfficientNet' object has no attribute 'fc'
Would anyone be able to help me in this matter.
Thanks & Best Regards
AMJS