WIN10 uses YOLOX to train its own data set (super-detailed illustration)

WIN10 uses YOLOX to train its own data set (super-detailed illustration)

Download YOLOX source code

GitHub URL: https://github.com/Megvii-BaseDetection/YOLOX

Configure the environment and modify the source code

Add weights file

Create VOCdevkit folder

Create a new one under the datasets folder with the following make_dir.pycontents:

import os

os.makedirs(r'VOCdevkit\VOC2007\Annotations')
os.makedirs(r'VOCdevkit\VOC2007\ImageSets\Main')
os.makedirs(r'VOCdevkit\VOC2007\JPEGImages')

After running, the following folders will be generated:
insert image description here

Add dataset

Put xml files and pictures in Annotations and JPEGImages respectively

insert image description here

Divide training set and test set

Create a new one under the datasets folder with the following make_voc_data.pycontents:

import os
import random

train_pr = 0.7

xml_names = os.listdir('VOCdevkit/VOC2007/Annotations')
nums = len(xml_names)

train_nums = int(train_pr * nums)

list = range(nums)
train_index = random.sample(list, train_nums)

train_val = open('VOCdevkit/VOC2007/ImageSets/Main/trainval.txt', 'w')
test = open('VOCdevkit/VOC2007/ImageSets/Main/test.txt', 'w')

for i in list:
    name = xml_names[i].split('.')[0] + '\n'
    if i in train_index:
        train_val.write(name)
    else:
        test.write(name)

train_val.close()
test.close()

After running, the following files will be generated:
insert image description here

Modify the categories to the classes of your own training samples

Modify the path of the file:YOLOX-main\exps\example\yolox_voc\yolox_voc_s.py

Modify the value of num_classes to the number of classes you train yourself, for example:

self.num_classes = 5

Edit category

Modify the path of the file:YOLOX-main\yolox\data\datasets\voc_classes.py

VOC_CLASSES is modified to the category of its own dataset

VOC_CLASSES=(
    'cat',
    'person',
    'horse',
    'car',
    'dog'
)

start training

Enter the following command in Terminal:
python tools/train.py -f exps/example/yolox_voc/yolox_voc_s.py -d 1 -b 1 -c weights/yolox_s.pth
insert image description here

Errors that may occur during training

ModuleNotFoundError: No module named ‘yolox’

Solution: Add the following code to the top of train.py

import sys
sys.path.append(r'D:\pythonProjects\YOLOX-main2')  #路径为自己的绝对路径

FileNotFoundError: [Errno 2] No such file or directory: ‘D:\XXX\datasets\VOCdevkit\VOC2012\ImageSets\Main\trainval.txt’

Solution: remove the 2012 format and change it to the following code

image_sets=[('2007', 'trainval')],

Unreachable xml, FileNotFoundError: [Errno 2] No such file or directory: '000009.xml'

Solution: Change to relative path
Modify the path of the file:YOLOX-main2\yolox\evaluators\voc_eval.py

Change it to the following code:

tree = ET.parse(os.path.join(r'datasets/VOCdevkit/VOC2007/Annotations',filename))

training completed

After the training is completed, a folder will be automatically generated YOLOX_outputs\yolox_voc_s, select best_ckpt.pthCopy weightsto Folder in the folder

Modify the in to tools\demo.py, and the imported header file toCOCO_CLASSESVOC_CLASSESfrom yolox.data.datasets.voc_classes import VOC_CLASSES

test

Enter the following command in Terminal:
python tools/demo.py image -f exps/example/yoLox_voc/yolox_voc_s.py -c weights/best_ckpt.pth --device gpu --save_result --path assets/
insert image description here

After the test is completed, a YOLOX_outputs\yolox_voc_s\vis_resfolder will be automatically generated, and you can see the results of your own test.
insert image description here

Guess you like

Origin blog.csdn.net/qq_44824148/article/details/122445760