Giter Club home page Giter Club logo

keras-cam's Introduction

Keras implementation of class activation mapping

Paper / project page: http://cnnlocalization.csail.mit.edu

Paper authors' code with Caffe / matcaffe interface: https://github.com/metalbubble/CAM

Blog post on this repository: http://jacobcv.blogspot.com/2016/08/class-activation-maps-in-keras.html

Checkpoint with person/not person weights: https://drive.google.com/open?id=0B1l5JSkBbENBdk95ZW1DOUhqQUE

enter image description here

This project implements class activation maps with Keras.

Class activation maps are a simple technique to get the image regions relevant to a certain class.

This was fined tuned on VGG16 with images from here: http://pascal.inrialpes.fr/data/human

The model in model.py is a two category classifier, used to classify person / not a person.

python cam.py --model_path cam_checkpoint.hdf5 --image_path=image.jpg

usage: cam.py [-h] [--train TRAIN] [--image_path IMAGE_PATH]
          [--output_path OUTPUT_PATH] [--model_path MODEL_PATH]
          [--dataset_path DATASET_PATH]

optional arguments:
  -h, --help            show this help message and exit
  --train TRAIN         Train the network or visualize a CAM
  --image_path IMAGE_PATH
                        Path of an image to run the network on
  --output_path OUTPUT_PATH
                        Path of an image to run the network on
  --model_path MODEL_PATH
                        Path of the trained model
  --dataset_path DATASET_PATH
                        Path to image dataset. Should have pos/neg folders,
                        like in the inria person dataset.
                        http://pascal.inrialpes.fr/data/human/

keras-cam's People

Contributors

jacobgil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

keras-cam's Issues

Error in model construction

Trying to train the network results in the following error:

Traceback (most recent call last):
  File "cam.py", line 60, in <module>
    train(args.dataset_path)
  File "cam.py", line 11, in train
    model = get_model()
  File "~/master/code/keras-cam-master/model.py", line 53, in get_model
    model = VGG16_convolutions()
  File "~/master/code/keras-cam-master/model.py", line 17, in VGG16_convolutions
    model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1'))
  File "/Users/username/.virtualenvs/keraspython2/lib/python2.7/site-packages/keras/models.py", line 312, in add
    output_tensor = layer(self.outputs[0])
  File "/Users/username/.virtualenvs/keraspython2/lib/python2.7/site-packages/keras/engine/topology.py", line 487, in __call__
    self.build(input_shapes[0])
  File "/Users/username/.virtualenvs/keraspython2/lib/python2.7/site-packages/keras/layers/convolutional.py", line 410, in build
    self.W = self.init(self.W_shape, name='{}_W'.format(self.name))
  File "/Users/username/.virtualenvs/keraspython2/lib/python2.7/site-packages/keras/initializations.py", line 57, in glorot_uniform
    fan_in, fan_out = get_fans(shape, dim_ordering=dim_ordering)
  File "/Users/username/.virtualenvs/keraspython2/lib/python2.7/site-packages/keras/initializations.py", line 15, in get_fans
    receptive_field_size = np.prod(shape[2:])
  File "/Users/username/.virtualenvs/keraspython2/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2539, in prod
    out=out, **kwargs)
  File "/Users/username/.virtualenvs/keraspython2/lib/python2.7/site-packages/numpy/core/_methods.py", line 35, in _prod
    return umr_prod(a, axis, dtype, out, keepdims)
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

Running version Keras==1.1.1

License?

Hi Jacob,

Can you please let me what license do you have for your code. I used some bits of your code to build CAM visualizations in a project that will be made public soon, and I need to make sure our code gets the right credit.

Thank you.
George

Dense layers after GAP

Rather a question than an issue, but I was wondering whether a Dense layer in between the GAP and the final softmax activation layer could be added.
Thank you.

Problem about the dense layer

I had a problem with the approach which is clear in this line:

model.add(Dense(2, activation = 'softmax', init='uniform'))

As it is mentioned in the paper, trained weights in this layer are used for a weighted sum over the last produced activation maps.
For predicting a non-linear function and class score in an MLP, there should be at least two layers (one hidden layer and an output layer like Softmax).
But here, right after the GAP layer, only one FC layer with two units is added for classification.
Can anyone explain the reason?
And why the number of units is 2?

Error when passing Image path

Hi

When I tried the model by passing an image path I got:

Using Theano backend.
Traceback (most recent call last):
File "cam.py", line 62, in
visualize_class_activation_map(args.model_path, args.image_path, args.output_path)
File "cam.py", line 18, in visualize_class_activation_map
model = load_model(model_path)
File "/home/vyraun/anaconda2/lib/python2.7/site-packages/keras/models.py", line 121, in load_model
f = h5py.File(filepath, mode='r')
File "/home/vyraun/anaconda2/lib/python2.7/site-packages/h5py/_hl/files.py", line 267, in init
name = name.encode(sys.getfilesystemencoding())
AttributeError: 'NoneType' object has no attribute 'encode'

I guess I am not passing the parameter correctly.

Error while running command

I am getting following error while running the command : python cam.py --model_path cam_checkpoint.hdf5 --image_path=image.jpg

ValueError: bad marshal data (unknown type code)

I tried running this in both python versions, updated all the packages but still no success.

Please help.

K.function doesn't work

Hi
I follow your code to get the hidden layer output of an RNN. But it failed when I using K.function([model.layers[0].input], [model.layers[-1].output]), which causes an error:

MissingInputError: ("An input of the graph, used to compute DimShuffle{x,x}(keras_learning_phase), was not provided and not given a value.Use the Theano flag exception_verbosity='high',for more information on this error.", keras_learning_phase)

So is there any trick in your code? Or probably because I load model with model_from_json and model.load_weights?

error with the load_model_weights function

@jacobgil

I’am using your code for VGG-16 pre-trained model for Keras but with my own weights.

f = h5py.File(weights_path) nb_layers = len(f.attrs["layer_names"]) for k in range(nb_layers): if k >= len(model_MS.layers): # without the last (fully-connected) layers in the savefile break g = f['layer_{}'.format(k)] weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])] model.layers[k].set_weights(weights) model.layers[k].trainable = False f.close() print

This is the error I'm having:
KeyError: "Can't open attribute (can't locate attribute: 'nb_params')"

g has only this attributes : ['weight_names']

I described my error in this post:
https://stackoverflow.com/questions/58806714/keyerror-cant-open-attribute-cant-locate-attribute-nb-params

Model Error

I downloaded the checkpoint from the mentioned link: https://drive.google.com/open?id=0B1l5JSkBbENBdk95ZW1DOUhqQUE

When i run the command: python cam.py --model_path cam_checkpoint.hdf5 --image_path=image.jpg

I get the following error: File "/home/user/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1570, in _create_c_op
raise ValueError(str(e))
ValueError: Dimension 0 in both shapes must be equal, but are 3 and 64. Shapes are [3,3,5,64] and [64,3,3,3]. for 'Assign' (op: 'Assign') with input shapes: [3,3,5,64], [64,3,3,3].

The full Error is:
Traceback (most recent call last):
File "cam.py", line 61, in
visualize_class_activation_map(args.model_path, args.image_path, args.output_path)
File "cam.py", line 18, in visualize_class_activation_map
model = load_model(model_path)
File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 246, in load_model
topology.load_weights_from_hdf5_group(f['model_weights'], model.layers)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 3189, in load_weights_from_hdf5_group
K.batch_set_value(weight_value_tuples)
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2365, in batch_set_value
assign_op = x.assign(assign_placeholder)
File "/home/user/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 615, in assign
return state_ops.assign(self._variable, value, use_locking=use_locking)
File "/home/user/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/state_ops.py", line 283, in assign
validate_shape=validate_shape)
File "/home/user/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_state_ops.py", line 60, in assign
use_locking=use_locking, name=name)
File "/home/user/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/home/user/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3392, in create_op
op_def=op_def)
File "/home/user/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1734, in init
control_input_ops)
File "/home/user/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1570, in _create_c_op
raise ValueError(str(e))
ValueError: Dimension 0 in both shapes must be equal, but are 3 and 64. Shapes are [3,3,5,64] and [64,3,3,3]. for 'Assign' (op: 'Assign') with input shapes: [3,3,5,64], [64,3,3,3].

Any idea why is this happening?

Error in Visualization Code

See line 34: https://github.com/jacobgil/keras-cam/blob/master/cam.py

Shouldnt it be class_weights[1, :] instead of class_weights[:, 1]?

If this were a multiclass problem with 3 classes for instance my proposal would take the weight vector associated with that class. The way the code is now it would take the 2nd element in Class 0s weight vector, the 2nd element in Class 1's weight vector, and the 2nd element in Class 3's weight vector.

Am I missing something obvious? I looked at the class weight out put and confirmed that is what your current code is doing on line 34 which I cannot wrap my head around. I may be just confused about how CAM works.

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.