Giter Site home page Giter Site logo

chainer-grad-cam's Introduction

Chainer Grad-CAM

Experimental Chainer implementation of Grad-CAM without modifying network structure.

Caution

This implementation depends on Chainer internal implementation, so it may not work on later version of Chainer.

Requirements

  • Chainer 3.0
  • OpenCV

Sample Usage

import package

from lib import gradcam

Make Chainer neural network model

from chainercv.links import VGG16

model = VGG16(pretrained_model='imagenet')

Load image and make input array

image = cv2.imread(image_path)
image_size = (224, 224)
image = cv2.resize(image, image_size)

x = np.float32(image)
x = x.transpose((2, 0, 1))[np.newaxis,::-1,:,:]
x -= model.mean

Make gradient for model output

y_grad = np.zeros((1, 1000), dtype=np.float32)
y_grad[0, args.label] = 1.0

Calculate Grad-CAM

gcam = gradcam.gradcam(model, x, [model.conv5_3.conv, F.relu], y_grad=y_grad)

Make Grad-CAM heatmap

gcam = gcam[0]
heatmap_image = gradcam.heatmap(gcam, image_size)
cv2.imwrite(heatmap_path, heatmap_image)

Make Grad-CAM heatmap over input image

overlay_image = gradcam.overlay(image, gcam)
cv2.imwrite(overlay_path, overlay_image)

Sample program usage

$ python src/vgg16.py image_path heatmap_path overlay_path label [-g gpu_device]

Parameters

  • image_path: (Required) Input image file path
  • heatmap_path: (Required) File path for heatmap image
  • overlay_path: (Required) File path for heatmap over input image
  • label: (Required) Class label index
  • gpu_device: (Optional) GPU device ID, negative value indicates CPU (default: -1)

Example

$ python src/vgg16.py image/cat_dog.jpg cat_dog_heatmap.jpg cat_dog_overlay.jpg 242
Input image Heatmap Heatmap over input image
Input image Heatmap image Heatmap over input image

chainer-grad-cam's People

Contributors

dsanno avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.