Closed HugoTex98 closed 3 years ago
Hi Hugo,
this is related to issue #227: iNNvestigate currently expects a keras
Model instead of a tf.keras
model.
This will be fixed with the next release of iNNvestigate!
In the meantime, you can get your code running by using keras.models.Sequential
instead of tf.keras.models.Sequential
:
import keras
import numpy as np
import tensorflow as tf
from keras.layers import Conv2D, Dense, Dropout, InputLayer, Reshape
import innvestigate
import innvestigate.utils as iutils
def ConnectomeCNN(input_shape, keep_pr=0.65, n_filter=32, n_dense1=64, n_classes=2):
bias_init = tf.constant_initializer(value=0.001)
input_1 = InputLayer(input_shape=input_shape, name="input")
conv1 = Conv2D(
filters=n_filter,
kernel_size=(1, input_shape[1]),
strides=(1, 1),
padding="valid",
activation="selu",
kernel_initializer="glorot_uniform",
bias_initializer=bias_init,
name="conv1",
input_shape=input_shape,
)
dropout1 = Dropout(keep_pr, name="dropout1")
conv2 = Conv2D(
filters=n_filter * 2,
kernel_size=(input_shape[1], 1),
strides=(1, 1),
padding="valid",
activation="selu",
kernel_initializer="glorot_uniform",
bias_initializer=bias_init,
name="conv2",
)
dropout2 = Dropout(keep_pr, name="dropout2")
reshape = Reshape((n_filter * 2,), name="reshape")
dense1 = Dense(
n_dense1, activation="selu", name="dense1", kernel_regularizer="l1_l2"
) # kernel_regularizer = regularizers.l1(0.0001))
if n_classes == 1:
activation = "sigmoid"
else:
activation = "softmax"
output = Dense(n_classes, activation=activation, name="output")
model = keras.models.Sequential(
[input_1, conv1, dropout1, conv2, dropout2, reshape, dense1, output]
)
return model
# Create dummy input data
input_shape = (10, 10, 3)
batch_size = 2
data_x = np.random.rand(batch_size, *input_shape)
# Create model without trailing softmax
model = ConnectomeCNN(input_shape)
model = iutils.keras.graph.model_wo_softmax(model)
# Analyze model
analyzer = innvestigate.create_analyzer("lrp.epsilon", model)
a = analyzer.analyze(data_x)
Thank you so much @adrhill !!
All the best!!
Hello!
I have been trying to apply an analyzer on a tf.keras model, as exemplified below.
I am able to create the intended analyzer to the model in cause, but when I try to apply the analyzer to my data
analysis = LRP_analyzer.analyze(data_FC)
it gives me this error"AttributeError: 'Node' object has no attribute 'output_masks’”
.I am currently using Tensorflow 1.11.0 and Keras 2.2.4 versions.
Any idea about the possible origin of the error? Thanks in advance!