Use Faster-RCNN training data set process (learning record)

Regarding the theoretical part, I read the RCNN theory explanation of "Thunderbolt Wz" at station b. As a beginner, I can understand it, and students who need it can watch it by themselves.

Table of contents

1. Environment preparation

2. Training steps

3. Testing process 

4. Calculate the map


1. Environment preparation

I am using colab+tensorflow1.14.0 for training, other linux system training is equivalent

(I also tried windows+pytorch, but it is very troublesome to download pycocotools when configuring the environment, and I have to use Microsoft visual c++14. My computer is installed with vscode, which is different from the online tutorial, and I can’t find it. related tutorial, so I gave up)

If you don’t know how to use colab, you can refer to this article by yourself. Colab usage method records_Tao Renxiong’s Blog-CSDN Blog

2. Training steps

(1) Download the source code

!git clone https://github.com/dBeker/Faster-RCNN-TensorFlow-Python3 

(2) Download the expansion file

!pip install -r requirements.txt

(3) Download and add the pre-trained model

      The pre-training model in the source code uses VGG16, and the VGG16 model can be downloaded directly:

!wget -P ./data/imagenet_weights/ http://download.tensorflow.org/models/vgg_16_2016_08_28.tar.gz

      The name of the downloaded model should be vgg_16.ckpt. After renaming it to vgg16.ckpt, save the model in the data\imagenet_weights\ folder.

      Other models can also be used instead of VGG16, and other models can be downloaded from the link below:

      https://github.com/tensorflow/models/tree/master/research/slim#pre-trained-models

(4) Modify training parameters

Open the config.py file under the lib\config folder of the source code, and modify some important parameters, such as:

① network parameters

       This parameter defines the pre-training model network. The vgg16 model is used by default in the source code. We don’t need to modify it when we use vgg16. If we use other models in the previous step, we need to modify it.

②learning_rate

       This parameter is the learning rate. If it is set too large, oscillation may occur. If it is set too small, the convergence speed will be very slow. Therefore, we can first default to 0.001 of the source code for experiments, and then take multiple experiments such as 0.01 or 0.0001 to find the relative optimal value after operation.

③batch_size

       This parameter indicates the data batch size during gradient descent, which can generally be 16, 32, 64, 128, 256, etc. My personal understanding is that the larger the batch_size setting, the faster the rate of gradient descent during training, and it also has higher direction accuracy, but it consumes more memory; the smaller the batch_size setting, although saving memory, the training rate is lower. Slow, the convergence effect may not be very good. Therefore, if the memory allows, try to set it as large as possible.

④max_iters

       The max_iters parameter indicates the number of steps for training the maximum iteration. The source code is 40000. I experimented with 4000 and 40000 steps, and found that the mAP value in the later test results is not much different. I will continue to study in the future. This parameter can be carried out according to the 40000 of the source code first (it will take several days to run...)

⑤snapshot_iterations

       This parameter indicates how many iterations to generate a result model at intervals.

⑥roi_bg_threshold_low 和 roi_bg_threshold_high

       This parameter represents the threshold that is set as ROI (region of interest) in the background. If an error such as Exception: image invalid, skipping appears later, changing the roi_bg_threshold_low parameter to 0.0 will solve the problem.

(5) Prepare the data set

 Download the voc2007 dataset to data/VOCdevkit2007

!wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
!wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar
!wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCdevkit_08-Jun-2007.tar

decompress

!tar xvf VOCtrainval_06-Nov-2007.tar
!tar xvf VOCtest_06-Nov-2007.tar
!tar xvf VOCdevkit_08-Jun-2007.tar

If you use your own training data set and mark it as voc format, the labeling process will not be described in detail, you can check it yourself

yolo dataset annotation software installation + usage process

Organize the data set into the following format

Among them, the main place is the training and verification txt file, and the division procedure is as follows:

# 数据集划分集类
import os
from sklearn.model_selection import train_test_split

image_path = r'F:/111/data/VOCDevkit2007/VOC2007/JPEGImages'
image_list = os.listdir(image_path)
names = []

for i in image_list:
    names.append(i.split('.')[0])     # 获取图片名
trainval,test = train_test_split(names,test_size=0.5,shuffle=446)   # shuffle()中是图片总数目
validation,train = train_test_split(trainval,test_size=0.5,shuffle=446)

with open('F:/111/data/VOCDevkit2007/VOC2007/ImageSets/Main/trainval.txt','w') as f:
    for i in trainval:
        f.write(i+'\n')
with open('F:/111/data/VOCDevkit2007/VOC2007/ImageSets/Main/test.txt','w') as f:
    for i in test:
        f.write(i+'\n')
with open('F:/111/data/VOCDevkit2007/VOC2007/ImageSets/Main/validation.txt','w') as f:
    for i in validation:
        f.write(i+'\n')
with open('F:/111/data/VOCDevkit2007/VOC2007/ImageSets/Main/train.txt','w') as f:
    for i in train:
        f.write(i+'\n')

print('完成!')

(6) Generate corresponding files

Enter the ./data/coco/PythonAPI folder path, and run the following two commands respectively:

!python setup.py build_ext --inplace
!python setup.py build_ext install

Enter the ./lib/utils folder path and run the following command:

!python setup.py build_ext --inplace

In fact, I reported an error in this step, as follows, but it seems that it will not affect the normal training later, if there is any impact, please tell me

!python setup.py build_ext --inplace
running build_ext
building 'lib.utils.cython_bbox' extension
x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/local/lib/python3.7/dist-packages/numpy/core/include -I/lib/utils -I/usr/include/python3.7m -c ../../../lib/utils/bbox.c -o build/temp.linux-x86_64-3.7/../../../lib/utils/bbox.o
In file included from /usr/local/lib/python3.7/dist-packages/numpy/core/include/numpy/ndarraytypes.h:1969:0,
                 from /usr/local/lib/python3.7/dist-packages/numpy/core/include/numpy/ndarrayobject.h:12,
                 from /usr/local/lib/python3.7/dist-packages/numpy/core/include/numpy/arrayobject.h:4,
                 from ../../../lib/utils/bbox.c:770:
/usr/local/lib/python3.7/dist-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:17:2: warning: #warning "Using deprecated NumPy API, disable it with " "#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it with " \
  ^~~~~~~
x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/local/lib/python3.7/dist-packages/numpy/core/include -I/lib/utils -I/usr/include/python3.7m -c ../../../lib/utils/bbox.c -o build/temp.linux-x86_64-3.7/../../../lib/utils/bbox.o
In file included from /usr/local/lib/python3.7/dist-packages/numpy/core/include/numpy/ndarraytypes.h:1969:0,
                 from /usr/local/lib/python3.7/dist-packages/numpy/core/include/numpy/ndarrayobject.h:12,
                 from /usr/local/lib/python3.7/dist-packages/numpy/core/include/numpy/arrayobject.h:4,
                 from ../../../lib/utils/bbox.c:770:
/usr/local/lib/python3.7/dist-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:17:2: warning: #warning "Using deprecated NumPy API, disable it with " "#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it with " \
  ^~~~~~~
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -g -fwrapv -O2 -Wl,-Bsymbolic-functions -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.7/../../../lib/utils/bbox.o build/temp.linux-x86_64-3.7/../../../lib/utils/bbox.o -o /content/drive/MyDrive/colab/RCNN/Faster-RCNN/data/coco/PythonAPI/lib/utils/cython_bbox.cpython-37m-x86_64-linux-gnu.so
build/temp.linux-x86_64-3.7/../../../lib/utils/bbox.o:(.bss+0x0): multiple definition of `__pyx_module_is_main_lib__utils__cython_bbox'
build/temp.linux-x86_64-3.7/../../../lib/utils/bbox.o:(.bss+0x0): first defined here
build/temp.linux-x86_64-3.7/../../../lib/utils/bbox.o: In function `PyInit_cython_bbox':
/content/drive/MyDrive/colab/RCNN/Faster-RCNN/data/coco/PythonAPI/../../../lib/utils/bbox.c:4470: multiple definition of `PyInit_cython_bbox'
build/temp.linux-x86_64-3.7/../../../lib/utils/bbox.o:/content/drive/MyDrive/colab/RCNN/Faster-RCNN/data/coco/PythonAPI/../../../lib/utils/bbox.c:4470: first defined here
collect2: error: ld returned 1 exit status
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

(7) Modify category

打开lib/datasets目录中的pascal_voc.py文件,第34行self._classes表示目标检测的类别,将其修改为自己数据集的类别。注意不能修改 “_background_”,它表示图片的背景。

(8)删除缓存文件

 打开源码中data/cache目录,删掉上一次训练生成的.pkl缓存文件。打开default/voc_2007_trainval/default目录,删掉上次训练生成的模型。

注意以后每次训练都要删掉上述两个文件夹中的缓存文件和模型,不删会报错的。

(9)运行

每次生成的模型都会保存在default/voc_2007_trainval/default目录下

!python train.py

报错:

第一次运行时会出现以下错误

AttributeError: module 'tensorflow' has no attribute 'app'

解决方法:

因为colab自动预装最新的TensorFlow 2.X,而源码所使用的TensorFlow是1.x的,所以我们需要将新版的卸载,并安装旧版本

!pip uninstall tensorflow
!pip install tensorflow-gpu==1.14.0

3.测试过程 

(1)添加训练模型

 新建Faster-RCNN-TensorFlow-Python3-master/output/vgg16/voc_2007_trainval/default目录。把训练生成的模型(default/voc_2007_trainval/default目录下的四个文件)复制到新建目录下,并重命名为如下图:

(2)修改demo.py文件

①修改目标类别

         修改demo.py文件中line32,CLASSES中的类别要修改为之前步骤中相同的类别。注意 “_background_”不要修改。

②修改网络模型

         找到demo.py文件中line35、line36,将其修改为如下图所示:

③修改预训练模型

 找到demo.py文件中line104,将其修改为'vgg16',如下图:

Find line148 in the demo.py file and change it to the names of the pictures used for testing. Note that the name of the test image stored in the data/demo directory is the same.

(3) Run the demo.py file

4. Calculate the map

mAP (mean Average Precision), which is the average value of AP of each category, reflects the overall accuracy of the performance of a target detection model.

(1) Modify the pascal_voc.py file

       Open the pascal_voc.py file, find line189, and modify the content of "filename" as shown below:

(2) Modify the demo.py file

       Open the demo.py file, find line31, and add two modules:

# Add these two imports
from lib.utils.test import test_net
from lib.datasets.factory import get_imdb

       After adding, as shown in the figure:

       Then, find the last line of plt.show(), and add two lines of code above it:

# Add these two lines of code
imdb = get_imdb("voc_2007_trainval")
test_net(sess, net, imdb, 'default')

       After adding, as shown in the figure:

(3) Run the demo.py file

       Create a new data/VOCDevkit2007/results/VOC2007/Main directory, then run the demo.py file, and wait until the end of the run to see the calculation results of the mAP indicator! Post the calculation results of my own model!

Guess you like

Origin blog.csdn.net/hhb3329/article/details/126841297