1.准备数据集labelimg进行图片标注把图片放入JPEGImages文件夹,xml放入Annotations文件夹
2.建立自定义数据文件夹myData
3.修改myData.names
我的 myData.names
person
fire
smoke
hat
4.进入cfg文件夹 修改cfg 文件,具体内容根据自己数据集来修改调参
5.cfg文件夹下建立my_data.data
我的my_data_data
classes= 4
train = ./myData/myData_train.txt
valid = ./myData/myData_test.txt
names = ./myData/myData.names
backup = ./myData
6.下载初始权重
7.运行myData目录下的test.py
import os
import random
trainval_percent = 0.1
train_percent = 0.9
xmlfilepath = 'Annotations'
txtsavepath = 'ImageSets\Main'
total_xml = os.listdir(xmlfilepath)
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)
ftrainval = open('ImageSets/Main/trainval.txt', 'w')
ftest = open('ImageSets/Main/test.txt', 'w')
ftrain = open('ImageSets/Main/train.txt', 'w')
fval = open('ImageSets/Main/val.txt', 'w')
for i in list:
name = total_xml[i][:-4] + '\n'
if i in trainval:
ftrainval.write(name)
if i in train:
ftest.write(name)
else:
fval.write(name)
else:
ftrain.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()
8.cd … python voc_label.py (按照自己的类修改)
#coding=utf-8
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
# 源代码sets=[('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test')]
sets = [('myData', 'train'),("myData","test")] # 改成自己建立的myData
classes = ["person", "fire", "smoke","hat"] # 改成自己的类别
list1 = []
def convert(size, box):
dw = 1. / (size[0])
dh = 1. / (size[1])
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)
def convert_annotation(year, image_id):
print(image_id)
in_file = open('myData/Annotations/%s.xml' % (image_id),encoding='utf-8') # 源代码VOCdevkit/VOC%s/Annotations/%s.xml
out_file = open('myData/labels/%s.txt' % (image_id), 'w') # 源代码VOCdevkit/VOC%s/labels/%s.txt
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:
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')
wd = getcwd()
for year, image_set in sets:
if not os.path.exists('myData/labels/'): # 改成自己建立的myData
os.makedirs('myData/labels/')
image_ids = open('myData/ImageSets/Main/%s.txt' % (image_set)).read().strip().split()
list_file = open('myData/%s_%s.txt' % (year, image_set), 'w')
for image_id in image_ids:
list_file.write('%s/myData/JPEGImages/%s.jpg\n' % (wd, image_id))
try:
convert_annotation(year, image_id)
except:
list1.append([image_id])
# input("::")
list_file.close()
print(list1)
9.vim Makefile
修改CUDA CUDNN OPENCV 为1
然后 make
10 训练./darknet detector train cfg/my_data.data cfg/yolov4.cfg yolov4.weights -dont_show -clear -gpus 0,1 -map
11.计算anchors ./darknet detector calc_anchors cfg/my_data.data -num_of_clusters 9 -width 512 -height 512 -show