RetinaNet目标检测网络Tensorflow1.13模型.h5文件保存.pb+openvino转换IR文件和推理

前记:
模型运行环境:Linux18.04+Tensorflow1.13.2+python3.6.6+keras2.3.1
模型转换环境:windows10 64位+openvino2020.0428+

直接使用网上开源的目标检测模型:keras-retinanet
目的,套用keras-retinanet模型,在自有数据集上实现目标检测及部署
1、图像数据标注及存储
准备好原图文件与标记文件.xml
/VOCdevkit2007/VOC2007/目录下,有三个文件夹
在这里插入图片描述
Annotations:存放.xml文件
ImageSets:存放训练和测试目标列表的文本文件
JPEGImages:存放.jpg/.bmp/.png
设置训练和测试对象

/VOCdevkit2007/VOC2007/ImageSets/Main/

在这里插入图片描述
2、修改/keras_retinanet/preprocessing/目录下pascal_voc.py文件中标签类别(根据自己数据的类别修改),同时,修改图片数据的后缀格式
在这里插入图片描述
在这里插入图片描述
3、根据需要,修改/keras_retinanet/bin/目录下train.py,将模型参数中的最大宽度和最小宽度调整为自己需要的尺寸
在这里插入图片描述
4、下载网上训练好的模型文件:resnet50_coco_best_v2.1.0.h5,

5、执行训练

python keras_retinanet/bin/train.py --weights snapshots/resnet50_coco_best_v2.1.0.h5 --freeze-backbone --epochs 30 --batch-size 10 --steps 100 --lr 1e-3 pascal VOCdevkit2007/VOC2007
关于各个参数的设置,github官网有给出(https://github.com/fizyr/keras-retinanet)

6、转换推理模型
运用convert_model.py将训练得到的模型文件resnet50_pascal_02.h5转换成可用于推理的模型文件

python keras_retinanet/bin/convert_model.py snapshots/resnet50_pascal_02.h5 snapshots/model.h5

7、推理测试

model.h5  //通过训练自己数据集获得的可推理模型文件
resnet50_coco_best_v2.1.0.h5  //sota模型文件
resnet50_pascal_01.h5  //训练过程中获得的中间过程的模型文件(非可推理模型),01代表第一次保存
resnet50_pascal_02.h5  //训练过程中获得的中间过程的模型文件(非可推理模型),02代表第二次保存,依据训练是设置的epochs大小来看,这里是每一个epochs保存一次
...

8、将.h5模型转换为.pb模型文件
上面提到训练好的模型有两个,一个是为可推理模型文件model.h5,一个为非推理模型文件resnet50_pascal_02.h5,两者需要的转换,在官网有给出解释
Converting a training model to inference model

The training procedure of keras-retinanet works with training models. 
These are stripped down versions compared to the inference model and 
only contains the layers necessary for training (regression and classification values). 
If you wish to do inference on a model (perform object detection on an image), 
you need to convert the trained model to an inference model. This is done as follows:

# Running directly from the repository:
keras_retinanet/bin/convert_model.py /path/to/training/model.h5 /path/to/save/inference/model.h5

# Using the installed script:
retinanet-convert-model /path/to/training/model.h5 /path/to/save/inference/model.h5
Most scripts (like retinanet-evaluate) also support converting on the fly, using the --convert-model argument.

然后,我将转换后的.h5文件进行模型推理测试,结果没问题。

8.1 将.h5转换成.pb文件的代码如下,

import keras.models
from keras_retinanet import models
from keras.models import load_model
import tensorflow as tf
import os 
import os.path as osp
from keras import backend as K
#路径参数
input_path = '/snapshots/'  #全局路径
weight_file = 'model.h5' #全局路径下的模型文件
weight_file_path = osp.join(input_path,weight_file)
output_graph_name = weight_file[:-3] + '.pb'
#转换函数
def h5_to_pb(h5_model,output_dir,model_name,out_prefix = "output_"):
    if osp.exists(output_dir) == False:
        os.mkdir(output_dir)
    out_nodes = []
    for i in range(len(h5_model.outputs)):
        out_nodes.append(out_prefix + str(i + 1))
        tf.identity(h5_model.output[i],out_prefix + str(i + 1))
    sess = K.get_session()

    from tensorflow.python.framework import graph_util,graph_io
    init_graph = sess.graph.as_graph_def()
    main_graph = graph_util.convert_variables_to_constants(sess,init_graph,out_nodes)
    graph_io.write_graph(main_graph,output_dir,name = model_name,as_text = False)
#输出路径
output_dir = osp.join(os.getcwd(),"pb_model")  #在当前路径下新建pb_model文件夹存放.pb文件
#加载模型
h5_model = models.load_model(weight_file_path)
h5_to_pb(h5_model,output_dir = output_dir,model_name = output_graph_name)
print('model saved')

运行代码后可以将model.h5文件保存为单独的model.pb文件。

8.2 直接使用转换后的model.pb文件在openvino下进行IR文件转换,执行代码(注意文件的路径问题)

python mo_tf.py --saved_model_dir model.pb --input_shape [1,800,800,3] --mean_value [127.5,127.5,127.5] --scale 127.5 --data_type=FP32

运行后提示错误

[ ERROR ]  Cannot infer shapes or values for node "output_3".
[ ERROR ]  0
[ ERROR ]
[ ERROR ]  It can happen due to bug in custom shape infer function <function Identity.infer at 0x000001E6F876F310>.
[ ERROR ]  Or because the node inputs have incorrect values/shapes.
[ ERROR ]  Or because input shapes are incorrect (embedded to the model or passed via --input_shape).
[ ERROR ]  Run Model Optimizer with --log_level=DEBUG for more information.
[ ANALYSIS INFO ]  Your model looks like TensorFlow RetinaNet Model.
To generate the IR, provide model to the Model Optimizer with the following parameters:
        --input_model <path_to_model>/<model>.pb
        --input_shape [1,600,600,3]
        --tensorflow_use_custom_operations_config <OPENVINO_INSTALL_DIR>/deployment_tools/model_optimizer/extensions/front/tf/retinanet.json
        --reverse_input_channels
[ ERROR ]  Exception occurred during running replacer "REPLACEMENT_ID" (<class 'extensions.middle.PartialInfer.PartialInfer'>): Stopped shape/value propagation at "output_3" node.
 For more information please refer to Model Optimizer FAQ (https://docs.openvinotoolkit.org/latest/_docs_MO_DG_prepare_model_Model_Optimizer_FAQ.html), question #38.

看到提示,TensorFlow RetinaNet Model需要单个.pb文件、input_shape、tensorflow_use_custom_operations_config文件和reverse_input_channels,所以我根据提示在openvino安装路径下找到retinanet.json,并拷贝至与模型相同的目录下,再次运行,然后还是报错。

python mo_tf.py --input_model model.pb --input_shape [1,600,600,3] --tensorflow_use_custom_operations_config retinanet.json --reverse_input_channels

一直出问题,困扰了我大半天,然后后面想着是不是保存的.h5文件的问题,于是resnet50_pascal_02.h5转成resnet50_pascal_02.pb文件,在用openvino进行转换
终于,运行没有报错,能够保存成C++部署所需的IR文件(.bin和.xml)

resnet50_pascal_02.bin
resnet50_pascal_02.mapping
resnet50_pascal_02.xml

具体为什么,我还需要进行深入了解,先在这里做个记录。

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值