主要针对几个方面:
一、标注数据集(自己用的labelimg,注意事项,有些朋友用的其他的标注软件,但是需要注意标注出来的XML格式和labelimg的XMl可能有区别)
二、数据转换tfrecord
三、训练ckpt模型
四、转换pb模型
五、测试pb模型
六、转换tflite模型
七、部署安卓
一、标注数据集
1、下载安装labelimg,下载地址如下(安装使用方法自行百度):
https://github.com/tzutalin/labelImg
2、贴出两种工具标注出不同的XML,大家注意(问题:1、区别自己分析后面转换为TXT、CSV等过段格式容易出问题,别混 着。2、标注时,标注框坐标注意别标出为负数。3、修改name、path等内容时注意别多加):
#第一种labelimg标注
<?xml version="1.0" ?><annotation>
<folder>phone</folder>
<filename>5.jpg</filename>
<path>/home/hanqing/SSD-Tensorflow-master/VOC2019/JPEGImages/phone/5.jpg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>416</width>
<height>416</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>phone</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>117</xmin>
<ymin>72</ymin>
<xmax>352</xmax>
<ymax>316</ymax>
</bndbox>
</object>
#第二种标注精灵标注
<?xml version="1.0" ?><doc>
<path>/home/hanqing/SSD-Tensorflow-master/VOC2019/JPEGImages/phone/sj1286.jpg</path>
<outputs>
<object>
<item>
<name>camera</name>
<bndbox>
<xmin>125</xmin>
<ymin>97</ymin>
<xmax>132</xmax>
<ymax>103</ymax>
</bndbox>
</item>
<item>
<name>camera</name>
<bndbox>
<xmin>123</xmin>
<ymin>118</ymin>
<xmax>129</xmax>
<ymax>125</ymax>
</bndbox>
</item>
<item>
<name>phone</name>
<bndbox>
<xmin>23</xmin>
<ymin>69</ymin>
<xmax>197</xmax>
<ymax>359</ymax>
</bndbox>
</item>
</object>
</outputs>
<time_labeled>1577951027395</time_labeled>
<labeled>true</labeled>
<size>
<width>416</width>
<height>416</height>
<depth>3</depth>
</size>
</doc>
二、数据转换tfrecord
1、在目录VOC2019/下新建py文件:train_test_split.py内容如下
#新建py文件:train_test_split.py
#内容如下:
#-*-coding:utf-8-*-
import os
import random
import time
import shutil
xmlfilepath = './xml'
saveBasePath = "./Annotations" #存区分xml文件位置
trainval_percent = 0.9 #测试/训练比例
train_percent = 0.85
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.join(os.getcwd(), 'Annotations/{}'.format(directory))
# if(not os.path.exists(xml_path)):
# os.mkdir(xml_path)
# # shutil.copyfile(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.join(os.getcwd(), 'Annotations/{}'.format(directory))
if (not os.path.exists(xml_path)):
os.mkdir(xml_path)
filePath = os.path.join(xmlfilepath, name)
newfile = os.path.join(saveBasePath, os.path.join(directory, name))
shutil.copyfile(filePath, newfile)
else:
# fval.write(name)
# print("val")
# print("val: "+name+" "+str(val_num))
directory = "validation"
xml_path = os.path.join(os.getcwd(), 'Annotations/{}'.format(directory))
if (not os.path.exists(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))
shutil.copyfile(filePath, newfile)
# print(name)
else: # test set
# ftest.write(name)
# print("test")
# print("test: "+name+" "+str(test_num))
directory = "test"
xml_path = os.path.join(os.getcwd(), 'Annotations/{}'.format(directory))
if (not os.path.exists(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))
shutil.copyfile(filePath, newfile)
# 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 : "