【问题标题】:Using pre-trained Inception_v4 model使用预训练的 Inception_v4 模型
【发布时间】:2017-08-15 03:12:27
【问题描述】:

https://github.com/tensorflow/models/tree/master/slim

这提供了 Inception v1-4 预训练模型的检查点的下载链接。但是,tar.gz 仅包含 .ckpt 文件。

在使用 Inception v3 2012 [This link] 的教程中,tar.gz 包含用于分类的 .pb 和 .pbtxt 文件。

如何仅使用 .ckpt 文件生成相应的 .pb 和 .pbtxt 文件? 要么 有没有其他方法可以使用 .ckpt 文件进行分类?

【问题讨论】:

    标签: python machine-learning tensorflow classification tf-slim


    【解决方案1】:

    即使我也在尝试 inception_v4 模型。在我的搜索过程中,我可以找到包含权重的检查点文件。所以为了使用它,需要从 inception_v4.py 加载 inception_v4 图,并且需要从检查点文件中恢复会话。以下代码将读取检查点文件并创建 protobuf 文件。

    import tensorflow as tf
    slim = tf.contrib.slim
    import tf_slim.models.slim.nets as net
    # inception_v3_arg_scope
    import tf_slim
    import inception_v4 as net
    import cv2
    
    
    # checkpoint file
    checkpoint_file = '/home/.../inception_v4.ckpt' 
    
    # Load Session
    sess = tf.Session()
    arg_scope = net.inception_v4_arg_scope()
    input_tensor = tf.placeholder(tf.float32, [None, 299, 299, 3])
    with slim.arg_scope(arg_scope):
        logits, end_points = net.inception_v4(inputs=input_tensor)
    
    saver = tf.train.Saver()
    saver.restore(sess, checkpoint_file)
    f = tf.gfile.FastGFile('./mynet.pb', "w")
    f.write(sess.graph_def.SerializeToString())
    f.close()
    
    # reading the graph
    #
    with tf.gfile.FastGFile('./mynet.pb', 'rb') as fp:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(fp.read())
    
        with tf.Session(graph=tf.import_graph_def(graph_def, name='')) as sess:
        # op = sess.graph.get_operations()
        # with open('./tensors.txt', mode='w') as fp:
        #     for m in op:
        #     #     print m.values()
        #         fp.write('%s \n' % str(m.values()))
        cell_patch = cv2.imread('./car.jpg')
        softmax_tensor = sess.graph.get_tensor_by_name('InceptionV4/Logits/Predictions:0')
        predictions = sess.run(softmax_tensor, {'Placeholder:0': cell_patch})
    

    但是上面的代码不会给你预测。因为我在向图表提供输入时遇到了问题。但是使用检查点文件可能是一个很好的起点。

    检查点从以下链接下载checkpoints

    【讨论】:

      猜你喜欢
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 2021-07-09
      • 2022-10-19
      • 1970-01-01
      • 2019-08-16
      相关资源
      最近更新 更多