Skip to main content

No project description provided

Project description

APalysis

Introduction

This project aims to:

  1. Visualize the raw activations and latent space activations of a single or a group of images for any neural networks.
  2. Compare individual and class activation pathways for different datasets.
  3. Apply visual analytics on the classification ambiguousness of the classes in a dataset.
  4. 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)

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

apalysis-0.0.2.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

apalysis-0.0.2-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file apalysis-0.0.2.tar.gz.

File metadata

  • Download URL: apalysis-0.0.2.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.13 Linux/6.6.8-2-MANJARO

File hashes

Hashes for apalysis-0.0.2.tar.gz
Algorithm Hash digest
SHA256 b6229c63b647fa160a25ac67f4346fca45ea452c363a6db25ba1f5a8b56fbe6b
MD5 881dad17ee649363aab4479f8493674b
BLAKE2b-256 cfa7614dd437b7abfc793615c44214793eab3ca0995184f5eacce6b777f88cd1

See more details on using hashes here.

File details

Details for the file apalysis-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: apalysis-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.13 Linux/6.6.8-2-MANJARO

File hashes

Hashes for apalysis-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d83a757fd8610a399309f80335f63db48d37e80915305f0c740501ec5830ec28
MD5 201e7a32ed4f7ebf16d5dad64ed8ce66
BLAKE2b-256 04e8399e5608bef790317ba9b329bd2f4d37ee0ba280db9453ebaa8510ba8b72

See more details on using hashes here.

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