赞
踩
本次的YOLO v3实战是基于DataFountain的一个比赛:智能盘点—钢筋数量AI识别,baseline model就选用上次讲解YOLO v3理论YunYang复现的YOLO v3。本次系列也和正常我们做比赛的流程一样分为两部分,这次也是第一部分将会带大家跑通baseline(比赛的话可能会对比多个,这里仅跑YOLO v3),第二部分将会分析baseline出现的问题结合赛题背景进行改进。
import os
train_file=open('data/train_data_VOC/ImageSets/Main/train.txt','w')
test_file=open('data/test_VOC/ImageSets/Main/test.txt','w')
for _,_,train_files in os.walk('data/train_data_VOC/JPEGImages'):
continue
for _,_,test_files in os.walk('data/test_VOC/JPEGImages'):
continue
for file in train_files:
train_file.write(file.split('.')[0]+'\n')
for file in test_files:
test_file.write(file.split('.')[0]+'\n')
import csv import os, sys from glob import glob from PIL import Image src_img_dir = r'data/train_data_VOC/JPEGImages'#图片地址 src_txt_dir = r'data/yolo'#生成txt地址 img_lists = glob(src_img_dir + '/*jpg') img_basenames = [] for item in img_lists: img_basenames.append(os.path.basename(item)) img_names = [] for item in img_basenames: temp1, temp2 = os.path.splitext(item) img_names.append(temp1) c = [] filename = r'/home/cristianoc/tensorflow-yolov3/data/yolo/train.csv' with open(filename) as f: reader = csv.reader(f) head_now = next(reader) l = [] b = [] for cow in reader: label = cow[0] l.append(label) bbox = cow[1] b.append(bbox) label = [] for item in l: temp1, temp2 = os.path.splitext(item) label.append(temp1) for img in img_names: img_file = src_txt_dir + os.sep + img + '.txt' fp = open(img_file, 'w') for i in range(len(label)): if label[i] == img: fp.write(str(b[i])) fp.write('\n')
import csv import os, sys from glob import glob from PIL import Image src_img_dir = r'data/train_data_VOC/JPEGImages' src_txt_dir = r'data/yolo' src_xml_dir = r'data/train_data_VOC/Annotations' img_lists = glob(src_img_dir + '/*jpg') img_basenames = [] for item in img_lists: img_basenames.append(os.path.basename(item)) img_names = [] for item in img_basenames: temp1, temp2 = os.path.splitext(item) img_names.append(temp1) for img in img_names: im = Image.open((src_img_dir + os.sep + img + '.jpg')) width, height = im.size gt = open(src_txt_dir + os.sep + img + '.txt').read().splitlines() xml_file = open((src_xml_dir + os.sep + img + '.xml'), 'w') xml_file.write('<annotation>\n') xml_file.write(' <folder>VOC2007</folder>\n') xml_file.write(' <filename>' + str(img) + '.jpg' + '</filename>\n') xml_file.write(' <size>\n') xml_file.write(' <width>' + str(width) + '</width>\n') xml_file.write(' <height>' + str(height) + '</height>\n') xml_file.write(' <depth>3</depth>\n') xml_file.write(' </size>\n') for img_each_label in gt: spt = img_each_label.split(' ') xml_file.write(' <object>\n') xml_file.write(' <name>' + str('rebar') + '</name>\n') xml_file.write(' <pose>Unspecified</pose>\n') xml_file.write(' <truncated>0</truncated>\n') xml_file.write(' <difficult>0</difficult>\n') xml_file.write(' <bndbox>\n') xml_file.write(' <xmin>' + str(spt[0]) + '</xmin>\n') xml_file.write(' <ymin>' + str(spt[1]) + '</ymin>\n') xml_file.write(' <xmax>' + str(spt[2]) + '</xmax>\n') xml_file.write(' <ymax>' + str(spt[3]) + '</ymax>\n') xml_file.write(' </bndbox>\n') xml_file.write(' </object>\n') xml_file.write('</annotation>')
python scripts/voc_annotation.py --data_path data/test_VOC
分别生成我们的训练标注文件和验证标注文件,这样我们的数据就准备好了。config.py
中修改我们读入的类别路径:__C.YOLO.CLASSES = "./data/classes/class.names"
config.py
下修改对应训练标注文件和验证标注文件的路径,改为刚才生成好的即可。__C.YOLO.ORIGINAL_WEIGHT
是convert_weights.py
转换权重文件的源文件,__C.YOLO.DEMO_WEIGHT
是转换后生成的目标权重文件(用于将在COCO预训练好的权重文件转换后生成预训练模型)__C.YOLO.DEMO_WEIGHT
就是预训练模型。python convert_weight.py --train_from_coco
开始转换:Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。