Giter Club home page Giter Club logo

attngan's Introduction

Self-Attention Visualization for GAN

This is pytorch implementation of Self-Attention GAN.

Code was heavily borrowed from heykeetae

The main purpose of this project is to visualize attention map. As in paper, self-attention can be seen as query, key and value triple and for generating pixel in every location, all locations will be queried resulting in probability distribution over all locations. Probability can be seen as weights which sum to one. Using weights and corresponding values we can get a real number used to adjust the value of certain location a little bit, by doing so for all locations we finally influence all pixels.

Here we only interested in weights, or probability which describes where and what level the model should pay attention to for per location. We specified one, and visualized the weights.

Example results

This is the feature maps(dim=64) taken from second last layer of generator. Red crossing stands for target location, and Blue dots are 10 locations that have greatest weights. The bigger, the greater weight they represent.

Here is one pixel on the hair and its attention map. The attention map(left) is the same one above.
(pixel of red dot in the right image is related to, but not only to red crossing in the above)

another result

Here is one location on the eye, with the same treating.

One might ask what does it mean when higher weights are assigned to certain location. In the original paper, the authors argued that self-attention helps network leverage complementary features in distant portions of the image(e.g. cat has 4 legs when generating cat image). In my experiment, my observation is, for location on the hair, it seems to conceptually capture hair outline like a sketch; higher weights may tell the model to focus more on those locations which indicate structure information.

However on the eye it seems failed to show meaningful results as I expected.(the color in the background didn't match pixel on the eye, which is black)

Prerequisites

  • Linux or macOS
  • Python3
  • NVIDIA GPU + CUDA CuDNN

Usage

1. Clone the repository

$ git clone https://github.com/houze-liu/Attention-Visualization-GAN.git
$ cd Attention-Visualization-GAN

2. Put in dataset

download celebA and put it under ./dataset, so it should be like ./dataset/celebA/IMAGES

3. Train

$ python main.py

After training, visualize_attn.ipynb should be able to load model.

Recent update

training monitor through visdom

$ pip install visdom

and run it:

$ visdom

Open the url in your browser and it should be like this:

TODO:

  • change where self-attention was inserted; different layer may show different results.

attngan's People

Contributors

houze-liu avatar

Watchers

James Cloos avatar  avatar paper2code - bot 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.