发布于2019-08-15 11:59 阅读(1101) 评论(0) 点赞(5) 收藏(1)
git clone https://github.com/ultralytics/yolov3.git
pip install -r requirements.txt
在data文件下建立上面三个文件(Annotations、images与ImageSets,labels后续我们脚本生成)其中Annotations存放xml文件,images图像,ImageSets新建Main文件存放train与test文件(脚本生成),labeles是标签文件
划分训练集与测试集(为了充分利用数据集我们只划分这两个),生成的在ImageSets / Main文件下
- import os
- import random
-
- trainval_percent = 0.2 #可自行进行调节
- train_percent = 1
- 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()
建立voc_labels文件生成labels标签文件
- import xml.etree.ElementTree as ET
- import pickle
- import os
- from os import listdir, getcwd
- from os.path import join
-
- sets = ['train', 'test']
-
- classes = ['apple','orange'] #自己训练的类别
-
-
- def convert(size, box):
- dw = 1. / size[0]
- dh = 1. / size[1]
- 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_id):
- in_file = open('data/Annotations/%s.xml' % (image_id))
- out_file = open('data/labels/%s.txt' % (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').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 image_set in sets:
- if not os.path.exists('data/labels/'):
- os.makedirs('data/labels/')
- image_ids = open('data/ImageSets/Main/%s.txt' % (image_set)).read().strip().split()
- list_file = open('data/%s.txt' % (image_set), 'w')
- for image_id in image_ids:
- list_file.write('data/images/%s.jpg\n' % (image_id))
- convert_annotation(image_id)
- list_file.close()
在data目录下新建fruit.data,配置训练的数据,fruit.name预测的类别名字
- classes=2
- train=data/train.txt
- valid=data/test.txt
- names=data/fruit.names
-
具体cfg参数的意义可以参考我的博客
- [net]
- # Testing
- #batch=1
- #subdivisions=1
- # Training
- batch=64
- subdivisions=16
- width=416
- height=416
- channels=3
- momentum=0.9
- decay=0.0005
- angle=0
- saturation = 1.5
- exposure = 1.5
- hue=.1
-
- learning_rate=0.001
- burn_in=1000
- max_batches = 50000
- policy=steps
- steps=4000,45000
- scales=.1,.1
-
- [convolutional]
- batch_normalize=1
- filters=32
- size=3
- stride=1
- pad=1
- activation=leaky
-
- # Downsample
-
- [convolutional]
- batch_normalize=1
- filters=64
- size=3
- stride=2
- pad=1
- activation=leaky
-
- [convolutional]
- batch_normalize=1
- filters=32
- size=1
- stride=1
- pad=1
- activation=leaky
-
- 。。。。。
-
- [convolutional]
- size=1
- stride=1
- pad=1
- filters=21 #3*(类别数+4+1)
- 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=2 #类别数
- num=9
- jitter=.3
- ignore_thresh = .7
- truth_thresh = 1
- random=1
-
-
- [route]
- layers = -4
-
- [convolutional]
- batch_normalize=1
- filters=256
- size=1
- stride=1
- pad=1
- activation=leaky
-
- [upsample]
- stride=2
-
- [route]
- layers = -1, 61
-
-
-
- [convolutional]
- batch_normalize=1
- filters=256
- size=1
- stride=1
- pad=1
- activation=leaky
-
- [convolutional]
- batch_normalize=1
- size=3
- stride=1
- pad=1
- filters=512
- activation=leaky
-
- [convolutional]
- batch_normalize=1
- filters=256
- size=1
- stride=1
- pad=1
- activation=leaky
-
- [convolutional]
- batch_normalize=1
- size=3
- stride=1
- pad=1
- filters=512
- activation=leaky
-
- [convolutional]
- batch_normalize=1
- filters=256
- size=1
- stride=1
- pad=1
- activation=leaky
-
- [convolutional]
- batch_normalize=1
- size=3
- stride=1
- pad=1
- filters=512
- activation=leaky
-
- [convolutional]
- size=1
- stride=1
- pad=1
- filters=21 #3*(类别数+4+1)
- 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=2 #类别数
- num=9
- jitter=.3
- ignore_thresh = .7
- truth_thresh = 1
- random=1
-
-
-
- [route]
- layers = -4
-
- [convolutional]
- batch_normalize=1
- filters=128
- size=1
- stride=1
- pad=1
- activation=leaky
-
- [upsample]
- stride=2
-
- [route]
- layers = -1, 36
-
-
-
- [convolutional]
- batch_normalize=1
- filters=128
- size=1
- stride=1
- pad=1
- activation=leaky
-
- [convolutional]
- batch_normalize=1
- size=3
- stride=1
- pad=1
- filters=256
- activation=leaky
-
- [convolutional]
- batch_normalize=1
- filters=128
- size=1
- stride=1
- pad=1
- activation=leaky
-
- [convolutional]
- batch_normalize=1
- size=3
- stride=1
- pad=1
- filters=256
- activation=leaky
-
- [convolutional]
- batch_normalize=1
- filters=128
- size=1
- stride=1
- pad=1
- activation=leaky
-
- [convolutional]
- batch_normalize=1
- size=3
- stride=1
- pad=1
- filters=256
- activation=leaky
-
- [convolutional]
- size=1
- stride=1
- pad=1
- filters=21 # 3*(类别数+4+1)
- 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=2 #自己的类别
- num=9
- jitter=.3
- ignore_thresh = .7
- truth_thresh = 1
- random=1
python train.py --data data/fruit.data --cfg cfg/yolov3.cfg --epochs 10 #后面的epochs自行更改
python detect.py --data-cfg data/fruit.data --cfg cfg/yolov3.cfg --weights weights/best.pt
python test.py --data data/fruit.data --cfg cfg/yolov3.cfg --weights weights/latest.pt
python -c "from utils import utils; utils.plot_results()"
未完待续。。。
链接:https://www.pythonheidong.com/blog/article/36016/501d003e23c9b9d2f325/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!