APalysis
Introduction
This project aims to:
- Visualize the raw activations and latent space activations of a single or a group of images for any neural networks.
- Compare individual and class activation pathways for different datasets.
- Apply visual analytics on the classification ambiguousness of the classes in a dataset.
- Visual analytics to identify opportunities for pruning of neural networks.
Quickstart (w/ Docker)
You can run the following the start the whole dockerized application quickly in localhost.
docker run -p 8000:8000/tcp rahatzamancse/apalysis
And open http://localhost:8000 in your browser.
Installation
The project is available on PYPI. So you can install using
pip install apalysis
You will need a running redis server for the project to work. You can install redis using
# Install redis
sudo apt install redis-server # For debian-based distros
# sudo pacman -S redis # For arch
# Run redis
redis-server --daemonize yes
You can also install redis using docker. You can find the docker image here.
Usage
Tensorflow
# Import everything
from activation_pathway_analysis_backend.apalysis_tf import APAnalysisTensorflowModel
import tensorflow as tf
import tensorflow_datasets as tfds
import numpy as np
from nltk.corpus import wordnet as wn
# Load the tensorflow model
model = tf.keras.applications.vgg16.VGG16(weights='imagenet')
model.compile(loss="categorical_crossentropy", optimizer="adam")
# Load the dataset
# The dataset needs to be in tensorflow_datasets format
ds, info = tfds.load(
'imagenette/320px-v2',
shuffle_files=False,
with_info=True,
as_supervised=True,
batch_size=None,
)
# For classification, the labels is just a list of names for each encoded class
labels = names=list(map(lambda l: wn.synset_from_pos_and_offset(
l[0], int(l[1:])).name(), info.features['label'].names))
dataset = ds['train']
# To feed the dataset into the model, we need to provide a preprocessing function
vgg16_input_shape = tf.keras.applications.vgg16.VGG16().input.shape[1:3].as_list()
@tf.function
def preprocess(x, y):
x = tf.image.resize(x, vgg16_input_shape, method=tf.image.ResizeMethod.BILINEAR)
x = tf.keras.applications.vgg16.preprocess_input(x)
return x, y
# We also need to provide a preprocessing inverse function to convert the preprocessed image back to the original image
# This is used to display the original image in the frontend
# This will be automated in the future
def preprocess_inv(x, y):
x = x.squeeze(0)
# Again adding the mean pixel values
x[:, :, 0] += 103.939
x[:, :, 1] += 116.779
x[:, :, 2] += 123.68
x = x[:, :, ::-1]
x = np.clip(x, 0, 255).astype('uint8')
return x, y
# Create the server and run it
server = APAnalysisTensorflowModel(
model=model,
dataset=dataset,
label_names=labels,
preprocess=preprocess,
preprocess_inverse=preprocess_inv,
log_level="info",
)
server.run_server(host="localhost", port=8000)
PyTorch
Very similar to the above example.
# Imports
from activation_pathway_analysis_backend.apalysis_torch import APAnalysisTorchModel
import torch
import torchvision.models as models
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
import numpy as np
from nltk.corpus import wordnet as wn
# Load the model
model = models.vgg16(pretrained=True)
loss_fn = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters())
# We do not need any preprocessing function to provide to the server for pytorch. we can provide it to the dataset instead.
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,)) # MNIST mean and std
])
dataset = datasets.MNIST('/run/media/insane/SSD Games/Pytorch', train=True, download=True, transform=transform)
# Start the server
server = APAnalysisTorchModel(
model=model,
input_shape=(1, 3, 224, 224),
dataset=dataset,
label_names=labels,
log_level="info",
)
server.run_server(host="localhost", port=8000)
Metadata
Release files for APalysis 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| apalysis-0.0.2.tar.gz | 16.4 kB | Without provenance | Details for apalysis-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| apalysis-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for apalysis-0.0.2-py3-none-any.whl |
Total release size: 38.6 kB
Release files / apalysis-0.0.2.tar.gz
| Download URL | apalysis-0.0.2.tar.gz |
|---|---|
| Size | 16.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b6229c63b647fa160a25ac67f4346fca45ea452c363a6db25ba1f5a8b56fbe6b
|
|
BLAKE2b-256 checksum How to use checksums |
cfa7614dd437b7abfc793615c44214793eab3ca0995184f5eacce6b777f88cd1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.10.13 Linux/6.6.8-2-MANJARO
|
Release files / apalysis-0.0.2-py3-none-any.whl
| Download URL | apalysis-0.0.2-py3-none-any.whl |
|---|---|
| Size | 22.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d83a757fd8610a399309f80335f63db48d37e80915305f0c740501ec5830ec28
|
|
BLAKE2b-256 checksum How to use checksums |
04e8399e5608bef790317ba9b329bd2f4d37ee0ba280db9453ebaa8510ba8b72
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.10.13 Linux/6.6.8-2-MANJARO
|
Security for APalysis 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.