SoFunction
Updated on 2024-11-12

python deep learning multi-label classifier and pytorch implementation source code

multilabel classifier

Multi-label categorization tasks are different from multi-categorization tasks, where a multi-categorization task is to classify an instance into a certain category, and multi-label categorization tasks are to classify an instance into more than one category. Multi-label categorization tasks have have two main features:

  • The number of class labels is uncertain; some samples may have only one class label, while others may have dozens or even hundreds of class labels.
  • The class labels are interdependent, e.g., a sample containing a blue sky class label has a high probability of containing white clouds

As shown in the figure below, which is an example of multi-label classification learning, there are multiple categories in an image, house, tree, cloud, etc., and the deep learning model needs to classify and recognize them one by one.

Multi-label classifier loss function

code implementation

A simplified code implementation of pytorch, a multi-label classifier for images, is shown below.

from torchvision import datasets, transforms
from  import DataLoader, Dataset
import torch
import  as nn
import  as optim
import  as F
import os
class CNN():
    def __init__(self):
        super().__init__()
        self.Sq1 = (         
            nn.Conv2d(in_channels=1, out_channels=16, kernel_size=5, stride=1, padding=2),   # (16, 28, 28)                           #  output: (16, 28, 28)
            (),                    
            nn.MaxPool2d(kernel_size=2),    # (16, 14, 14)
        )
        self.Sq2 = (
            nn.Conv2d(in_channels=16, out_channels=32, kernel_size=5, stride=1, padding=2),  # (32, 14, 14)
            (),                      
            nn.MaxPool2d(2),                # (32, 7, 7)
        )
         = (32 * 7 * 7, 100)  
    def forward(self, x):
        x = self.Sq1(x)
        x = self.Sq2(x)
        x = ((0), -1)    
        x = (x)
        ## Sigmoid activation   
        output = (x)  # 1/(1+e**(-x))
        return output
def loss_fn(pred, target):
    return -(target * (pred) + (1 - target) * (1 - pred)).sum()
def multilabel_generate(label):
    Y1 = F.one_hot(label, num_classes = 100)
    Y2 = F.one_hot(label+10, num_classes = 100)
    Y3 = F.one_hot(label+50, num_classes = 100) 	
    multilabel = Y1+Y2+Y3
    return multilabel
        
# def multilabel_generate(label):
# 	multilabel_dict = {}
# 	multi_list = []
# 	for i in range([0]):
# 		multi_list.append(multilabel_dict[label[i].item()])
# 	multilabel_tensor = (multi_list)
#     return multilabel
def train():
    epoches = 10
    mnist_net = CNN()
    mnist_net.train()
    opitimizer = (mnist_net.parameters(), lr=0.002)
    mnist_train = ("mnist-data", train=True, download=True, transform=())
    train_loader = (mnist_train, batch_size= 128, shuffle=True)
    for epoch in range(epoches):
    	loss = 0 
    	for batch_X, batch_Y in train_loader:
    		opitimizer.zero_grad()
    		outputs = mnist_net(batch_X)
    		loss = loss_fn(outputs, multilabel_generate(batch_Y)) / batch_X.shape[0]
    		()
    		()
    		print(loss)
if __name__ == '__main__':
	train()

Above is the python deep learning of multi-label classifier and pytorch source code in detail, more information about multi-label classifier pytorch source code please pay attention to my other related articles!