Giter Site home page Giter Site logo

torchprism's Introduction

PRISM - Principal Image Sections Mapping

PRISM logo

A novel tool that utilizes Principal Component Analysis to display discriminative featues detected by a given convolutional neural network. It complies with virtually all CNNs.

Table of Contents

Usage

For user's convenience we have prepared an argument-feedable excutable prism. In order to use it, please prepare virtual env:

python3 -m venv venv
source ./venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
./prism

prism arguments

Argument Description Result
none Default PRISM exection with Gradual Extrapolation applied Vanilla result
--no-gradual-extrapolation Skipping Gradual Extrapolation Disabled Gradual Extrapolation
--inclusive Quantize colours and show only common for all images in batch Only common features
--inclusive & --no-gradual-extrapolation Quantize colours and show only common for all images in batch. Skip GE! Inclusive, no GE
--exclusive Quantize colours and show only unique features for images in batch Only unique features
--exclusive & --no-gradual-extrapolation Quantize colours and show only unique features for images in batch. Skip GE! Exclusive, no GE
--exclusive & --inclusive Quantize original PRISM output Skip GE! Quantized vanilla result without GE
--split-rgb Split PRISM output into separate RGB channels. RGB split
--split-rgb & --no-gradual-extrapolation Also split into RGB, but without Gradual Extrapolation. Skip GE! Note it can also go with --inclusive or --exclusive RGB split, no GE

Other arguments

Argument Description Default
--input=/path/to/... Path from where to take images. Note it is a glob, so value ./samples/**/*.jpg will mean: jpg images from ALL subfolders of samples ./samples/*.jpg
--model=model-name Model to be used with PRISM. Note that Gradual Extrapolation may not behave properly for some models outside vgg family. vgg16
--help Print help details and exit

Demo

Simplest snippet of working code.

import sys
sys.path.insert(0, "../")
from torchprism import PRISM
from torchvision import models
from utils import load_images, draw_input_n_prism

# load images into batch
input_batch = load_images()

model = models.vgg11(pretrained=True)
model.eval()
PRISM.register_hooks(model)

model(input_batch)
prism_maps_batch = PRISM.get_maps()

drawable_input_batch = input_batch.permute(0, 2, 3, 1).detach().cpu().numpy()
drawable_prism_maps_batch = prism_maps_batch.permute(0, 2, 3, 1).detach().cpu().numpy()

draw_input_n_prism(drawable_input_batch, drawable_prism_maps_batch)

First we have to import PRISM and torch models., as well as functions for preparing input images as simple torch batch and function to draw batches. Next we have to load the model, in this case a pretrained vgg11 has been chosen and then we have to call the first PRISM method to register required hooks in the model. With such a prepared model we can perform the classification and, since the actual output is not needed, we can just ignore it. Model execution is followed by using the second PRISM method to calculate features maps for the processed batch. Finally we have to prepare both input and PRISM output so they can be drawn and as the last step we call a method that displays them using e.g. matplotlib.

Results

The results allow us to see the discriminative features found by the model. On the sample images below we can see wolves

Snippet result

We can notice that all wolves have similar colors - features, found on their bodies. Furthermore the coyote also shows almost identical characteristics except the mouth element. wolves have a black stain around their noses, while coyote does not.

Variance for PCs

Also an image with variance is being plotted.

Read more

torchprism's People

Contributors

szandala avatar konradszafer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.