yolov3-训练过程小测

本文介绍了YOLOv3模型的训练过程,包括获取和标注训练数据,由XML转TXT,训练数据集划分,编写Python脚本处理数据,以及训练命令和预训练模型的使用。训练日志显示模型在不同区域的IOU和分类准确性,表明训练过程有效。
摘要由CSDN通过智能技术生成

 

1. 获取训练数据

本次测试的训练数据为公路上的检测视频。

2. LabelImage软件进行标记--得到xml文件

下载标记软件进行图像标记,得到每张图片的xml文件。可从下面链接中下载Ubuntu版本

https://download.csdn.net/download/sinat_24221743/10586394

3. 由xml得到txt文件

1)将所有的图片分为训练图片和测试图片,参考python代码如下:

#-*- coding:utf-8 -*-

#将所有图片分为训练样本和测试样本,存在train.txt和val.txt里

import os
from os import listdir, getcwd
from os.path import join
if __name__ == '__main__':
    source_folder='/home/xxx/images/'#地址是所有图片的保存地点
    dest='/home/xxx/labels/train_val/train.txt' #保存train.txt的地址
    dest2='/home/xxx/labels/train_val/val.txt'  #保存val.txt的地址
    file_list=os.listdir(source_folder)       #赋值图片所在文件夹的文件列表
    train_file=open(dest,'a')                 #打开文件
    val_file=open(dest2,'a')                  #打开文件
    for file_obj in file_list:                #访问文件列表中的每一个文件
        file_path=os.path.join(source_folder,file_obj)
        #file_path保存每一个文件的完整路径
        file_name,file_extend=os.path.splitext(file_obj)
        #file_name 保存文件的名字,file_extend保存文件扩展名
        file_num=int(file_name)
        #把每一个文件命str转换为 数字 int型 每一文件名字都是由四位数字组成的  如 0201 代表 201     高位补零  
        if(file_num<1000):                     #保留1000个文件用于训练
            #print file_num
            train_file.write(file_name+'\n')  #用于训练前900个的图片路径保存在train.txt里面,结尾加回车换行
        else :
            val_file.write(file_name+'\n')    #其余的文件保存在val.txt里面
    train_file.close()#关闭文件
    val_file.close()

2) 根据得到的train.txt和val.txt,分别将训练和验证集对应图片的路径存到test_train.txt和test_val.txt中

参考python代码如下:
# train.txt, generate by creat_list.py, save train image id and names
# val.txt, generate by creat_list.py, save validate image id and names
# need .xml label files,xml files name accord with train image
# this script generate indrared_train.txt file ,used to save train image complete path, and used by the file: voc.data yolo.c
# this script also can generate indrared_val.txt file ,used to save validate image complete path, and used by the file: voc.data #yolo.c
# this script also can generate txt format's yolo recongnition label file, convert by every train or validate xml file, txt format file #name accord xml file name, but differ context and extension name
#this script need accord xml file path and need generate txt complete save path

import xml.etree.ElementTree as ET
import pickle
import os
from os.path import join
classes = ["person", "car", "bus", "truck", "bike", "motorbike", "traffic light", "traffic sign", "taxi"] # class

def convert(size, box):
    dw = 1./size[0]
    dh = 1./size[1]
    x = (box[0] + box[1])/2.0
    y = (box[2] + box[3])/2.0
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)

def convert_annotation(image_id):
    in_file = open('/home/xxx/xml/%s.xml'%(image_id)) #xml file path
    out_file = open('/home/xxx/labels/%s.txt'%(image_id),'w')
    tree = ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')  # size label data
    w = int(size.find('width').text) #size label data width
    h = int(size.find('height').text)#size label data height
    for obj in root.iter('object'):
       # difficult = obj.find('difficult').text   #diffcult label
    cls = obj.find('name').text
        if cls not in classes :#or int(difficult) == 1:        
        continue    
    cls_id = classes.index(cls)
    xmlbox = obj.find('bndbox')   #visit bounding box label data and process
    b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
    bb = convert((w,h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')

image_ids = open('/home/xxx/labels/train.txt').read().strip().split()  #train data
#image_ids = open('/home/xxx/labels/val.txt').read().strip().split()  #validate data
list_file = open('/home/xxx/labels/test_train.txt', 'w')     #write result to test_train.txt file,train set
#list_file = open('/home/xxx/labels/test_val.txt', 'w')     #write result to test_val.txt file,validate set       
for image_id in image_ids:
    list_file.write('/home/xxx/images/%s.jpg\n'%(image_id))  
    convert_annotation(image_id)   
list_file.close() #close file

4. 训练

训练命令:./darknet detector train cfg/myv3_det.data cfg/my_yolov3.cfg darknet53.conv.74

训练需要.data文件,.cfg文件,故需要制作v3.data以及myolov3.cfg

v3.data文件格式如下:其中包括类别数classes,训练图像路径train,类别名字names,以及训练权重值保存路径backup。

classes= 9
train = /home/xxx/data/test_train.txt
names = /home/xxx/data/myv3.names
backup= /home/xxx/train/darknetv3/backup

myolov3.cfg参考的为yolov3.cfg的网络配置文件,基本参数没有修噶只是将其中的max_batches改小了。classes改成自分类别数,对应的3处filters=(classes+5)*3进行修改,random=0,为1时跑不起来。

注:训练:batch=64;subdivisions=8

有需要可以到这个博客资源页下载。

3)预训练模型下载darknet53.conv.74

darknet53.conv.74下载链接:https://pjreddie.com/media/files/darknet53.conv.74

为了后续的的模型评估,保存训练log的命令

./darknet detector train cfg/myv3_det.data cfg/my_yolov3.cfg darknet53.conv.74 -gpus 0,1 2&gt;1 | tee train_yolov3.log

训练日志截取最后一部分

Region 82 Avg IOU: 0.896143, Class: 0.999871, Obj: 0.999981, No Obj: 0.007939, .5R: 1.000000, .75R: 1.000000,  count: 15
Region 94 Avg IOU: 0.890928, Class: 0.999866, Obj: 0.999650, No Obj: 0.007638, .5R: 1.000000, .75R: 1.000000,  count: 31
Region 106 Avg IOU: 0.850831, Class: 0.999735, Obj: 0.997689, No Obj: 0.002961, .5R: 1.000000, .75R: 0.934783,  count: 46
Region 82 Avg IOU: 0.894584, Class: 0.999749, Obj: 0.999900, No Obj: 0.005563, .5R: 1.000000, .75R: 1.000000,  count: 7
Region 94 Avg IOU: 0.876964, Class: 0.998086, Obj: 0.974973, No Obj: 0.007528, .5R: 1.000000, .75R: 0.970588,  count: 34
Region 106 Avg IOU: 0.751409, Class: 0.982504, Obj: 0.981287, No Obj: 0.002457, .5R: 0.869565, .75R: 0.673913,  count: 46
Region 82 Avg IOU: 0.907001, Class: 0.999966, Obj: 0.999952, No Obj: 0.003827, .5R: 1.000000, .75R: 1.000000,  count: 4
Region 94 Avg IOU: 0.906861, Class: 0.999881, Obj: 0.999952, No Obj: 0.003209, .5R: 1.000000, .75R: 1.000000,  count: 12
Region 106 Avg IOU: 0.761513, Class: 0.986614, Obj: 0.982316, No Obj: 0.003174, .5R: 0.920000, .75R: 0.653333,  count: 75
Region 82 Avg IOU: 0.899414, Class: 0.990468, Obj: 0.999981, No Obj: 0.004179, .5R: 1.000000, .75R: 1.000000,  count: 4
Region 94 Avg IOU: 0.904084, Class: 0.999623, Obj: 0.999115, No Obj: 0.006000, .5R: 1.000000, .75R: 1.000000,  count: 24
Region 106 Avg IOU: 0.772183, Class: 0.977742, Obj: 0.965412, No Obj: 0.003011, .5R: 0.926471, .75R: 0.691176,  count: 68
Region 82 Avg IOU: 0.900164, Class: 0.999946, Obj: 0.999902, No Obj: 0.010270, .5R: 1.000000, .75R: 1.000000,  count: 17
Region 94 Avg IOU: 0.875355, Class: 0.999732, Obj: 0.999801, No Obj: 0.004528, .5R: 1.000000, .75R: 0.888889,  count: 18
Region 106 Avg IOU: 0.838250, Class: 0.999476, Obj: 0.998630, No Obj: 0.001781, .5R: 1.000000, .75R: 0.900000,  count: 30
Region 82 Avg IOU: 0.907581, Class: 0.999919, Obj: 0.999991, No Obj: 0.009320, .5R: 1.000000, .75R: 1.000000,  count: 14
Region 94 Avg IOU: 0.894800, Class: 0.999890, Obj: 0.999795, No Obj: 0.006231, .5R: 1.000000, .75R: 1.000000,  count: 21
Region 106 Avg IOU: 0.833003, Class: 0.998644, Obj: 0.960275, No Obj: 0.001889, .5R: 1.000000, .75R: 0.861111,  count: 36
Region 82 Avg IOU: 0.904388, Class: 0.999917, Obj: 0.999940, No Obj: 0.008733, .5R: 1.000000, .75R: 1.000000,  count: 10
Region 94 Avg IOU: 0.905678, Class: 0.999758, Obj: 0.999667, No Obj: 0.008275, .5R: 1.000000, .75R: 1.000000,  count: 32
Region 106 Avg IOU: 0.832016, Class: 0.999359, Obj: 0.985120, No Obj: 0.002848, .5R: 1.000000, .75R: 0.816326,  count: 49
Region 82 Avg IOU: 0.894894, Class: 0.999847, Obj: 0.999894, No Obj: 0.004353, .5R: 1.000000, .75R: 1.000000,  count: 6
Region 94 Avg IOU: 0.890745, Class: 0.999662, Obj: 0.994521, No Obj: 0.005623, .5R: 1.000000, .75R: 1.000000,  count: 23
Region 106 Avg IOU: 0.856922, Class: 0.999253, Obj: 0.992790, No Obj: 0.003102, .5R: 1.000000, .75R: 0.892857,  count: 56
10200: 0.485842, 0.531434 avg, 0.001000 rate, 4.296927 seconds, 652800 images

其中:

       Region xx: cfg文件中yolo-layer的索引;
        Avg IOU:当前迭代中,预测的box与标注的box的平均交并比,越大越好,期望数值为1;
        Class: 标注物体的分类准确率,越大越好,期望数值为1;
        obj: 越大越好,期望数值为1;
        No obj: 越小越好;
        .5R: 以IOU=0.5为阈值时候的recall; recall = 检出的正样本/实际的正样本
        0.75R: 以IOU=0.75为阈值时候的recall;

5. 评估

首先将保存的log日志进行筛选:去除不可解析的log后使log文件格式化,生成新的log文件供可视化工具绘图

选取其中loss和iou数据分别存在train_log_loss.txt和train_log_iou.txt中。

python代码参考:

    # coding=utf-8
    # 该文件用来提取训练log,去除不可解析的log后使log文件格式化,生成新的log文件供可视化工具绘图
import inspect
import os
import random
import sys
def extract_log(log_file,new_log_file,key_word):
    with open(log_file, 'r') as f:
      with open(new_log_file, 'w') as train_log:   
      #f = open(log_file)
        #train_log = open(new_log_file, 'w')
        for line in f:
        # 去除多gpu的同步log
         if 'Syncing' in line:
          continue
        # 去除除零错误的log
         if 'nan' in line:
          continue
         if key_word in line:
          train_log.write(line)
    f.close()
    train_log.close()
     
extract_log('my_train_yolov3.log','train_log_loss.txt','images')
extract_log('my_train_yolov3.log','train_log_iou.txt','IOU')

对于可视化分析过程及代码可参考博客:https://blog.csdn.net/vvyuervv/article/details/72868749

6. 测试

对视频进行测试命令

./darknet detector demo cfg/v3.data cfg/myolov3.cfg myolov3_final.weights data/test.avi

对图片进行测试命令

./darknet detect cfg/my_yolov3.cfg my_yolov3_final.weights data/0011.jpg

简单测试效果还可以。图突然贴不上了,就不放效果图了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值