Neural network visualization toolkit for tf.keras
Project description
tf-keras-vis
Notes
We've released v0.7.0
! In this release, the gradient calculation of ActivationMaximization is changed for the sake of fixing a critical problem. Although the calculation result are now a bit different compared to the past versions, you could avoid it by using legacy implementation as follows:
# from tf_keras_vis.activation_maximization import ActivationMaximization
from tf_keras_vis.activation_maximization.legacy import ActivationMaximization
In addition to above, we've also fixed some problems related Regularizers. Although we newly provide tf_keras_vis.activation_maximization.regularizers
module that includes the regularizers whose bugs are fixed, like ActivationMaximization, you could also use legacy implementation as follows:
# from tf_keras_vis.activation_maximization.regularizers import Norm, TotalVariation2D
from tf_keras_vis.utils.regularizers import Norm, TotalVariation2D
Please see the release note for details. If you face any problem related to this release, please feel free to ask us in Issues page!
Overview
tf-keras-vis is a visualization toolkit for debugging tf.keras.Model
in Tensorflow2.0+.
Currently supported methods for visualization include:
- Feature Visualization
- ActivationMaximization
- Class Activation Maps
- Saliency Maps
tf-keras-vis is designed to be light-weight, flexible and ease of use. All visualizations have the features as follows:
- Support N-dim image inputs, that's, not only support pictures but also such as 3D images.
- Support batch wise processing, so, be able to efficiently process multiple input images.
- Support the model that have either multiple inputs or multiple outputs, or both.
- Support the mixed-precision model.
And in ActivationMaximization,
- Support Optimizers that are built to tf.keras.
Visualizations
Visualizing Dense Layer
Visualizing Convolutional Filer
GradCAM
The images above are generated by GradCAM++
.
Saliency Map
The images above are generated by SmoothGrad
.
Usage
- ActivationMaximization (Visualizing Convolutional Filter)
from matplotlib import pyplot as plt
from tf_keras_vis.activation_maximization import ActivationMaximization
from tf_keras_vis.activation_maximization.callbacks import Progress
from tf_keras_vis.activation_maximization.input_modifiers import Jitter, Rotate2D
from tf_keras_vis.activation_maximization.regularizers import TotalVariation2D, Norm
from tf_keras_vis.utils.model_modifiers import ExtractIntermediateLayer, ReplaceToLinear
from tf_keras_vis.utils.scores import CategoricalScore
# Create the visualization instance.
# All visualization classes accept a model and model-modifier, which, for example,
# replaces the activation of last layer to linear function so on, in constructor.
activation_maximization = \
ActivationMaximization(YOUR_MODEL_INSTANCE,
model_modifier=[ExtractIntermediateLayer('layer_name'),
ReplaceToLinear()],
clone=False)
# You can use Score class to specify visualizing target you want.
# And add regularizers or input-modifiers as needed.
activations = \
activation_maximization(CategoricalScore(FILTER_INDEX),
steps=200,
input_modifiers=[Jitter(jitter=16), Rotate2D(degree=1)],
regularizers=[TotalVariation2D(weight=1.0),
Norm(weight=0.3, p=1)],
optimizer=tf.keras.optimizers.RMSprop(1.0, 0.999),
callbacks=[Progress()])
## Since v0.6.0, calling `astype()` is NOT necessary.
# activations = activations[0].astype(np.uint8)
# Render
plt.imshow(activations[0])
- Gradcam++
from matplotlib import pyplot as plt
from matplotlib import cm
from tf_keras_vis.gradcam_plus_plus import GradcamPlusPlus
from tf_keras_vis.utils.model_modifiers import ReplaceToLinear
from tf_keras_vis.utils.scores import CategoricalScore
# Create GradCAM++ object
gradcam = GradcamPlusPlus(YOUR_MODEL_INSTANCE,
model_modifier=ReplaceToLinear(),
clone=True)
# Generate cam with GradCAM++
cam = gradcam(CategoricalScore(CATEGORICAL_INDEX),
SEED_INPUT,
penultimate_layer=-1)
## Since v0.6.0, calling `normalize()` is NOT necessary.
# cam = normalize(cam)
plt.imshow(SEED_INPUT_IMAGE)
heatmap = np.uint8(cm.jet(cam[0])[..., :3] * 255)
plt.imshow(heatmap, cmap='jet', alpha=0.5) # overlay
Please see the guides below for more details:
Getting Started Guides
[NOTE] If you have ever used keras-vis, you may feel that tf-keras-vis is similar with keras-vis. Actually tf-keras-vis derived from keras-vis, and both provided visualization methods are almost the same. But please notice that tf-keras-vis APIs does NOT have compatibility with keras-vis.
Requirements
- Python 3.6-3.9
- tensorflow>=2.0.4
Installation
- PyPI
$ pip install tf-keras-vis tensorflow
- Source (for development)
$ git clone https://github.com/keisen/tf-keras-vis.git
$ cd tf-keras-vis
$ pip install -e .[develop]
Use Cases
- chitra
- A Deep Learning Computer Vision library for easy data loading, model building and model interpretation with GradCAM/GradCAM++.
Known Issues
- With InceptionV3, ActivationMaximization doesn't work well, that's, it might generate meaninglessly blur image.
- With cascading model, Gradcam and Gradcam++ don't work well, that's, it might occur some error. So we recommend to use FasterScoreCAM in this case.
channels-first
models and data is unsupported.
ToDo
- Guides
- Visualizing multiple attention or activation images at once utilizing batch-system of model
- Define various score functions
- Visualizing attentions with multiple inputs models
- Visualizing attentions with multiple outputs models
- Advanced score functions
- Tuning Activation Maximization
- Visualizing attentions for N-dim image inputs
- Publish API documentations as a website
- We're going to add some methods such as below
- Deep Dream
- Style transfer
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tf-keras-vis-0.7.0.tar.gz
.
File metadata
- Download URL: tf-keras-vis-0.7.0.tar.gz
- Upload date:
- Size: 38.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bffccfab3c014076afd000dcdc926ff1569f1ba25f036fb2c7c4fb2f9c67884 |
|
MD5 | 63d85324cb7b4fdb85d2a80f9d2ee0bf |
|
BLAKE2b-256 | 64ed8c3caca025dc21b39087d9539015f78ba1c79195ab4762034fe97b84c9bb |
File details
Details for the file tf_keras_vis-0.7.0-py3-none-any.whl
.
File metadata
- Download URL: tf_keras_vis-0.7.0-py3-none-any.whl
- Upload date:
- Size: 52.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5efed4eb85d497ad32769a8bd2dee2d41f45a59bca66063fac1827e6e3143f8 |
|
MD5 | 9db328ed1550fefd12540022ee5231f5 |
|
BLAKE2b-256 | a7d72d7da444aad229f7941f0da99ed07a3fa0ad33f11a3cf85d244ef6cd0cfd |