Use VOC data set to train your own YOLOv3 model-python, tensorflow, keras

Environment configuration

1. Environmental choice

python 3.6
tenserflow 1.4.0
keras 2.2.4

2.python3.6 download and configuration
① download link
https://www.filehorse.com/download-python-64/35070/
② installation
installation process is relatively simple, not here described pay more.
③Configure environment variables
Add the path to the PATH environment variable according to the location of your installation . Below is my installation path

F:\PythonLocation
F:\PythonLocation`Scripts`
description:
PythonLocation is the name of the installation path I obtained by myself, and it needs to correspond to your own path

3. Install the corresponding module
win+R and enter cmd, execute the following command

pip install --upgrade pip #更新pip
pip install tensorflow==1.4.0 #安装 tensorflow
pip install --upgrade numpy #numpy是一个基础的数组计算包,有时候版本较老会报错 
pip install keras==2.2.4 #yolo train.py文件里面要用到keras这个高层神经网络API
pip install pillow #安装 图片处理模块PIL(pillow)
pip install matplotlib #安装 2D绘图库matplotlib
pip install opencv-python #安装 opencv视觉库

Use of official models

1. Download the official
model①Download of yolov3.weights weight file
Download link: https://pjreddie.com/media/files/yolov3.weights

说明:
Put the downloaded file into the keras-yolo3 file

②Download of keras-yolo3 code
Download link on Github: https://github.com/qqwweee/keras-yolo3
Baidu network disk sharing link: https://pan.baidu.com/s/1KwiR4itdofL0DmhaJtbGJA
Extraction code: pfy2

说明:
It is recommended to use Baidu network disk sharing link to download. The content downloaded by Github may have some modified content, which will cause some errors in subsequent operations. The yolov3.weights weight file is already included in the Baidu network disk sharing, and the download of the weight file can be ignored without adding it.

Insert picture description here
③Conversion weight file

python convert.py yolov3.cfg yolov3.weights model_data/yolo.h5

Insert picture description here
After the execution is complete
Insert picture description here
④Picture recognition

python yolo_video.py --image

Prompt to enter the image path (the location of the image is in the keras-yolo3-master directory, in the same directory as the weight file)
Insert picture description here
execution effect
Insert picture description here

Make a VOC data set

The full name of VOC is Visual Object Classes, which comes from The PASCAL Visual Object Classes (VOC) Challenge. This challenge started from 2005 to 2012. Every year, the organizer will provide some picture samples for challengers to identify and classify.
PASCAL VOC official website: http://host.robots.ox.ac.uk/pascal/VOC/ 1. VOC
format
Insert picture description here
format catalog description
Insert picture description here

How to get the format of VOC data set

①Download the official VOC data set, delete the content inside, and only keep each folder
②Create a folder set manually according to this format

This folder is placed directly in the project file keras-yolo3-master.
2. Prepare samples and XML tags
① Download LabelImg
LabelImg is a visual image calibration tool. Faster R-CNN, YOLO, SSD and other target detection network required data sets, all need to use this tool to calibrate the target in the image. The generated XML file follows the PASCAL VOC format.
Download link:
https://github.com/tzutalin/labelImg/releases

It can be installed from the source code, or you can directly download the exe free installation version


②Image annotation Run Labelmg
Insert picture description here
usage method

打开文件夹(JPEGImagess)->设置保存文件夹(annotations) 设置自动保存(view ->auto Saving)->标注框(Create RectBox)并命名 快捷键A保存xml->快捷键D下一张

Insert picture description here
③Generate index Create a new **.py file** (name can be named)
in the VOC2007 folder and the
Insert picture description here
code is as follows

import os
import random
 
trainval_percent = 0.1
train_percent = 0.9
xmlfilepath = 'Annotations'
txtsavepath = 'ImageSets\Main'
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)
 
ftrainval = open('ImageSets/Main/trainval.txt', 'w')
ftest = open('ImageSets/Main/test.txt', 'w')
ftrain = open('ImageSets/Main/train.txt', 'w')
fval = open('ImageSets/Main/val.txt', 'w')
 
for i in list:
    name = total_xml[i][:-4] + '\n'
    if i in trainval:
        ftrainval.write(name)
        if i in train:
            ftest.write(name)
        else:
            fval.write(name)
    else:
        ftrain.write(name)
 
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()

After running the code, generate test.txt,train.txt,trainval.txt,val.txtfiles under ImageSets/Main
Insert picture description here

说明:
{class}_train.txt Save all the indexes of the training set of
class {class}_val.txt Save all the indexes of the validation set of
class {class}_trainval.txt Save all the indexes of the training validation set of class

YOLO model training

1. Generate training index
In the project file keras-yolo3-master , modify the voc_annotation.py file

The main thing is to change the content of the classes inside to the object to be trained (to be consistent with the previous mark)

Insert picture description here
运行该程序,得到下面几个文件,将文件的前缀2007去掉
Insert picture description here
2.修改配置文件
首先要修改一下 model_data 文件夹中的coco_classes.txtvoc_classes.txt,将里面的对象改成自己要训练的对象名称
3.执行训练
直接将model_data文件夹中原版的yolo.h5复制,改名为yolo_weights.h5,将其作为预训练权重
Insert picture description here
生成yolo_weights.h5文件之后,在keras-yolo3-master工程文件夹中找到train. py,根据需要,可以更改里面的迭代次数epochs 等参数。如果显存小的话,可以改小batch_size。
Insert picture description here
运行train.py
Insert picture description here

这个训练过程会花费时间比较久。

训练完成Insert picture description here

遇到问题

1.AttributeError: ‘str’ object has no attribute 'decode
Insert picture description here
解决方法:
卸载原来的h5py模块,安装2.10版本

pip install h5py==2.10 -i https://pypi.tuna.tsinghua.edu.cn/simple/

参考资料

1. The keras version yolov3 prompts str object has no attribute decode
2. [Keras+TensorFlow+Yolo3] This article masters image labeling, training, and recognition (tf2 filling holes)
3. [YOLO] Use the VOC data set to train your own YOLOv3 model (Keras /TensorFlow)

Guess you like

Origin blog.csdn.net/qq_43279579/article/details/114654516