keras-vis
keras-vis copied to clipboard
using keras-vis with tf.keras
-
[x] Check that you are up-to-date with the master branch of keras-vis. You can update with: pip install git+git://github.com/raghakot/keras-vis.git --upgrade --no-deps
-
[x] If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.
-
[x] If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with: pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps
-
[x] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).
Hi,
Kudos to the work done with the library so far. However when using it with tf.keras I am facing the following error:
ValueError: Unknown initializer: GlorotUniform
Here is the code I used to train the model:
from tensorflow.keras.layers import Conv3D, MaxPool3D, Flatten, Dense
from tensorflow.keras.layers import Dropout, Input, BatchNormalization
from tensorflow.keras.layers import AvgPool3D
from tensorflow.keras import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import ReLU
from tensorflow.keras.layers import GlobalAveragePooling3D
from tensorflow.keras.layers import Add
def block_a(inputs, block_name, filters):
with tf.name_scope(block_name):
bn = BatchNormalization()(inputs)
relu = ReLU()(bn)
conv1 = Conv3D(filters=filters, strides=(1, 1, 1), kernel_size=(3, 3, 3), padding='same')(relu)
bn1 = BatchNormalization()(conv1)
relu1 = ReLU()(bn1)
conv2 = Conv3D(filters=filters, strides=(1, 1, 1), kernel_size=(3, 3, 3), padding='same')(relu1)
return conv2
def block_a(inputs, block_name, filters):
with tf.name_scope(block_name):
bn = BatchNormalization()(inputs)
relu = ReLU()(bn)
conv1 = Conv3D(filters=filters, strides=(1, 1, 1), kernel_size=(3, 3, 3), padding='same')(relu)
bn1 = BatchNormalization()(conv1)
relu1 = ReLU()(bn1)
conv2 = Conv3D(filters=filters, strides=(1, 1, 1), kernel_size=(3, 3, 3), padding='same')(relu1)
return conv2
def block_c(inputs, block_name, filters):
with tf.name_scope(block_name):
conv1a = Conv3D(filters=filters, strides=(1, 1, 1), kernel_size=(3, 3, 3), padding='same')(inputs)
bn1a = BatchNormalization()(conv1a)
relu1a = ReLU()(bn1a)
conv1b = Conv3D(filters=filters, strides=(1, 1, 1), kernel_size=(3, 3, 3), padding='same')(relu1a)
bn1b = BatchNormalization()(conv1b)
relu1b = ReLU()(bn1b)
return relu1b
inputs = Input(shape=(110, 110, 110, 1), name='input')
block1 = block_c(inputs, 'block_c', 32)
stepdown = Conv3D(filters=64, strides=(2, 2, 2), kernel_size=(3, 3, 3), padding='same')(block1)
block2 = block_a(stepdown, 'block_a1', 64)
addblock2 = Add()([block2, stepdown])
block3 = block_a(addblock2, 'block_a2', 64)
addblock3 = Add()([addblock2, block3])
block4 = block_b(addblock3, 'block_b1', 64)
block5 = block_a(block4, 'block_a3', 64)
addblock5 = Add()([block5, block4])
block6 = block_a(addblock5, 'block_a4', 64)
addblock6 = Add()([block6, addblock5])
block7 = block_b(addblock6, 'block_b2', 128)
block8 = block_a(block7, 'block_a5', 128)
addblock8 = Add()([block8, block7])
block9 = block_a(addblock8, 'block_a6', 128)
addblock9 = Add()([block9, addblock8])
pool = GlobalAveragePooling3D()(addblock9)
dense = Dense(128, activation='relu')(pool)
output = Dense(1, activation='softmax')(dense)
model = Model(inputs=inputs, outputs=output)
model.compile(optimizer=Adam(lr=1e-3), loss='binary_crossentropy', metrics=['accuracy'])
Getting the cam visualizations:
from vis.visualization import visualize_cam
from vis.utils import utils
from tensorflow.keras import activations
layer_idx = utils.find_layer_idx(model, 'dense_1')
model.layers[layer_idx].activation = activations.linear
model = utils.apply_modifications(model)
Here is the error that I get:
ValueError: Unknown initializer: GlorotUniform
And when I change the utils.apply_modifications(model) to utils.apply_modifications(model, custom_objects={"GlorotUniform": tf.keras.initializers.glorot_uniform}) I get
TypeError: tuple indices must be integers or slices, not list
Using tensorflow 1.12.0
Any help will be appreciated.
Hi, @jashshah .
The problem seems to be caused by tf.keras or something else.
At least, I believe keras-vis don't have the cause of the problem,
because utils.apply_modifications(model) just call keras.models.Model.save() and keras.models.load_model() internally.
https://github.com/raghakot/keras-vis/blob/668b0e11dab93f3487f23c17e07f40554a8939e9/vis/utils/utils.py#L112-L113
I suggest you ask StackOverFlow or Tensorflow's community this problem.
try this line of code it worked for me:
with CustomObjectScope({'GlorotUniform': glorot_uniform()}): .............................................
I have the same problem
Try this code if you used a custom loss for training your model, it also worked for me:
from keras.utils import CustomObjectScope
from keras.initializers import glorot_uniform
from tensorflow.keras.models import load_model
def myLoss(y_true, y_pred):
...
return Something
model = load_model('yourModel.hdf5', custom_objects={'myLoss': myLoss, "GlorotUniform": tf.keras.initializers.glorot_uniform})
Query : can't use Keras-viz : from vis.visualization import visualize_cam There is a compatibility issue with scipy version. scipy.misc import imresize not available in colab
Solution : Check that you are up-to-date with the master branch of keras-vis. You can update with: pip install git+git://github.com/raghakot/keras-vis.git --upgrade --no-deps
Above solution resolves my issue. Thanks.
I have the same problem on TensorFlow 1.14.0.
When I call
from tensorflow import keras
model = utils.apply_modifications(model)
ValueError: Unknown initializer: GlorotUniform
I get a different second error... when I call
model = utils.apply_modifications(model, custom_objects={"GlorotUniform": keras.initializers.glorot_uniform})
I get error
TypeError: Unexpected keyword argument passed to optimizer: name
I also get a different error when from
import keras
TypeError: glorot_uniform() got an unexpected keyword argument 'dtype'
I suspect the problem is... I am using tensorflow's version of keras
from tensorflow import keras
Whereas... this utils.py in keras-viz is using the keras library directly
from keras.models import load_model
Perhaps there is a mismatch between the two versions.
https://stackoverflow.com/questions/53183865/unknown-initializer-glorotuniform-when-loading-keras-model
Installing TensorFlow version 2.0 and passing the custom objects solved the issue for me.
from tensorflow.python.keras.models import load_model
model = load_model('model.h5',custom_objects={"adam": tf.keras.optimizers.Adam,
"mae":tf.keras.losses.mean_absolute_error})
Installing TensorFlow version 2.0 and passing the custom objects solved the issue for me.
Does this package support TF 2.0? When I run with TF 2.0, I get:
module 'tensorflow' has no attribute 'placeholder'
I'm trying to use the library with Tensorflow 1.14 using tf.keras, however if I install it with pip install git+git://github.com/raghakot/keras-vis.git --upgrade --no-deps I get an error saying that the keras module has not been found. That's expected.
Instead, if I install it with
pip install git+git://github.com/raghakot/keras-vis.git --upgrade
I get TypeError: tuple indices must be integers or slices, not list
when I run model = utils.apply_modifications(model)
[Edit] I have ported the library to tf.keras here https://github.com/alessandro-montanari/keras-vis/tree/tf-keras-support It seems to be working but has not been tested deeply yet.
Sorry for inconvenience. We've NOT been working for support Tensorflow 2.0 yet. For now, you guys can use alternative library that I developed for my own experiments. Would you please try it if it's okay.
https://github.com/keisen/tf-keras-vis
Hey,
I'm getting one of two errors: Error 1:
(gcloud.beta.ai-platform.versions.create) Create Version failed. Bad model detected with error: "Failed to load model: Unexpected error when loading the model: Unexpected keyword argument passed to optimizer: learning_rate (Error code: 0)"
Error 2:
(gcloud.beta.ai-platform.versions.create) Create Version failed. Bad model detected with error: "Failed to load model: Unexpected error when loading the model: Unexpected keyword argument passed to optimizer: name (Error code: 0)"
(the only diff in the above to errors is that one complains about learning rate, and the other complains about name)
both errors pop up even though I have made no code changes. I am trying to publish a model onto the ai-platform following this guide exactly: https://cloud.google.com/ml-engine/docs/tensorflow/custom-prediction-routine-keras
I've tried the custom_objects every way I think possible. Does anyone know a solution?
I'm trying to use the library with Tensorflow 1.14 using tf.keras, however if I install it with
pip install git+git://github.com/raghakot/keras-vis.git --upgrade --no-depsI get an error saying that the keras module has not been found. That's expected.Instead, if I install it with
pip install git+git://github.com/raghakot/keras-vis.git --upgradeI getTypeError: tuple indices must be integers or slices, not listwhen I runmodel = utils.apply_modifications(model)[Edit] I have ported the library to tf.keras here https://github.com/alessandro-montanari/keras-vis/tree/tf-keras-support It seems to be working but has not been tested deeply yet.
I am getting same errror
try this line of code it worked for me:
with CustomObjectScope({'GlorotUniform': glorot_uniform()}): .............................................
When I do that I get:
TypeError: glorot_uniform() got an unexpected keyword argument 'dtype'
I am facing the same issue with Keras Vis.
ValueError: Unknown initializer: GlorotUniform
model.layers[layer_index].activation = activations.linear model = utils.apply_modifications(model)
I have tried all the possible approaches mentioned above. Also tried re-installing via pip install git+https://github.com/raghakot/keras-vis.git -U
Let me know if a solution is available for this.
I switched to tf explain.
https://github.com/sicara/tf-explain