This repository was archived by the owner on Jan 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Update sample model submission for new submission processing #7
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
44ae91e
Fix hook problem
franzigeiger 6fe61b7
Add documentation, change naming
franzigeiger ceeaa7a
Change score_model function call
franzigeiger 7b3c469
Add bibtex method, remove test modules (now located in model-tools)
franzigeiger 7de53f8
Add bibtex method and tensorflow example
franzigeiger fed1540
add database tests
franzigeiger 7b386b1
fix small issues
franzigeiger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,15 @@ | |
| from model_tools.activations.pytorch import PytorchWrapper | ||
| from model_tools.activations.pytorch import load_preprocess_images | ||
|
|
||
| from test import test_models | ||
|
|
||
|
|
||
| # This is an example implementation for submitting alexnet as a pytorch model | ||
| # If you use pytorch, don't forget to add it to the setup.py | ||
|
|
||
| # Attention: It is important, that the wrapper identifier is unique per model! | ||
| # The results will otherwise be the same due to brain-scores internal result caching mechanism. | ||
| # Please load your pytorch model for usage in CPU. There won't be GPUs available for scoring your model. | ||
| # If the model requires a GPU, contact the brain-score team directly. | ||
| from model_tools.check_submission import check_models | ||
|
|
||
|
|
||
| def get_model_list(): | ||
| return ['alexnet'] | ||
|
|
@@ -30,5 +33,18 @@ def get_layers(name): | |
| 'classifier.2', 'classifier.5'] | ||
|
|
||
|
|
||
| def get_bibtex(model_identifier): | ||
| return """@incollection{NIPS2012_4824, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add an |
||
| title = {ImageNet Classification with Deep Convolutional Neural Networks}, | ||
| author = {Alex Krizhevsky and Sutskever, Ilya and Hinton, Geoffrey E}, | ||
| booktitle = {Advances in Neural Information Processing Systems 25}, | ||
| editor = {F. Pereira and C. J. C. Burges and L. Bottou and K. Q. Weinberger}, | ||
| pages = {1097--1105}, | ||
| year = {2012}, | ||
| publisher = {Curran Associates, Inc.}, | ||
| url = {http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf} | ||
| }""" | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| test_models.test_base_models(__name__) | ||
| check_models.check_base_models(__name__) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import logging | ||
| import os | ||
|
|
||
| import s3 | ||
|
|
||
| from model_tools.activations import TensorflowWrapper, TensorflowSlimWrapper | ||
| from model_tools.activations.keras import load_images, KerasWrapper | ||
| import keras.applications | ||
|
|
||
|
|
||
| # This is an example implementation for submitting resnet50 as a tensorflow SLIM model to brain-score | ||
| # If you use tensorflow, don't forget to add it and its dependencies to the setup.py | ||
| from model_tools.utils import fullname | ||
|
|
||
|
|
||
| def get_model_list(): | ||
| return ['resnet50'] | ||
|
|
||
|
|
||
| def get_model(name): | ||
| assert name == 'resnet50' | ||
| model = TFSlimModel.init('resnet-50_v1', net_name='resnet_v1_50', preprocessing_type='vgg', | ||
| image_size=224, labels_offset=0) | ||
| model_preprocessing = keras.applications.resnet50.preprocess_input | ||
| load_preprocess = lambda image_filepaths: model_preprocessing(load_images(image_filepaths, image_size=224)) | ||
| wrapper = KerasWrapper(model, load_preprocess) | ||
| wrapper.image_size = 224 | ||
| return wrapper | ||
|
|
||
|
|
||
| def get_layers(name): | ||
| assert name == 'resnet-50' | ||
| return [f'block{i + 1}_pool' for i in range(5)] + ['fc1', 'fc2'] | ||
|
|
||
|
|
||
| def get_bibtex(model_identifier): | ||
| return """@article{DBLP:journals/corr/HeZRS15, | ||
| author = {Kaiming He and | ||
| Xiangyu Zhang and | ||
| Shaoqing Ren and | ||
| Jian Sun}, | ||
| title = {Deep Residual Learning for Image Recognition}, | ||
| journal = {CoRR}, | ||
| volume = {abs/1512.03385}, | ||
| year = {2015}, | ||
| url = {http://arxiv.org/abs/1512.03385}, | ||
| archivePrefix = {arXiv}, | ||
| eprint = {1512.03385}, | ||
| timestamp = {Wed, 17 Apr 2019 17:23:45 +0200}, | ||
| biburl = {https://dblp.org/rec/journals/corr/HeZRS15.bib}, | ||
| bibsource = {dblp computer science bibliography, https://dblp.org} | ||
| }""" | ||
|
|
||
|
|
||
| class TFSlimModel: | ||
| @staticmethod | ||
| def init(identifier, preprocessing_type, image_size, net_name=None, labels_offset=1, batch_size=64, | ||
| model_ctr_kwargs=None): | ||
| import tensorflow as tf | ||
| from nets import nets_factory | ||
|
|
||
| tf.compat.v1.reset_default_graph() | ||
| placeholder = tf.compat.v1.placeholder(dtype=tf.string, shape=[batch_size]) | ||
| preprocess = TFSlimModel._init_preprocessing(placeholder, preprocessing_type, image_size=image_size) | ||
|
|
||
| net_name = net_name or identifier | ||
| model_ctr = nets_factory.get_network_fn(net_name, num_classes=labels_offset + 1000, is_training=False) | ||
| logits, endpoints = model_ctr(preprocess, **(model_ctr_kwargs or {})) | ||
| if 'Logits' in endpoints: # unify capitalization | ||
| endpoints['logits'] = endpoints['Logits'] | ||
| del endpoints['Logits'] | ||
|
|
||
| session = tf.compat.v1.Session() | ||
| TFSlimModel._restore_imagenet_weights(identifier, session) | ||
| wrapper = TensorflowSlimWrapper(identifier=identifier, endpoints=endpoints, inputs=placeholder, session=session, | ||
| batch_size=batch_size, labels_offset=labels_offset) | ||
| wrapper.image_size = image_size | ||
| return wrapper | ||
|
|
||
| @staticmethod | ||
| def _init_preprocessing(placeholder, preprocessing_type, image_size): | ||
| import tensorflow as tf | ||
| from preprocessing import vgg_preprocessing, inception_preprocessing | ||
| from model_tools.activations.tensorflow import load_image | ||
| preprocessing_types = { | ||
| 'vgg': lambda image: vgg_preprocessing.preprocess_image( | ||
| image, image_size, image_size, resize_side_min=image_size), | ||
| 'inception': lambda image: inception_preprocessing.preprocess_for_eval( | ||
| image, image_size, image_size, central_fraction=None) | ||
| } | ||
| assert preprocessing_type in preprocessing_types | ||
| preprocess_image = preprocessing_types[preprocessing_type] | ||
| preprocess = lambda image_path: preprocess_image(load_image(image_path)) | ||
| preprocess = tf.map_fn(preprocess, placeholder, dtype=tf.float32) | ||
| return preprocess | ||
|
|
||
| @staticmethod | ||
| def _restore_imagenet_weights(name, session): | ||
| import tensorflow as tf | ||
| var_list = None | ||
| if name.startswith('mobilenet'): | ||
| # Restore using exponential moving average since it produces (1.5-2%) higher accuracy according to | ||
| # https://github.com/tensorflow/models/blob/a6494752575fad4d95e92698dbfb88eb086d8526/research/slim/nets/mobilenet/mobilenet_example.ipynb | ||
| ema = tf.train.ExponentialMovingAverage(0.999) | ||
| var_list = ema.variables_to_restore() | ||
| restorer = tf.compat.v1.train.Saver(var_list) | ||
|
|
||
| restore_path = '' # TODO restore model weights | ||
| restorer.restore(session, restore_path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.