pythontensorflowkerastensorattributeerror

AttributeError: EagerTensor object has no attribute 'astype'


I am trying to do a GradCAM Heatmap in Google Colab like so:

import tensorflow as tf
from tensorflow.keras import backend as K
from tf_keras_vis.activation_maximization import ActivationMaximization
from tf_keras_vis.utils.callbacks import Print

def model_modifier(m):
    m.layers[-1].activation = tf.keras.activations.linear

activation_maximization = ActivationMaximization(model, model_modifier)

loss = lambda x: K.mean(x[:, 1])

activation = activation_maximization(loss, callbacks=[Print(interval=100)])
image = activation[0].astype(np.uint8)         # <----- error

f, ax = plt.subplots(figsize=(10, 5), subplot_kw={'xticks': [], 'yticks': []})
ax.imshow(image)
plt.show()

but I get an error

AttributeError: EagerTensor object has no attribute 'astype'. 

Solution

  • Convert to numpy first with activation[0].numpy(), then you should be able to use numpy array methods.