【问题标题】:unable to use Trained Tensorflow model无法使用经过训练的 Tensorflow 模型
【发布时间】:2018-08-13 13:11:32
【问题描述】:

我是深度学习和 Tensorflow 的新手。我将预训练的 tensorflow inceptionv3 模型重新训练为 saved_model.pb 以识别不同类型的图像,但是当我尝试使用带有以下代码的 fie 时。

with tf.Session() as sess:
    with tf.gfile.FastGFile("tensorflow/trained/saved_model.pb",'rb') as  f:
        graph_def = tf.GraphDef()
        tf.Graph.as_graph_def()
        graph_def.ParseFromString(f.read())
        g_in=tf.import_graph_def(graph_def)
        LOGDIR='/log'
        train_writer=tf.summary.FileWriter(LOGDIR)
        train_writer.add_graph(sess.graph)

它给了我这个错误 -

 File "testing.py", line 7, in <module>
graph_def.ParseFromString(f.read())
google.protobuf.message.DecodeError: Error parsing message

我尝试了很多解决方案,我可以找到解决这个问题的方法,tensorflow/python/tools 中使用 graph_def.ParseFromString(f.read()) 函数的模块是给我同样的错误。请告诉我如何解决这个问题或告诉我如何避免 ParseFromString(f.read()) 函数。任何帮助,将不胜感激。谢谢!

【问题讨论】:

    标签: python-3.x image-processing tensorflow deep-learning tensorboard


    【解决方案1】:

    我假设您使用 TensorFlow 提供的 tf.saved_model.Builder 保存了经过训练的模型,在这种情况下,您可以执行以下操作:

    加载模型

    export_path = './path/to/saved_model.pb'
    
    # We start a session using a temporary fresh Graph
    with tf.Session(graph=tf.Graph()) as sess:
        '''
        You can provide 'tags' when saving a model,
        in my case I provided, 'serve' tag 
        '''
    
        tf.saved_model.loader.load(sess, ['serve'], export_path)
        graph = tf.get_default_graph()
    
        # print your graph's ops, if needed
        print(graph.get_operations())
    
        '''
        In my case, I named my input and output tensors as
        input:0 and output:0 respectively
        ''' 
        y_pred = sess.run('output:0', feed_dict={'input:0': X_test})
    

    为了在此处提供更多上下文,这就是我保存模型的方式,该模型可以按上述方式加载。

    保存模型

    
    x = tf.get_default_graph().get_tensor_by_name('input:0')
    y = tf.get_default_graph().get_tensor_by_name('output:0')
    
    export_path = './models/'
    builder = tf.saved_model.builder.SavedModelBuilder(export_path)
    signature = tf.saved_model.predict_signature_def(
                    inputs={'input': x}, outputs={'output': y}
                    )
    
    # using custom tag instead of: tags=[tf.saved_model.tag_constants.SERVING]
    builder.add_meta_graph_and_variables(sess=obj.sess,
                                         tags=['serve'],
                                         signature_def_map={'predict': signature})
    builder.save()
    
    

    这会将您的 protobuf ('saved_model.pb') 保存在上述文件夹(此处为'models')中,然后可以按上述方式加载。

    【讨论】:

      【解决方案2】:

      请使用frozen_inference_graph.pb加载模型, 而不是使用 saved_model.pb

      Model_output
      - saved_model
        - saved_model.pb
      - checkpoint
      - frozen_inference_graph.pb     # Main model 
      - model.ckpt.data-00000-of-00001
      - model.ckpt.index
      - model.ckpt.meta
      - pipeline.config
      

      【讨论】:

      • 这实际上是我的问题!谢谢!
      【解决方案3】:

      你在保存模型时是否传递了 as_text=False ?请看:TF save/restore graph fails at tf.GraphDef.ParseFromString()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-25
        • 1970-01-01
        • 2020-01-03
        • 1970-01-01
        • 1970-01-01
        • 2020-11-04
        • 2018-03-24
        • 1970-01-01
        相关资源
        最近更新 更多