I am dealing with a multi-label classification problem ,the image belongs to one of the
10 classes from two distinct labels i.e desired output is [batch_size,2,10],how can i modify ResNet50 to Get Multiple outputs
If I understand your use case correctly you are dealing with two “labels”, each consisting of 10 classes.
Each sample belongs to one particular class of each label.
I think in that particular use case you could use two linear layers, one for each label, and return these two outputs:
class MyModel(nn.Module):
def __init__(self, num_classes1, num_classes2):
super(MyModel, self).__init__()
self.model_resnet = models.resnet18(pretrained=True)
num_ftrs = self.model_resnet.fc.in_features
self.model_resnet.fc = nn.Identity()
self.fc1 = nn.Linear(num_ftrs, num_classes1)
self.fc2 = nn.Linear(num_ftrs, num_classes2)
def forward(self, x):
x = self.model_resnet(x)
out1 = self.fc1(x)
out2 = self.fc2(x)
return out1, out2
Thanks so much ,really helped a lot
I am getting the following error
AttributeError: module ‘torch.nn’ has no attribute ‘Identity’
Probably you are using an older PyTorch version, as the nn.Identity
module was introduced in 1.1.0
.
Have a look here for install instructions.
I used nn.Sequential() it worked fine ,actually i am implementing all this in Kaggle ,the pytorch version must be an older one.Thanks Peter for all the help I was stuck with this for a long time:yum:
Hey can you tell me which loss function and optimizer did you use and did you freeze the other layers??
Sure , I am using SGD with learning_rate of 0.20 .It is working fine ,try this .Hope it helps you .