Skip to main content

A PyTorch port of the MatLab EmoNet network by Kragel et al., 2019.

Project description

EmoNet: A PyTorch port

This package contains a PyTorch port of the EmoNet network originally developed in MatLab described in the paper "Emotion schemas are embedded in the human visual system" by Krager et al., 2019. It is being distributed with explicit permission from the original (first) author.

The original model can be found at: https://github.com/ecco-laboratory/EmoNet
A PyTorch port by this lab is also available at: https://github.com/ecco-laboratory/emonet-pytorch.

Installation

To install this repository, either clone the GitLab project, or install using pip install emonet-py.

ℹ️ If you install through pip, the data folder is not included, as it is too big for distribution through PyPI.
Instead, the data will be downloaded automatically from the GitLab repository by the check_data_files.py script when running the code.
The files will automatically be deleted when uninstalling the package.

Contents

The data folder contains:

  • The original model parameters, as exported from MatLab (*.bz2 files).
  • The original mean pixel values used to preprocess images (img_mean.txt).
  • Two demo images to verify the integrity of the port, i.e., that the outputs generated by the PyTorch model closely match the original MatLab model outputs (demo_*.jpg).
  • A PyTorch state_dict object containing the PyTorch translation of the original weights, to be used in conjunction with an AlexNetBig instance to obtain the EmoNet model (emonet.pth).

The package emonet_py contains the following scripts:

  • alexnet_big.py: defines the original AlexNet model, compared to the updated version that comes with torchvision.models.
  • check_data_files.py: a script that will check whether the data folder, and all expected files in it, are present. If not, they will be downloaded automatically from the GitLab repository.
  • convert_emonet_matlab_weights.py: this script can be used to translate the MatLab model parameters to PyTorch. See its internal documentation for details on this process.
  • A demonstration script showing how to use the model (demo.py).
  • emonet.py: the script defining the EmoNet model, as well as a class, EmoNetPreProcess, to load and preprocess images using the same image normalization used by the original MatLab model.
  • emonet_arousal.py: an arousal prediction model, consisting of an extra linear layer following the EmoNet output layer (see paper).
  • emonet_valence.py: a valence prediction model, consisting of an extra linear layer following the EmoNet output layer (see paper).
  • test_integrity.py: a UnitTest to check the integrity of the ported model. Note that the arousal and valence models are also ports of the original models.

Usage

To load and use EmoNet, simply do (see emonet.py/demo.py):

import os
from emonet_py.emonet import EmoNet, EmoNetPreProcess
from emonet_py.emonet_arousal import EmoNetArousal
from emonet_py.emonet_valence import EmoNetValence

if __name__ == '__main__':
    emonet = EmoNet(b_eval=True)
    emonet_pp = EmoNetPreProcess()
    img_big = os.path.join('..', 'data', 'demo_big.jpg')
    img_loaded = emonet_pp(img_big)
    pred = emonet.emonet(img_loaded.unsqueeze(0))
    emonet.prettyprint(pred, b_pc=True)

    emo_aro = EmoNetArousal()
    print(f"Arousal: {emo_aro(img_loaded.unsqueeze(0))}")

    emo_val = EmoNetValence()
    print(f"Valence: {emo_val(img_loaded.unsqueeze(0))}")

Licensing

This repository is made available under an MIT license (see LICENSE.md). This is in agreement with the original repository, which also uses an MIT license.

Author: Laurent Mertens
Mail: laurent.mertens@kuleuven.be

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

emonet_py-1.0.5.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

emonet_py-1.0.5-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file emonet_py-1.0.5.tar.gz.

File metadata

  • Download URL: emonet_py-1.0.5.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for emonet_py-1.0.5.tar.gz
Algorithm Hash digest
SHA256 33b56a65d69a6f1baa0eeb55c9010c0b7e5b370d54cd3ec91219f49e88b9ac63
MD5 0efb5a53f222d07c0f6db16e3aba3aeb
BLAKE2b-256 ce58a9ee63b74c80835f0c7150c17249d191a919953a730c5936f833658b47be

See more details on using hashes here.

File details

Details for the file emonet_py-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: emonet_py-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for emonet_py-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 891b5d933e7013ef4007428a26b161f0ad873fd5c5f5bfed6f604c7507417742
MD5 6974c8fb4c5bc82e9989b3d353195849
BLAKE2b-256 74c6866012c276d59d31a63f3973a711d9ae82a4e0faeaf2257244fc9c9db4a9

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