|
| 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.5_192/mobilenet_v2_0.py", |
| 11 | + version_id="dL9QG5zqfCw_n5fkpQlxrts1BGmXjBDr", |
| 12 | + sha1="d5c7af8768f9f2475367ac1e48e204cc5cf004a0") |
| 13 | +model_weight_path = load_weight_file(bucket="brainscore-vision", folder_name="models", |
| 14 | + relative_path="mobilenet_v2_0.5_192/mobilenet_v2_0.5_192_frozen.pth", |
| 15 | + version_id="jgYzWCcWd8DIzvjvg4V8xL2O.vaqqaIh", |
| 16 | + sha1="e5aa083caa4833fccd48af0c578a45064824dd7f") |
| 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_5_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_5_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/abs-1801-04381, |
| 47 | + author = {Mark Sandler and |
| 48 | + Andrew G. Howard and |
| 49 | + Menglong Zhu and |
| 50 | + Andrey Zhmoginov and |
| 51 | + Liang{-}Chieh Chen}, |
| 52 | + title = {Inverted Residuals and Linear Bottlenecks: Mobile Networks for Classification, |
| 53 | + Detection and Segmentation}, |
| 54 | + journal = {CoRR}, |
| 55 | + volume = {abs/1801.04381}, |
| 56 | + year = {2018}, |
| 57 | + url = {http://arxiv.org/abs/1801.04381}, |
| 58 | + eprinttype = {arXiv}, |
| 59 | + eprint = {1801.04381}, |
| 60 | + timestamp = {Tue, 12 Jan 2021 15:30:06 +0100}, |
| 61 | + biburl = {https://dblp.org/rec/journals/corr/abs-1801-04381.bib}, |
| 62 | + bibsource = {dblp computer science bibliography, https://dblp.org} |
| 63 | +} |
| 64 | +''' |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == '__main__': |
| 68 | + # Use this method to ensure the correctness of the BaseModel implementations. |
| 69 | + # It executes a mock run of brain-score benchmarks. |
| 70 | + check_models.check_base_models(__name__) |
0 commit comments