Package allows visualize convolutional layers from keras models.
Project description
Keras Conv Visualizer
Package allows visualize convolutional layers from keras models.
Table of contents
General info
This package is a set of tools for visualizing convolutional layers from keras models. At this moment includes:
Libraries
- Keras - version 2.4.3
- Matplotlib - version 3.3.3
- NumPy - version 1.19.4
- OpenCV - version 4.4.0.46
- TensorFlow - version 2.4.0rc1
Setup
- Install from PyPi:
pip install keras-conv-visualizer
Documentation
Status: in progress
Filters visualization
import matplotlib.pyplot as plt
from tensorflow.keras.applications import VGG16
from keras_conv_visualizer.filters import FilterVisualization
# Model has to have standarized input (std=0, var=1)!
model = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3))
layer_name = "block5_conv3"
# First parameter - trained keras model, second - input_size
fv = FilterVisualization(model)
# First parameter - layer feature index (ex. block1_conv1 has (224, 224, 64) index is from 0 to 63)
# Second parameter - layer name
loss, img = fv.visualize_filter(0, layer_name)
plt.imshow(img)
Result:
Grad-CAM activation visualization
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.keras.applications import VGG16, imagenet_utils
import matplotlib.pyplot as plt
from keras_conv_visualizer.gradcam import GradCAM
img_path = 'elephant.jpg'
# load the input image from disk (in Keras/TensorFlow format) and preprocess it
image = load_img(img_path, target_size=(224, 224))
image = img_to_array(image)
image = imagenet_utils.preprocess_input(image)
model = VGG16(weights="imagenet", input_shape=(224, 224, 3))
cam = GradCAM(model)
# First parameter - image tensor, second - image path, third - alpha value for heatmap (transparency)
heatmap, output = cam.make_superimposed_img(image, img_path, alpha=0.6)
plt.imshow(heatmap)
plt.imshow(output)
Results:
Input image | Heatmap | Superimposed image |
Intermediate activations visualization
from keras.models import load_model
from keras.preprocessing import image
from keras_conv_visualization.intermediate_activations import IntermediateActivations
# load the input image from disk (in Keras/TensorFlow format) and preprocess it
img = image.load_img('some_image.png', target_size=(96, 96), color_mode='grayscale')
img_tensor = image.img_to_array(img)
img_tensor /= 255
model = load_model('some_model.h5')
int_activations = IntermediateActivations(model)
int_activations.plot_intermediate_activations(img_tensor)
Input image:
Results:
PyPi
TODO
- Add shap values
- Automatically recognition input size for FilterVisualization
Development
Want to contribute? Great!
To fix a bug or enhance an existing module, follow these steps:
- Fork the repo
- Create a new branch (
git checkout -b improve-feature
) - Make the appropriate changes in the files
- Verify if they are correct
- Add changes to reflect the changes made
- Commit changes
- Push to the branch (
git push origin improve-feature
) - Create a Pull Request
Status
Library is: in progress
Contact
albert.lis.1996@gmail.com - feel free to contact me!
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 keras_conv_visualizer-0.0.3.tar.gz
.
File metadata
- Download URL: keras_conv_visualizer-0.0.3.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02f3c820fc9392daf9048cf4863fb640bacc1f33b32ee148e5aa445242a4540c |
|
MD5 | b0c8b21e250ce38843013fc0d86799e1 |
|
BLAKE2b-256 | f3d6da097831412658534c7e51b66f935c3105263e07906deecabf3d9053cb85 |
File details
Details for the file keras_conv_visualizer-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: keras_conv_visualizer-0.0.3-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29b89012aa9be32500478cb536f25a5ff4c19756d88e5ad52761bef0275bd998 |
|
MD5 | bcf0ed7fa0b9bcb503598f7fd5843006 |
|
BLAKE2b-256 | cfe665f563bbb16ec2974582d96e3b335df47da04d56537e8c46d5c210b9bc77 |