Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ep/add inception v4 #1099

Merged
merged 5 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions brainscore_vision/models/inception_v4_pytorch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from brainscore_vision import model_registry
from brainscore_vision.model_helpers.brain_transformation import ModelCommitment
from .model import get_model, get_layers

model_registry['inception_v4_pytorch'] = lambda: ModelCommitment(identifier='inception_v4_pytorch',
activations_model=get_model('inception_v4_pytorch'),
layers=get_layers('inception_v4_pytorch'))
64 changes: 64 additions & 0 deletions brainscore_vision/models/inception_v4_pytorch/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from brainscore_vision.model_helpers.activations.pytorch import PytorchWrapper
from brainscore_vision.model_helpers.activations.pytorch import load_preprocess_images
import ssl
import functools
import timm
from brainscore_vision.model_helpers.check_submission import check_models

ssl._create_default_https_context = ssl._create_unverified_context

'''
This is a Pytorch implementation of inception_v4.

Previously on Brain-Score, this model existed as a Tensorflow model, and was converted via:
https://huggingface.co/docs/timm/en/models/inception-v4

Disclaimer: This (pytorch) implementation's Brain-Score scores might not align identically with Tensorflow
implementation.

'''


MODEL = timm.create_model('inception_v4', pretrained=True)

def get_model(name):
assert name == 'inception_v4_pytorch'
preprocessing = functools.partial(load_preprocess_images, image_size=299)
wrapper = PytorchWrapper(identifier='inception_v4_pytorch', model=MODEL,
preprocessing=preprocessing,
batch_size=4) # doesn't fit into 12 GB GPU memory otherwise
wrapper.image_size = 299
return wrapper


def get_layers(name):
assert name == 'inception_v4_pytorch'
layers = []
layers += ['Conv2d_1a_3x3']
layers += ['Mixed_3a']
layers += ['Mixed_4a']
layers += [f'Mixed_5{i}' for i in ['a', 'b', 'c', 'd', 'e']]
layers += [f'Mixed_6{i}' for i in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']]
layers += [f'Mixed_7{i}' for i in ['a', 'b', 'c', 'd']]
layers += ['global_pool']

return layers


def get_bibtex(model_identifier):
"""
A method returning the bibtex reference of the requested model as a string.
"""
return """@misc{szegedy2016inceptionv4,
title={Inception-v4, Inception-ResNet and the Impact of Residual Connections on Learning},
author={Christian Szegedy and Sergey Ioffe and Vincent Vanhoucke and Alex Alemi},
year={2016},
eprint={1602.07261},
archivePrefix={arXiv},
primaryClass={cs.CV}
}
"""


if __name__ == '__main__':
check_models.check_base_models(__name__)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
torch
torchvision
timm
8 changes: 8 additions & 0 deletions brainscore_vision/models/inception_v4_pytorch/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest
import brainscore_vision


@pytest.mark.travis_slow
def test_has_identifier():
model = brainscore_vision.load_model('inception_v4_pytorch')
assert model.identifier == 'inception_v4_pytorch'
Loading