MMclassfication custom data set training and visualization

The basic introduction of mmclassification will not be elaborated here. In the early stage, because the work needs to realize the function of pictures, and because I have been using mmlab related libraries, I chose mmcls. In fact, it is similar to the use process of other mmlabs. Basically, it may be able to run after the data set structure is settled.

1. Custom data set

I use the data format of ImageNet. On the one hand, those without the registration mechanism are directly modified on the basis of the original code, which is more convenient. On the other hand, because I also use image1k for the subsequent config, I may have to face less of a problem.

1. The data format of imagenet in mmcls

The overall tree structure is as follows:
—data
|----meta
|——|—train.txt
|——|—val.txt
|——|—test.txt
|——train
|——|—class1
|————|—01.jpg
|————|—02.jpg
|———|—…
|——|—class2
|————|—01.jpg
|————| —02.jpg
|————|—…
|——test
|——|—class1
|————|—01.jpg
|————|—02.jpg
|————| —…
|——|—class2
|————|—01.jpg
|————|—02.jpg
|————|—…
|——val
|——|—class1
|—— ———|—01.jpg
|———|—02.jpg
|———|—…
|——|—class2
|————|—01.jpg
|————|—02 .jpg
|————|—…
As shown in the figure:
insert image description here

Among them, the three stored in meta are address directories containing image tags:
insert image description here
insert image description here
namely: path + space + tag index
train, val, and test are all stored in image format files. If you already have the corresponding train. txt and other files, that is, after you already have the label address, you can put all the images together. If not, create a folder for each category, and then generate the corresponding label. Since I am using a custom dataset, I use the second method, as follows:
insert image description here
insert image description here

I have a total of two labels, namely can and cant
train, val, and test three folders. My data is divided according to 7:2:1.
Specifically, set up a general folder first, create folders under the folder according to the label category, and store
the code for the corresponding picture division. I will also attach it.

import os, random, shutil

##############################################################
## 分割数据集,首先将所有数据放在同一个文件夹下###################
def moveFile(fileDir, val_ratio, test_ration2):
    pathDir1 = os.listdir(fileDir)  # 取图片的原始路径
    filenumber = len(pathDir1)
    val_number = int(filenumber * val_ratio)  # 抽取验证集的数量
    test_number = int(filenumber * test_ratio)  # 抽取测试集的数量
    val_sample = random.sample(pathDir1, val_number)  # 随机选取val图片

    # 先抽取验证集图片
    for name1 in val_sample:
        shutil.move(os.path.join(fileDir, name1), os.path.join(valDir, name1))

    # 从剩余的图片中抽取测试集
    pathDir2 = os.listdir(fileDir)  # 取图片的原始路径
    test_sample = random.sample(pathDir2, test_number)  # 随机选取val图片
    for name2 in test_sample:
        shutil.move(os.path.join(fileDir, name2), os.path.join(testDir, name2))

    return


if __name__ == '__main__':
    ori_path = './data/space_all/train'  # 最开始train的文件夹路径
    test_Dir = './data/space_all/test'  # 移动到新的文件夹路径
    val_Dir = './data/space_all/val'  # 移动到新的文件夹路径
    val_ratio, test_ratio = 0.1, 0.2  # 抽取比例  ******自己改*******
    for firstPath in os.listdir(ori_path):
        fileDir = os.path.join(ori_path, firstPath)  # 原图片文件夹路径
        valDir = os.path.join(val_Dir, firstPath)  # val下子文件夹名字
        testDir = os.path.join(test_Dir, firstPath)  # test下子文件夹名字
        if not os.path.exists(valDir):  # 如果val下没有子文件夹,就创建
            os.makedirs(valDir)
        if not os.path.exists(testDir):  # 如果test下没有子文件夹,就创建
            os.makedirs(testDir)
        moveFile(fileDir, val_ratio, test_ratio)  # 从每个子类别开始逐个划分
    print("Successfully splited!")

The next step is to generate the corresponding txt file:

## 制作标签,在此之前需要用上半部分代码划分好数据集

path='./data/space_all/train'


data_path = pathlib.Path(path)
all_images_path = list(data_path.glob('*/*'))
all_images_path = [str(path) for path in all_images_path]  # 所有图片路径名存入列表
random.shuffle(all_images_path)  # 打散

print(len(all_images_path))
# print(all_images_path)  # 打印前五个

# 开始制作标签
label_names = sorted(item.name for item in data_path.glob('*/') if item.is_dir())
print(label_names)  # 打印类别名  注:下一步是制作与类别名对应的标签
label_to_index = dict((name, index) for index, name in enumerate(label_names))

all_image_labels = [label_to_index[pathlib.Path(path).parent.name] for path in all_images_path]


for image, label in zip(all_images_path[:5], all_image_labels[:5]):
   print(image, '-----', label)

filename='./data/space_all/train.txt'     # ***这里也要记得改***

print(image + " " + str(label) + "\n")
with open(filename,'w') as f:
   for image,label in zip(all_images_path,all_image_labels):
       f.write(image+" "+str(label)+"\n")
print("\nAll images and labels have been written in  .txt!\n")

After completion, the data preparation is complete.

Two, training

The process of using mmcls is also relatively simple. It is the same as the process of using other mmlab code bases, that is, to modify the corresponding files under config. Here I am using resnet18_8xb32_in1k.py, the meaning of this naming is to use resnet18, 8 cards, batchsize32, and the data set is imagenet1k. You can open this file first.
insert image description here
There are quite a lot in configs/resnet/resnet18_8xb32_in1k.py . In fact, you only need to modify the part of / base /datasets/imagenet_bs32.py to view this part of the file. insert image description here
If you don't make other fine-tuning, except for the path corresponding to the data, you don't need to change anything else.
Then you need to add your own data labels to the ImageNet dataset labels. Of course, you can also create new ones here. I'm lazy and didn't do it.
The specific operation is as follows:
In mmcls/imagenet.py, add your own label to CLASS: insert image description here
do not modify the others.
Then you can train. There are two methods, one is to run directly, but remember to modify the running configuration, and the other is to enter commands in the terminal. I'm lazy, so I choose the first one, so I don't have to input so many commands each time.
insert image description here
insert image description here
The first part is to specify the config file and the second is to specify the working archive folder. Then you can run it. A log file, checkpoints file, and config file will be generated in the specified folder, and you can fine-tune your model training process in this config file.
The specific parameters are introduced by me in other articles, so I won’t go into details here:insert image description here
In fact, this file is the system that automatically integrates the four files under the previous config. When you run it next time, you can directly modify it here without affecting other training configurations, which will be more reasonable. Including paths and other basic settings. Then just use this as your config when setting up the running configuration.

3. Verification

The training indicators are mainly acc-top1 and acc-top5, top-1 is the one with the best classification, and top-5 is the best classification of the first five draws (because it should be). The training results will be stored in the log.json file. We can use the last generated checkpoints file, latest.pth, to verify the test set. The main use is tools/test.py, which is the same as train, and has two operating modes. Here I directly use instructions.

python tools/test.py work_dir/resnet18_8xb32_in1k.py work_dir/latest.pth --metrics accuracy --metric-option topk=1

The first part here is the address of the config file, the second part is the address of the checkpoint used, metrics is the set evaluation standard, accuracy is selected, and topk=1 is the value of acc-top1.

insert image description here
You can also output the result to a json file, the code is as follows:

python tools/test.py work_dir/resnet18_8xb32_in1k.py work_dir/latest.pth --out result.json --out-items all

The same is the keyword that can be selected for output, I output all of them

4. Visualization

You can visualize the training log, just use the built-in analysis tool directly. For example, to visualize the top1 changes in the log, the code is:

python tools/analysis_tools/analyze_logs.py plot_curve work_dir/20230214_153510.log.json --keys accuracy_top-1 --legend accuracy --out result.jpg

Among them, plot_curve is to draw the curve, keys is the key selected for drawing the curve, which can be multiple, legend is the legend, and out is the name of the saved file.
insert image description here

5. Forecast

If you want to use the trained model to predict new pictures, you can directly call the api. The code is relatively simple. I wrote it directly with jupyter lab. Please click on the source code yourself, I will post a photo
insert image description here

Summarize

In fact, these are written very clearly in the docs document, but many people may be too lazy to read it, and I am the same, but I found that many tutorials found on the Internet are incomplete, so I can only read it slowly, maybe there are incomplete places , You can tell me that everyone can exchange and learn together. The overall feeling of mmlab is that if you get started, it is really convenient and easy to use. I recommend you to try it.

Guess you like

Origin blog.csdn.net/onepunch_k/article/details/129041676