Quick Start remember yolov3
- Foreword
- Reference Code
- Data sets marked
- Use VOC2007 data set, a new folder below
- Generating a training set / test set
- Yolo3 training set to generate the desired / validation set
- Modify the parameter file yolov3.cfg
- Modify voc_classes.txt file
- Modify train.py file
- He began training its own model
- Wait for training
- Modify the model position
- Forecast data
- postscript
Foreword
Depth new to learn, to try their own data sets with yolov3 training, check a lot of relevant information, a few detours, so remember to write a blog quick start method does not involve the principle, serve as a catalyst for your reference.
Reference Code
qqwwee: hard-yolo3
Data sets marked
Use labelimage annotation tools, Baidu itself can, if they flash back, the measured affinity may be solved by mounting pip, self Baidu.
Use VOC2007 data set, a new folder below
Generating a training set / test set
Run the following code.
import os
import random
trainval_percent = 0.8
train_percent = 1
xmlfilepath = 'Annotations'#自行修改路径(xml文件地址)
txtsavepath = 'ImageSets\Main'#自行修改路径(四个txt文件所在地址)
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()
Yolo3 training set to generate the desired / validation set
Above it has been completed the production of voc2007 data set, but yolov3 and can not use these data sets directly, so we have to generate a data set that can be used yolov3
run keras-yolov3 directory voc_annotation.p the y-file.
Note: The original category needs to be modified to your own categories (code to cat / dog for example)
import xml.etree.ElementTree as ET
from os import getcwd
sets=[('2007', 'train'), ('2007', 'val'), ('2007', 'test')]
classes = ["cat","dog"]
def convert_annotation(image_id, list_file):
in_file = open('../VOCdevkit/VOC2007/Annotations/%s.xml'%(image_id),encoding='utf-8')
tree=ET.parse(in_file)
root = tree.getroot()
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult)==1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (int(xmlbox.find('xmin').text), int(xmlbox.find('ymin').text), int(xmlbox.find('xmax').text), int(xmlbox.find('ymax').text))
list_file.write(" " + ",".join([str(a) for a in b]) + ',' + str(cls_id))
wd = getcwd()
for year, image_set in sets:
image_ids = open('../VOCdevkit/VOC%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split()
list_file = open('%s_%s.txt'%(year, image_set), 'w')
for image_id in image_ids:
list_file.write('../VOCdevkit/VOC2007/JPEGImages/%s.jpg'%(image_id))
convert_annotation(image_id, list_file)
list_file.write('\n')
list_file.close()
After running, it will generate the following three txt file
manually remove 2007_
Modify the parameter file yolov3.cfg
We use the notepad or directly open pycharm yolov3.cfg , the Ctrl + F. Direct search keyword Yolo , a total of three full Yolo keywords are modified according to the format of FIG.
Modify voc_classes.txt file
The category into which their own label category to cat / dog, for example
Modify train.py file
Directly to this function, modify the arrow argument is false
He began training its own model
The following run directly train.py file
Wait for training
Began training the following screen appears, it can wait
Modify the model position
After training is completed, will be under the logs / 000 creates a directory named trained_weights_final.h5 H5 file. Yolo.py open file, wherein the parameter is modified as follows
Forecast data
In yolo_video.py file directory, ie the directory open a command line window
, type python yolo_video.py -h to see help
predict the picture, for example, the command is: Python yolo_video.py --image
will be prompted for a file name, file type Enter to address
postscript
Deep learning beginner, initiate the above process only for your reference, if wrong, welcomed the guidance.