当前位置:   article > 正文

训练自己的Yolov4-tiny_yolov4-tiny训练

yolov4-tiny训练

Yolov4-tiny 行人检测

yolov4

首先把包下好,然后解压(或者git clone)

编译体验一下

cd darknet 
sudo gedit Makefile  # 修改一下
'''
GPU=1      # 要不要用GPU
CUDNN=1
CUDNN_HALF=0
OPENCV=1    # 需要编译好的opencv,注意是编译好的不是pip下的
AVX=0
OPENMP=0
LIBSO=1     # 生成.so,,,方便使用Python开发
ZED_CAMERA=0
ZED_CAMERA_v2_8=0
................
'''

make -j4   
将权重文件yolov4.weights 或者 yolov4-tiny.weights 拷贝至 darknet 目录下(需下载)
然后就可以使用了
./darknet detector test ./cfg/coco.data ./cfg/yolov4.cfg ./yolov4.weights

Yolov4 图片检测
./darknet detect cfg/yolov4.cfg yolov4.weights data/dog.jpg 
./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights data/dog.jpg

Yolov4-tiny 图片的检测
./darknet detect cfg/yolov4-tiny.cfg yolov4-tiny.weights data/dog.jpg # 简写版
./darknet detector test cfg/coco.data cfg/yolov4-tiny.cfg yolov4-tiny.weights
data/dog.jpg 

改变检测阈值,默认0.25
./darknet detect cfg/yolov4-tiny.cfg yolov4-tiny.weights data/dog.jpg -thresh 0.1

Yolov4 视频的检测
./darknet detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights data/123.mp4

Yolov4-tiny 视频的检测
./darknet detector demo cfg/coco.data cfg/yolov4-tiny.cfg yolov4-tiny.weights
data/xxx.mp4

Yolov4-tiny 摄像头实时检测方法:
./darknet detector demo cfg/coco.data cfg/yolov4-tiny.cfg yolov4-tiny.weights
/dev/video1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

训练自定义对象

给的权重分类太多,只需要行人,所以。。。。这里训练的是yolov4_tiny ,其他方法差不多,也可以借鉴。

  1. 在 darknet/cfg目录下建立 yolo-people.cfg ,内容同yolov4-custom.cfg,然后修改:
batch=64

subdivisions=16

max_batches = 6000   # 最小6000,应为classes*2000

steps=4800,5400    # max_batches的80% and 90%
width=416 height=416  # 或者32 的倍数

[yolo]下的
classes = 1   # yolov4_tiny 需要修改两个

[convolutional]   # 修改两次
size=1
stride=1
pad=1
filters=18   # classes+5) *3
activation=linear
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  1. 在darknet/data 目录下建立people.data, people.names 两个文件,另一个。。。conv.29 是预训练的权重(需要去原文下载)。。。还有那个 train.txt 文件,生成方式见代码
    在这里插入图片描述
    内容
people.names 
people   # 写个类名就行

people.data
classes = 1
train = data/train.txt
valid = data/test.txt     
names = data/people.names
backup = backup/

train.txt
data/obj/2008_002516.jpg    # 图片的路径
data/obj/000005.jpg
data/obj/000007.jpg
data/obj/000009.jpg
data/obj/000012.jpg
data/obj/000016.jpg
data/obj/000017.jpg
data/obj/000019.jpg
data/obj/000020.jpg
data/obj/000021.jpg
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

搞数据集

用的VOC2007+2012,把其中people挑出来就行了。。。。

在这里插入图片描述
将图片放到data/obj目录下,那些.txt 就是标签格式就是0 0.523 0.5915915915915916 0.182 0.6306306306306306 类别(只有一个类就全是0了) 空格 <x_center> <y_center> <width> <height> 生成方式见代码:

参考:脑补链接

# 生成labels

import xml.etree.ElementTree as ET
import pickle
import os,random
from os import listdir, getcwd
from os.path import join

wd = getcwd() + '/VOC07+12'  # 修改为自己的路径
classes = ['person']

def convert(size, box):   # 转为<x_center> <y_center> <width> <height>
    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(wd+'/Annotations/%s.xml'%
        (image_id))
    out_file = open(wd+'/labels/%s.txt'%
        (image_id), 'w')
    tree=ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)

    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:   # 非class跳过
            continue
        cls_id = classes.index(cls)
        xmlbox = obj.find('bndbox')
        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')  # 写入内容


xmlfilepath=wd+'/Annotations'  # xml文件的路径
saveBasePath=wd+"/ImageSets/Main"
print(wd)

ftrainval = open(os.path.join(saveBasePath,'trainval.txt'), 'w')  
ftest = open(os.path.join(saveBasePath,'test.txt'), 'w')  
ftrain = open(os.path.join(saveBasePath,'train.txt'), 'w')  
fval = open(os.path.join(saveBasePath,'val.txt'), 'w')  

temp_xml = os.listdir(xmlfilepath)
total_xml = []
for xml in temp_xml:
    if xml.endswith(".xml"):
        total_xml.append(xml)
num=len(total_xml)  

trainval_percent=1
train_percent=1

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)  

for i  in list:  
    image_id = total_xml[i][:-4]    
    convert_annotation(image_id)
    
    name='data/obj/'+total_xml[i][:-4]+'.jpg'+'\n'   # 写入 train.txt 文件
    if i in trainval:  
        ftrainval.write(name)  
        if i in train:  
            ftrain.write(name)  
        else:  
            fval.write(name)  
    else:  
        ftest.write(name)  

ftrainval.close()  
ftrain.close()  
fval.close()  
ftest .close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91

这样数据集就搞好了,放到对应目录然后就可以开始训练了。

./darknet detector train data/people.data cfg/yolo-people.cfg data/yolov4-tiny.conv.29
训练好的权重

链接: https://pan.baidu.com/s/1beeIXFWeHp2-wmQ7vY-hcw 密码: gde6

话说训练的好快啊,一个小时就ok 了。。。。。。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/488348
推荐阅读
相关标签
  

闽ICP备14008679号