Giter Club home page Giter Club logo

keras-visualize-activations's Introduction

Extract the activation maps of your Keras models

license dep1 dep2

Short code and useful examples to show how to get the activations for each layer for Keras.


A random seven from MNIST


Activation map of CONV1 of LeNet


Activation map of FC1 of LeNet


Activation map of Softmax of LeNet. Yes it's a seven!


The function for visualizing the activations is in the script read_activations.py

Inputs:

  • model: Keras model
  • model_inputs: Model inputs for which we want to get the activations (for example 200 MNIST images)
  • print_shape_only: If set to True, will print the entire activations arrays (might be very verbose!)
  • layer_name: Will retrieve the activations of a specific layer, if the name matches one of the existing layers of the model.

Outputs:

  • returns a list of each layer (by order of definition) and its corresponding activations.

I provide a simple example to see how it works with the MNIST model. I separated the training and the visualizations because if the two are done sequentially, we have to re-train the model every time we want to visualize the activations! Not very practical! Here are the main steps:

Running python model_train.py will do:

  • define the model
  • if no checkpoints are detected:
    • train the model
    • save the best model in checkpoints/
  • load the model from the best checkpoint
  • read the activations

Shapes of the activations (one sample):

----- activations -----
(1, 26, 26, 32)
(1, 24, 24, 64)
(1, 12, 12, 64)
(1, 12, 12, 64)
(1, 9216)
(1, 128)
(1, 128)
(1, 10) # softmax output!

Shapes of the activations (200 samples):

----- activations -----
(200, 26, 26, 32)
(200, 24, 24, 64)
(200, 12, 12, 64)
(200, 12, 12, 64)
(200, 9216)
(200, 128)
(200, 128)
(200, 10)

model_multi_inputs_train.py contains very simple examples to visualize activations with multi inputs models.

keras-visualize-activations's People

Contributors

zhhongzhi avatar

Watchers

James Cloos avatar Frank Wei 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.