- I trained a sequential model called image_learner. See below.
- When I tried to pass it through GradcamPlusPlus, it threw the error in the title.
Model: "sequential_3"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ conv2d_9 (Conv2D) │ (None, 64, 64, 8) │ 136 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ bn0 (BatchNormalization) │ (None, 64, 64, 8) │ 256 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dropout_9 (Dropout) │ (None, 64, 64, 8) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ max_pooling2d_9 (MaxPooling2D) │ (None, 32, 32, 8) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ conv2d_10 (Conv2D) │ (None, 32, 32, 16) │ 2,064 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ bn1 (BatchNormalization) │ (None, 32, 32, 16) │ 128 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dropout_10 (Dropout) │ (None, 32, 32, 16) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ max_pooling2d_10 (MaxPooling2D) │ (None, 16, 16, 16) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ conv2d_11 (Conv2D) │ (None, 16, 16, 32) │ 8,224 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ bn2 (BatchNormalization) │ (None, 16, 16, 32) │ 64 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dropout_11 (Dropout) │ (None, 16, 16, 32) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ max_pooling2d_11 (MaxPooling2D) │ (None, 8, 8, 32) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ flatten_3 (Flatten) │ (None, 2048) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense_6 (Dense) │ (None, 8) │ 16,392 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense_7 (Dense) │ (None, 2) │ 18 │
└─────────────────────────────────┴────────────────────────┴───────────────┘
Total params: 81,400 (317.97 KB)
Trainable params: 27,058 (105.70 KB)
Non-trainable params: 224 (896.00 B)
Optimizer params: 54,118 (211.40 KB)
from tf_keras_vis.gradcam_plus_plus import GradcamPlusPlus
# Create GradCAM++ object
gradcam = GradcamPlusPlus(image_learner,
model_modifier=replace2linear,
clone=True)
# Generate heatmap with GradCAM++
cam = gradcam(score,
X,
penultimate_layer=-1)
## Since v0.6.0, calling `normalize()` is NOT necessary.
# cam = normalize(cam)
# Render
f, ax = plt.subplots(nrows=1, ncols=3, figsize=(12, 4))
for i, title in enumerate(image_titles):
heatmap = np.uint8(cm.jet(cam[i])[..., :3] * 255)
ax[i].set_title(title, fontsize=16)
ax[i].imshow(images[i])
ax[i].imshow(heatmap, cmap='jet', alpha=0.5)
ax[i].axis('off')
plt.tight_layout()
plt.savefig('images/gradcam_plus_plus.png')
plt.show()
AttributeError Traceback (most recent call last)Cell In[37], line 41 from tf_keras_vis.gradcam_plus_plus import GradcamPlusPlus3 # Create GradCAM++ object----> 4 gradcam = GradcamPlusPlus(image_learner,5 model_modifier=replace2linear,6 clone=True)8 # Generate heatmap with GradCAM++9 cam = gradcam(score,10 X,11 penultimate_layer=-1)
File ~/Library/Caches/pypoetry/virtualenvs/tilda-defect-classific-LX6T3Nl7-py3.12/lib/python3.12/site-packages/tf_keras_vis/init.py:42, in ModelVisualization.init(self, model, model_modifier, clone)40 self.model.set_weights(model.get_weights())41 for modifier in model_modifiers:---> 42 new_model = modifier(self.model)43 if new_model is not None:44 self.model = new_model
File ~/Library/Caches/pypoetry/virtualenvs/tilda-defect-classific-LX6T3Nl7-py3.12/lib/python3.12/site-packages/tf_keras_vis/utils/model_modifiers.py:52, in ReplaceToLinear.call(self, model)51 def call(self, model) -> None:---> 52 layers = (model.get_layer(name=name) for name in model.output_names)53 for layer in layers:54 layer.activation = tf.keras.activations.linear
AttributeError: 'Sequential' object has no attribute 'output_names'
-
I tried the following. None worked.
-
Setting the image_learner.output_names to some values (the attribute did not previously exist).
-
Saving and re-loading the model.
-
I think it may have to do with the replace2linear function. How? I don’t know.
Any input would be appreciated.
will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.