How to simplify the code of multi-label classification?

I built a model of multi-labels classification. The model like this:

class Net(nn.Module):
	def __init__(self):
		super(Net, self).__init__()
		self.conv1 = nn.Conv2d(1, 32, 3)
		self.conv2 = nn.Conv2d(32, 32, 3)
		self.pool24 = nn.MaxPool2d(2, 2)
		self.conv3 = nn.Conv2d(32, 64, 3)
		self.conv4 = nn.Conv2d(64, 64, 3)
		self.conv5 = nn.Conv2d(64, 128, 3)
		self.conv6 = nn.Conv2d(128, 128, 3)
		self.pool6 = nn.MaxPool2d(3, 2)
		self.fc1 = nn.Linear(128 * 5 * 29, 65)
		self.fc2 = nn.Linear(128 * 5 * 29, 65)
		self.fc3 = nn.Linear(128 * 5 * 29, 65)
		self.fc4 = nn.Linear(128 * 5 * 29, 65)
		self.fc5 = nn.Linear(128 * 5 * 29, 65)
		self.fc6 = nn.Linear(128 * 5 * 29, 65)
		self.fc7 = nn.Linear(128 * 5 * 29, 65)


	def forward(self, x):
		x = self.conv2(F.relu(self.conv1(x)))
		x = self.pool24(F.relu(x))
		x = self.conv4(F.relu(self.conv3(x)))
		x = self.pool24(F.relu(x))
		x = self.conv6(F.relu(self.conv5(x)))
		x = self.pool6(F.relu(x))
		x = x.view(x.shape[0],-1)
		x = nn.Dropout(0.5)(x)
		fc1 = self.fc1(x)
		fc2 = self.fc2(x)
		fc3 = self.fc3(x)
		fc4 = self.fc4(x)
		fc5 = self.fc5(x)
		fc6 = self.fc6(x)
		fc7 = self.fc7(x)
		return fc1,fc2,fc3,fc4,fc5,fc6,fc7

It trains data well.
and calculate the loss with nn.CrossEntropyLoss()

output1,output2,output3,output4,output5,output6,output7 = net(inputs)
		loss1 = criterion(output1, labels[:, 0])
		loss2 = criterion(output2, labels[:, 1])
		loss3 = criterion(output3, labels[:, 2])
		loss4 = criterion(output4, labels[:, 3])
		loss5 = criterion(output5, labels[:, 4])
		loss6 = criterion(output6, labels[:, 5])
		loss7 = criterion(output7, labels[:, 6])
		loss = (loss1 + loss2 + loss3 +loss4 +loss5 +loss6 +loss7)/7.

I’m not sure it is correct. I think maybe it looks complicated.

You could also use nn.BCELoss for multi-label classification with just one output layer given the probabilities of all class occurrences.

Could you explain your output shapes? Why do you return logits of shape 65 in each fcX layer?

It is the licence plate. There are 7 number in one licence plate,each number have 65 choices.

Thanks for the clarification.
So we basically have multiple predictions (7 numbers) using a multi-class classification (not multi-label).
In a multi-label classification, each sample might have more than one target class.
Using license plates this approach seems to be invalid, since neither a single figure can be more than one class (e.g. it’s either an A or a 7, but not both), nor does it make sense to use the multi-label classification on the whole license plate, since letters and numbers can be repeated.

Given that information, I think your approach looks good to me.
If you don’t like the code repetition, you could use a nn.ModuleList, creating and applying your linear layers in a loop. But it wouldn’t bother me that much.

1 Like

I see. Thank you. :grin:

what is the suggested way to do the multi label multi class classification - any sample code, pointers will really help.

I have 7 Labels in target and each Labels have different number of distinct values ranging from 10 to 200.

I have implemented it using 7 different SLMC using Logistic regression, want to convert in a single model using pytorrch.