Created
April 10, 2020 13:56
-
-
Save ozgurshn/5764c33f9d1ebde500a9bd119eee7a84 to your computer and use it in GitHub Desktop.
MAke CoreML model input and output size flexible
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
| ##https://heartbeat.fritz.ai/advanced-tips-for-core-ml-77c9e0231a9 | |
| from coremltools.models.neural_network import flexible_shape_utils | |
| def make_mlmodel_flexible(spec, size_range=(100, 1920): | |
| """Make input and output sizes of a Core ML model flexible. | |
| Args: | |
| spec (NeuralNetwork_pb2): a Core ML neural network spec | |
| size_range ([Int]): a tuple containing the min and max input sizes. | |
| """ | |
| size_range_spec = flexible_shape_utils.NeuralNetworkImageSizeRange() | |
| size_range_spec.add_width_range(size_range) | |
| size_range_spec.add_height_range(size_range) | |
| flexible_shape_utils.update_image_size_range( | |
| spec, feature_name='image', size_range=size_range_spec | |
| ) | |
| size_range_spec = flexible_shape_utils.NeuralNetworkImageSizeRange() | |
| size_range_spec.add_width_range(size_range) | |
| size_range_spec.add_height_range(size_range) | |
| flexible_shape_utils.update_image_size_range( | |
| spec, feature_name='stylizedImage', size_range=size_range_spec | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment