AI学习(五):⽤tensorflowobjectdetection训练⾃⼰的数据集0.前⾔
本章,我们来制作周杰伦的图⽚检测,先看⼀下效果
检测效果还是不错的,这⾥我使⽤了100张train图⽚和50张test图⽚进⾏标注(标注图⽚的时候尽量清楚的,不要⼀些犄⾓旮旯的,我吃过亏的,训练效果很不好),训练步数40000+,batch size=24。接下来我们开始吧。
1.labellmg的下载和使⽤
2.⽣成CSV⽂件
打开spyder,新建⽂件,将下⾯这段代码输⼊进去,重命名为xml_to_csv
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 16 00:52:02 2018
@author: Xiang Guo
"""
import os
import glob
import pandas as pd
ElementTree as ET
os.chdir('F:\\ML\\models-master\\research\\object_detection\\images\\train')
path = 'F:\\ML\\models-master\\research\\object_detection\\images\\train'
def xml_to_csv(path):
xml_list = []
for xml_file in glob.glob(path + '/*.xml'):
tree = ET.parse(xml_file)
root = t()
for member in root.findall('object'):
value = (root.find('filename').text,
int(root.find('size')[0].text),
int(root.find('size')[1].text),
member[0].text,
spyder怎么用int(member[4][0].text),
int(member[4][1].text),
int(member[4][2].text),
int(member[4][3].text)
)
xml_list.append(value)
column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']    xml_df = pd.DataFrame(xml_list, columns=column_name)
return xml_df
def main():
image_path = path
xml_df = xml_to_csv(image_path)
_csv('zjl_train.csv', index=None)
print('Successfully converted xml to csv.')
main()
绿⾊的路径是要对应到你的train⽂件和test⽂件的,⽣成train的csv时候是…\train,⽣成test的csv⽂件的时候是…\test.
这⾥选择你想⽣成的⽂件的名字。点击下图标红的按钮。
最终我在train⽂件夹中⽣成了 zjl_train.csv,在test⽂件夹中⽣成了zjl_test.csv。
3.⽣成record⽂件。
把刚刚⽣成的train.csv和test.csv⽂件复制到object_detection⽂件下的data⽂件中
打开spyder,复制下⾯代码,保存命名为generate_tfr.py。
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 16 01:04:55 2018
@author: Xiang Guo
"""
"""
Usage:
# From tensorflow/models/
# Create train data:
python generate_tfr.py --csv_input=data/zjl_train.csv  --output_path=d
# Create test data:
python generate_tfr.py --csv_input=data/zjl_test.csv  --output_path=d
python generate_tfr.py --csv_input=data/zjl_test.csv  --output_path=d """
import os
import io
import pandas as pd
import tensorflow as tf
from PIL import Image
from object_detection.utils import dataset_util
from collections import namedtuple, OrderedDict
os.chdir('F:\\ML\\models-master\\research\\object_detection\\')
flags = tf.app.flags
flags.DEFINE_string('csv_input', '', 'Path to the CSV input')
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
FLAGS = flags.FLAGS
# TO-DO replace this with label map
def class_text_to_int(row_label):
if row_label == 'zjl':
return 1
else:
None
def split(df, group):
data = namedtuple('data', ['filename', 'object'])
gb = df.groupby(group)
return [data(filename, gb.get_group(x)) for filename, x in ups.keys(), gb.groups)]
def create_tf_example(group, path):
with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
encoded_jpg = ad()
encoded_jpg_io = io.BytesIO(encoded_jpg)
image = Image.open(encoded_jpg_io)
width, height = image.size
filename = de('utf8')
image_format = b'jpg'
xmins = []
xmaxs = []
ymins = []
ymaxs = []
classes_text = []
classes = []
for index, row in group.object.iterrows():
xmins.append(row['xmin'] / width)
xmaxs.append(row['xmax'] / width)
ymins.append(row['ymin'] / height)
ymaxs.append(row['ymax'] / height)
classes_text.append(row['class'].encode('utf8'))
classes.append(class_text_to_int(row['class']))
tf_example = tf.train.Example(ain.Features(feature={
'image/height': dataset_util.int64_feature(height),
'image/width': dataset_util.int64_feature(width),
'image/filename': dataset_util.bytes_feature(filename),
'image/source_id': dataset_util.bytes_feature(filename),
'image/encoded': dataset_util.bytes_feature(encoded_jpg),

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。