tensorflow2.9.0保存的h5模型怎么转成pb模型,实现opencv调用
时间: 2024-05-12 08:18:50 浏览: 88
要将TensorFlow 2.9.0保存的h5模型转换为pb模型,可以使用以下步骤:
1. 加载h5模型
```python
import tensorflow as tf
model = tf.keras.models.load_model('path/to/model.h5')
```
2. 转换为pb模型
```python
from tensorflow.compat.v1 import graph_util
from tensorflow.compat.v1 import session
from tensorflow.compat.v1 import keras
with session.Session() as sess:
keras.backend.set_session(sess)
output_names = [i.name.split(':')[0] for i in model.outputs]
constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), output_names)
graph_io.write_graph(constant_graph, 'path/to/output', 'model.pb', as_text=False)
```
3. 使用OpenCV调用pb模型
```python
import cv2
net = cv2.dnn.readNetFromTensorflow('path/to/model.pb')
image = cv2.imread('path/to/image.jpg')
blob = cv2.dnn.blobFromImage(image, size=(224, 224), swapRB=True, crop=False)
net.setInput(blob)
output = net.forward()
```
这样就可以将TensorFlow 2.9.0保存的h5模型转换为pb模型,并使用OpenCV调用了。
阅读全文
相关推荐











