Git Product home page Git Product logo

class-activation-mapping-cam-in-mxnet's Introduction

MxNetR for the Class Activation Mapping (CAM)

Background

This project propose a simple example to expose the implicit attention of Convolutional Neural Networks on the image. The paper is published at CVPR'16. The basic idea is based on global average pooling (GAP) layer in the last of network. The framework of the Class Activation Mapping (CAM) is as below:

F1

As illustrated in above figure, global average pooling (GAP) outputs the spatial average of the feature map of each unit at the last convolutional layer. A weighted sum of these values is used to generate the final output. Similarly, we compute a weighted sum of the feature maps of the last convolutional layer to obtain our class activation maps. We describe this more formally below for the case of softmax. The same technique can be applied to regression and other losses.

Fortunately, some of popular networks such as DenseNet, SqueezeNet, ResNet already use global average pooling (GAP) at the end, so we can directly use pre-trained model to generate the class activation mapping (CAM) without any modification. Here is a sample script to generate class activation mapping (CAM) by a pre-trained DenseNet-169.

Note: If you cannot find the 'densenet-imagenet-169-0-0125.params' file in 'model' directory, you can download it from here. This pre-trained parameter is contributed by bruinxiong/densenet.mxnet. After download, you can put this file in the 'model' directory.

Requirements

You can install the package directly in the R console.

cran <- getOption("repos")
cran["dmlc"] <- "https://s3-us-west-2.amazonaws.com/apache-mxnet/R/CRAN/"
options(repos = cran)
install.packages("mxnet")

You can install the package directly in the R console.

## try http:// if https:// URLs are not supported
source("https://bioconductor.org/biocLite.R")
biocLite("EBImage")

You can install the package directly in the R console.

install.packages(c('magrittr', 'imager'))

Start to try

The example code can be found from 'code/Class-Activation-Mapping.R', and the demo output is shown as following.

F2

The function was been coded in 'code/Single fucntion.R', it also support chinese labels.

F3

It's worth noting that denseNet can be applied to image with any size after a minor modification. The function was been coded in 'code/Single function (support any size).R'. However, because the limitation about architecture, the image size must be resized to multiple of 32. Moreover, this modification will decrease the accuracy.

F4

Other pre-trained MxNet Network

MXNet Model Zoo provides number of pre-trained model by imagenet. You can download them to modify this script. Among Inception v3 w/BatchNorm, ResidualNet152, ResNext101-64x4d can be directly used to generate the class activation mapping (CAM).

class-activation-mapping-cam-in-mxnet's People

Contributors

xup6fup avatar

Watchers

 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.