On the environment of win10 yolox to install and train your own data set

Posted by Fabrizzio PHP on Mon, 10 Jan 2022 22:52:54 +0100

Recently, I was burst by yolox. In addition, I have too many plots for yolo series. I tried yolof before. Now, yolox and openvino reasoning happen to need this part. Now I'll try to make a preliminary exploration.
1, Basic configuration
cuda 10.1
2, Environment construction
1. Installing yolox

git clone git@github.com:Megvii-BaseDetection/YOLOX.git
pip3 install -U pip && pip3 install -r requirements.txt
pip3 install -v -e .  # or  python3 setup.py develop

Run PIP3 install - V - E on the fourth line# or python3 setup. Py development will have an error:

This problem needs to be solved in setup Py file, modify line 50 as follows:

2. Install apex
APEX is open source by NVIDIA and perfectly supports PyTorch framework. It is a tool for changing data format to reduce the occupation of model display memory. The most valuable one is amp (Automatic Mixed Precision), which tests most operations of the model with Float16 data type, and some special operations still use Float32. And users can perfectly migrate their training code to the model through only three lines of code. Experiments show that using Float16 as the data type of most operations does not reduce the parameters. In some experiments, it can increase the Batch size, improve the accuracy and training speed.
I didn't install apex according to the official tutorial because there was an error in that method. My installation steps are as follows:
① Download the apex locally and unzip it
Download address https://github.com/NVIDIA/apex
② Put the extracted files into your own environment
The anconda environment I used found the environment name / lib / site packages
③ Enter the apex file in the environment
pip install -r requirements.txt
④ Install apex
python setup.py install

3. Installing pycocotools
Another one didn't follow the official operation
① pip install cython
② The installation premise is vc++
Download coco API
Download address https://github.com/cocodataset/cocoapi
③ Enter the python API directory
cd PythonAPI
④ Execute installation command
python setup.py install
Installation error:
Invalid numeric parameter "/ wno CPP
Edit setup Py file, delete as shown below

3, Run demo
Run the demo to test the installation environment
1. Download pre training model
2. Execute the following instructions

python tools/demo.py image -n yolox-s -c /path/to/your/yolox_s.pth --path assets/dog.jpg --conf 0.25 --nms 0.45 --tsize 640 --save_result --device gpu

-n is the name of the model
-c is the weight file address
– path is the path of the test image
– conf confidence threshold
– iou threshold of NMS
– tsize test picture size
–save_result save reasoning results
As long as the environment can be built according to the above part, this part will not make mistakes.

Note: torch and torch vision versions must match, and torch version must be greater than or equal to 1.7
Later, I will put the version information of each package

4, Train your own dataset
1. Dataset preparation
For the official coco dataset, I adopted the voc dataset. The format of the voc dataset is as follows:

├── data 
│   ├── VOCdevkit
│   │   ├── VOC2007
│   │   │   ├── Annotations #xml file
│   │   │   ├── JPEGImages #picture
│   │   │   ├── ImageSets
│   │   │   │   ├── Main
│   │   │   │   │   ├── test.txt 
│   │   │   │   │   ├── trainval.txt

Data division code:

import os
import random

trainval_percent = 0.1
train_percent = 0.9
xmlfilepath = './Exp/Annotations/'
txtsavepath = './Exp/images/'
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)
os.makedirs('./data/VOCdevkit/VOC2007/ImageSets', exist_ok=True)
ftest = open('./data/VOCdevkit/VOC2007/ImageSets/test.txt', 'w')
os.makedirs('./data/VOCdevkit/VOC2007/ImageSets', exist_ok=True)
ftrain = open('./data/VOCdevkit/VOC2007/ImageSets/trainval.txt', 'w')

for i in list:
    name = total_xml[i][:-4] + '\n'
    if i in trainval:


2. Modify code
Modify exps/example/yolox_voc/yolox_voc_s.py modification document is as follows:

This location is modified to its own actual number of categories without background, that is, the background is not a category

Modify yolox/data/datasets/voc_classes.py file and modify it to its own class alias:

3. Execute commands for training

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

-d how many graphics cards are used for training
-b batch size
– fp16 is half precision training on
The following errors were encountered during:

`Original Traceback (most recent call last):
  File "E:\Anaconda3\envs\yolox\lib\site-packages\torch\utils\data\_utils\worker.py", line 198, in _worker_loop
    data = fetcher.fetch(index)
  File "E:\Anaconda3\envs\yolox\lib\site-packages\torch\utils\data\_utils\fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "E:\Anaconda3\envs\yolox\lib\site-packages\torch\utils\data\_utils\fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "d:\code\yolox-main\yolox\data\datasets\datasets_wrapper.py", line 121, in wrapper
    ret_val = getitem_fn(self, index)
  File "d:\code\yolox-main\yolox\data\datasets\mosaicdetection.py", line 91, in __getitem__
    img, _labels, _, _ = self._dataset.pull_item(index)
  File "d:\code\yolox-main\yolox\data\datasets\voc.py", line 145, in pull_item
    target = self.load_anno(index)
  File "d:\code\yolox-main\yolox\data\datasets\voc.py", line 126, in load_anno
    target = self.target_transform(target)
  File "d:\code\yolox-main\yolox\data\datasets\voc.py", line 64, in __call__
    label_idx = self.class_to_ind[name]
KeyError: '1'`

This part thinks that it is named 1 when marking data, but it is other class aliases when modifying class aliases, so an error occurs. This problem does not occur in YOLOv5. The specific reasons are as follows.


Modify yolox data. get_ yolox_ Datadir() function

③: RuntimeError: DataLoader worker (pid(s) 17496, 8068, 18732, 8452) exited unexpectedly

Modify yolox under exp_ base. Self. Py file data_ num_ Workers set it to 0

Basically, there is no big problem.

5, Summary
1. There are not many problems in building the environment, because the environment is the foundation, there will be problems, as long as you are patient to solve them.
2. For training their own data sets, there are a lot of problems in this part. I feel only personal, and the code is chaotic. For example, for the processing of coco and voc data sets, it is not necessary to force the user to process them into the corresponding format according to his code style. Maybe it is to better let beginners contact them, I feel that this greatly increases its troubles. The reason is that the code has strict requirements for its specifications.
3. This effect has not been verified personally. The difference between engineering and academic is the landing. Its effect needs to be studied. At the end of the morning, I hope it will be helpful for my own learning and deployment. I hope this can be very good and true. Now even the Ministry and department should be informed.
Farewell, everyone, and do and cherish.

Topics: Python Pytorch Computer Vision Deep Learning Object Detection