水果识别
水果识别-搭建框架
水果识别-数据集制作
水果识别-训练
水果识别-测试
训练包括:
- 更改voc_label.py生成label文件
- 修改要训练的网络配置文件
- 修改voc.names
- 修改voc.data
- 训练
- 更改voc_label.py生成label文件,该文件在/darknet/scripts/目录下
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758import xml.etree.ElementTree as ETimport pickleimport osfrom os import listdir, getcwdfrom os.path import join# sets=[('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test')]# classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]sets=[('2007', 'train')] 这里只用到训练,数据集命名为VOC2017classes = ["apple", "pear", "banana"] 只用了三个类别作为展示def convert(size, box):dw = 1./(size[0])dh = 1./(size[1])x = (box[0] + box[1])/2.0 - 1y = (box[2] + box[3])/2.0 - 1w = box[1] - box[0]h = box[3] - box[2]x = x*dww = w*dwy = y*dhh = h*dhreturn (x,y,w,h)def convert_annotation(year, image_id):in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml'%(year, image_id))out_file = open('VOCdevkit/VOC%s/labels/%s.txt'%(year, 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').textcls = obj.find('name').textif cls not in classes or int(difficult)==1:continuecls_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('VOCdevkit/VOC%s/labels/'%(year)):os.makedirs('VOCdevkit/VOC%s/labels/'%(year))image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split()list_file = open('%s_%s.txt'%(year, image_set), 'w')for image_id in image_ids:list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg\n'%(wd, year, image_id))convert_annotation(year, image_id)list_file.close()
修改好后执行python voc_label.py
在scripts/VOCdevkit/VOC2007目录下生成了labels文件夹,文件夹里面长这个样子的:同时会在/darknet/scripts目录下生成2007_train.txt文件
- 配置文件的修改,我用到的yolov3,所以就修改/cfg/yolove-voc.cfg
123456789101112131415161718192021222324252627282930313233343536373839404142434445[net]# Testing# batch=1# subdivisions=1# Training # 训练的时候把Testing的参数注释掉,把Training的参数取消注释batch=64subdivisions=16width=416height=416channels=3momentum=0.9decay=0.0005angle=0saturation = 1.5exposure = 1.5hue=.1learning_rate=0.001 # 根据需要修改学习率,就是梯度下降的速率,越大训练速度越快但会牺牲准确率burn_in=1000max_batches = 100000 # 训练的总次数policy=stepssteps=100,10000,70000,80000,90000 # 训练到相应次数后学习率变化scales=10,10,.1,.1,.1 # 学习率变化情况,是累乘操作......[convolutional]size=1stride=1pad=1filters=24 # filters = 3 × (calsses + 5)# yolov2和tiny-yolov2中为 filter= num ×(classes + coords + 1)activation=linear[yolo]mask = 0,1,2anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326classes=3 # 修改类别数量num=9jitter=.3ignore_thresh = .5truth_thresh = 1random=1 # 为1时会启用Multi-Scale Training,随机使用不同尺寸的图片进行训练,显存小可以置为0 - 修改voc.names,该文件在/darknet/data目录下
1234applepearbanana
该文件下记录所有类别名称。 - 修改voc.data,该文件在/darknet/cfg目录下
123456classes= 3 //类别数量train = /home/embedded/lm/darknet/scripts/2007_train.txt //指定训练样本// valid = /home/pjreddie/data/voc/2007_test.txt 指定测试数据,训练用不到names = /home/embedded/lm/darknet/data/voc.names //指向上一步修改的voc.names文件backup = /home/embedded/lm/darknet/backup/yolov3_voc_weights //指定训练后的权值放在哪儿 - 训练不要与训练权重直接训练:
12./darknet detector train cfg/voc.data cfg/yolov3_voc.cfg
也可以下载预训练权重进行训练:
12./darknet detector train cfg/voc.data cfg/yolov3_voc.cfg yolo.weights
训练过程中可以随时停止,停止以后可以在保存的权重处接着开始训练,保存的权重就当做预训练权重,yolov3开始没训练100次保存一次权重,过1000以后每训练10000次保存一次权重。然后就等待漫长的训练过程……
最新评论
感谢博主,让我PyTorch入了门!
博主你好,今晚我们下馆子不?
博主,你的博客用的哪家的服务器。
您好,请问您对QNN-MO-PYNQ这个项目有研究吗?想请问如何去训练自己的数据集从而实现新的目标检测呢?
where is the source code ? bomb1 188 2 8 0 0 hello world 0 0 0 0 0 0 1 1 9?5
在安装qemu的过程中,一定在make install 前加入 sudo赋予权限。
所以作者你是训练的tiny-yolov3还是yolov3...
很有用