【TFlite】从tensorflow模型训练到lite模型移植
前⾔
本⽂使⽤tensorflow下的ssdlite-mobilenet v2物体检测模型,并转换为tflite模型,并完成测试
1. 安装 TensorFlow Object Detection API
1.1 下载tensorflow-master和models-master
1.2 安装依赖项、编译⼯具
pip install matplotlib pillow lxml Cython pycocotools
sudo apt-get install protobuf-compiler
1.3 使⽤proto编译
cd models/research/
protoc object_detection/protos/*.proto --python_out=.
1.4 添加环境变量
在.bashrc中添加环境变量,路径根据实际情况补充完整,然后source更新环境变量
export PYTHONPATH=$PYTHONPATH:/.../models/research:/.../models/research/slim
1.5 测试models是否安装成功:
python object_detection/builders/model_builder_test.py
返回OK则OK
2. TF Record格式数据准备
使⽤label-image标注⼯具对样本进⾏标注,得到VOC格式数据。将所有的图⽚放⼊images/⽂件夹,标注得到的xml⽂件保存到merged_xml/⽂件夹内,并新建⽂件夹Annotations/。
2.1 训练集划分
新建train_test_split.py把xml数据集分为了train 、test、 validation三部分,并存储在Annotations⽂件夹中,train为训练集占76.5%,test为测试集10%,validation为验证集13.5%,train_test_split.py代码如下:
import os
import random
import time
import shutil
xmlfilepath=r'merged_xml'
saveBasePath=r"./Annotations"
trainval_percent=0.9
train_percent=0.85
total_xml = os.listdir(xmlfilepath)
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)
print("train and val size",tv)
print("train size",tr)
# print(total_xml[1])
start = time.time()
# print(trainval)
# print(train)
test_num=0
val_num=0
train_num=0
# for directory in ['train','test',"val"]:
# xml_path = os.path.wd(), 'Annotations/{}'.format(directory)) # if(not ists(xml_path)):
# os.mkdir(xml_path)
# # pyfile(filePath, newfile)
# print(xml_path)
for i in list:
name=total_xml[i]
# print(i)
if i in trainval: #train and val set
# ftrainval.write(name)
if i in train:
# ftrain.write(name)
# print("train")
# print(name)
# print("train: "+name+" "+str(train_num))
directory="train"
train_num+=1
xml_path = os.path.wd(), 'Annotations/{}'.format(directory)) if(not ists(xml_path)):
os.mkdir(xml_path)
filePath=os.path.join(xmlfilepath,name)
newfile=os.path.join(saveBasePath,os.path.join(directory,name))
else:
# fval.write(name)
# print("val")
# print("val: "+name+" "+str(val_num))
directory="validation"
xml_path = os.path.wd(), 'Annotations/{}'.format(directory)) if(not ists(xml_path)):
os.mkdir(xml_path)
val_num+=1
filePath=os.path.join(xmlfilepath,name)
newfile=os.path.join(saveBasePath,os.path.join(directory,name))
# print(name)
else: #test set
# ftest.write(name)
# print("test")
# print("test: "+name+" "+str(test_num))
directory="test"
xml_path = os.path.wd(), 'Annotations/{}'.format(directory)) if(not ists(xml_path)):
os.mkdir(xml_path)
test_num+=1
filePath=os.path.join(xmlfilepath,name)
newfile=os.path.join(saveBasePath,os.path.join(directory,name))
# print(name)
# End time
end = time.time()
seconds=end-start
print("train total : "+str(train_num))
print("validation total : "+str(val_num))
print("test total : "+str(test_num))
total_num=train_num+val_num+test_num
print("total number : "+str(total_num))
print( "Time taken : {0} seconds".format(seconds))
2.2 xml⽂件转换为csv中间⽂件,新建csvdata/⽬录存放⽣成的csv⽂件,代码如下:
import os
import glob
import pandas as pd
ElementTree as ET
def xml_to_csv(path):
xml_list = []
for xml_file in glob.glob(path + '/*.xml'):
tree = ET.parse(xml_file)
root = t()
# print(root)
print(root.find('filename').text)
for member in root.findall('object'):
value = (root.find('filename').text,
int(root.find('size')[0].text), #width
int(root.find('size')[1].text), #height
member[0].text,
int(member[4][0].text),
int(float(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():
for directory in ['train','test','validation']:
xml_path = os.path.wd(), 'Annotations/{}'.format(directory))
# image_path = os.path.wd(), 'merged_xml')
xml_df = xml_to_csv(xml_path)
# _csv('whsyxt.csv', index=None)
_csv('csvdata/tf_{}.csv'.format(directory), index=None)
print('Successfully converted xml to csv.')
main()
在csvdata/⽂件夹下⽣成训练、验证和测试的csv格式⽂件:
2.3 由csv格式数据⽣成tf record格式数据,新建generate_tfrecord.py脚本,并新建tfdata/⽂件夹,代码如下:
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import os
import io
import pandas as pd
import tensorflow as tf
from PIL import Image
from object_detection.utils import dataset_util
from object_detection.utils import label_map_utilpython处理xml文件
from collections import namedtuple
flags = tf.app.flags
flags.DEFINE_string('csv_input', '', 'Path to the CSV input')
flags.DEFINE_string('images_input', '', 'Path to the images input')
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
flags.DEFINE_string('label_map_path', '', 'Path to label map proto')
FLAGS = flags.FLAGS
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, label_map_dict, images_path):
with tf.gfile.GFile(os.path.join(
images_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(label_map_dict[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/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),
'image/format': dataset_util.bytes_feature(image_format),
'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
'image/object/class/label': dataset_util.int64_list_feature(classes),
}))
return tf_example
def main(_):
writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
label_map_dict = label__label_map_dict(FLAGS.label_map_path) images_path = FLAGS.images_input
examples = pd.read_csv(FLAGS.csv_input)
grouped = split(examples, 'filename')
for group in grouped:
tf_example = create_tf_example(group, label_map_dict, images_path)
writer.write(tf_example.SerializeToString())
writer.close()
output_path = FLAGS.output_path
print('Successfully created the TFRecords: {}'.format(output_path))
if __name__ == '__main__':
tf.app.run()
⽤法:
python generate_tfrecord.py \
--csv_input=./csvdata/tf_train.csv \
-
-images_input=images \
--output_path=./d \
--label_map_path=./label_map.pbtxt
类似地依次⽣成训练、验证和测试数据集:
3. 模型训练
3.1 创建label_map.pbtxt
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论