数据整理
KERAS-YOLOV3的GITHUB地址:https://github.com/yangchengtest/keras-yolo3
该项目支持的数据结构:
One row for one image;
Row format: image_file_path box1 box2 … boxN;
Box format: x_min,y_min,x_max,y_max,class_id (no space).
For VOC dataset, try python voc_annotation.py
Here is an example:
path/to/img1.jpg 50,100,150,200,0 30,50,200,120,3
path/to/img2.jpg 120,300,250,600,2
…
KITTI数据集:
http://www.cvlibs.net/datasets/kitti/eval_object.php?obj_benchmark
这里选取第1个图片集 Download left color images of object data set (12 GB)和标注文件 Download training labels of object data set (5 MB)
LABEL格式:
Car 0.00 0 -1.67 642.24 178.50 680.14 208.68 1.38 1.49 3.32 2.41 1.66 34.98 -1.60
关于数据定义,参考了这篇博文:
https://blog.csdn.net/jesse_mx/article/details/65634482
该数据集的标注一共分为8个类别:’Car’, ’Van’, ’Truck’, ’Pedestrian’, ’Person (sit- ting)’, ’Cyclist’, ’DontCare’ 和’Misc’
在代码中合并’Car’, ’Van’, ’Truck’为’Car’,合并 ’Pedestrian’, ’Person (sit- ting)’为’Pedestrian’。
最后实际运行还有三个分类:’Pedestrian’、 ’Person (sit- ting)’、’Pedestrian’。
这里主要使用:TYPE和BBOX两个参数
处理代码如下:
import glob
lable_list = glob.glob('data_object_label_2/training/label_2/*')
label_result=[]
type_list=[]
for label in label_list:
bbox=[]
with open(label) as r_label:
for each_line in r_label:
labeldata = each_line.strip().split(' ')
if labeldata[0] in ['Truck','Van','Tram']:
labeldata[0] = labeldata[0].replace(labeldata[0],'Car')
if labeldata[0] == 'Person_sitting':
labeldata[0] = labeldata[0].replace(labeldata[0],'Pedestrian')
if labeldata[0] == 'DontCare':
continue
if labeldata[0] == 'Misc':
continue
if labeldata[0] not in type_list:
type_list.append(labeldata[0])
bbox_item=[type_list.index(labeldata[0]),int(float(labeldata[4])),int(float(labeldata[5])),int(float(labeldata[6])),
int(float(labeldata[7]))]
bbox.append(bbox_item)
imgfilename="data_object_image_2/testing/image_2/"+label[37:44]+"png"
strline=imgfilename
for box in bbox:
strbox=" "+str(box[1])+","+str(box[2])+","+str(box[3])+","+str(box[4])+","+str(box[0])
strline=strline+strbox
label_result.append(strline)
try:
outputfilename="train.txt"
with open(outputfilename,'w+') as w_output:
for line in label_result:
w_output.write(line+"\n")
except IOError as ioerr:
print('File error:'+str(ioerr))
训练相关代码修改
annotation_path = 'train.txt'
log_dir = 'logs/000/'
classes_path = 'model_data/voc_classes.txt'
anchors_path = 'model_data/yolo_anchors.txt'
需要修改voc_classes.txt,保证分类与我们的模型一致。
按照GITHUB上的流程进行操作。
Make sure you have run python convert.py -w yolov3.cfg yolov3.weights model_data/yolo_weights.h5
The file model_data/yolo_weights.h5 is used to load pretrained weights.
Modify train.py and start training.
python train.py
Use your trained weights or checkpoint weights in yolo.py.
Remember to modify class path or anchor path.
最后得到训练后的模型。
定制修改
本来以为简单到爆表的操作,实际过程中发现有些地方需要修改。必须改的:
annotation_path = 'train.txt'
log_dir = 'logs/000/'
classes_path = 'model_data/voc_classes.txt'
anchors_path = 'model_data/yolo_anchors.txt'
self.model_path = 'model_data/trained_weights_final.h5'
self.anchors_path = 'model_data/yolo_anchors.txt'
self.classes_path = 'model_data/voc_classes.txt'
预训练模型修改
看一下网络框架,主要区别在于最右下角的CONVS层,YOLOV3官网提供的预训练模型是基于COCO 80分类的,而本文的框架只有3个分类,那么在预加载模型的时候就需要进行修改。
(ps:你也可以不改,这三层就直接重新训练。不过使用修改后预训练模型收敛更快)
注意:如果你修改最后三层filters,而convert.py不对应修改,整个预训练模型加载都会是错误的
本开源框架是使用了CONVERT.PY文件进行weights转化。
通过读取yolov3.cfg文件,按字节顺序读取。有需要的可以了解一下configparser,我个人只是大概看了一下。
读取预加载模型的需要按照80分类来读取预加载模型,但是写入模型的时候需要按照3分类的方式写入。
硬编码了一下,filters等于255的时候,进行强制转化。。。
有需要的可以参考我提交的GITHUB:
https://github.com/yangchengtest/keras-yolov3-kitti.git
剩下的就和原来一样了,可以直接训练了。
LR使用了
reduce_lr = ReduceLROnPlateau(monitor=’val_loss’, factor=0.1, patience=3, verbose=1)
可以关注一下。
最后修改YOLO.PY的配置文件,就可以看到结果了。
图片批量处理
原来的接口,图片一张一张看,我嫌烦。
改了一下,支持多图片批量处理。
python yolo.py -s test_images -d output_images
视频写入有问题
需要在CV读取完成后释放资源:
return_value, frame = vid.read()
if not return_value:
break
本项目的GITHUB在:
https://github.com/yangchengtest/keras-yolov3-kitti.git
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)