Giter VIP home page Giter VIP logo

alokwhitewolf / guided-attention-inference-network Goto Github PK

View Code? Open in Web Editor NEW
232.0 10.0 44.0 5.23 MB

Contains implementation of Guided Attention Inference Network (GAIN) presented in Tell Me Where to Look(CVPR 2018). This repository aims to apply GAIN on fcn8 architecture used for segmentation.

License: MIT License

Python 100.00%
neural-network computer computer-vision attention attention-mechanism deep-learning cvpr2018

guided-attention-inference-network's Introduction

Guided Attention for FCN

About

Chainer implementation of Tell Me Where To Look. This is an experiment to apply Guided Attention Inference Network(GAIN) as presented in the paper to Fully Convolutional Networks(FCN) used for segmentation purposes. The trained FCN8s model is fine tuned using guided attention.

GAIN

GAIN is based on supervising the attention maps that is produced when we train the network for the task of interest.

Image

FCN

Fully Convolutional Networks is a network architecture that consists of convolution layers followed by deconvolutions to give the segmentation output

Image

Approach

  • We take the fully trained FCN8 network and add a average pooling and fully connected layers after its convolutional layers. We freeze the convolutional layers and train the fully connected networks to classify for the objects. We do this in order to get GradCAMs for a particular class to be later used during GAIN

Image

  • Next we train the network as per the GAIN update rule. However in this implementation I have also considered the segmentation loss along with the GAIN updates/loss. This is because, I found using only the GAIN updates though did lead to convergence of losses, but also resulted in quite a significant dip in segmentation accuracies. In this step, the fully connected ayers are freezed and are not updated.

Loss Curves

For classification training

Image

Segmentation Loss during GAIN updates

Image

Qualitative Results

| Original Image | PreTrained GCAMs | Post GAIN GCAMs |

Image

Image

Image

Image

Quantitative Results

For FCN8s

Implementation Accuracy Accuracy Class Mean IU FWAVACC Model File
Original 91.2212 77.6146 65.5126 84.5445 fcn8s_from_caffe.npz
Experimental 90.5962 80.4099 64.6869 83.9952 To make public soon

How to use

pip install chainer
pip install chainercv
pip install cupy
pip install fcn

Training

For training the classifier, download. the pretrained FCN8s chainer model

python3 train_classifier.py --device 0

This will automatically download the pretrained file and train the classifier on it. You might run into an error of " xxx.txt file not found " while running this script. To solve this, at the place where your fcn library is installed, get the missing file from the fcn repository over github, and take care to put the exisiting file by making the same directory structure as asked in the error message. For more details, refer to this issue

For GAIN updates,

python3 train_GAIN.py --modelfile <path to the trained model with trained classifier> --device 0

The accuracy of original implementation is computed with (evaluate.py <path to the trained fcn8 model>) which has been borrowed from wkentaro's implementation

Visualization

visualize.py 

required arguements -

 --pretrained <path to the model file with trained classifier but not trained through GAIN method>
 --trained <path to the model trained with GAIN>

optional arguements -

  --device=-1 <device to assign to model, default uses cpu>
  --whole=False < whether to test on whole valid dataset>
  --shuffle=False <shuffles fataset loader>
  --no=10 <if whole is False, then no of images to visualize>

To Do

  • Push Visualization Code

Using GAIN for other models

I have attempted to make GAIN as modular as possible so that it can be used on some other model as well. All you would need to do is make GAIN class( which itself inherits chainer.Chain) as parent class to your model. Each GAIN model needs to have a few particular instance variables in order to be able to function. GAIN module has methods to instantiate every single one of them. I would advice you to lookup models/fcn8.py as well as GAIN.py to have an idea about them.

  • GAIN_functions - An ordered dict consisting of names of steps and it's associated functions.
  • final_conv_layer - Name of the step after which no convolutions happen
  • grad_target_layer - Name of the step from where gradients are to be collected for computing GradCAM

Credits

The original FCN module and the fcn package is courtesy of wkentaro

Citation

If you find this code useful in your research, please consider citing:

@misc{Alok2018,
Author = {Bishoyi, Alok Kumar},
Title = {Guided Attention Inference Network},
Year = {2018},
Publisher = {GitHub},
journal = {Github repository},
howpublished = {\url{https://github.com/alokwhitewolf/Guided-Attention-Inference-Network}},
}

guided-attention-inference-network's People

Contributors

alokwhitewolf 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

guided-attention-inference-network's Issues

Pretrained model not available

The pretrained model is not available. I tried following the code "python3 train_classifier.py --device 0" but the file is not there for usage.

No heating after a minimal training with some not too bad accuracy

Hi, thank you for your work.
Do you have any suggestions on this behavior ?

image

I ran the training for the classificator (without GAIN) the minimal amount of iterations just to have a snapshot of the model, same with GAIN (I've tried to use the previously pretrained model, and also without pretraining, same accuracy, no change).

The accuracy for both with and without GAIN is about 72%, although maybe it is explainable, why the result is the same (not trained enough), but isn't there should be any heating (in the meaning of the heating maps) just with 72% accuracy ?

Is this expected or anomalous behavior ?
I didn't use any pretrained models of FCN8, because I didn't find them available now, so I can look only at those results.
If it is anomalous, what can be the reason, where should I look for the problem for your opinion ?

Thank you in advance, appreciate your help.

KeyError: 'fc6/b is not a file in the archive'

After training classifer, it generates snapshot. When i use this snapshot to train gain, this error occurs. Maybe fc6/b is bias of conv layer, which is not in classifier model. In classifier, it is fully connected layer named fc6_cl.
I am new to chainer, how to load snapshot to Gain model?

The implement of attention loss

Thanks for this great implement. I go through your codes, and find that this repo has implemented the classification loss and attention mining loss in the paper. Do you have the plan to add the attention loss (directly compare the l2 distance of the attention map and pixel-wise mask) in the paper? Meanwhile, I notice that you add the segmentation loss in this repo. This segmentation loss is helpful for generating an accurate attention map?

some variables are assigned but never used

In visualize.py file, some variables are assigned but never used, for example, trainer in line 47 and undefined variable in line 49 etc.

Could you please check this out?

Do I need to install any deep-learning libraries?

Thanks a lot for your great work.
I have read your paper and now try to reproduce your work.
For this purpose, do I need to install any deep-learning libraries, such as pytorch or tensorflow?
Thanks a lot.

Trying to understand why xp.random.choice was used in set_init_grad function when the labels are available

Thanks for your code.
I had a question of the set_init_grad code reference here:

def set_init_grad(self, var, label):
var.grad = self.xp.zeros_like(var.data)
if label is None:
class_id = F.argmax(var).data
var.grad[0][class_id] = 1
else:
class_id = self.xp.random.choice(label, 1)
var.grad[0][class_id] = 1
return class_id

On line 61, you use xp.random.choice(label,1). Can you explain why are you using random choice when you know the ground truth label index? I did not see that in the original paper and was curious to know if it improved your accuracy or performance. @alokwhitewolf

ValueError: Loss is nan.

Thank you for sharing the implementation. I tried to reproduce by running 'train_classifier.py' which produces classifier_model_14640, then running train_GAIN.py with the classifier_model_14640 as the pretrained FCN8 model. However it throw out this error 'ValueError: Loss is nan.' at [line 206, fcn8.py]. All variables after conv2 seem to be all nan. Please could you have a look at this problem? Many thanks.

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.