Skip to main content

Extracting image features from state-of-the-art neural networks for Computer Vision made easy

Project description


:notebook_with_decorative_cover: Table of Contents

:star2: About the Project

thingsvision is a Python package that let's you easily extract image representations from many state-of-the-art neural networks for computer vision. In a nutshell, you feed thingsvision with a directory of images and tell it which neural network you are interested in. thingsvision will then give you the representation of the indicated neural network for each image so that you will end up with one feature vector per image. You can use these feature vectors for further analyses. We use the word features for short when we mean "image representation".

:rotating_light: Note: some function calls mentioned in the paper have been deprecated. To use this package successfully, exclusively follow this README and the Documentation. :rotating_light:

(back to top)

:mechanical_arm: Functionality

With thingsvision, you can:

  • extract features for any imageset from many popular networks.
  • extract features for any imageset from your custom networks.
  • extract features for >26,000 images from the THINGS image database.
  • optionally turn off the standard center cropping performed by many networks before extracting features.
  • extract features from HDF5 datasets directly (e.g. NSD stimuli)
  • conduct basic Representational Similarity Analysis (RSA) after feature extraction.
  • perform Centered Kernel Alignment (CKA) to compare image features across model-module combinations.

(back to top)

:file_cabinet: Model collection

Neural networks come from different sources. With thingsvision, you can extract image representations of all models from:

(back to top)

:running: Getting Started

:computer: Setting up your environment

Working locally.

First, create a new conda environment with Python version 3.8, 3.9, or 3.10 e.g. by using conda and the environment.yml file like so

$ conda env create --prefix /path/to/conda/envs/thingsvision --file envs/environment.yml
$ conda activate thingsvision

Then, activate the environment and simply install thingsvision via running the following pip command in your terminal.

$ pip install --upgrade thingsvision

Google Colab.

Alternatively, you can use Google Colab to play around with thingsvision by uploading your image data to Google Drive (via directory mounting). You can find the jupyter notebook using PyTorch here and the TensorFlow example here.

(back to top)

:mag: Basic usage

thingsvision was designed to make extracting features as easy as possible. Start by importing all the necessary components and instantiating a thingsvision extractor. Here we're using AlexNet from the torchvision library as the model to extract features from and also load the model to GPU for faster inference:

import torch
from thingsvision import get_extractor
from thingsvision.utils.storing import save_features
from thingsvision.utils.data import ImageDataset, DataLoader

model_name = 'alexnet'
source = 'torchvision'
device = 'cuda' if torch.cuda.is_available() else 'cpu'

extractor = get_extractor(
  model_name=model_name,
  source=source,
  device=device
)

Next, create the Dataset and Dataloader for your images. Here, we have all our images in a single directory root, which can also contain subfolders (e.g. for individual classes), so we're using the ImageDataset class.

root='path/to/root/img/directory' # (e.g., './images/)
batch_size = 32

dataset = ImageDataset(
  root=root,
  out_path='path/to/features',
  backend=extractor.get_backend(),
  transforms=extractor.get_transformations()
)

batches = DataLoader(
  dataset=dataset,
  batch_size=batch_size, 
  backend=extractor.get_backend()
)

Now all that is left is to extract the image features and store them to disk! Here we're extracting features from the last convolutional layer of AlexNet (features.10), but if you don't know which modules are available for a given model, just call extractor.show_model() to print all modules.

module_name = 'features.10'

features = extractor.extract_features(
  batches=batches,
  module_name=module_name,
  flatten_acts=True  # flatten 2D feature maps from convolutional layer
)

save_features(features, out_path='path/to/features', file_format='npy')

For more examples and explanations of additional functionality like how to optionally turn off center cropping, how to use HDF5 datasets (e.g. NSD stimuli), how to perform RSA or CKA, or how to easily extract features for the THINGS image database, please refer to the Documentation.

(back to top)

:wave: How to contribute

If you come across problems or have suggestions please submit an issue!

(back to top)

:warning: License

This GitHub repository is licensed under the MIT License - see the LICENSE.md file for details.

(back to top)

:page_with_curl: Citation

If you use this GitHub repository (or any modules associated with it), please cite our paper for the initial version of thingsvision as follows:

@article{Muttenthaler_2021,
	author = {Muttenthaler, Lukas and Hebart, Martin N.},
	title = {THINGSvision: A Python Toolbox for Streamlining the Extraction of Activations From Deep Neural Networks},
	journal ={Frontiers in Neuroinformatics},
	volume = {15},
	pages = {45},
	year = {2021},
	url = {https://www.frontiersin.org/article/10.3389/fninf.2021.679838},
	doi = {10.3389/fninf.2021.679838},
	issn = {1662-5196},
}

(back to top)

:gem: Contributions

Here, we list all the people that have made and still make this project possible. We only list contributors that have made substantial contributions without which core functionalities would not work and who are still actively involved. Contributors are listed in descending order according to their quality $\cup$ quantity of contributions.

(back to top)

Project details


Release history Release notifications | RSS feed

This version

2.2.4

Download files

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

Source Distribution

thingsvision-2.2.4.tar.gz (34.9 kB view hashes)

Uploaded Source

Built Distribution

thingsvision-2.2.4-py3-none-any.whl (100.3 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