系统:win10
环境:anaconda,python3.6,tensorflow-gpu1.13
材料准备:TensorFlow object detection API - r1.13,数据集
注:默认根目录为TensorFlow object detection API
参考文件:链接
提取码jlj8
1、api安装:
打开控制台或powershell(shift+鼠标右键可以看到),进入到research/slim,指令如下:
cd research/slim/
构建、执行安装文件
python setup.py build
python setup.py install
返回上一级,同样
cd ..
python setup.py build
python setup.py install
2、环境监测
执行环境监测脚本
protoc object_detection/protos/*.proto --python_out=.
python object_detection/builders/model_builder_test.py
pause
倘若出现如图OK字样,则表示环境部署完成
3、数据准备、模型制作
进入到object_detection/images文件夹,将准备好的数据集分别存放于test和train目录下,做测试集和训练集,建议测试集:训练集=3:7
相关配置文件
(倘若按提供的学习文件操作,则无需修改配置文件):
回到object_detection目录,执行如下脚本制作数据
python xml_to_csv.py
python train_generate_tfrecord.py
python test_generate_tfrecord.py
此时data目录下会生成对应的csv和tfrecord文件
执行以下脚本进行模型训练:
python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/ssd_mobilenet_v1_pets.config
!这里有配置信息需要个人按需求改,一般是修改分类数及类别,以下列出主要配置文件:
training/ssd_mobilenet_v1_pets.config:主要记录模型训练的配置,
num_steps
训练步数num_classes
分类数batch_size
一次喂入的数据量(不过电脑性能比较差可以调低点,超出电脑承受能力会报错)
training/object-detection.pbtxt:记录类别ID与类别名,格式如下:
item {
id: 1
name: 'example'
}
其中id为类别ID,唯一
name为对应的类别名
test_generate_tfrecord.py
train_generate_tfrecord.py:样本的测试和训练代码,其中都要改class_text_to_int
方法下的类名与返回的数值与你在training/object-detection.pbtxt设定的一致。部分代码如下:
def class_text_to_int(row_label):
if row_label == 'example':
return 1
else:
return 0
阶段性结果检验:
在当前目录下(research\object_detection)用jupyter notebook打开my_object_detection_tutorial.ipynb文件,把要检测的图片放到test_images文件夹下,在jupyter下run all,即可检验模型训练结果。
4、将模型转成PB文件
在research\object_detection下执行以下脚本:
python export_inference_graph.py --input_type image_tensor --pipeline_config_path training/ssd_mobilenet_v1_pets.config --trained_checkpoint_prefix training/model.ckpt-1000 --output_directory myFinalModelPB
即可在myFinalModelPB文件夹下保存对应的pb文件
5、将模型转成TFLite文件
在当前目录下,执行以下python代码,在当前目录下生成output_graph.tflite
import tensorflow as tf
in_path = r'H:\Projects\PC Projects\(face)models-master\research\object_detection\myFinalModelPB\frozen_inference_graph.pb'
out_path = r'H:\Projects\PC Projects\(face)models-master\research\object_detection\tflite_model\output_graph.tflite'
input_tensor_name = ['BFD']
input_tensor_shape = {'Placeholder': [1, 224, 224, 3]}
class_tensor_name = ['final_result']
convertr = tf.lite.TFLiteConverter.from_frozen_graph(in_path, input_arrays=input_tensor_name
, output_arrays=class_tensor_name
, input_shapes=input_tensor_shape)
# convertr=tf.lite.TFLiteConverter.from_saved_model(saved_model_dir=in_path,input_arrays=[input_tensor_name],output_arrays=[class_tensor_name])
tflite_model = convertr.convert()
with open(out_path, 'wb') as f:
f.write(tflite_model)
tflite生成参考自博客https://blog.csdn.net/SpiritYzw/article/details/105629397
感谢大佬!
如有错误欢迎批评指正!