lutzroeder / netron

Visualizer for neural network, deep learning and machine learning models
https://netron.app
MIT License
27.96k stars 2.77k forks source link

TensorFlow: 2.0 saved_model support #342

Closed lutzroeder closed 3 months ago

lutzroeder commented 5 years ago

Examples: 0342.zip

gundersena commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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.

leondgarse commented 3 years ago

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 commented 3 years ago

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

Thanks

apivovarov commented 3 years ago

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.

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 commented 3 years ago

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 commented 3 years ago

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")