mmcls multi-label classification practice (1): making multi-label data

Multi-Label: Multi-label classification means that when there are multiple attributes in a picture or text, multi-label classification can classify and identify multiple attributes in a picture. The following will use the mmclassification framework to complete a multi-label classification practice.

The difference between Multi-class and Multi-label
First, we need to build a data set. It should be noted that the data set for multi-label classification is different from multi-classification data. There is only one '1' in the label of the Multi-class, and there are multiple attributes in the Multi-label, and the label can have multiple '1'. It is relatively simple to construct a multi-class data set. It only needs to classify pictures of different categories into different files. The Multi-Label dataset needs to write the label as txt, and customize the dataset to read it.
insert image description here

Multi-label construction data set
It is also relatively simple to construct a Multi-label data set. First of all, it is necessary to classify the pictures according to their attributes, and group the pictures into folders with different attribute names (since a picture may have multiple attributes, the same picture can be stored in different attribute folders). We use dictionaries to map images to attributes. Traverse the pictures in all folders, use the picture name as the key of the dictionary, and the file name as the value of the dictionary. After traversing, you can get the label of the image attribute and save it in txt.

import os

file_handle = open('val.txt', mode='w')
img_dic = {
    
    }
label2idx = {
    
    'LH':0, 'PS':1, 'SL':2, 'WH':3, 'ZC':4}

for home, dirs, files in os.walk('/home/mmclassification-master/data/multi_val/'):
    for filename in files:
        if filename not in img_dic:
            img_dic[filename] = [label2idx[home.split('/')[-1]]]
        else:
            img_dic[filename].append(label2idx[home.split('/')[-1]])
     
for k, v in img_dic.items():
    file_handle.writelines(os.path.join("/home/mmclassification-master/data/multi_val",\
                             k) + ' ' + str(v)[1:-1].replace(" ", "") +'\n')
file_handle.close()

The constructed txt file is roughly as shown in the figure below, and each line contains the image name and its corresponding attribute label. Next, we will customize the dataset to read the data.
insert image description here

Guess you like

Origin blog.csdn.net/litt1e/article/details/125315752