这两天看了YOLO相关论文,跑了以下YOLOv3的代码,以下为相关过程的一些总结,持续更新,9.19。
separate.py
import os
import shutil
import random
src_dir = 'dogTrainImg' #数据集源文件
obj_dir = 'dogTestImg' #测试图片目录
if not os.path.exists(obj_dir):
os.makedirs(obj_dir)
for root, dirs, files in os.walk(src_dir):
for file in files:
file_name = str(file)
oldAdd = src_dir + '/' + file_name
newAdd = obj_dir + '/' + file_name
rad = random.randint(0,99) #设计随机数
if rad <= 30: #约取30%的图片,移动到目标文件为测试集
shutil.move(oldAdd, newAdd)
moveXml.py
import os
import shutil
src_dir = 'dogTrainXml' #源标签目录
img_dir = 'dogTestImg' #测试图片目录
obj_dir = 'dogTestXml' #测试标签目录
if not os.path.exists(obj_dir):
os.makedirs(obj_dir)
list1 = [] #测试图片id
for root, dirs, files in os.walk(img_dir):
for file in files:
fileName = str(file)
index = fileName.rfind('.') #去后缀
fileName = fileName[:index]
list1.append(fileName)
list2 = [] #源标签id
for root, dirs, files in os.walk(src_dir):
for file in files:
fileName = str(file)
index = fileName.rfind('.')
fileName = fileName[:index]
list2.append(fileName)
oldAdd = src_dir + '/' + fileName + '.xml'
newAdd = obj_dir + '/' + fileName + '.xml'
findlist = [x for x in list1 if x in list2] #取名称相同的id(好像是废话)
if fileName in findlist: #把测试图片对于的标签移动到‘测试标签目录‘
shutil.move(oldAdd, newAdd)
xml
目录下的所有文件做成txt
路径索引:textGen.py
import os
src_dir = [('dogTrainXml'), ('dogTestXml')]
obj_dir = [('dogTrain'), ('dogTest')]
for sets in src_dir:
test = open(obj_dir[src_dir.index(sets)] + '.txt', 'w')
for root, dirs, files in os.walk(sets):
for file in files:
fileName = str(file)
index = fileName.rfind('.')
fileName = fileName[:index]
test.write(fileName + '\n')
print("%s is done" % (sets))
test.close()
voc_label.py
(修改)输出每张图片
Bounding-box
的(x, y, w, h)
归一化结果
输出
.jpg
文件的索引路经
#coding:utf-8
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets=[('dogTrain'), ('dogTest')]
classes = ["dog"]
def convert(size, box):
dw = 1./size[0]
dh = 1./size[1]
#normalize
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_set,image_id):
in_file = open('%sXml/%s.xml' % (image_set, image_id))
print('ok')
out_file = open('labels/%slabs/%s.txt' % (image_set, image_id), 'w')
#read datas from in_file
print("up to now!")
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
#迭代、循环读取xml文件中类的信息
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
#(多目标情况下)类Id索引
cls_id = classes.index(cls)
#查找boundingBox
xmlbox = obj.find('bndbox')
#把boundingBox的位置信息存入b集合
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 image_set in sets:
if not os.path.exists('labels/'):
os.makedirs('labels/')
#如果分类目录不存在新建分类目录
if not os.path.exists('labels/%slabs/' % image_set):
os.makedirs('labels/%slabs/' % image_set)
#打开数据集路径
image_ids = open('%s.txt' % (image_set)).read().strip().split()
#写入归一化数据
list_file = open('labels/%s.txt' % (image_set), 'w')
for image_id in image_ids:
list_file.write('%s/%sXml/%s.jpg\n' % (wd, image_set, image_id))
convert_annotation(image_set, image_id)
list_file.close()
dogTrain.txt
和dogTest.txt
放到darknet/cfg
目录下,修改darknet/cfg/voc.data
——classes= 1
train = dogTrain.txt #这俩文件打开是每张图片的绝对路径
valid = dogTest.txt
names = data/voc.names
backup = backup #模型生成目录
darknet/data/voc.names
——dog
dogTrainlabs
里面的.txt
文件放入dogTrainImg
目录——mv dogTrainlabs/*.txt ../dogTrainImg
.jpg
文件和id
对应的.txt
文件。不过,还需要修改darknet/cfg/yolov3-voc.cfg
,主要是三块儿——[convolutional]
size=1
stride=1
pad=1
filters=18 #修改这里,数量为(classes + 5) * 3,故单类为18,下同
activation=linear
[yolo]
mask = 6,7,8
anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326
classes=1 #修改这里为单类,下同
num=9
jitter=.3
ignore_thresh = .5
truth_thresh = 1
random=1
[convolutional]
size=1
stride=1
pad=1
filters=18 #修改这里
activation=linear
[yolo]
mask = 3,4,5
anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326
classes=1 #修改这里
num=9
jitter=.3
ignore_thresh = .5
truth_thresh = 1
random=1
[convolutional]
size=1
stride=1
pad=1
filters=18 #修改这里
activation=linear
[yolo]
mask = 0,1,2
anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326
classes=1 #修改这里
num=9
jitter=.3
ignore_thresh = .5
truth_thresh = 1
random=1
darknet/examples/darknet.c
第441
行——test_detector("cfg/voc.data", argv[2], argv[3], filename, thresh, .5, outfile, fullscreen); #将coco.data换为voc.data,用于后续测试标注
wget https://pjreddie.com/media/files/darknet53.conv.74
GPU
训练如下——./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74 -gpus 0,1,2,3
./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc.backup -gpus 0,1,2,3
./darknet detect cfg/yolov3-voc.cfg backup/yolov3-voc_900.weights data/dog.jpg
nohup ./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74 -gpus 0,1,2,3
./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc.backup -gpus 0,1,2,3 >log.file 2>&1 &
ps -aux|grep darknet
Id
——ps -aux|grep darknet | grep -v grep | awk '{print $2}'
kill -9 Id
kill -CONT Id
跑完测试发现仍存在以下问题
1、Bounding-box的位置对于有遮挡的情况时,不是很准确
2、小目标仍然存在识别不到的情况
暂且就这么多,不知道是不是我数据集的问题,希望能够与各位看官交流交流。