人工智能视频教程 ai vip技术 人工智能数学基础 爬虫 python机器学习 tensorflow深度学习 20+个企业AI实战项目

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 105|回复: 2

[课堂笔记] Keras模型如何转tensorflow的pb文件?

[复制链接]

1102

主题

1459

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
12024
QQ
发表于 2020-9-17 17:53:33 | 显示全部楼层 |阅读模式
Keras模型如何转tensorflow的pb文件?

# keras的模型 转 pb文件 这样不行
model = load_model('./model/plate_type.h5', compile=False)
print('模型 车牌分类的:plate_type.h5 已经加载好了!')
# tf.saved_model.save(model, "model_pb_plate_type")




  1. # -*- coding: utf-8 -*-
  2. __author__ = u'江锦炬 微信:715781570'
  3. __date__ = '2020/9/17 下午5:34'
  4. __product__ = 'PyCharm'
  5. __filename__ = 'keras_to_pb_dfy'

  6. import tensorflow as tf
  7. from tensorflow.python.framework import graph_io
  8. from tensorflow.keras.models import load_model


  9. # Clear any previous session.
  10. tf.keras.backend.clear_session()

  11. save_pb_dir = './model_pb_plate_type'
  12. # keras的模型文件h5是否都要包含(网络结构与权重)
  13. model_fname = './model/plate_type.h5'

  14. def freeze_graph(graph, session, output, save_pb_dir='.', save_pb_name='frozen_model.pb', save_pb_as_text=False):
  15.     with graph.as_default():
  16.         # 删除训练的节点
  17.         graphdef_inf = tf.graph_util.remove_training_nodes(graph.as_graph_def())
  18.         graphdef_frozen = tf.graph_util.convert_variables_to_constants(session, graphdef_inf, output)
  19.         graph_io.write_graph(graphdef_frozen, save_pb_dir, save_pb_name, as_text=save_pb_as_text)
  20.         return graphdef_frozen

  21. # This line must be executed before loading Keras model.
  22. tf.keras.backend.set_learning_phase(0)

  23. model = load_model(model_fname)

  24. session = tf.keras.backend.get_session()

  25. INPUT_NODE = [t.op.name for t in model.inputs]
  26. OUTPUT_NODE = [t.op.name for t in model.outputs]
  27. # 节点: ['conv2d_11_input'] ['activation_19/Softmax']
  28. print("节点:", INPUT_NODE, OUTPUT_NODE)
  29. frozen_graph = freeze_graph(session.graph, session, [out.op.name for out in model.outputs], save_pb_dir=save_pb_dir)


复制代码




让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

1102

主题

1459

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
12024
QQ
 楼主| 发表于 2020-9-17 17:55:33 | 显示全部楼层
将pb转成 openvino的 xml bin
  1. jiang@jiang-Ubuntu:~$ python3 ~/intel/openvino/deployment_tools/model_optimizer/mo.py --framework=tf --data_type=FP32 --output_dir=/home/jiang/py3_openvino_works/vehicle/model_openvino/ptype/FP32 --model_name=plate_type '--input_shape=[1,9,34,3]' --input=conv2d_11_input --output=activation_19/Softmax --input_model=/home/jiang/py3_openvino_works/vehicle/model_ori/plate_type.pb
  2. Model Optimizer arguments:
  3. Common parameters:
  4.         - Path to the Input Model:         /home/jiang/py3_openvino_works/vehicle/model_ori/plate_type.pb
  5.         - Path for generated IR:         /home/jiang/py3_openvino_works/vehicle/model_openvino/ptype/FP32
  6.         - IR output name:         plate_type
  7.         - Log level:         ERROR
  8.         - Batch:         Not specified, inherited from the model
  9.         - Input layers:         conv2d_11_input
  10.         - Output layers:         activation_19/Softmax
  11.         - Input shapes:         [1,9,34,3]
  12.         - Mean values:         Not specified
  13.         - Scale values:         Not specified
  14.         - Scale factor:         Not specified
  15.         - Precision of IR:         FP32
  16.         - Enable fusing:         True
  17.         - Enable grouped convolutions fusing:         True
  18.         - Move mean values to preprocess section:         False
  19.         - Reverse input channels:         False
  20. TensorFlow specific parameters:
  21.         - Input model in text protobuf format:         False
  22.         - Path to model dump for TensorBoard:         None
  23.         - List of shared libraries with TensorFlow custom layers implementation:         None
  24.         - Update the configuration file with input/output node names:         None
  25.         - Use configuration file used to generate the model with Object Detection API:         None
  26.         - Use the config file:         None
  27. Model Optimizer version:        
  28. /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  29.   _np_qint8 = np.dtype([("qint8", np.int8, 1)])
  30. /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  31.   _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
  32. /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  33.   _np_qint16 = np.dtype([("qint16", np.int16, 1)])
  34. /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  35.   _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
  36. /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  37.   _np_qint32 = np.dtype([("qint32", np.int32, 1)])
  38. /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  39.   np_resource = np.dtype([("resource", np.ubyte, 1)])
  40. /usr/local/lib/python3.6/dist-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  41.   _np_qint8 = np.dtype([("qint8", np.int8, 1)])
  42. /usr/local/lib/python3.6/dist-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  43.   _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
  44. /usr/local/lib/python3.6/dist-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  45.   _np_qint16 = np.dtype([("qint16", np.int16, 1)])
  46. /usr/local/lib/python3.6/dist-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  47.   _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
  48. /usr/local/lib/python3.6/dist-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  49.   _np_qint32 = np.dtype([("qint32", np.int32, 1)])
  50. /usr/local/lib/python3.6/dist-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  51.   np_resource = np.dtype([("resource", np.ubyte, 1)])

  52. [ SUCCESS ] Generated IR version 10 model.
  53. [ SUCCESS ] XML file: /home/jiang/py3_openvino_works/vehicle/model_openvino/ptype/FP32/plate_type.xml
  54. [ SUCCESS ] BIN file: /home/jiang/py3_openvino_works/vehicle/model_openvino/ptype/FP32/plate_type.bin
  55. [ SUCCESS ] Total execution time: 1.43 seconds.
  56. [ SUCCESS ] Memory consumed: 239 MB.
复制代码
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

1102

主题

1459

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
12024
QQ
 楼主| 发表于 2020-9-18 15:01:42 | 显示全部楼层
车牌识别的:
  1. jiang@jiang-Ubuntu:~$ python3 ~/intel/openvino/deployment_tools/model_optimizer/mo.py --framework=tf --data_type=FP32 --output_dir=/home/jiang/py3_openvino_works/vehicle/model_openvino/recog/FP32 --model_name=ocr_plate_all_w_rnn_2 '--input_shape=[1,160,40,3]' --input=input_1 --output=activation_13/truediv --input_model=/home/jiang/py3_openvino_works/vehicle/model_ori/ocr_plate_all_w_rnn_2_infer.pb
  2. Model Optimizer arguments:
  3. Common parameters:
  4.         - Path to the Input Model:         /home/jiang/py3_openvino_works/vehicle/model_ori/ocr_plate_all_w_rnn_2_infer.pb
  5.         - Path for generated IR:         /home/jiang/py3_openvino_works/vehicle/model_openvino/recog/FP32
  6.         - IR output name:         ocr_plate_all_w_rnn_2
  7.         - Log level:         ERROR
  8.         - Batch:         Not specified, inherited from the model
  9.         - Input layers:         input_1
  10.         - Output layers:         activation_13/truediv
  11.         - Input shapes:         [1,160,40,3]
  12.         - Mean values:         Not specified
  13.         - Scale values:         Not specified
  14.         - Scale factor:         Not specified
  15.         - Precision of IR:         FP32
  16.         - Enable fusing:         True
  17.         - Enable grouped convolutions fusing:         True
  18.         - Move mean values to preprocess section:         False
  19.         - Reverse input channels:         False
  20. TensorFlow specific parameters:
  21.         - Input model in text protobuf format:         False
  22.         - Path to model dump for TensorBoard:         None
  23.         - List of shared libraries with TensorFlow custom layers implementation:         None
  24.         - Update the configuration file with input/output node names:         None
  25.         - Use configuration file used to generate the model with Object Detection API:         None
  26.         - Use the config file:         None
  27. Model Optimizer version:        
  28. /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  29.   _np_qint8 = np.dtype([("qint8", np.int8, 1)])
  30. /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  31.   _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
  32. /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  33.   _np_qint16 = np.dtype([("qint16", np.int16, 1)])
  34. /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  35.   _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
  36. /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  37.   _np_qint32 = np.dtype([("qint32", np.int32, 1)])
  38. /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  39.   np_resource = np.dtype([("resource", np.ubyte, 1)])
  40. /usr/local/lib/python3.6/dist-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  41.   _np_qint8 = np.dtype([("qint8", np.int8, 1)])
  42. /usr/local/lib/python3.6/dist-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  43.   _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
  44. /usr/local/lib/python3.6/dist-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  45.   _np_qint16 = np.dtype([("qint16", np.int16, 1)])
  46. /usr/local/lib/python3.6/dist-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  47.   _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
  48. /usr/local/lib/python3.6/dist-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  49.   _np_qint32 = np.dtype([("qint32", np.int32, 1)])
  50. /usr/local/lib/python3.6/dist-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  51.   np_resource = np.dtype([("resource", np.ubyte, 1)])

  52. [ SUCCESS ] Generated IR version 10 model.
  53. [ SUCCESS ] XML file: /home/jiang/py3_openvino_works/vehicle/model_openvino/recog/FP32/ocr_plate_all_w_rnn_2.xml
  54. [ SUCCESS ] BIN file: /home/jiang/py3_openvino_works/vehicle/model_openvino/recog/FP32/ocr_plate_all_w_rnn_2.bin
  55. [ SUCCESS ] Total execution time: 2.87 seconds.
  56. [ SUCCESS ] Memory consumed: 276 MB.
复制代码
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2020-10-30 03:50 , Processed in 0.274354 second(s), 27 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表