自有数据集上使用keras训练YOLOv3目标检测

基于《自有数据集上,如何用keras最简单训练YOLOv3目标检测》整理  https://blog.csdn.net/sinat_26917383/article/details/85614247
原项目地址:keras-yolo3-improved
selfdata_keras_yolov3.ipynb,训练ipynb
selfdata_yolov3_test.ipynb,预测ipynb
yolo_matt.py,预测时候改进输出结果
首先基于作者数据进行训练,而后自己提供数据集解决专门问题。

一、数据准备
地址,xmin,ymin,xmax,ymax,类别ID然后空格下一个box,每张图一行。
images /images_all / 86900fb6gy1fl4822o7qmj22ao328qv7.jpg 10, 259, 399, 580, 27
images /images_all /b95fe9cbgw1eyw88vlifjj20c70hsq46.jpg 10, 353, 439, 640, 29
images /images_all / 005CsCZ0jw1f1n8kcj8m1j30ku0kumz6.jpg 75, 141, 343, 321, 27

二、网络训练
原keras源码中有两段训练:
    第一段冻结前面的 249层进行迁移学习(原有的yolov3)
    第二段解冻全部层进行训练
由于使用专业领域图像,与原有的yolov3训练集差异太大, 所以第一阶段的迁移学习阶段没用,故 直接开始第二段或重新根据darknet53训练。
可能需要预下载的模型:
yolo_weights.h5 预训练模型(用作迁移)
python convert.py -w yolov3.cfg yolov3.weights model_data /yolo_weights.h5
darknet53.weights (用作重新训练)
wget https : //pjreddie.com/media/files/darknet53.conv.74
yolo.h5 (yolov3-VOC训练模型,可以直接用来做预测 )
python convert.py yolov3.cfg yolov3.weights model_data /yolo.h5

训练时候需要的参数
     class yolo_args :
        annotation_path = 'train.txt'
        log_dir = 'logs/003/'
        classes_path = 'model_data/class_file_en.txt'
        anchors_path = 'model_data/yolo_anchors.txt'
        input_shape = ( 416, 416) # multiple of 32, hw
        # 608*608  416*416  320*320
        val_split = 0. 1
        batch_size = 16
        epochs_stage_1 = 10
        stage_1_train = False
        epochs_finally = 100
        finally_train = True
        weights_path =   'logs/003/ep009-loss33.297-val_loss32.851.h5'
        # 可以使用'model_data/tiny_yolo_weights.h5' 也可以使用tiny_yolo的:'model_data/yolo_weights.h5'
    # train
    _main(yolo_args)

其中:
annotation_path就是数据集准备的txt
log_dir ,Model存放地址,譬如:events.out.tfevents. 1545966202、ep077 -loss19. 318 -val_loss19. 682.h5
classes_path ,分类内容
anchors_path ,yolo anchors,可自行调整,也可以使用默认的
input_shape ,一般是 416
epochs_stage_1 = 10和 stage_1_train = False,是同一个,也就是是否进行迁移学习(stage_1_train ),要学习的话,学习几个epoch(epochs_stage_1 )
epochs_finally = 100和 finally_train = True ,是,是否进行后面开放所有层的学习(finally_train ),学习几个epoch(epochs_finally)
weights_path ,调用model的路径

三、预测和结果优化
预测代码参考:
import sys
import argparse
from yolo import YOLO, detect_video
from PIL import Image

yolo_test_args = {
    "model_path" : 'model_data/yolo.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,
}

yolo_test = YOLO( **yolo_test_args)
image = Image. open( 'images/part1/path1.jpg')
r_image = yolo_test.detect_image(image)
r_image.show()
预测结果优化:
import sys
import argparse
from yolo_matt import YOLO, detect_video
from PIL import Image

yolo_test_args = {
    "model_path" : 'logs/003/ep077-loss19.318-val_loss19.682.h5',
    "anchors_path" : 'model_data/yolo_anchors.txt',
    "classes_path" : 'model_data/class_file_en.txt',
    "score" : 0. 2, # 0.2
    "iou" : 0. 1, # 0.45
    "model_image_size" : ( 416, 416),
    "gpu_num" : 1,
}

yolo_test = YOLO( **yolo_test_args)

# 输出内容整理
def _get_class(classes_path) :
    classes_path = os.path.expanduser(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 yolov3_output(image,out_boxes,out_scores,out_classes) :
    output = []
    yolo_classes = _get_class(yolo_test_args[ 'classes_path'])
    for n,box in enumerate(out_boxes) :
        y_min, x_min, y_max, x_max = box
        y_min = max( 0, np.floor(y_min + 0. 5).astype( 'int32'))
        x_min = max( 0, np.floor(x_min + 0. 5).astype( 'int32'))
        y_max = min(image.size[ 1], np.floor(y_max + 0. 5).astype( 'int32'))
        x_max = min(image.size[ 0], np.floor(x_max + 0. 5).astype( 'int32'))
        score = out_scores[n]
        yo_class = yolo_classes[out_classes[n]]
        output.append({ 'y_min' :y_min, 'x_min' :x_min, 'y_max' :y_max, 'x_max' :x_max,\
                        'width' :image.size[ 0], 'height' :image.size[ 1],\
                        'score' :score, 'yo_class' :yo_class})
    return output
    
image = Image. open( 'images/images_all/path1.jpg')
r_image,out_boxes, out_scores, out_classes = yolo_test.detect_image(image)
output = yolov3_output(r_image,out_boxes,out_scores,out_classes)

输出结果类似:
{
'path1.jpg' : 
[{ 'y_min' : 416,   'x_min' : 34,   'y_max' : 754,   'x_max' : 367,   'width' : 440,   'height' : 783,   'score' : 0. 9224778,   'yo_class' : 'class1'},
  { 'y_min' : 428,   'x_min' : 3,   'y_max' : 783,   'x_max' : 352,   'width' : 440,   'height' : 783,   'score' : 0. 2180994,   'yo_class' : 'class2'}]
  }






猜你喜欢

转载自www.cnblogs.com/jsxyhelu/p/12312170.html