继续训练篇:
(笔者这里是ssd_mobilenet_v2_quantized_300x300_coco.config)
修改    fine_tune_checkpoint: '/models-master/logs/turnout/model.ckpt-324833'
然后执行训练语句继续训练

Windows篇
1. Anaconda
2.下载Tensorflow model、 SSD预训练模型和文件库
链接1(Tensorflow model)https://github.com/tensorflow/models/tree/r1.13.0
解压到
C:\tensorflow1
并重命名为models

链接2(ssd) http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_quantized_300x300_coco_2019_01_03.tar.gz
下载解压到
C:\tensorflow1\models\research\object_detection

链接3(文件库)
https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10 下载解压到
C:\tensorflow1\models\research\object_detection

链接4(protuf)https://github.com/google/protobuf/releases
安装protuf  (非常容易出错,我这里选择的是win版 3.4)         将bin文件夹中的【protoc.exe】放到C:\Windows 并cmd进入models\research\目录下
protoc object_detection/protos/*.proto --python_out=.

3. 设置conda虚拟环境

C:\>  conda create -n tensorflow1 pip python=3.6.7
 C:\>  activate tensorflow1
 (tensorflow1) C:\> conda install tensorflow-gpu=1.13(使用官方model则tensorflow-gpu=1.15.0)
 (tensorflow1) C:\> conda install -c anaconda protobuf
 (tensorflow1) C:\> pip install pillow
 (tensorflow1) C:\> pip install lxml
 (tensorflow1) C:\> pip install Cython
 (tensorflow1) C:\> pip install contextlib2
 (tensorflow1) C:\> pip install jupyter
 (tensorflow1) C:\> pip install matplotlib
 (tensorflow1) C:\> pip install pandas
 (tensorflow1) C:\> pip install opencv-python

设置python环境变量
(tensorflow1) C:\> set PYTHONPATH=C:\tensorflow1\models;C:\tensorflow1\models\research;C:\tensorflow1\models\research\slim

4. 编译Protobuf
(tensorflow1) C:\> cd C:\tensorflow1\models\research
(tensorflow1) C:\tensorflow1\models\research>protoc object_detection/protos/*.proto --python_out=.

如果是tf2,python setup.py install有好多坑。。。。

然后找到setup.py文件(在C:\tensorflow1\models\research\object_detection\packages\tf1里面的setup.py文件install下载的东西我们上面已经下载过了,复制到research再执行一遍也行,主要是C:\tensorflow1\models\research\slim里面的setup.py),复制到C:\tensorflow1\models\research

执行:

(tensorflow1) C:\tensorflow1\models\research> python setup.py build
 (tensorflow1) C:\tensorflow1\models\research> python setup.py install

5. 标注图片
将训练图片和测试图片分别放在

C:\tensorflow1\models\research\object_detection\images\
中的train和test文件夹下

标注步骤请参阅 我之前写的训练yolov5模型的教程

6. 生成训练数据
运行
(tensorflow1) C:\tensorflow1\models\research\object_detection> python xml_to_csv.py
会在 \object_detection\images中生成两个文件
train_labels.csv 和 test_labels.csv

修改object_detection\目录下的generate_tfrecord.py
要修改的部分是

# TO-DO replace this with label map
 def class_text_to_int(row_label):
     if row_label == 'nine':
         return 1
     elif row_label == 'ten':
         return 2
     elif row_label == 'jack':
         return 3
     elif row_label == 'queen':
         return 4
     elif row_label == 'king':
         return 5
     elif row_label == 'ace':
         return 6
     else:
         None


将这部分内容中的row_label==’ ’ 修改为要训练的类别,请根据自己的情况适当增删
比如

# TO-DO replace this with label map
 def class_text_to_int(row_label):
     if row_label == 'basketball':
         return 1
     elif row_label == 'shirt':
         return 2
     elif row_label == 'shoe':
         return 3
     else:
         None

然后生成TFRecord 文件

python generate_tfrecord.py --csv_input=images\train_labels.csv --image_dir=images\train --output_path=train.record
python generate_tfrecord.py --csv_input=images\test_labels.csv --image_dir=images\test --output_path=test.record

会在object_detection\目录下生成train.record和test.record文件

7. 创建Label Map
label map 文件为object_detection\training\labelmap.pbtxt
修改其内容,原内容是

item {
   id: 1
   name: 'nine'
 }item {
   id: 2
   name: 'ten'
 }item {
   id: 3
   name: 'jack'
 }item {
   id: 4
   name: 'queen'
 }item {
   id: 5
   name: 'king'
 }item {
   id: 6
   name: 'ace'
 }

修改为自己的训练类别和对应ID,ID就是第7步中每个类别返回的数字
比如

if row_label == 'basketball':
      return 1


则在labelmap.pbtxt中修改为

item {
   id: 1
   name: 'basketball'
 }

8. 配置训练文件
将C:\tensorflow1\models\research\object_detection\samples\configs中的ssd_mobilenet_v2_quantized_300x300_coco.config复制到object_detection\training文件夹中

然后修改该文件

1. num_classes : 修改为自己的训练类别
 2. fine_tune_checkpoint : "C:/tensorflow1/models/research/object_detection/ssd_mobilenet_v2_quantized_300x300_coco_2019_01_03/model.ckpt"
 ## in train_input_reader section
 3. input_path : "C:/tensorflow1/models/research/object_detection/train.record"       
 4. label_map_path: "C:/tensorflow1/models/research/object_detection/training/labelmap.pbtxt"
 5. num_examples:修改为\images\test 中的图片数量
 ## in eval_input_reader section
 6. input_path : "C:/tensorflow1/models/research/object_detection/test.record"
 7. label_map_path: "C:/tensorflow1/models/research/object_detection/training/labelmap.pbtxt"
 8.batch_size 调整为1-6,太大带不起

9. 开始训练
重新训练时需要清除training\中除了labelmap和ssd_mobilenet。。。以外的所有文件,否则会报错
train.py 原本位于 Object_detection下的legacy文件夹内,请移动到Object_detection下,然后运行

(tensorflow1) C:\tensorflow1\models\research\object_detection> python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/ssd_mobilenet_v2_quantized_300x300_coco.config
当loss降到2以下的时候,就可以停止训练 , 直接 Ctrl + C即可

继续训练模型:修改ssd_mobilenet_v2_quantized_300x300_coco.config文件

fine_tune_checkpoint : "C:/tensorflow1/models/research/object_detection/ssd_mobilenet_v2_quantized_300x300_coco_2019_01_03/model.ckpt"

改为

fine_tune_checkpoint: 'C:/tensorflow1/models/research/object_detection/training/model.ckpt-9827'

10. 生成.pb文件
python export_inference_graph.py --input_type image_tensor --pipeline_config_path training/ssd_mobilenet_v2_quantized_300x300_coco.config --trained_checkpoint_prefix training/model.ckpt-XXXX --output_directory inference_graph

将model.ckpt-XXXX替换为training\目录中数值最大的一个文件,这步之后会在\object_detection\inference_graph中生成一个frozen_inference_graph.pb文件

11. 测试Tensorflow模型
修改Object_detection_image.py,将NUM_CLASSES的数值修改为训练的类别数量
将IMAGE_NAME修改为测试图片的路径
然后运行

(tensorflow1) C:\tensorflow1\models\research\object_detection> python Object_detection_image.py

应该可以看到检测结果

12. 导出 frozen inference graph

(tensorflow1) C:\tensorflow1\models\research\object_detection>  mkdir TFLite_model
 (tensorflow1) C:\tensorflow1\models\research\object_detection> set CONFIG_FILE=C:\\tensorflow1\models\research\object_detection\training\ssd_mobilenet_v2_quantized_300x300_coco.config
 (tensorflow1) C:\tensorflow1\models\research\object_detection> set CHECKPOINT_PATH=C:\\tensorflow1\models\research\object_detection\training\model.ckpt-XXXX
 (tensorflow1) C:\tensorflow1\models\research\object_detection> set OUTPUT_DIR=C:\\tensorflow1\models\research\object_detection\TFLite_model

注意将上述步骤中的model.ckpt-XXXX替换为training\目录中数值最大的一个文件

(tensorflow1) C:\tensorflow1\models\research\object_detection> python export_tflite_ssd_graph.py --pipeline_config_path=%CONFIG_FILE% --trained_checkpoint_prefix=%CHECKPOINT_PATH% --output_directory=%OUTPUT_DIR% --add_postprocessing_op=true

之后会在 \object_detection\TFLite_model中生成tflite_graph.pb 和 tflite_graph.pbtxt

二、模型转换.pb 到 .tflite

执行
pb_to_tflite.py(自创)   
如下

’‘’‘’‘’‘’‘’‘’‘’
 import tensorflow as tf
  
 in_path = "tflite_graph.pb"                 #只需换成你模型中的值就OK
  
 # 模型输入节点
 input_tensor_name = ["normalized_input_image_tensor"]
 input_tensor_shape = {"normalized_input_image_tensor":[1,300,300,3]}
 # 模型输出节点
 classes_tensor_name = ['TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3']
  
 converter = tf.lite.TFLiteConverter.from_frozen_graph(in_path,
                                             input_tensor_name, classes_tensor_name,
                                             input_tensor_shape)
  
 converter.allow_custom_ops=True
 converter.post_training_quantize = True
 tflite_model = converter.convert()
  
 open("SSD.tflite", "wb").write(tflite_model)
 ‘’‘’‘’‘’‘’‘’‘’‘

在/media/liuxuwei/Windows/tensorflow1/models/research/object_detection/TFLite_model下新建txt文件
命名为labelmap.txt,这是TFlite所需要的label map文件。根据windows篇第6步中的训练类别,填写labelmap.txt
比如

basketball
shirt
shoe

到这里Tflite模型就完成了