|
| 1 | +import functools |
| 2 | +from brainscore_vision.model_helpers.activations.pytorch import load_preprocess_images |
| 3 | +from brainscore_vision.model_helpers.activations.pytorch import PytorchWrapper |
| 4 | +from brainscore_vision.model_helpers.check_submission import check_models |
| 5 | +from brainscore_vision.model_helpers.s3 import load_weight_file |
| 6 | +import torch |
| 7 | +import imp |
| 8 | + |
| 9 | +model_path = load_weight_file(bucket="brainscore-vision", folder_name="models", |
| 10 | + relative_path="mobilenet_v2_0.75_192/mobilenet_v2_0.py", |
| 11 | + version_id=".Cx.l320j.RVVHTABgJpQO.deOHI.ldd", |
| 12 | + sha1="8d253c2faad210834b4d39b9ccc644165ed8e3f6") |
| 13 | +model_weight_path = load_weight_file(bucket="brainscore-vision", folder_name="models", |
| 14 | + relative_path="mobilenet_v2_0.75_192/mobilenet_v2_0.75_192_frozen.pth", |
| 15 | + version_id="XNBog9emIZQ7ngr8v2X1Om6YQsV7okFU", |
| 16 | + sha1="af063236e83cb92fd78ed3eb7d9d2d4a65d794ab") |
| 17 | +MainModel = imp.load_source('MainModel',model_path.as_posix()) |
| 18 | +model = torch.load(model_weight_path.as_posix()) |
| 19 | + |
| 20 | +def get_model(name): |
| 21 | + """ |
| 22 | + This method fetches an instance of a base model. The instance has to be callable and return a xarray object, |
| 23 | + containing activations. There exist standard wrapper implementations for common libraries, like pytorch and |
| 24 | + keras. Checkout the examples folder, to see more. For custom implementations check out the implementation of the |
| 25 | + wrappers. |
| 26 | + :param name: the name of the model to fetch |
| 27 | + :return: the model instance |
| 28 | + """ |
| 29 | + assert name == 'mobilenet_v2_0_75_192' |
| 30 | + preprocessing = functools.partial(load_preprocess_images, image_size=192) |
| 31 | + wrapper = PytorchWrapper(identifier=name, model=model, preprocessing=preprocessing) |
| 32 | + wrapper.image_size = 192 |
| 33 | + return wrapper |
| 34 | + |
| 35 | + |
| 36 | +def get_layers(name): |
| 37 | + assert name == 'mobilenet_v2_0_75_192' |
| 38 | + return list(dict(model.named_modules()).keys())[1:] |
| 39 | + |
| 40 | + |
| 41 | +def get_bibtex(name): |
| 42 | + """ |
| 43 | + A method returning the bibtex reference of the requested model as a string. |
| 44 | + """ |
| 45 | + return ''' |
| 46 | +@article{DBLP:journals/corr/ZophVSL17, |
| 47 | + author = {Barret Zoph and |
| 48 | + Vijay Vasudevan and |
| 49 | + Jonathon Shlens and |
| 50 | + Quoc V. Le}, |
| 51 | + title = {Learning Transferable Architectures for Scalable Image Recognition}, |
| 52 | + journal = {CoRR}, |
| 53 | + volume = {abs/1707.07012}, |
| 54 | + year = {2017}, |
| 55 | + url = {http://arxiv.org/abs/1707.07012}, |
| 56 | + eprinttype = {arXiv}, |
| 57 | + eprint = {1707.07012}, |
| 58 | + timestamp = {Mon, 13 Aug 2018 16:48:00 +0200}, |
| 59 | + biburl = {https://dblp.org/rec/journals/corr/ZophVSL17.bib}, |
| 60 | + bibsource = {dblp computer science bibliography, https://dblp.org} |
| 61 | +} |
| 62 | +''' |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == '__main__': |
| 66 | + # Use this method to ensure the correctness of the BaseModel implementations. |
| 67 | + # It executes a mock run of brain-score benchmarks. |
| 68 | + check_models.check_base_models(__name__) |
0 commit comments