Search code examples
pythonresnettransfer-learningtorchvisionimage-classification

How can I use the Resnet architecture to classify a custom labeled set of images?


I am trying to use transfer learning on resnet 152 to classify a data set of images that are custom labeled 0/1 as containing the object of interest or not. I have referenced multiple tutorials and haven't been able to figure it out. I will put some links below that I have previously referenced, but first code I am attempting to use.

I started with trying to use this. PyTorch transfer learning with pre-trained ImageNet model

# Load the pretrained model
model = models.resnet152(pretrained=True)

classifier_name, old_classifier = model._modules.popitem()

for param in model.parameters():
    param.requires_grad = False

classifier_input_size = old_classifier.in_features

classifier = nn.Sequential(OrderedDict([
                           ('fc1', nn.Linear(classifier_input_size, hidden_layer_size)),
                           ('activation', nn.SELU()),
                           ('dropout', nn.Dropout(p=0.5)),
                           ('fc2', nn.Linear(hidden_layer_size, output_layer_size)),
                           ('output', nn.LogSoftmax(dim=1))
                           ]))

But I get NameError, "OrderedDict" is not defined. I would like to understand what I am doing wrong on my classifier step here. After that I'm still struggling to understand how I could use this new model and classifier on my own dataset of images (how the images should be fed, specified as 0/1,specified as training/test, etc). Any help or tutorials on that you can point me to would be greatly appreciated.


Solution

  • you need to import OrderedDict using below command

    from collections import OrderedDict 
    

    then it will work.

    for more information visit Link