Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TensorFlow: 2.0 saved_model support #342

Open
lutzroeder opened this issue Oct 6, 2019 · 8 comments
Open

TensorFlow: 2.0 saved_model support #342

lutzroeder opened this issue Oct 6, 2019 · 8 comments

Comments

@lutzroeder
Copy link
Owner

lutzroeder commented Oct 6, 2019

Examples:
0342.zip

@lutzroeder lutzroeder changed the title TensorFlow 2.0 support TensorFlow 2.0 saved_model support Oct 6, 2019
@gundersena
Copy link

Hi, this is a great program - thanks. I am using a subclassed model and therefore can only save_weights (TensorFlow SavedModel checkpoint). What is the easiest way to convert this into a supported file-type for Netron?

@HarryVancao
Copy link

I appreciate your efforts on this. Can you clarify the status on this feature? Is there supported in the desktop or browser version of Netron?

@devinlife
Copy link

I appreciate your efforts on netron. I really like netron!!!
I'm waiting for update for supporting TF2.0 saved_model.
I save models as Keras H5 type now.

@lutzroeder lutzroeder changed the title TensorFlow 2.0 saved_model support TensorFlow: 2.0 saved_model support Aug 7, 2020
@leondgarse
Copy link

leondgarse commented May 20, 2021

I think a temporary easiest way is using tflite, just works if model is not too complicated. There are times we cannot save a h5 format:

# A test model
class MyModel(tf.keras.Model):
    def __init__(self):
        super(MyModel, self).__init__()
        self.conv1 = keras.layers.Conv2D(32, 3, activation='relu')
        self.flatten = keras.layers.Flatten()
        self.d1 = keras.layers.Dense(10, activation='softmax')

    def call(self, x):
        x = self.conv1(x)
        x = self.flatten(x)
        return self.d1(x)
model = MyModel()
model(tf.ones([1, 28, 28, 3]))
model.save('aa')

# From a loaded saved_model:
cc = tf.lite.TFLiteConverter.from_keras_model(model)
# From a saved_model directory:
# cc = tf.lite.TFLiteConverter.from_saved_model('aa')
open('aa.tflite', 'wb').write(cc.convert())

@yuval-neureality
Copy link

Is TF 2.0 savedModel supported? it's looks weird. converting the TF2.0 SavedModel to ONNX works well with Netron though.

Thanks

@apivovarov
Copy link

I created basic TF image classification model as described in the tutorial (using tensorflow==2.5.0)
I saved the model.
There is saved_model.pb file Inside the saved model directory (which should contain the model graph).
When I open it in Netron I see just single super-node StatefulPartitionedCall with bunch on inputs.

  • How to see the actual graph?
  • Is it possible in general?
  • Can I "trace" saved model with some test input and save the graph?

Script to create and save test TF model:

import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10)
])

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])


class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images = train_images / 255.0
test_images = test_images / 255.0
model.fit(train_images, train_labels, epochs=10)

model.save("mymodel")

@apivovarov
Copy link

To visualize saved model "mymodel" with tensorboard

import tensorflow as tf
model=tf.saved_model.load("mymodel")
sig=model.signatures["serving_default"]
logdir="log"
writer = tf.summary.create_file_writer(logdir)
with writer.as_default():
  tf.summary.graph(sig.graph)

Looks a bit ugly , but at least smth https://www.dropbox.com/s/bhjugtur1gsxk0m/saved_model_serving_default_graph.png?dl=0

@apivovarov
Copy link

apivovarov commented Jul 2, 2021

The situation improved in TF 2.5.0. Now model.save("model_dir") creates one additional file in the model folder - keras_metadata.pb.
The difference btw prev versions is that now we can load the model back and get fully-featured Keras model.

model=tf.keras.models.load_model("model_dir")
type(model)
# tensorflow.python.keras.engine.sequential.Sequential

model._is_graph_network
# True

model.summary() # Works!

Now we can save this loaded "saved model" in h5 format and open it in Netron.

tf.keras.models.save_model(model, "model.h5")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants