Under Windows credited with yolov3 / keras / tensorflow own training data set (not involving principle)

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

Here Insert Picture Description

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
Here Insert Picture Description
manually remove 2007_
Here Insert Picture Description

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.
Here Insert Picture Description

Modify voc_classes.txt file

The category into which their own label category to cat / dog, for example
Here Insert Picture Description
Here Insert Picture Description

Modify train.py file

Directly to this function, modify the arrow argument is false
Here Insert Picture Description

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
Here Insert Picture Description

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
Here Insert Picture Description

Forecast data

In yolo_video.py file directory, ie the directory open a command line window
Here Insert Picture Description
, type python yolo_video.py -h to see help
Here Insert Picture Description
predict the picture, for example, the command is: Python yolo_video.py --image
Here Insert Picture Description
will be prompted for a file name, file type Enter to address
Here Insert Picture Description

postscript

Deep learning beginner, initiate the above process only for your reference, if wrong, welcomed the guidance.

Published an original article · won praise 2 · Views 282

Guess you like

Origin blog.csdn.net/shuaigeek/article/details/105210500