Set up YOLOX environment and train your own data set under Windows 10

Posted by jeffwhite on Sun, 12 Dec 2021 11:21:44 +0100

catalog:

1. Necessary environment

2.YOLOX installation and model reasoning

3. Train your data set and test

1. Necessary environment

My environment is: Windows 10 + Python 3 9+CUDA11. 1+CUDNN8. one point one

There are many tutorials on the Internet. This article mainly records the use of YOLOX. (there is a small pit in the installation of torch here. If torch is not installed at the beginning and installed with requirements.txt, the installed version is the cpu version, but here we use the gpu version. Be careful not to step on the pit.)

2.YOLOX installation

Configure the environment of YOLOX

2.1 go to the github official website to download the source code: https://github.com/Megvii-BaseDetection/YOLOX

2.2 installation dependency package:

pip install -r requirements.txt

2.3 installation yolox

python setup.py install

2.4 download apex and unzip it to the address in the project folder: https://gitcode.net/mirrors/nvidia/apex?utm_source=csdn_github_accelerator

2.5} cd into the apex folder and install apex

cd apex-master
python setup.py install

2.6 return to yolox's home folder and install pycocotools

pip install pycocotools

2.7 download the pre training model, which can be downloaded from the website given in step 2.1

2.8 test effect

Test picture instruction:

python tools/demo.py image -f exps/default/yolox_s.py -c ./yolox_s.pth --path assets/dog.jpg --conf 0.3 --nms 0.65 --tsize 640 --save_result --device gpu

effect:

 3. Train your own dataset

The steps of creating data sets here will not be repeated.

3.1 first set the folder directory as required:

Put the image in JPEGImages and the xml file in Annotations

3.2 using txt_write.py, divide the dataset and write it to the main folder to generate trainval Txt and test txt.

import os
import random

trainval_percent = 0.1
train_percent = 0.9
xmlfilepath = 'E:/YOLOX-main/datasets/VOC/VOCdevkit/VOC2007/Annotations'
txtsavepath = 'E:/YOLOX-main/datasets/VOC\VOCdevkit/VOC2007/ImageSets'
total_xml = os.listdir(xmlfilepath)

num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)

ftest = open('E:/YOLOX-main/datasets/VOC/VOCdevkit/VOC2007/ImageSets/Main/test.txt', 'w')
ftrain = open('E:/YOLOX-main/datasets/VOC/VOCdevkit/VOC2007/ImageSets/Main/trainval.txt', 'w')

for i in list:
    name = total_xml[i][:-4] + '\n'
    if i in trainval:
        ftest.write(name)
    else:
        ftrain.write(name)

ftrain.close()
ftest.close()


3.3} modify yolox / data / dataloading Py is as follows:

3.4. Modify exps/example/yolox_voc/yolox_voc_s.py is as follows:

 

 

Modify num_ Change the number of classes to your own

3.5 modify yolox/data/datasets/voc_classes.py is your own category

3.6 modify yolox/evaluators/voc_eval.py is as follows

In yolox/exp/yolox_base.py

Modify self data_ num_ workers = 0

Execute Python setup Py install update yolox

At this time, the following error is reported when running the training code: error #15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.

Solution: in train Add code to py file:

import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'

3.7 running code to start training:

python tools/train.py -f exps/example/yolox_voc/yolox_voc_s.py -d 1 -b 4 --fp16 -o -c yolox_s.pth

-d Number of graphics cards used
-b Batch size
–fp16 Turn on half precision training
-c Load pre training model

If CUDA out of memory memory overflow occurs, remove – fp16 -o and change the code to the following:

python tools/train.py -f exps/example/yolox_voc/yolox_voc_s.py -d 1 -b 4 -c yolox_s.pth

If you continue the previous batch of recovery training, change it to the following form:

Change - resume to True, - c followed by the weight file to continue training
The code is as follows:

python tools/train.py -f exps/example/yolox_voc/yolox_voc_s.py -d 1 -b 4 -c YOLOX_outputs/yolox_voc_s/latest_ckpt.pth

Operation screenshot:

In order to demonstrate, this training has trained for 2 rounds.

Results stored in:

Test:

4.1. Modify demo before test Py is as follows:

Comment out COCO_CLASSES, join VOC_CLASSES

 

 

4.2 test code

python tools/demo.py image -f exps/default/yolox_s.py -c ./latest_ckpt.pth --path assets/001.jpg --conf 0.3 --nms 0.65 --tsize 640 --save_result --device gpu

Results:

Because the number of training rounds is 2, the identification box is not very accurate.

At this point, YOLOX reappears successfully.

end!

 

Topics: AI Pytorch Computer Vision Deep Learning Object Detection