Comments (7)
My solution was not to define the model as sequential, but to add the target layer directly.
x = Lambda(target_layer, output_shape = target_category_loss_output_shape)(input_model.output)
model = Model(inputs=input_model.input, outputs=x)
The grad_cam function looks like this now:
nb_classes = 1000
target_layer = lambda x: target_category_loss(x, category_index, nb_classes)
x = Lambda(target_layer, output_shape = target_category_loss_output_shape)(input_model.output)
model = Model(inputs=input_model.input, outputs=x)
model.summary()
loss = K.sum(model.output)
conv_output = [l for l in model.layers if l.name is layer_name][0].output
grads = normalize(_compute_gradients(loss, [conv_output])[0])
gradient_function = K.function([model.input], [conv_output, grads])
output, grads_val = gradient_function([image])
output, grads_val = output[0, :], grads_val[0, :, :, :]
weights = np.mean(grads_val, axis = (0, 1))
cam = np.ones(output.shape[0 : 2], dtype = np.float32)
for i, w in enumerate(weights):
cam += w * output[:, :, i]
cam = cv2.resize(cam, (224, 224))
cam = np.maximum(cam, 0)
heatmap = cam / np.max(cam)
#Return to BGR [0..255] from the preprocessed image
image = image[0, :]
image -= np.min(image)
image = np.minimum(image, 255)
cam = cv2.applyColorMap(np.uint8(255*heatmap), cv2.COLORMAP_JET)
cam = np.float32(cam) + np.float32(image)
cam = 255 * cam / np.max(cam)
return np.uint8(cam), heatmap´´´
from keras-grad-cam.
@kobotschick One problem I had with your solution was a message that a list doesn't have an attributed called dtype. The solution was to change gradient_function = K.function([model.input], [conv_output, grads])
to gradient_function = K.function(model.input, [conv_output, grads])
from keras-grad-cam.
full grad_cam function along with import as follows:
from keras.models import Model
def grad_cam(input_model, image, category_index, layer_name):
nb_classes = 1000
target_layer = lambda x: target_category_loss(x, category_index, nb_classes)
x = Lambda(target_layer, output_shape = target_category_loss_output_shape)(input_model.output)
model = Model(inputs=input_model.input, outputs=x)
model.summary()
loss = K.sum(model.output)
conv_output = [l for l in model.layers if l.name is layer_name][0].output
grads = normalize(_compute_gradients(loss, [conv_output])[0])
gradient_function = K.function([model.input], [conv_output, grads])
output, grads_val = gradient_function([image])
output, grads_val = output[0, :], grads_val[0, :, :, :]
weights = np.mean(grads_val, axis = (0, 1))
cam = np.ones(output.shape[0 : 2], dtype = np.float32)
for i, w in enumerate(weights):
cam += w * output[:, :, i]
cam = cv2.resize(cam, (224, 224))
cam = np.maximum(cam, 0)
heatmap = cam / np.max(cam)
#Return to BGR [0..255] from the preprocessed image
image = image[0, :]
image -= np.min(image)
image = np.minimum(image, 255)
cam = cv2.applyColorMap(np.uint8(255*heatmap), cv2.COLORMAP_JET)
cam = np.float32(cam) + np.float32(image)
cam = 255 * cam / np.max(cam)
return np.uint8(cam), heatmap
from keras-grad-cam.
@kobotschick Thanks you for your proposals, I have dealed with the problem to follow the https://github.com/vense/keras-grad-cam/blob/master/grad-cam.py
from keras-grad-cam.
@kobotschick Where is Model() function defined?
from keras-grad-cam.
For everyone who is facing the same issue: I forked this repository and fixed all errors I was facing.
#27
https://github.com/PowerOfCreation/keras-grad-cam
from keras-grad-cam.
The following change works for me :
gradient_function = K.function([model.layers[0].get_input_at(1)], [conv_output, grads])
instead of
gradient_function = K.function([model.layers[0].get_input_at(0)], [conv_output, grads])
from keras-grad-cam.
Related Issues (20)
- Regarding the gradient
- ValueError: Tried to convert 'x' to a tensor and failed. Error: None values not supported. HOT 9
- 'Node' object has no attribute 'output_masks' HOT 2
- saliency is NaN for VGG16 like model with BatchNorm HOT 1
- It does not match exactly. Why?
- Grad-Cam for custom defined architecture HOT 3
- 3D images HOT 2
- zero mean intensity of gradient for some cases HOT 9
- high accuracy model with weak heatmap
- I feel using the gradient of last conv layer rule is more reasonable
- How can i use it with fully convolutional network??
- Running with cifar10 datset
- You must feed a value for placeholder tensor 'input_1_1' with dtype float and shape [?,299,299,3] HOT 1
- Apply GradCam to Cnn+LSTM HOT 2
- GradCam calculation
- 环境配置
- question: why replace keras.activations.relu to tf.nn.relu
- Requesting help with GradCam on Segmentation
- Grad-CAM for timeseries custom architecture
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from keras-grad-cam.