Keras yolov3训练自己的数据集

一. 环境的搭建
系统:windows10
显卡:1080Ti
python:3.7.3
keras:2.2.4
Tensorflow:1.13.1

二. GitHub下载keras-yolo3-master及权重
keras-yolo3-master下载链接
yolov3.weights下载链接

三. 权重转换及yolov3测试:
1.在keras-yolo3-master目录下执行python脚本:
python convert.py yolov3.cfg yolov3.weights model_data/yolo.h5

在model_data文件下产生了keras需要的.H5文件:
Keras yolov3训练自己的数据集_第1张图片
2.使用yolo_video进行图片或视频检测。
我是在jupyter notebook里面运行代码的,需要把yolo_video.py里面代码copy
jupyter 里面运行。
Keras yolov3训练自己的数据集_第2张图片
Keras yolov3训练自己的数据集_第3张图片
Keras yolov3训练自己的数据集_第4张图片

四. 建立自己的数据集:
1.收集图片
2.建立下面的文件夹结构:
VOCdevkit
–VOC2007
–Annotations(存放label.XML)
–ImageSets
–Main (存放数据集划分.txt)
–JPEGImages (存放图片.JPG)
3.最好把图片resize到416,416大小再进行label,这是个比较大的抗,不然后面测试精度有问题。label工具使用labelImg,可以自己网上下载。
Keras yolov3训练自己的数据集_第5张图片
4.生成训练集,验证集,测试集。代码如下:

import os
import random
trainval_percent = 0.2
train_percent = 0.8
xmlfilepath = 'C:/Users/admin/AI/models/research/object_detection/keras-yolo3-master/VOCdevkit/VOC2007/Annotations'
txtsavepath = 'C:/Users/admin/AI/models/research/object_detection/keras-yolo3-master/VOCdevkit/VOC2007/ImageSets/Main'
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
print(tv)
print(tr)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)
ftrainval = open('C:/Users/admin/AI/models/research/object_detection/keras-yolo3-master/VOCdevkit/VOC2007/ImageSets/Main/trainval.txt', 'w')
ftest = open('C:/Users/admin/AI/models/research/object_detection/keras-yolo3-master/VOCdevkit/VOC2007/ImageSets/Main/test.txt', 'w')
ftrain = open('C:/Users/admin/AI/models/research/object_detection/keras-yolo3-master/VOCdevkit/VOC2007/ImageSets/Main/train.txt', 'w')
fval = open('C:/Users/admin/AI/models/research/object_detection/keras-yolo3-master/VOCdevkit/VOC2007/ImageSets/Main/val.txt', 'w')
for i in list:
    name = total_xml[i][:-4] + '\n'
    name = total_xml[i][:-4] + '\n'
    if i in trainval: # trainval  20%(test&val)
        ftrainval.write(name)
        if i in train: #test  20%80%=16%
            ftest.write(name)
        else: #val   20%*20*=4%
            fval.write(name)
    else: #train  80%
        ftrain.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()

5.运行voc_annotation.py 生成2007_test.txt,2007_val.txt,2007_train.txt用于
后面的训练。(classes与路径根据自己的需要修改)
6.修改model_data下面的coco_classes.txt,voc_classes.txt,为自己的类别,
每类一行。yolo_anchors可以不修改。

五. 启动训练
1.在train.py中,如下代码根据自己数据修改:

    annotation_path = '2007_train.txt'
    log_dir = 'logs/000/'
    classes_path = 'model_data/voc_classes.txt'
    anchors_path = 'model_data/yolo_anchors.txt'
如果训练的东西是全新的种类,建议重新训练,不用加载之前的权重进行预训练(finetune)
load_pretrained=False, freeze_body=False,使用下面的train.py:
import tensorflow as tf
import numpy as np
import keras.backend as K
from keras.layers import Input, Lambda
from keras.models import Model
from keras.callbacks import TensorBoard, ModelCheckpoint, EarlyStopping
from yolo3.model import preprocess_true_boxes, yolo_body, tiny_yolo_body, yolo_loss
from yolo3.utils import get_random_data
def _main(): 
    annotation_path = '2007_train.txt'
    log_dir = 'logs/000/'
    classes_path = 'model_data/voc_classes.txt'
    anchors_path = 'model_data/yolo_anchors.txt'
    class_names = get_classes(classes_path)
    anchors = get_anchors(anchors_path)
    input_shape = (416,416) # multiple of 32, hw
    model = create_model(input_shape, anchors, len(class_names) )
    train(model, annotation_path, input_shape, anchors, len(class_names), log_dir=log_dir)   
    sess.close()
def train(model, annotation_path, input_shape, anchors, num_classes, log_dir='logs/'):
    model.compile(optimizer='adam', loss={
        'yolo_loss': lambda y_true, y_pred: y_pred})
    logging = TensorBoard(log_dir=log_dir)
    checkpoint = ModelCheckpoint(log_dir + "ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5",
        monitor='val_loss', save_weights_only=True, save_best_only=True, period=1)
    batch_size = 2
    val_split = 0.1
    with open(annotation_path) as f:
        lines = f.readlines()
    np.random.shuffle(lines)
    num_val = int(len(lines)*val_split)
    num_train = len(lines) - num_val
    print('Train on {} samples, val on {} samples, with batch size {}.'.format(num_train, num_val, batch_size))
    model.fit_generator(data_generator_wrap(lines[:num_train], batch_size, input_shape, anchors, num_classes),
            steps_per_epoch=max(1, num_train//batch_size),
            validation_data=data_generator_wrap(lines[num_train:], batch_size, input_shape, anchors, num_classes),
            validation_steps=max(1, num_val//batch_size),
            epochs=200,
            initial_epoch=0)
    model.save_weights(log_dir + 'trained_weights.h5')
def get_classes(classes_path):
    with open(classes_path) as f:
        class_names = f.readlines()
    class_names = [c.strip() for c in class_names]
    return class_names
def get_anchors(anchors_path):
    with open(anchors_path) as f:
        anchors = f.readline()
    anchors = [float(x) for x in anchors.split(',')]
    return np.array(anchors).reshape(-1, 2)
def create_model(input_shape, anchors, num_classes, load_pretrained=False, freeze_body=False,
            weights_path='model_data/yolo_weights.h5'):
    K.clear_session() # get a new session
    image_input = Input(shape=(None, None, 3))
    h, w = input_shape
    num_anchors = len(anchors)
    y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \
        num_anchors//3, num_classes+5)) for l in range(3)]
    model_body = yolo_body(image_input, num_anchors//3, num_classes)
    print('Create YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))
    if load_pretrained:
        model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
        print('Load weights {}.'.format(weights_path))
        if freeze_body:
            # Do not freeze 3 output layers.
            num = len(model_body.layers)-7
            for i in range(num): model_body.layers[i].trainable = False
            print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))
    model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
        arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
        [*model_body.output, *y_true])
    model = Model([model_body.input, *y_true], model_loss)
    return model
def data_generator(annotation_lines, batch_size, input_shape, anchors, num_classes):
    n = len(annotation_lines)
    np.random.shuffle(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        for b in range(batch_size):
            i %= n
            image, box = get_random_data(annotation_lines[i], input_shape, random=True)
            image_data.append(image)
            box_data.append(box)
            i += 1
        image_data = np.array(image_data)
        box_data = np.array(box_data)
        y_true = preprocess_true_boxes(box_data, input_shape, anchors, num_classes)
        yield [image_data, *y_true], np.zeros(batch_size)
def data_generator_wrap(annotation_lines, batch_size, input_shape, anchors, num_classes):
    n = len(annotation_lines)
    if n==0 or batch_size<=0: return None
    return data_generator(annotation_lines, batch_size, input_shape, anchors, num_classes)
if __name__ == '__main__':    
    _main()

2. loss下降到十左右就可以进行停止训练了。

六. 模型测试
图片的批量测试参考下面的代码:

# -*- coding: utf-8 -*-
"""
功能:keras-yolov3 进行批量测试 并 保存结果
项目来源:https://github.com/qqwweee/keras-yolo3
"""
import colorsys
import os
from timeit import default_timer as timer
import time
import numpy as np
from keras import backend as K
from keras.models import load_model
from keras.layers import Input
from PIL import Image, ImageFont, ImageDraw
from yolo3.model import yolo_eval, yolo_body, tiny_yolo_body
from yolo3.utils import letterbox_image
from keras.utils import multi_gpu_model

path = './test/'  #待检测图片的位置
# 创建创建一个存储检测结果的dir
result_path = './result'
if not os.path.exists(result_path):
    os.makedirs(result_path)
# result如果之前存放的有文件,全部清除
for i in os.listdir(result_path):
    path_file = os.path.join(result_path,i)  
    if os.path.isfile(path_file):
        os.remove(path_file)
#创建一个记录检测结果的文件
txt_path =result_path + '/result.txt'
file = open(txt_path,'w') 

class YOLO(object):
    _defaults = {
        "model_path": 'model_data/trained_weights_final.h5',  #根据自己的模型文件修改
        "anchors_path": 'model_data/yolo_anchors.txt', #根据自己的模型文件修改
        "classes_path": 'model_data/coco_classes.txt', #根据自己的模型文件修改
        "score" : 0.3,
        "iou" : 0.45,
        "model_image_size" : (416, 416),
        "gpu_num" : 1,
    }
    @classmethod
    #classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法
    def get_defaults(cls, n):
        if n in cls._defaults:
            return cls._defaults[n]
        else:
            return "Unrecognized attribute name '" + n + "'"
    def __init__(self, **kwargs):
        self.__dict__.update(self._defaults) # set up default values
        self.__dict__.update(kwargs) # and update with user overrides
        self.class_names = self._get_class()
        self.anchors = self._get_anchors()
        self.sess = K.get_session()
        self.boxes, self.scores, self.classes = self.generate()
    def _get_class(self):
        classes_path = os.path.expanduser(self.classes_path)
        with open(classes_path) as f:
            class_names = f.readlines()
        class_names = [c.strip() for c in class_names]
        return class_names
    def _get_anchors(self):
        anchors_path = os.path.expanduser(self.anchors_path)
        with open(anchors_path) as f:
            anchors = f.readline()
        anchors = [float(x) for x in anchors.split(',')]
        return np.array(anchors).reshape(-1, 2)
    def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model or weights must be a .h5 file.'
        # Load model, or construct model and load weights.
        num_anchors = len(self.anchors)
        num_classes = len(self.class_names)
        is_tiny_version = num_anchors==6 # default setting
        try:
            self.yolo_model = load_model(model_path, compile=False)
            print('load model')
        except:
            self.yolo_model = tiny_yolo_body(Input(shape=(None,None,3)), num_anchors//2, num_classes) \
                if is_tiny_version else yolo_body(Input(shape=(None,None,3)), num_anchors//3, num_classes)
            self.yolo_model.load_weights(self.model_path) # make sure model, anchors and classes match
            print('load weights')
        else:
            assert self.yolo_model.layers[-1].output_shape[-1] == \
                num_anchors/len(self.yolo_model.output) * (num_classes + 5), \
                'Mismatch between model and given anchor and class sizes'
        print('{} model, anchors, and classes loaded.'.format(model_path))
        # Generate colors for drawing bounding boxes.
        hsv_tuples = [(x / len(self.class_names), 1., 1.)
                      for x in range(len(self.class_names))]
        self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors = list(
            map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
                self.colors))
        np.random.seed(10101)  # Fixed seed for consistent colors across runs.
        np.random.shuffle(self.colors)  # Shuffle colors to decorrelate adjacent classes.
        np.random.seed(None)  # Reset seed to default.
        # Generate output tensor targets for filtered bounding boxes.
        self.input_image_shape = K.placeholder(shape=(2, ))
        if self.gpu_num>=2:
            self.yolo_model = multi_gpu_model(self.yolo_model, gpus=self.gpu_num)
        boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors,
                len(self.class_names), self.input_image_shape,
                score_threshold=self.score, iou_threshold=self.iou)
        return boxes, scores, classes
        def detect_image(self, image):
        start = timer() # 开始计时
        if self.model_image_size != (None, None):
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')
        print(image_data.shape) #打印图片的尺寸
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.
        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })
        print('Found {} boxes for {}'.format(len(out_boxes), 'img')) # 提示用于找到几个bbox        
        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                    size=np.floor(2e-2 * image.size[1] + 0.2).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 500          
        # 保存框检测出的框的个数
        file.write('find  '+str(len(out_boxes))+' target(s) \n')       
        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]
            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)
            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))            
            # 写入检测位置            
            file.write(predicted_class+'  score: '+str(score)+' \nlocation: top: '+str(top)+'、 bottom: '+str(bottom)+'、 left: '+str(left)+'、 right: '+str(right)+'\n')            
            print(label, (left, top), (right, bottom))
            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])
            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle(
                    [left + i, top + i, right - i, bottom - i],
                    outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin), tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw
        end = timer()
        print('time consume:%.3f s '%(end - start))
        return image
    def close_session(self):
        self.sess.close()
  
  # 图片检测
if __name__ == '__main__':
    t1 = time.time()
    yolo = YOLO()   
    for filename in os.listdir(path):        
        image_path = path+'/'+filename
        portion = os.path.split(image_path)
        file.write(portion[1]+' detect_result:\n')        
        image = Image.open(image_path)
        r_image = yolo.detect_image(image)
        file.write('\n')
        #r_image.show() 显示检测结果
        image_save_path = './result/result_'+portion[1]        
        print('detect result save to....:'+image_save_path)
        r_image.save(image_save_path)
    time_sum = time.time() - t1
    file.write('time sum: '+str(time_sum)+'s') 
    print('time sum:',time_sum)
    file.close() 
    yolo.close_session()
 

测试结果图片:
Keras yolov3训练自己的数据集_第6张图片

你可能感兴趣的:(AI)