darknet训练yolov3时的一些注意事项

训练需要用到的文件:

1)       .data文件。该文件包含一些配置信息,具体为训练的总类别数,训练数据和验证数据的路径,类别名称,模型存放路径等。

例如coco.data

classes= 80 # 训练总类别数
train  = /newdata/hejunlin/code/darknet/data/coco/trainvalno5k.txt #训练数据路径
valid = /newdata/hejunlin/code/darknet/data/coco/5k.txt #验证集路径
names = data/coco.names #每一类名称
backup = backup/ #模型存放路径
eval=coco

需要用到训练数据trainvalno5k.txt和验证数据5k.txt。

其中,这两个文件内容为训练/验证图片的路径,每一行为一张图像的路径。部分内容如下:

/home/xxx/code/darknet/data/coco/images/val2014/COCO_val2014_000000000164.jpg
/home/xxx/code/darknet/data/coco/images/val2014/COCO_val2014_000000000192.jpg
/home/xxx/code/darknet/data/coco/images/val2014/COCO_val2014_000000000283.jpg

上面的为训练/验证图片数据的路径,每一张图片对应的标签,即每张图片中包含物体的bbox信息存放在txt文件中,txt文件名与图片的文件名一致。标注txt文件内容如下:

44 0.3704921875 0.6309484777517563 0.06092187500000001 0.13524590163934427
67 0.2723671875 0.781311475409836 0.5416093750000001 0.4373770491803278
1 0.6927578125 0.4888290398126464 0.170953125 0.6501639344262296
49 0.229265625 0.6178571428571429 0.034875 0.06742388758782202
51 0.10209375000000001 0.8534309133489462 0.10643750000000002 0.09562060889929742
51 0.1526015625 0.7213348946135831 0.11885937499999999 0.09672131147540983
79 0.15256250000000002 0.5000351288056206 0.300875 0.23037470725995318

其中,每一行表示图片中一个object的类别号和bbox信息。Bbox保存的形式为(x,y,w,h)

分别表示object归一化后中心点坐标,宽度和高度。

注意:

在读入训练数据时,只给程序输入了图片所在路径,而标签数据的路径并没有直接给,是通过对图片路径进行修改得到的,比如在训练coco数据时,输入的trainvalno5k.txt文件中只包含所有图片的具体路径,如:

/home/xxx/code/darknet/data/coco/images/val2014/COCO_val2014_000000000164.jpg

而COCO_val2014_000000000164.jpg的标签并没有给程序,是通过该函数替换掉图片路径中的images为labels,并替换掉后缀.jpg为.txt得到的,最终得到:

/home/xxx/code/darknet/data/coco/labels/val2014/COCO_val2014_000000000164.txt

这种替换的前提是,标签数据文件夹labels与图片数据文件夹images具有相同的父目录。

         另外,直接把txt标签文件放在与图片同一路径下也没问题。

         详细信息可以查看源码src/data.c文件中find_replace函数。

2)       .cfg文件

主要包含训练的一些配置信息,如输入图像大小、学习率、数据增强等。还包括训练的网络结结构。

附:coco的json格式标注信息转换为darknet训练的标注txt文件。

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

from __future__ import print_function
import os, sys, zipfile
import numpy as np
import json

def convert(size, box):
    dw = 1./(size[0])
    dh = 1./(size[1])
    x = box[0] + box[2] / 2.0
    y = box[1] + box[3] / 2.0
    w = box[2]
    h = box[3]
    #x = (box[0] + box[1])/2.0 - 1
    #y = (box[2] + box[3])/2.0 - 1
    #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)


json_file='coco/coco2017/annotations/instances_val2017.json' # # Object Instance 类型的标注


data=json.load(open(json_file,'r'))

ana_txt_save_path = "./val_coco2017/"
if not os.path.exists(ana_txt_save_path):
    os.makedirs(ana_txt_save_path)

for img in data['images']:
    print(img["id"])
    #print(img["file_name"])
    filename = img["file_name"]
    img_width = img["width"]
    img_height = img["height"]
    #print(img["height"])
    #print(img["width"])
    img_id = img["id"]
    ana_txt_name = filename.split(".")[0] + ".txt"
    f_txt = open(os.path.join(ana_txt_save_path, ana_txt_name), 'w')
    for ann in data['annotations']:
        if ann['image_id']==img_id:
            #annotation.append(ann)
            #print(ann["category_id"], ann["bbox"])
            box = convert((img_width,img_height), ann["bbox"])
            f_txt.write("%s %s %s %s %s\n"%(ann["category_id"], box[0], box[1], box[2], box[3]))
    f_txt.close()
    break

附:

Darknet YOLO 训练问题集锦

猜你喜欢

转载自www.cnblogs.com/hejunlin1992/p/9925293.html