keplr-io / quiver

Interactive convnet features visualization for Keras
https://keplr-io.github.io/quiver/
MIT License
1.77k stars 223 forks source link

RuntimeError "The layer has never been called and thus has no defined input shape." #83

Open tomassams opened 4 years ago

tomassams commented 4 years ago

I'm trying to install and run quiver to visualize a Keras model I've trained.

Running Windows 10 and Anaconda.

I installed quiver with git on a fresh conda environment

pip install git+git://github.com/keplr-io/quiver.git

When I tried to launch, I got the imsave error from #78

After fixing that, I got the following error:

AttributeError: module 'keras.backend' has no attribute 'image_dim_ordering'

After fixing that (replacing image_dim_ordering as noted in this issue in util.py) I'm getting the following:

Starting webserver from: D:\Anaconda3\envs\quiver_env\lib\site-packages\quiver_engine
Traceback (most recent call last):
  File "quiver.py", line 22, in <module>
    server.launch(model)
  File "D:\Anaconda3\envs\quiver_env\lib\site-packages\quiver_engine\server.py", line 157, in launch
    mean=mean, std=std
  File "D:\Anaconda3\envs\quiver_env\lib\site-packages\quiver_engine\server.py", line 47, in get_app
    single_input_shape, input_channels = get_input_config(model)
  File "D:\Anaconda3\envs\quiver_env\lib\site-packages\quiver_engine\util.py", line 43, in get_input_config
    model.get_input_shape_at(0)[1:3],
  File "D:\Anaconda3\envs\quiver_env\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 2010, in get_input_shape_at
    'input shape')
  File "D:\Anaconda3\envs\quiver_env\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 2603, in _get_node_attribute_at_index
    'and thus has no defined ' + attr_name + '.')
RuntimeError: The layer has never been called and thus has no defined input shape.

Here's my launch code:

model = keras.models.load_model('model.hdf5')
server.launch(model)

Is there a specific Tensorflow/Keras version I need to use or what am I doing wrong?

ghylander commented 3 years ago

also having this issue after going through the previous imsave and image_dim_ordering

if anyone knows a fix please share

ghylander commented 3 years ago

i think i fixed it in util.py, replace the get_input_config function with this one:

`def get_input_config(model): ''' returns a tuple (inputDimensions, numChannels) '''

return (
    model.get_config(0),
    model.get_config(0)
) if K.image_data_format() == 'th' else (
    #tf ordering
    model.get_layer(index=0).get_config(),
    model.get_layer(index=0).get_config()
)`
ghylander commented 3 years ago

probably needs more debuggin, but it doesn't throw an exception and it opens the localhost server

ghylander commented 3 years ago

the correct fix seems to be as follows:

def get_input_config(model):

    return (
        model.get_layer(index=0).get_config()["batch_input_shape"][0:3],
        model.get_layer(index=0).get_config()["batch_input_shape"][3]
    ) if K.image_data_format() == 'th' else (
        #tf ordering
        model.get_layer(index=0).get_config()["batch_input_shape"][0:3],
        model.get_layer(index=0).get_config()["batch_input_shape"][3]
    )
lsewcx commented 2 months ago

model = load_model('my_model.h5') model(tf.keras.Input((28, 28, 1))) #必须有输入才能得到输出

just like this you can run