Issue when using get_activations
pabcrepe opened this issue · 6 comments
Hi,
I tried to use your example code prior to use this library with my own code. However, I had an issue when I launched the function get_activations from the keract library.
The issue that is raised is: "AssertionError: Not a model or layer!".
I do not why, since the first parameter of function get_activations is a Model object (<keras.engine.training.Model object at 0x7f8c9c1137b8>).
I would appreciate if you could help me out.
Thank you!
Pablo.
@pabcrepe Hi,
Can you share a snippet that I can run to reproduce the error. Thank you!
I used the example code:
import numpy as np
from keras import Input, Model
from keras.layers import Dense, concatenate
from keract import get_activations
# model definition
i1 = Input(shape=(10,), name='i1')
i2 = Input(shape=(10,), name='i2')
a = Dense(1, name='fc1')(i1)
b = Dense(1, name='fc2')(i2)
c = concatenate([a, b], name='concat')
d = Dense(1, name='out')(c)
model = Model(inputs=[i1, i2], outputs=[d])
# inputs to the model
x = [np.random.uniform(size=(32, 10)), np.random.uniform(size=(32, 10))]
# call to fetch the activations of the model.
activations = get_activations(model, x, auto_compile=True)
# print the activations shapes.
[print(k, '->', v.shape, '- Numpy array') for (k, v) in activations.items()]
Moreover, the library versions I have installed are:
- Numpy 1.18.5
- Keras 2.3.1
- Keract 4.2.2
I run the code through Google Colab, and it raises the error "AssetionError: Not a model or layer!" when running _get_nodes function from get_activations.
Thank you!
@pabcrepe great catch. Please update the imports from:
from keras import Input, Model
from keras.layers import Dense, concatenate
to
from tensorflow.keras import Input, Model
from tensorflow.keras.layers import Dense, concatenate
That should solve it. I'm going to update the README.
The keras version does not really matter now. As long as you have tensorflow>=2.0, it's fine.
Thank you! It worked!
However, after solving the issue using your example code, I have a new issue using my code. I am loading a saved model which is an object keras.engine.training.Model that was created with a lower version of tensorflow (I think that it was version 1.14), and therefore, it cannot be used as a parameter in the get_activations function.
Do you know how I could convert this saved model to an object tensorflow.python.keras.engine.training.Model without modifying and training the model again? Is it possible?
Thank you again!
Solved. It was very easy. If I load the model m, I have to write:
from tensorflow.keras import Model
a = Model(m)
Now I have other issues with the inbound nodes of the loaded model =)
Yes replace keras
by tensorflow.keras
everywhere =)