Model metamers reveal divergent invariances between biological and artificial neural networks.
Feather, Leclerc, Mądry & McDermott. Nature Neuroscience (2023).
- Overview
- Repo Directories
- Installation Guide
- Feather et al. 2023 figure replications
- Metamer generation from the command line
- Null distribution generation from the command line
- Setup configuration for your own model
- Citation
- Authors
- Acknowledgments
- License
Deep neural network models of sensory systems are often proposed to learn representational transformations with invariances like those in the brain. To reveal these invariances we generated "model metamers" — stimuli whose activations within a model stage are matched to those of a natural stimulus. In the paper "Model metamers reveal divergent invariances between biological and artificial neural networks", we demonstrated that metamers for state-of-the-art supervised and unsupervised neural network models of vision and audition were often completely unrecognizable to humans when generated from deep model stages, suggesting differences between model and human invariances. Targeted model changes improved human-recognizability of model metamers, but did not eliminate the overall human-model discrepancy. The human-recognizability of a model's metamers was well predicted by their recognizability by other models, suggesting that models learn idiosyncratic invariances in addition to those required by the task. Metamer recognition dissociated from both traditional brain-based benchmarks and adversarial vulnerability, revealing a distinct failure mode of existing sensory models and providing a complementary benchmark for model assessment.
Here, we provide code for replicating the main analyses in the paper, including instructions to download PyTorch checkpoints for the analyzed models. We additionally provide code and a tutorial for generating metamers from a custom model, comparing the metamers to a "null distribution" to validate optimization success, and example experiments to run on Amazon Mechanical Turk to test human recognition of generated model metamers.
- model_analysis_folders: folders containing the files to build the pre-trained neural networks, and configuration files specifying the analysis parameters used in Feather et al. 2023.
- analysis_scripts: python files used to run each of the analyses for visual and audio metamer generation, null distribution construction, network evaluation (on clean and adversarial data), network-network comparisons, and fMRI prediction analysis for an auditory dataset.
- AuditoryBehavioralExperiments: Human data for each of the word-recognition human behavioral experiments. Also includes an html file as an example experiment for Amazon Mechanical Turk and the matlab scripts to replicate the ANOVA statistics.
- VisionBehavioralExperiments: Human data for each of the 16-way image classification human behavioral experiments. Also includes an html file as an example experiment for Amazon Mechanical Turk and the matlab scripts to replicate the ANOVA statistics.
- robustness: Scripts for training and model evaluation, based on https://github.com/MadryLab/robustness, with changes for metamer generation, model architectures, and training with auditory stimuli.
- notebooks: Jupyter notebooks for Figure replication, plotting the human and model recognition of metamers and the auditory fMRI analysis. Also includes notebooks that show example model metamers for each experiment.
- matlab_statistics_functions: matlab functions for running the anovas used for statistical comparisons.
The python module versions used for the analysis in Feather et al. 2023 are output into feather_metamers_conda_2022.yml. Using conda, this environment can be created with the following:
conda env create -f feather_metamers_conda_2022.yml
A .yml
file containing dependencies for newer models can be found here (also, try this if packages are no longer available from the original file).
yml_files_for_dependencies/feather_metamers_conda_2023_updated_for_new_model_requirements.yml
Currently, the repository is not set up for pip installation.
ANOVA statistics were performed in MATLAB 2021a (not included in the conda installation).
Some files are too large for uploading to github and are hosted elsewhere. A helper script is included for downloading.
- Visual model checkpoints
- Auditory model checkpoints
- Base stimulus sets necessary for metamer generation
- Auditory fMRI data
The helper script can be run with python download_large_files.py
. Note: Downloading all of the assets will take ~15GB of storage! If you only need some of the files, only run the necessary lines from download_large_files.py
.
All code for Feather et al. 2023 was run and tested on the MIT OpenMind Computing Cluster with the CentOS Linux 7 (Core) operating system. Model training and metamer generation rely on GPUs. Metamer generation was performed on NVIDIA GPUs with a minimum of 11 GB of RAM. GPUs used for model training are further documented for visual models and auditory models.
Metamer generation and model training work best on machines with GPUs. Running the full 24000 iterations of metamer generation used in Feather et al. for one image or sound can take between 5 minutes and 2 hours depending on the model architecture and the generation layer. Statistical tests, fMRI regression, and exploring the human behavioral data do not require a GPU.
In addition to the code released for metamer generation and null distribution comparison, we include code for replication of
results in Feather et al. 2023. Figure generation for data presented in main-text figures is included in notebooks documented below.
In addition, within each network analysis directory (ie model_analysis_folders/visual_models/cochresnet50)
we include the scripts used to submit the jobs to our computing cluster (all scripts of the form submit_*.sh
). The python commands within these
submission scripts contain the necessary information to replicate metamer generation, null distribution comparison, and fMRI regression analysis.
For convenience, a notebook is included at notebooks/All_Experiments_Metamer_Vs_Network_Predictions.ipynb which loads in and plots the human performance and generating network performance for audio and visual behavioral experiments in Feather et al. 2023.
Notebooks are also included that load example metamers for each experiment using jupyter widgets. The audio examples are at notebooks/AudioExperimentsListenToMetamers.ipynb and visual examples are at notebooks/VisualExperimentsViewMetamers.ipynb. Note that these, especially audio examples, may take time to load (samples are loaded from the Mcdermott lab website).
The data for proportion correct for each participant for each experiment are included in the repo in VisionBehavioralExperiments and AudioBehavioralExperiments
directories. Also included is the proportion correct for the generating model on the set of stimuli matched to each human participant. For example, the
file VisionBehavioralExperiments/EXP1_ANALYSIS/VisionExperiment1_network_vs_humans_datamatrix_alexnet_public.mat
contains the proportion correct for
each participant for metamers generated from each layer of the AlexNet architecture (corresponding to the 5th plot in Feather et al. 2023 Figure 2c).
It also contains the AlexNet proportion correct for the same sets of metamers. Note that experiments are numbered based on the scheme in
model_analysis_folders/all_model_info.py.
A summary of other network's predictions of metamers generated from one model is included within each model analysis folder within network_network_evaluations
. Notebooks are included at notebooks/AudioNetworkNetworkPredictions.ipynb and at notebooks/VisualNetworkNetworkPredictions.ipynb which load in the network-network predictions and make the plots included in Figure 8 of Feather et al. 2023.
The computed variance explained for each voxel for each split of data is saved within the regression_results/natsound_activations/
directory for each model included in Figure 7D of Feather et al. 2023. The notebook included at notebooks/fMRIBestLayerVsMetamerRecognition.ipynb walks through the analysis and statistics based on these regression values.
To recompute the variance explained for our models (or your own model) run the following:
- Compute the activations for the set of natural sounds with:
python measure_layer_activations_165_natural_sounds_pytorch.py 'build_network.py'
- Build a mapping between the model activations and the fMRI response, and test on held-out sounds
python run_regressions_all_voxels_om_natsounddata.py <LAYER_IDX> 'natsound_activations.h5' -Z
Using the model directory and build_network.py
file that is already assembled, you can generate metamers with the following:
Audio Networks:
python make_metamers_wsj400_behavior_only_save_metamer_layers <IDX> -D 'model_analysis_folders/audio_networks/<model_name>'
Visual Networks:
python make_metamers_imagenet_16_category_val_400_only_save_metamer_layers <IDX> -D 'model_analysis_folders/visual_networks/<model_name>'
To generate metamers with fewer iterations (recommended for debugging, quick model evaluation) reduce the number of iterations (number of updates at each learning rate) and the number of iteration repetitions (the number of learning rate drops). The total number of gradient steps used for metamer generation is ITERATIONS * NUMREPITERATIONS
Example:
python make_metamers_wsj400_behavior_only_save_metamer_layers <IDX> -I 100 -N 4 -D 'model_analysis_folders/audio_networks/<model_name>'
python make_metamers_imagenet_16_category_val_400_only_save_metamer_layers <IDX> -I 100 -N 4 -D 'model_analysis_folders/visual_networks/<model_name>'
The default datasets for generating metamers (used in Feather et al. 2023) have IDX values from 0-399. See the metamer generation scripts for additional options (including initialization changes, using custom sets of images or audio, etc).
Metamers will be output into the metamers
folder in the model directory that is specified.
For checking metamer optimization we recommend comparison to a null distribution generated from random pairs of image and audio.
Example script uses the training dataset specified within the build_network.py
file assembled for the model.
Null distribution will be output into the null_dist
folder in the model directory that is specified.
Example (100 null samples using training dataset specified in build_network.py
):
python make_null_distributions -N 100 -I 0 -R 0 --shuffle -D 'model_analysis_folders/visual_networks/<model_name>'
python make_null_distributions -N 100 -I 0 -R 0 -D 'model_analysis_folders/audio_networks/<model_name>'
- Create a new directory to store all of the model analysis files. Example directories to copy and modify are at the following:
- Visual Example: model_analysis_folders/visual_networks/example_blank_directory
- Audio Example: model_analysis_folders/audio_networks/example_blank_directory
- If you are using a new architecture, place the architecture in robustness/imagenet_models or robustness/audio_models. If you are
using an included architecture, you can move on to step 3. Otherwise, the architecture should have the following:
Compared to typical PyTorch models, the forward pass must have additional kwargs:
with_latent=False
,fake_relu=False
,no_relu=False
.
- The
with_latent
argument allows returning a dictionary ofall_outputs
, which contains the outputs from the intermediate stages of the PyTorch model. The keys for this dictionary will be used to specify the layers used for metamer generation. - The
fake_relu
argument allows for using a ReLU with a modified gradient for the metamer generation layer, which can improve optimization. To use this argument, the architecture will need to have flags to build in the appropriate ReLUs.
The included architectures have these modifications.
- Modify the
build_network.py
file in the directory that you created to include the name of the architecture to generate metamers from, a list of layers for metamer generation (these are the keys in theall_outputs
dictionary), and any additional dataset or model loading parameters that must be specified. You can also load a checkpoint by specifying a filepath in this code, if it is not included in the architecture file. There are TODO flags in theexample_blank_directory/build_network.py
files for these steps.
Note: If the model was trained with a different repository, you might need to remap some of the checkpoint keys. There are options for this in the model loader file (see model_analysis_folders/visual_models/resnet50_l2_3_robust/build_network.py for an example)
- Once the
build_network.py
file is set up, you can use any of the analysis_scripts included in the repo for metamer generation, null distribution measuring, or analysis! We typically place the output from the analysis in the model directory with thebuild_network.py
file, so that everything for a given model stays together.
This repository was released with the following pre-print. If you use this repository in your research, please cite as:
@article{feather2023model,
title={Model metamers illuminate divergences between biological and artificial neural networks},
author={Feather, Jenelle and Leclerc, Guillaume and M{\k{a}}dry, Aleksander and McDermott, Josh H},
journal={Nature Neuroscience},
year={2023},
}
- Jenelle Feather (https://github.com/jfeather)
- McDermott Lab: https://github.com/mcdermottLab
- MadryLab/Robustness Repo: https://github.com/MadryLab/robustness
This project is licensed under the MIT License - see the LICENSE.md file for details