tensorflow / lucid

A collection of infrastructure and tools for research in neural network interpretability.
Apache License 2.0
4.67k stars 654 forks source link

Model.load creates a model with no layers #219

Open virilo opened 4 years ago

virilo commented 4 years ago

Very small notebook (11 lines) showing how Model.load return an empty model.layers:

https://colab.research.google.com/drive/11DEJ0De30n0ZrdRMOzxPkzdFULWvuee9#scrollTo=Zw75gPQ3aKXk

virilo commented 4 years ago

Test code: (you can see it in Google Colab)

# -*- coding: utf-8 -*-
!pip uninstall lucid --yes
!pip install git+https://github.com/virilo/lucid.git
import lucid
lucid.__version__

!git clone https://github.com/virilo/lucid-issue
!find "lucid-issue/keras-2.2.4/" -type f  -exec mv {} . \;
!ls

from lucid.modelzoo.vision_models import Model
model = Model.load("saved_model.pb")
model.layers

len(model.layers)

len(model.layers) is zero

save_model.pb was created as follows:

from lucid.modelzoo.vision_models import Model as LucidModel
with K.get_session().as_default():
    images = tf.placeholder("float32", [None, 224, 224, 3], name="input")    LucidModel.save(
        "saved_model.pb",
        image_shape=[224, 224, 3],
        input_name='input',
        output_names=['dense_2/Softmax'], #['Softmax'],
        image_value_range=[-1.0,1.0],
    )
mariajmolina commented 4 years ago

@virilo Did you find a solution for this issue?

bsolino commented 4 years ago

How did you save the model? I am trying to replicate it but I'm encountering the following issue:

%tensorflow_version 1.x

import tensorflow as tf
from tensorflow.keras import backend as K

from lucid.modelzoo.vision_models import Model as LucidModel
with K.get_session().as_default():
    images = tf.placeholder("float32", [None, 224, 224, 3], name="input")
    LucidModel.save(
        "saved_model.pb",
        image_shape=[224, 224, 3],
        input_name='input',
        output_names=['dense_2/Softmax'], #['Softmax'],
        image_value_range=[-1.0,1.0],
    )
---------------------------------------------------------------------------

AssertionError                            Traceback (most recent call last)

<ipython-input-5-a1e7238040b7> in <module>()
     11         input_name='input',
     12         output_names=['dense_2/Softmax'], #['Softmax'],
---> 13         image_value_range=[-1.0,1.0],
     14     )

4 frames

/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/graph_util_impl.py in _assert_nodes_are_present(name_to_node, nodes)
    150   """Assert that nodes are present in the graph."""
    151   for d in nodes:
--> 152     assert d in name_to_node, "%s is not in graph" % d
    153 
    154 

AssertionError: dense_2/Softmax is not in graph

EDIT: To clarify, I am here because I'm encountering the same issue as you when I try to load my own models. I hoped that saving a known working model could give me an insight on what is happening under the hood.