-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Graph disconected: transfer-learning model with top-layer #63
Comments
Hi, @KENJI-JIN . At least, as far as I know, we can't avoid the error when we apply Gradcam to a model that contains a sub-model as a layer. So I recommend you to adopt ScoreCAM that is the gradient-free method. https://github.com/keisen/tf-keras-vis/blob/master/examples/attentions.ipynb |
Thank you for your response. I will try to adopt ScoreCAM. |
@KENJI-JIN , we found some problem in ScoreCAM, so we recommend you to temporally install and use the commit below: pip install -U git+https://github.com/keisen/tf-keras-vis.git@4a90becb02ed3d44825300fcb807dd58157787ba I'm sorry to have troubled you. Thanks! |
Thank you for your information. I will upgrade it. |
Hi @keisen Regarding these:
I applied ScoreCAM to a model that contained a sub-model as layer, but I still got the same error Here's a minimal, reproducible example: # ScoreCAM issue
# Version 0.7.2
import numpy as np
from tf_keras_vis.scorecam import ScoreCAM
from tf_keras_vis.utils.model_modifiers import ReplaceToLinear
from tf_keras_vis.utils.scores import CategoricalScore
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Input
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.utils import to_categorical
def get_model():
base_model = VGG16(
weights="imagenet", include_top=False, input_shape=(32, 32, 3)
)
base_model.trainable = False
inputs = Input(shape=(32, 32, 3))
x = base_model(inputs, training=False)
x = GlobalAveragePooling2D()(x)
outputs = Dense(10, activation='softmax')(x)
model = Model(inputs, outputs)
return model
if __name__ == '__main__':
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
y_train, y_test = to_categorical(y_train), to_categorical(y_test)
generator = ImageDataGenerator(rescale=1/255.)
data_gen = generator.flow(x_train, y_train, batch_size=64)
model = get_model()
model.compile(
loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"]
)
history = model.fit(
data_gen,
epochs=2 # just for the sake of this example
)
img, label = x_test[1]/255, np.argmax(y_test[1])
scorecam = ScoreCAM(model, model_modifier=ReplaceToLinear(), clone=True)
sc = scorecam(CategoricalScore(label), img, penultimate_layer=-1)
# the last line, raises:
# ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor
# (type_spec=TensorSpec(shape=(None, 32, 32, 3), dtype=tf.float32, name='input_3'),
# name='input_3', description="created by layer 'input_3'") at layer "block1_conv1".
# The following previous layers were accessed without issue: [] Do you have any idea what could be wrong? Thanks in advance |
@amessalas , you can avoid the error by improving def get_model():
base_model = VGG16(
weights="imagenet", include_top=False, input_shape=(32, 32, 3)
)
base_model.trainable = False
x = base_model.output
x = GlobalAveragePooling2D()(x)
outputs = Dense(10, activation='softmax')(x)
model = Model(base_model.inputs, outputs)
return model Thanks! |
Hi @KENJI-JIN , have you resolved this issue? Thanks! |
Thank you for contacting me. I haven't resolved this issue yet. Thank you in advance. |
@KENJI-JIN , I'm happy to hear that you were able to avoid the error.
I see. I'm looking forward to good news! Finally, if you like tf-keras-vis, please star this repository. |
I had exactly the same issue and found a solution for it. (watch out! i'm a beginner at this) Maybe it's easy to integrate into this library as the method should be generic enough? |
Hi @keisen ! I have a similar problem here, don't know if you could help me. This is the model i need to use grad-cam: Model: "functional_1" Layer (type) Output Shape Param # Connected to input_1 (InputLayer) [(None, 141, 898, 1 0 [] input_2 (InputLayer) [(None, 12, 144, 22 0 [] sequential (Sequential) (None, 64) 26448 ['input_1[0][0]'] sequential_1 (Sequential) (None, 96) 77424 ['input_2[0][0]'] concatenate (Concatenate) (None, 160) 0 ['sequential[0][0]', dense (Dense) (None, 16) 2576 ['concatenate[0][0]'] dense_1 (Dense) (None, 1) 17 ['dense[0][0]'] ================================================================================================== When i run Grad-Cam, i got this: ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 12, 144, 224, 1), dtype=tf.float32, name='conv3d_input'), name='conv3d_input', description="created by layer 'conv3d_input'") at layer "conv3d". The following previous layers were accessed without issue: [] Running ScoreCam i got the same answer... |
Hello, Does Anyone find a solution for this problem? Thank you |
Hi, I am also trying to apply functions from this package on a transfer learning problem and am getting a similar issue using Gradcam, below. Is there any update or ideas for work around on these features? Any more information about fixes or issues on this type of would be helpful. File "C:\Users\paperspace\anaconda3\lib\site-packages\tf_keras_vis\gradcam.py", line 105, in call File "C:\Users\paperspace\anaconda3\lib\site-packages\tf_keras_vis\gradcam.py", line 124, in _calculate_cam AttributeError: 'NoneType' object has no attribute 'ndim' Thanks, |
faced the same problem :( |
I built the transfer-learning model below.
top_layer
is used to resize images for utilizing more information of images .conv_base = MobileNetV3Small(weights='imagenet',
include_top=False,
input_shape=(224, 224, 3))
input_width=450
input_height=450
inputs = tf.keras.Input(shape=(input_height, input_width, 3))
top_layer = layers.Conv2D(filters=3, kernel_size=3, strides=1)(inputs)
top_layer = layers.MaxPool2D(pool_size=(2, 2))(top_layer)
x = conv_base(top_layer, training=False)
x = layers.GlobalAveragePooling2D()(x)
#x = layers.Dropout(0.2)(x)
outputs = layers.Dense(1)(x)
outputs = layers.Activation("sigmoid")(outputs)
model = Model(inputs, outputs)
Then when I try to call to GradCam I get:
ValueError: Graph disconnected:
I reffered this site. But I am using
top_layer
, so I could not do same way.https://stackoverflow.com/questions/60623869/gradcam-with-guided-backprop-for-transfer-learning-in-tensorflow-2-0
How can I solve this? Could you give me any idea?
The text was updated successfully, but these errors were encountered: