Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Load .pb into Tensorboard
import tensorflow as tf
from tensorflow.python.platform import gfile
with tf.Session() as sess:
model_filename ='PATH_TO_PB.pb'
with gfile.FastGFile(model_filename, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
g_in = tf.import_graph_def(graph_def)
LOGDIR='/logs/tests/1/'
train_writer = tf.summary.FileWriter(LOGDIR)
train_writer.add_graph(sess.graph)
@Simo-H

This comment has been minimized.

Copy link

commented Dec 27, 2017

Thank you very much for this gist, it was just what i needed.
I had a problem that the file was not writen to disk, adding these lines at the end of the file fixed the problem :

train_writer.flush()
train_writer.close()

Also it was necessary for me to change LOGDIR to LOGDIR='logs/tests/1/' (removed the first '\')
hope this help for anybody who have this problem as well.

@avielas

This comment has been minimized.

Copy link

commented May 12, 2018

thanks! it was helpful

@sifat62

This comment has been minimized.

Copy link

commented May 25, 2018

I have tried this code but this error message shown in Jupyter, any suggestion?


DecodeError Traceback (most recent call last)
in ()
5 with gfile.FastGFile(model_filename, 'rb') as f:
6 graph_def = tf.GraphDef()
----> 7 graph_def.ParseFromString(f.read())
8 g_in = tf.import_graph_def(graph_def)
9 LOGDIR='/logs'

DecodeError: Error parsing message

@andrewginns

This comment has been minimized.

Copy link

commented Jun 14, 2018

Perfect! This was super helpful

@xyzdcgan

This comment has been minimized.

Copy link

commented Jan 1, 2019

While adding my frozen.pb file to it, it gives me an error
Traceback (most recent call last):
File "convert.py", line 30, in
g_in = tf.import_graph_def(graph_def)
File "/home/ios/.local/lib/python2.7/site-packages/tensorflow/python/util/deprecation.py", line 432, in new_func
return func(*args, **kwargs)
File "/home/ios/.local/lib/python2.7/site-packages/tensorflow/python/framework/importer.py", line 671, in import_graph_def
node, 'Input tensor %r %s' % (input_name, te)))
ValueError: graph_def is invalid at node u'Genc/Conv/BatchNorm/AssignMovingAvg': Input tensor 'Genc/Conv/BatchNorm/moving_mean:0' Cannot convert a tensor of type float32 to an input of type float32_ref.

and if run it through another code which is:

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)
  	#print(sm)
  	if 1 != len(sm.meta_graphs):
  		print('More than one graph found. Not sure which to write')
  		sys.exit(1)

  	#graph_def = tf.GraphDef()
    #graph_def.ParseFromString(sm.meta_graphs[0])
    g_in = tf.import_graph_def(sm.meta_graphs[0].graph_def)

LOGDIR='YOUR_LOG_LOCATION'
train_writer = tf.summary.FileWriter(LOGDIR)
train_writer.add_graph(sess.graph)

and gave me error:

google.protobuf.message.DecodeError: Error parsing message

any solution for this..??

i converted to .pb file from checkpoint file using tensorflow frozen_python file.

@Santosh7vasa

This comment has been minimized.

Copy link

commented Feb 14, 2019

use tf.gfile.GFile instead of "gfile.FastGFile" and add
train_writer.flush()
train_writer.close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.