Article directory
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 a new weights folder
- Download yolox_s.pth weights: https://download.csdn.net/download/qq_44824148/75506518 . If it is not convenient to download, you can leave a message in the comment area or send an email.
Create VOCdevkit folder
Create a new one under the datasets folder with the following make_dir.py
contents:
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:
Add dataset
Put xml files and pictures in Annotations and JPEGImages respectively
Divide training set and test set
Create a new one under the datasets folder with the following make_voc_data.py
contents:
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:
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
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.pth
Copy weights
to Folder in the folder
Modify the in to tools\demo.py
, and the imported header file toCOCO_CLASSES
VOC_CLASSES
from 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/
After the test is completed, a YOLOX_outputs\yolox_voc_s\vis_res
folder will be automatically generated, and you can see the results of your own test.