Skip to main content

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:

filters.png

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:

input-image.png

Results:

PyPi

keras-conv-visualizer

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

keras_conv_visualizer-0.0.3.tar.gz (7.1 kB view hashes)

Uploaded Source

Built Distribution

keras_conv_visualizer-0.0.3-py3-none-any.whl (7.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page