利用RK3399pro平台,移植深度学习目标检测算法,实现嵌入式平台的深度学习应用
2. 步骤及解决过程 2.1 训练所需要的目标检测模型,利用Tensorflow object detection API完成模型的训练验证以及最终将模型转为.pb文件的形式,具体训练过程可参考如下博客:【Tensorflow object detection API】使用SSD-Mobilenet训练模型+ubuntu 16.04+python3(步骤十分清晰!)
【COCO数据集】ubuntu16.04+python+获取并保存特定的某一类(例如“person”类)
2.2 针对rknn-toolkitexample中在RK平台上使用tensorflow模型所需要的文件进行准备:(1)准备模型转换时候(从.pb文件转为.rknn文件)所需要的model_config.yml文件中的输入输出节点名称:
不能将该文件中的输入、输出节点,简单的理解为模型(.pb)文件节点分析后的第一个和最后一个节点,因为对于RK平台来说,模型的前处理(preprocessor)和后处理(postprocessor)部分不直接利用平台NPU计算,会影响速度,而是使用DSP来处理,对于模型中间部分的大量卷积操作使用NPU;因此,该yml文件中的输入和输出节点名称是模型排除前处理部分、后处理部分后的第一个和最后一个节点。模型的节点信息(name)可用如下代码获得:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
#coding:utf-8
import tensorflow as tf
import os
model_dir = '模型所在路径'
model_name = '模型名称'
def create_graph():
with tf.gfile.GFile(os.path.join(
model_dir, model_name), 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
# imports the graph from graph_def into the current default Graph.
tf.import_graph_def(graph_def, name='')
create_graph()
tensor_name_list = [tensor.name for tensor in tf.get_default_graph().as_graph_def().node]
for tensor_name in tensor_name_list:
a = tensor_name[0:13]
b = "Postprocessor"
if a != b:
print(tensor_name)
例如mobilenetv1-ssd 官方针对COCO数据集的模型的输入输出节点为:
inputs:
- FeatureExtractor/MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/batchnorm/mul_1
input-size-list:
- 300,300,3
outputs:
- concat
- concat_1
注意:对于每一个模型,哪怕模型没变化,只是训练的数据集变化了,也要重新确定其输入输出节点!!!
例如:对于上述mobilenetv1-ssd 针对VOC数据集训练后导出的模型的输入输出节点为:
inputs:
- FeatureExtractor/MobilenetV1/MobilenetV1/Conv2d_0/Conv2D
input-size-list:
- 300,300,3
outputs:
- concat_1
- concat
(2)准备test.py文件所需要的box_priors.txt文件:
在 tensorflowmodelsresearchobject_detectionmeta_architecturesssd_meta_arch.py文件中:
predictions_dict = {
'preprocessed_inputs': preprocessed_inputs,
'feature_maps': feature_maps,
'anchors': self._anchors.get()
}
代码后添加以下代码:
print("==================feature_maps=========================")
# 了解该模型使用的feature_map层
for feature in feature_maps:
print(feature)
print("==================anchor_numbers========================")
# 了解该模型从这些特征层(feature_map层)生成的一共的先验框个数
# 便于修改test.py文件中的 NUM_RESULTS = anchor_numbers先验框数量
print(self._anchors.get())
print("================save_box_priors.txt=====================")
# 保存该模型相关的box_priors.txt文件
sess = tf.Session()
with sess.as_default():
anchors_np = self._anchors.get().eval()
anchors_np = anchors_np.transpose()
min_x = anchors_np[0]
min_y = anchors_np[1]
max_x = anchors_np[2]
max_y = anchors_np[3]
cx = (min_x + max_x) / 2.0
cy = (min_y + max_y) / 2.0
w = max_x - min_x
h = max_y - min_y
np.savetxt('/home/jari/guoshi/workspace/mobilenetssd/models-master/research/object_detection/resnet_ssd_box_priors.txt', (cx, cy, w, h), fmt='%0.8f')
print("========================done_save_box_priors===============")
完成准备;



