LLTeng
LLTeng

Reputation: 395

To load pb file : DecodeError: Error parsing message

I'm trying to load a .pb file generated from pusher tfx pipeline. I'm using the following function to load the file, but I got the following error from the function. Please help.

error:

<ipython-input-40-af7ef7ac8a8b> in load_model()
      2     with tf.compat.v2.io.gfile.GFile('/home//saved_model.pb', "rb") as f:
      3         graph_def = tf.compat.v1.GraphDef()
----> 4         graph_def.ParseFromString(f.read())
      5 
      6     with tf.Graph().as_default() as graph:
DecodeError: Error parsing message

Function

def load_model():
    with tf.compat.v2.io.gfile.GFile('/home/saved_model.pb', "rb") as f:
        graph_def = tf.compat.v1.GraphDef()
        graph_def.ParseFromString(f.read())

    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def, name="")
    return graph

Upvotes: 3

Views: 12328

Answers (1)

Aayush Khurana
Aayush Khurana

Reputation: 141

Hey you can try this code for loading .pb files provided by tensorflow :

import tensorflow as tf
import sys
from tensorflow.python.platform import gfile
from tensorflow.core.protobuf import saved_model_pb2
from tensorflow.python.util import compat

with tf.Session() as sess:
    model_filename ='saved_model.pb'
    with gfile.FastGFile(model_filename, 'rb') as f:
        data = compat.as_bytes(f.read())
        sm = saved_model_pb2.SavedModel()
        sm.ParseFromString(data)
        g_in = tf.import_graph_def(sm.meta_graphs[0].graph_def)

Upvotes: 14

Related Questions