tensorflow inception_resnet_v2 ckpt2pb, 自定义输入输出节点

获取ckpt模型中的节点名称

from tensorflow.python import pywrap_tensorflow
checkpoint_path = 'model.ckpt-xxx'
reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
var_to_shape_map = reader.get_variable_to_shape_map()
for key in var_to_shape_map:
    print("tensor_name: ", key)

将ckpt模型转为pb模型

import sys
import tensorflow as tf
from nets import nets_factory

input_node = tf.placeholder(tf.float32, [1, 299, 299, 3], name='input')
output_node_names = 'InceptionResnetV2/Logits/Predictions'
network_fn = nets_factory.get_network_fn('inception_resnet_v2', num_classes=4, is_training=False)
ckpt = sys.argv[1]
pb = sys.argv[2]

with tf.Session() as sess:
    endpoint = network_fn(input_node)
    sess.run(tf.global_variables_initializer())

    input_graph_def = tf.get_default_graph().as_graph_def()
    # node_names = [n.name for n  in input_graph_def.node]
    # for node in node_names:
    #     print (node)
    output_graph_def = tf.graph_util.convert_variables_to_constants(
        sess,  # The session
        input_graph_def,  # input_graph_def is useful for retrieving the nodes
        output_node_names.split(",")
    )

with tf.gfile.GFile(pb, 'wb') as f:
    f.write(output_graph_def.SerializeToString())
 

TensorFlow 2.3.0中,tf_slim库已被弃用,因此无法直接使用`from tf_slim.nets import inception_resnet_v2`来引用inception_resnet_v2。但是,您可以使用TensorFlow官方的模型库(tensorflow/models)中的相应模型来代替。 首先,您需要从GitHub上克隆tensorflow/models仓库到本地: ``` git clone https://github.com/tensorflow/models.git ``` 然后,将models/research/slim目录添加到您的Python路径中。您可以通过以下方式实现: ```python import sys sys.path.append('/path/to/models/research/slim') ``` 现在,您可以使用官方模型库中的inception_resnet_v2模型了。示例代码如下: ```python import tensorflow as tf from official.vision.image_classification import imagenet_preprocessing from official.vision.image_classification import resnet_preprocessing # 导入inception_resnet_v2模型 from official.vision.image_classification.resnet import inception_resnet_v2 # 创建模型实例 model = inception_resnet_v2.InceptionResNetV2(weights=None) # 加载预训练权重(如果有的话) model.load_weights('path/to/pretrained/weights.h5') # 预处理输入图像 image_path = 'path/to/image.jpg' image = tf.io.read_file(image_path) image = tf.image.decode_jpeg(image, channels=3) image = resnet_preprocessing.preprocess_image(image, model.input_shape[1], model.input_shape[2]) image = tf.expand_dims(image, axis=0) # 进行推理 predictions = model.predict(image) # 打印预测结果 print(predictions) ``` 请确保您已经安装了所需的依赖项,并将路径替换为适当的路径。这样,您就可以在TensorFlow 2.3.0中使用inception_resnet_v2模型了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值