Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there some way of load a .pb file created in tf v1 on tensorflow v2?

I'm trying to load a .pb file that was created in tf v1 on a tfv2 dist, my question is, the version 2 does have compatibility with older pb?

I already tried a few things, but none of them worked. Trying to load the pb file directly with:

with tf.compat.v1.gfile.GFile("./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="")

The result when i run the code above is:

Traceback (most recent call last):
  File "read_tfv1_pb.py", line 7, in <module>
    graph_def.ParseFromString(f.read())
  File "D:\Anaconda3\envs\tf2\lib\site-packages\google\protobuf\message.py", line 187, in ParseFromString
    return self.MergeFromString(serialized)
  File "D:\Anaconda3\envs\tf2\lib\site-packages\google\protobuf\internal\python_message.py", line 1128, in MergeFromString
    if self._InternalParse(serialized, 0, length) != length:
  File "D:\Anaconda3\envs\tf2\lib\site-packages\google\protobuf\internal\python_message.py", line 1193, in InternalParse
    pos = field_decoder(buffer, new_pos, end, self, field_dict)
  File "D:\Anaconda3\envs\tf2\lib\site-packages\google\protobuf\internal\decoder.py", line 968, in _SkipFixed32
    raise _DecodeError('Truncated message.')
google.protobuf.message.DecodeError: Truncated message.

If not, is there a way that i can save the weights of the old pb and place them in a new model instance on tensorflow v2 to apply transfer learning / save with the new model structure ?

like image 751
Carlos Avatar asked Oct 18 '25 15:10

Carlos


1 Answers

Convert it to a tf.saved_model with the code from here Convert a graph proto (pb/pbtxt) to a SavedModel for use in TensorFlow Serving or Cloud ML Engine

I just noticed that your .pb name is saved_model.pb so perhaps it is already a tf.saved_model. If that's the case you can load it as

func = tf.saved_model.load('.').signatures["serving_default"] 
out = func( tf.constant(10,tf.float32) )
like image 137
dionKBastian Avatar answered Oct 21 '25 09:10

dionKBastian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!