From 84a71b37a236075e32a64c9efadb0c2db0ccb94e Mon Sep 17 00:00:00 2001 From: Katherine Fairchild Date: Thu, 25 Apr 2024 19:55:44 -0400 Subject: [PATCH] add AlexNet_SIN to models (#798) Co-authored-by: AutoJenkins --- .../models/AlexNet_SIN/__init__.py | 8 +++++ brainscore_vision/models/AlexNet_SIN/model.py | 29 +++++++++++++++++++ .../models/AlexNet_SIN/requirements.txt | 2 ++ brainscore_vision/models/AlexNet_SIN/test.py | 1 + 4 files changed, 40 insertions(+) create mode 100644 brainscore_vision/models/AlexNet_SIN/__init__.py create mode 100644 brainscore_vision/models/AlexNet_SIN/model.py create mode 100644 brainscore_vision/models/AlexNet_SIN/requirements.txt create mode 100644 brainscore_vision/models/AlexNet_SIN/test.py diff --git a/brainscore_vision/models/AlexNet_SIN/__init__.py b/brainscore_vision/models/AlexNet_SIN/__init__.py new file mode 100644 index 000000000..2e7694d9d --- /dev/null +++ b/brainscore_vision/models/AlexNet_SIN/__init__.py @@ -0,0 +1,8 @@ +from brainscore_vision import model_registry +from brainscore_vision.model_helpers.brain_transformation import ModelCommitment +from .model import get_model, LAYERS + +model_registry['AlexNet_SIN'] = lambda: ModelCommitment( + identifier='AlexNet_SIN', + activations_model=get_model(), + layers=LAYERS) diff --git a/brainscore_vision/models/AlexNet_SIN/model.py b/brainscore_vision/models/AlexNet_SIN/model.py new file mode 100644 index 000000000..dea518e19 --- /dev/null +++ b/brainscore_vision/models/AlexNet_SIN/model.py @@ -0,0 +1,29 @@ +import functools + +import torchvision.models +import torch + +from brainscore_vision.model_helpers.activations.pytorch import PytorchWrapper +from brainscore_vision.model_helpers.activations.pytorch import load_preprocess_images + +BIBTEX = """@article{geirhos2018imagenet, + title={ImageNet-trained CNNs are biased towards texture; increasing shape bias improves accuracy and robustness}, + author={Geirhos, Robert and Rubisch, Patricia and Michaelis, Claudio and Bethge, Matthias and Wichmann, Felix A and Brendel, Wieland}, + journal={arXiv preprint arXiv:1811.12231}, + year={2018} +}""" + +LAYERS = ['features.module.2', 'features.module.5', 'features.module.7', 'features.module.9', 'features.module.12', + 'classifier.2', 'classifier.5'] + +model_url = 'https://bitbucket.org/robert_geirhos/texture-vs-shape-pretrained-models/raw/0008049cd10f74a944c6d5e90d4639927f8620ae/alexnet_train_60_epochs_lr0.001-b4aa5238.pth.tar' + +def get_model(): + model = torchvision.models.alexnet(pretrained=False) + model.features = torch.nn.DataParallel(model.features) + checkpoint = torch.utils.model_zoo.load_url(model_url, map_location="cpu") + model.load_state_dict(checkpoint["state_dict"]) + preprocessing = functools.partial(load_preprocess_images, image_size=224) + wrapper = PytorchWrapper(identifier='AlexNet_SIN', model=model, preprocessing=preprocessing) + wrapper.image_size = 224 + return wrapper diff --git a/brainscore_vision/models/AlexNet_SIN/requirements.txt b/brainscore_vision/models/AlexNet_SIN/requirements.txt new file mode 100644 index 000000000..e7ccd30e3 --- /dev/null +++ b/brainscore_vision/models/AlexNet_SIN/requirements.txt @@ -0,0 +1,2 @@ +torch +torchvision \ No newline at end of file diff --git a/brainscore_vision/models/AlexNet_SIN/test.py b/brainscore_vision/models/AlexNet_SIN/test.py new file mode 100644 index 000000000..3a4bf67e3 --- /dev/null +++ b/brainscore_vision/models/AlexNet_SIN/test.py @@ -0,0 +1 @@ +# Left empty as part of 2023 models migration