From a63319be4c70271f229c9f5c3423a97c7535f08b Mon Sep 17 00:00:00 2001 From: Aseem Wadhwa Date: Mon, 18 May 2020 21:28:20 -0700 Subject: [PATCH] add deprecation warnings (#705) address review comments update the warning message in the decorator to work with classes --- .../converters/caffe/_caffe_converter.py | 4 +- .../converters/keras/_keras_converter.py | 5 +- coremltools/models/_deprecation.py | 26 +++++++ coremltools/models/model.py | 4 +- coremltools/models/neural_network/builder.py | 4 +- .../neural_network/flexible_shape_utils.py | 5 +- coremltools/models/neural_network/printer.py | 26 +++++-- .../neural_network/quantization_utils.py | 13 +++- .../neural_network/spec_inspection_utils.py | 73 +++++++++++-------- coremltools/models/utils.py | 31 ++++++-- coremltools/test/neural_network/test_model.py | 6 +- .../neural_network/test_neural_networks.py | 12 +-- .../neural_network/test_numpy_nn_layers.py | 2 +- .../test/neural_network/test_quantization.py | 4 +- 14 files changed, 145 insertions(+), 70 deletions(-) create mode 100644 coremltools/models/_deprecation.py diff --git a/coremltools/converters/caffe/_caffe_converter.py b/coremltools/converters/caffe/_caffe_converter.py index 98bb0c9fe..860674111 100644 --- a/coremltools/converters/caffe/_caffe_converter.py +++ b/coremltools/converters/caffe/_caffe_converter.py @@ -178,7 +178,7 @@ def convert(model, image_input_names=[], is_bgr=False, """ from ...models import MLModel - from ...models.utils import convert_neural_network_weights_to_fp16 as convert_neural_network_weights_to_fp16 + from ...models.utils import _convert_neural_network_weights_to_fp16 if model_precision not in _VALID_MLMODEL_PRECISION_TYPES: raise RuntimeError('Model precision {} is not valid'.format(model_precision)) @@ -197,7 +197,7 @@ def convert(model, image_input_names=[], is_bgr=False, pass if model_precision == _MLMODEL_HALF_PRECISION and model is not None: - model = convert_neural_network_weights_to_fp16(model) + model = _convert_neural_network_weights_to_fp16(model) return model diff --git a/coremltools/converters/keras/_keras_converter.py b/coremltools/converters/keras/_keras_converter.py index 165711405..42f867c00 100644 --- a/coremltools/converters/keras/_keras_converter.py +++ b/coremltools/converters/keras/_keras_converter.py @@ -11,8 +11,7 @@ from ...models import datatypes from ...models import MLModel as _MLModel from ...models import _MLMODEL_FULL_PRECISION, _MLMODEL_HALF_PRECISION, _VALID_MLMODEL_PRECISION_TYPES -from ...models.utils import convert_neural_network_weights_to_fp16 as convert_neural_network_weights_to_fp16 -from ...models.utils import convert_neural_network_spec_weights_to_fp16 as convert_neural_network_spec_weights_to_fp16 +from ...models.utils import _convert_neural_network_spec_weights_to_fp16 from ..._deps import HAS_KERAS_TF as _HAS_KERAS_TF from ..._deps import HAS_KERAS2_TF as _HAS_KERAS2_TF @@ -588,7 +587,7 @@ def convertToSpec(model, 'Keras not found or unsupported version or backend found. keras conversion API is disabled.') if model_precision == _MLMODEL_HALF_PRECISION and model is not None: - spec = convert_neural_network_spec_weights_to_fp16(spec) + spec = _convert_neural_network_spec_weights_to_fp16(spec) return spec diff --git a/coremltools/models/_deprecation.py b/coremltools/models/_deprecation.py new file mode 100644 index 000000000..5e506f414 --- /dev/null +++ b/coremltools/models/_deprecation.py @@ -0,0 +1,26 @@ +import warnings +import functools + +def deprecated(obj=None, suffix=""): + ''' + Decorator to mark a function or a class as deprecated + ''' + + def decorator_deprecation_warning(obj): + @functools.wraps(obj) + def wrapped(*args, **kwargs): + if isinstance(obj, type): + msg = "Class \"%s\" is deprecated and will be removed in the next release" % obj.__name__ + else: + msg = "Function \"%s\" is deprecated and will be removed in the next release" % obj.__name__ + if suffix: + msg += "; %s" % suffix + warnings.warn(msg, category=FutureWarning) + return obj(*args, **kwargs) + return wrapped + + if obj is None: + return decorator_deprecation_warning + + return decorator_deprecation_warning(obj) + diff --git a/coremltools/models/model.py b/coremltools/models/model.py index 7c0715b80..f0c84e457 100644 --- a/coremltools/models/model.py +++ b/coremltools/models/model.py @@ -16,6 +16,7 @@ from .utils import macos_version as _macos_version from .utils import save_spec as _save_spec from ..proto import Model_pb2 as _Model_pb2 +from coremltools.models._deprecation import deprecated _MLMODEL_FULL_PRECISION = 'float32' _MLMODEL_HALF_PRECISION = 'float16' @@ -113,7 +114,7 @@ def _get_proxy_and_spec(filename, use_cpu_only=False): return None, specification - +@deprecated class NeuralNetworkShaper(object): """ This class computes the intermediate tensor shapes for a neural network model. @@ -358,6 +359,7 @@ def predict(self, data, useCPUOnly=False, **kwargs): else: raise Exception('Unable to load CoreML.framework. Cannot make predictions.') + @deprecated def visualize_spec(self, port=None, input_shape_dict=None, title='CoreML Graph Visualization'): """ Visualize the model. diff --git a/coremltools/models/neural_network/builder.py b/coremltools/models/neural_network/builder.py index fe56670c4..f08b0525f 100644 --- a/coremltools/models/neural_network/builder.py +++ b/coremltools/models/neural_network/builder.py @@ -862,7 +862,7 @@ def inspect_layers(self, last=-1, verbose=False): for i, alayer in enumerate(self.nn_spec.layers[::-1]): if i >= last: break - layer_type, name, in_blobs, out_blobs, params_info = summarize_network_layer_info(alayer) + layer_type, name, in_blobs, out_blobs, params_info = _summarize_network_layer_info(alayer) print('[Id: {}], Name: {} (Type: {})'.format(n_layers - i - 1, name, layer_type)) print(' ' * 10 + 'Updatable: {}'.format(alayer.isUpdatable)) print(' ' * 10 + 'Input blobs: {}'.format(in_blobs)) @@ -924,7 +924,7 @@ def inspect_updatable_layers(self): """ for _, layer in enumerate(self.nn_spec.layers[::-1]): if layer.isUpdatable: - layer_type, name, in_blobs, out_blobs, _ = summarize_network_layer_info(layer) + layer_type, name, in_blobs, out_blobs, _ = _summarize_network_layer_info(layer) print('Name: {} (Type: {})'.format(name, layer_type)) print(' ' * 10 + 'Input blobs: {}'.format(in_blobs)) print(' ' * 10 + 'Output blobs: {}'.format(out_blobs)) diff --git a/coremltools/models/neural_network/flexible_shape_utils.py b/coremltools/models/neural_network/flexible_shape_utils.py index 0157a336d..ae0597ee1 100644 --- a/coremltools/models/neural_network/flexible_shape_utils.py +++ b/coremltools/models/neural_network/flexible_shape_utils.py @@ -12,6 +12,7 @@ from ... import _MINIMUM_FLEXIBLE_SHAPES_SPEC_VERSION from ... import _MINIMUM_NDARRAY_SPEC_VERSION from ..model import NeuralNetworkShaper +from coremltools.models._deprecation import deprecated _SEQUENCE_KEY = 'S' _BATCH_KEY = 'B' @@ -701,7 +702,7 @@ def add_multiarray_ndshape_enumeration(spec, feature_name, enumerated_shapes): spec.specificationVersion = max(_MINIMUM_NDARRAY_SPEC_VERSION, spec.specificationVersion) - +@deprecated def get_allowed_shape_ranges(spec): """ For a given model specification, returns a dictionary with a shape range object for each input feature name. @@ -717,7 +718,7 @@ def get_allowed_shape_ranges(spec): return output - +@deprecated def can_allow_multiple_input_shapes(spec): """ Examines a model specification and determines if it can compute results for more than one output shape. diff --git a/coremltools/models/neural_network/printer.py b/coremltools/models/neural_network/printer.py index c8b5cbbf0..6573cf8dc 100644 --- a/coremltools/models/neural_network/printer.py +++ b/coremltools/models/neural_network/printer.py @@ -3,16 +3,23 @@ # Use of this source code is governed by a BSD-3-clause license that can be # found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause -from .spec_inspection_utils import * +from .spec_inspection_utils import _get_feature_description_summary, \ + _summarize_neural_network_spec_code_style, \ + _summarize_neural_network_spec +from coremltools.models._deprecation import deprecated +@deprecated def print_network_spec_parameter_info_style(mlmodel_spec, interface_only=False): + return _print_network_spec_parameter_info_style(mlmodel_spec, interface_only=interface_only) + +def _print_network_spec_parameter_info_style(mlmodel_spec, interface_only=False): """ Print the network information summary. Args: mlmodel_spec : the mlmodel spec interface_only : Shows only the input and output of the network """ - inputs, outputs, layers_info = summarize_neural_network_spec(mlmodel_spec) + inputs, outputs, layers_info = _summarize_neural_network_spec(mlmodel_spec) print('Inputs:') for i in inputs: @@ -41,16 +48,19 @@ def print_network_spec_parameter_info_style(mlmodel_spec, interface_only=False): print('\n') - +@deprecated def print_network_spec_coding_style(mlmodel_spec, interface_only=False): + return _print_network_spec_coding_style(mlmodel_spec, interface_only=interface_only) + +def _print_network_spec_coding_style(mlmodel_spec, interface_only=False): """ Args: mlmodel_spec : the mlmodel spec interface_only : Shows only the input and output of the network """ - inputs = [(blob.name, get_feature_description_summary(blob)) for blob in mlmodel_spec.description.input] - outputs = [(blob.name, get_feature_description_summary(blob)) for blob in mlmodel_spec.description.output] + inputs = [(blob.name, _get_feature_description_summary(blob)) for blob in mlmodel_spec.description.input] + outputs = [(blob.name, _get_feature_description_summary(blob)) for blob in mlmodel_spec.description.output] input_names = [] print('Inputs:') @@ -83,7 +93,7 @@ def print_network_spec_coding_style(mlmodel_spec, interface_only=False): return print('\n') - summarize_neural_network_spec_code_style(nn_spec, input_names=input_names, output_names=output_names) + _summarize_neural_network_spec_code_style(nn_spec, input_names=input_names, output_names=output_names) def print_network_spec(mlmodel_spec, interface_only=False, style=''): @@ -95,6 +105,6 @@ def print_network_spec(mlmodel_spec, interface_only=False, style=''): """ if style == 'coding': - print_network_spec_coding_style(mlmodel_spec, interface_only=interface_only) + _print_network_spec_coding_style(mlmodel_spec, interface_only=interface_only) else: - print_network_spec_parameter_info_style(mlmodel_spec, interface_only=interface_only) + _print_network_spec_parameter_info_style(mlmodel_spec, interface_only=interface_only) diff --git a/coremltools/models/neural_network/quantization_utils.py b/coremltools/models/neural_network/quantization_utils.py index 7fadf49b7..c9b7ed4f4 100644 --- a/coremltools/models/neural_network/quantization_utils.py +++ b/coremltools/models/neural_network/quantization_utils.py @@ -31,6 +31,8 @@ from ... import (_MINIMUM_QUANTIZED_MODEL_SPEC_VERSION, _MINIMUM_FP16_SPEC_VERSION) +from coremltools.models._deprecation import deprecated + class QuantizedLayerSelector(object): """ This is the base class to implement custom selectors to skip certain @@ -752,7 +754,12 @@ def _lstmwp_to_fp16_lstmwp(lstm_wp, nbits, qm, i_size, o_size, has_peephole=True raise Exception('Unknown layer ' + layer_type + ' to be quantized') +@deprecated(suffix="instead use 'quantize_weights'.") def quantize_spec_weights(spec, nbits, quantization_mode, **kwargs): + return _quantize_spec_weights(spec, nbits, quantization_mode, **kwargs) + + +def _quantize_spec_weights(spec, nbits, quantization_mode, **kwargs): nn_model_types = ['neuralNetwork', 'neuralNetworkClassifier', 'neuralNetworkRegressor'] @@ -784,10 +791,10 @@ def quantize_spec_weights(spec, nbits, quantization_mode, **kwargs): # Recursively convert all pipeline models elif spec.WhichOneof('Type') == 'pipeline': for model_spec in spec.pipeline.models: - quantize_spec_weights(model_spec, nbits, quantization_mode, **kwargs) + _quantize_spec_weights(model_spec, nbits, quantization_mode, **kwargs) elif spec.WhichOneof('Type') in ['pipelineClassifier', 'pipelineRegressor']: - quantize_spec_weights(spec.pipeline, nbits, quantization_mode, **kwargs) + _quantize_spec_weights(spec.pipeline, nbits, quantization_mode, **kwargs) return spec @@ -1149,7 +1156,7 @@ def quantize_weights(full_precision_model, print("Quantizing using {} quantization".format(quantization_mode)) spec = full_precision_model.get_spec() - qspec = quantize_spec_weights(spec, nbits, qmode, **kwargs) + qspec = _quantize_spec_weights(spec, nbits, qmode, **kwargs) if macos_version() < (10, 14): print("WARNING! Unable to return a quantized MLModel instance since" diff --git a/coremltools/models/neural_network/spec_inspection_utils.py b/coremltools/models/neural_network/spec_inspection_utils.py index 5ddae4205..86b9ed2f3 100644 --- a/coremltools/models/neural_network/spec_inspection_utils.py +++ b/coremltools/models/neural_network/spec_inspection_utils.py @@ -1,5 +1,6 @@ from __future__ import print_function from ...proto import NeuralNetwork_pb2 as _NeuralNetwork_pb2 +from coremltools.models._deprecation import deprecated def _get_weight_param_summary(wp): @@ -53,8 +54,11 @@ def _get_lstm_weight_param_summary(lstm_wp): return ('\n' + ' '*8).join(lstm_wp_summary_list) - +@deprecated def get_feature_description_summary(feature): + return _get_feature_description_summary(feature) + +def _get_feature_description_summary(feature): if feature.type.HasField('multiArrayType'): shape = list(feature.type.multiArrayType.shape) int_shape = [int(x) for x in shape] @@ -62,8 +66,11 @@ def get_feature_description_summary(feature): else: return ('({})'.format(str(feature.type))).replace('\n', '') - +@deprecated def summarize_network_layer_info(layer): + return _summarize_network_layer_info(layer) + +def _summarize_network_layer_info(layer): """ Args: layer - an MLModel NeuralNetwork Layer protobuf message @@ -100,8 +107,11 @@ def summarize_network_layer_info(layer): return layer_type_str, layer_name, layer_inputs, layer_outputs, layer_field_content - +@deprecated def summarize_neural_network_spec(mlmodel_spec): + return _summarize_neural_network_spec(mlmodel_spec) + +def _summarize_neural_network_spec(mlmodel_spec): """ Summarize network into the following structure. Args: mlmodel_spec : mlmodel spec @@ -111,8 +121,8 @@ def summarize_neural_network_spec(mlmodel_spec): layers : list[(str, list[str], list[str], list[(str, str)])] - a list of layers represented by layer name, input blobs, output blobs, a list of (parameter name, content) """ - inputs = [(blob.name, get_feature_description_summary(blob)) for blob in mlmodel_spec.description.input] - outputs = [(blob.name, get_feature_description_summary(blob)) for blob in mlmodel_spec.description.output] + inputs = [(blob.name, _get_feature_description_summary(blob)) for blob in mlmodel_spec.description.input] + outputs = [(blob.name, _get_feature_description_summary(blob)) for blob in mlmodel_spec.description.output] nn = None if mlmodel_spec.HasField('neuralNetwork'): @@ -122,42 +132,42 @@ def summarize_neural_network_spec(mlmodel_spec): elif mlmodel_spec.HasField('neuralNetworkRegressor'): nn = mlmodel_spec.neuralNetworkRegressor - layers = [summarize_network_layer_info(layer) for layer in nn.layers] if nn != None else None + layers = [_summarize_network_layer_info(layer) for layer in nn.layers] if nn != None else None return (inputs, outputs, layers) -def prRed(skk, end=None): +def _prRed(skk, end=None): print("\033[91m {}\033[00m".format(skk), end=end) -def prLightPurple(skk, end=None): +def _prLightPurple(skk, end=None): print("\033[94m {}\033[00m".format(skk), end=end) -def prPurple(skk, end=None): +def _prPurple(skk, end=None): print("\033[95m {}\033[00m".format(skk), end=end) -def prGreen(skk, end=None): +def _prGreen(skk, end=None): print("\033[92m {}\033[00m".format(skk), end=end) def _print_layer_type_and_arguments(layer_type_str, layer_inputs, indentation, to_indent=True, shape=None, value=None): if to_indent: - prRed(indentation * '\t' + '{}'.format(layer_type_str), end='') + _prRed(indentation * '\t' + '{}'.format(layer_type_str), end='') else: - prRed('{}'.format(layer_type_str), end='') + _prRed('{}'.format(layer_type_str), end='') if shape is None: - prLightPurple('({})'.format(', '.join(layer_inputs))) + _prLightPurple('({})'.format(', '.join(layer_inputs))) elif value is not None: - prLightPurple('(shape = ', end='') + _prLightPurple('(shape = ', end='') print('{}, '.format(str(shape)), end='') - prLightPurple('value = ', end='') + _prLightPurple('value = ', end='') values = ','.join(["{0: 0.1f}".format(v) for v in value]).lstrip() print('[{}]'.format(values), end='') - prLightPurple(')') + _prLightPurple(')') else: - prLightPurple('(shape = ', end='') + _prLightPurple('(shape = ', end='') print('{}'.format(str(shape)), end='') - prLightPurple(')') + _prLightPurple(')') def _find_size(arr): s = 1 @@ -165,7 +175,12 @@ def _find_size(arr): s *= a return s +@deprecated def summarize_neural_network_spec_code_style(nn_spec, indentation=0, input_names=None, output_names=None): + return _summarize_neural_network_spec_code_style(nn_spec, indentation=indentation, + input_names=input_names, output_names=output_names) + +def _summarize_neural_network_spec_code_style(nn_spec, indentation=0, input_names=None, output_names=None): """ print nn_spec as if writing code """ @@ -182,34 +197,34 @@ def summarize_neural_network_spec_code_style(nn_spec, indentation=0, input_names if layer_type_str == 'loop': if len(layer.loop.conditionNetwork.layers) > 0: - prPurple(indentation * '\t' + 'Condition Network: ') - summarize_neural_network_spec_code_style(layer.loop.conditionNetwork, indentation=indentation) + _prPurple(indentation * '\t' + 'Condition Network: ') + _summarize_neural_network_spec_code_style(layer.loop.conditionNetwork, indentation=indentation) if layer.loop.conditionVar: layer_inputs.append(layer.loop.conditionVar) _print_layer_type_and_arguments(layer_type_str, layer_inputs, indentation) indentation += indentation_size - summarize_neural_network_spec_code_style(layer.loop.bodyNetwork, indentation=indentation) + _summarize_neural_network_spec_code_style(layer.loop.bodyNetwork, indentation=indentation) if len(layer.loop.conditionNetwork.layers) > 0: - prPurple(indentation * '\t' + 'Condition Network: ') - summarize_neural_network_spec_code_style(layer.loop.conditionNetwork, indentation=indentation) + _prPurple(indentation * '\t' + 'Condition Network: ') + _summarize_neural_network_spec_code_style(layer.loop.conditionNetwork, indentation=indentation) indentation -= indentation_size continue if layer_type_str == 'branch': _print_layer_type_and_arguments(layer_type_str, layer_inputs, indentation) - prRed(indentation * '\t' + 'IfBranch:') + _prRed(indentation * '\t' + 'IfBranch:') indentation += indentation_size - summarize_neural_network_spec_code_style(layer.branch.ifBranch, indentation=indentation) + _summarize_neural_network_spec_code_style(layer.branch.ifBranch, indentation=indentation) indentation -= indentation_size if len(layer.branch.elseBranch.layers) > 0: - prRed(indentation * '\t' + 'ElseBranch:') + _prRed(indentation * '\t' + 'ElseBranch:') indentation += indentation_size - summarize_neural_network_spec_code_style(layer.branch.elseBranch, indentation=indentation) + _summarize_neural_network_spec_code_style(layer.branch.elseBranch, indentation=indentation) indentation -= indentation_size continue if layer_type_str == 'loopBreak' or layer_type_str == 'loopContinue': - prRed(indentation * '\t' + layer_type_str) + _prRed(indentation * '\t' + layer_type_str) continue shape = None @@ -236,5 +251,5 @@ def summarize_neural_network_spec_code_style(nn_spec, indentation=0, input_names shape=shape, value=value) if output_names: - prRed('\n' + indentation * '\t' + 'return ', end='') + _prRed('\n' + indentation * '\t' + 'return ', end='') print('{}'.format(', '.join(output_names))) \ No newline at end of file diff --git a/coremltools/models/utils.py b/coremltools/models/utils.py index 44d7959ca..07b569440 100644 --- a/coremltools/models/utils.py +++ b/coremltools/models/utils.py @@ -13,6 +13,7 @@ import warnings import sys from coremltools.proto import Model_pb2 as _Model_pb2 +from coremltools.models._deprecation import deprecated from .._deps import HAS_SKLEARN as _HAS_SKLEARN @@ -190,19 +191,22 @@ def _wp_to_fp16wp(wp): del wp.floatValue[:] + def convert_neural_network_spec_weights_to_fp16(fp_spec): - nn_model_types = ['neuralNetwork', 'neuralNetworkClassifier', - 'neuralNetworkRegressor'] + return _convert_neural_network_spec_weights_to_fp16(fp_spec) - from .neural_network.quantization_utils import quantize_spec_weights +def _convert_neural_network_spec_weights_to_fp16(fp_spec): + from .neural_network.quantization_utils import _quantize_spec_weights from .neural_network.quantization_utils import _QUANTIZATION_MODE_LINEAR_QUANTIZATION - qspec = quantize_spec_weights(fp_spec, 16, _QUANTIZATION_MODE_LINEAR_QUANTIZATION) - + qspec = _quantize_spec_weights(fp_spec, 16, _QUANTIZATION_MODE_LINEAR_QUANTIZATION) return qspec def convert_neural_network_weights_to_fp16(full_precision_model): + return _convert_neural_network_weights_to_fp16(full_precision_model) + +def _convert_neural_network_weights_to_fp16(full_precision_model): """ Utility function to convert a full precision (float) MLModel to a half precision MLModel (float16). @@ -227,7 +231,7 @@ def convert_neural_network_weights_to_fp16(full_precision_model): >>> half_precision_model = coremltools.utils.convert_neural_network_weights_to_fp16(model) """ spec = full_precision_model.get_spec() - return _get_model(convert_neural_network_spec_weights_to_fp16(spec)) + return _get_model(_convert_neural_network_spec_weights_to_fp16(spec)) def _get_model(spec): @@ -668,7 +672,11 @@ def has_custom_layer(spec): return False +@deprecated def get_custom_layer_names(spec): + return _get_custom_layer_names(spec) + +def _get_custom_layer_names(spec): """ Returns a list of className fields which appear in the given protobuf spec @@ -691,8 +699,11 @@ def get_custom_layer_names(spec): return layers_out - +@deprecated def get_custom_layers(spec): + return _get_custom_layers(spec) + +def _get_custom_layers(spec): """ Returns a list of all neural network custom layers in the spec. @@ -715,7 +726,11 @@ def get_custom_layers(spec): return layers_out +@deprecated def replace_custom_layer_name(spec, oldname, newname): + return _replace_custom_layer_name(spec, oldname, newname) + +def _replace_custom_layer_name(spec, oldname, newname): """ Substitutes newname for oldname in the className field of custom layers. If there are no custom layers, or no @@ -735,7 +750,7 @@ def replace_custom_layer_name(spec, oldname, newname): An mlmodel spec. """ - layers = get_custom_layers(spec) + layers = _get_custom_layers(spec) for layer in layers: if layer.custom.className == oldname: layer.custom.className = newname diff --git a/coremltools/test/neural_network/test_model.py b/coremltools/test/neural_network/test_model.py index 0d37ad454..86ae00214 100644 --- a/coremltools/test/neural_network/test_model.py +++ b/coremltools/test/neural_network/test_model.py @@ -10,7 +10,7 @@ from coremltools.proto import Model_pb2 from coremltools.models.utils import rename_feature, save_spec, macos_version,\ - convert_neural_network_spec_weights_to_fp16, is_macos, \ + _convert_neural_network_spec_weights_to_fp16, is_macos, \ convert_double_to_float_multiarray_type from coremltools.models import MLModel, datatypes from coremltools.models.neural_network import NeuralNetworkBuilder @@ -190,7 +190,7 @@ def test_convert_nn_spec_to_half_precision(self): output_name='out' ) model = MLModel(builder.spec) - spec = convert_neural_network_spec_weights_to_fp16(model.get_spec()) + spec = _convert_neural_network_spec_weights_to_fp16(model.get_spec()) self.assertIsNotNone(spec) # simple network without quantization layer @@ -207,7 +207,7 @@ def test_convert_nn_spec_to_half_precision(self): k=8 ) model = MLModel(builder.spec) - spec = convert_neural_network_spec_weights_to_fp16(model.get_spec()) + spec = _convert_neural_network_spec_weights_to_fp16(model.get_spec()) self.assertIsNotNone(spec) @unittest.skip diff --git a/coremltools/test/neural_network/test_neural_networks.py b/coremltools/test/neural_network/test_neural_networks.py index 10628be96..b75a64642 100644 --- a/coremltools/test/neural_network/test_neural_networks.py +++ b/coremltools/test/neural_network/test_neural_networks.py @@ -9,8 +9,8 @@ import coremltools from coremltools._deps import HAS_KERAS_TF from coremltools._deps import HAS_TF -from coremltools.models.utils import get_custom_layer_names, \ - replace_custom_layer_name, macos_version, is_macos +from coremltools.models.utils import _get_custom_layer_names, \ + _replace_custom_layer_name, macos_version, is_macos from coremltools.proto import Model_pb2 if HAS_KERAS_TF: @@ -400,12 +400,12 @@ def setUpClass(self): self.spec = spec def test_get_custom_names(self): - names = get_custom_layer_names(self.spec) + names = _get_custom_layer_names(self.spec) self.assertEqual(names, {'name1', 'name2'}) def test_change_custom_name(self): - replace_custom_layer_name(self.spec, 'name1', 'notname1') - names = get_custom_layer_names(self.spec) + _replace_custom_layer_name(self.spec, 'name1', 'notname1') + names = _get_custom_layer_names(self.spec) self.assertEqual(names, {'notname1', 'name2'}) # set it back for future tests - replace_custom_layer_name(self.spec, 'notname1', 'name1') + _replace_custom_layer_name(self.spec, 'notname1', 'name1') diff --git a/coremltools/test/neural_network/test_numpy_nn_layers.py b/coremltools/test/neural_network/test_numpy_nn_layers.py index 1253f68f6..fa93a08cd 100644 --- a/coremltools/test/neural_network/test_numpy_nn_layers.py +++ b/coremltools/test/neural_network/test_numpy_nn_layers.py @@ -121,7 +121,7 @@ def _test_model(self, # If we want to test the half precision case if model_precision == _MLMODEL_HALF_PRECISION: - model = coremltools.utils.convert_neural_network_weights_to_fp16( + model = coremltools.utils._convert_neural_network_weights_to_fp16( model) try: diff --git a/coremltools/test/neural_network/test_quantization.py b/coremltools/test/neural_network/test_quantization.py index 2620a2b19..aaa713b24 100644 --- a/coremltools/test/neural_network/test_quantization.py +++ b/coremltools/test/neural_network/test_quantization.py @@ -93,7 +93,7 @@ def _test_model(self, model, num_samples=1, mode='random', delta=1e-2, # result in 0 quantization error. coreml_spec = coreml_model.get_spec() - quantization_utils.quantize_spec_weights( + quantization_utils._quantize_spec_weights( spec=coreml_spec, nbits=self.qbits, quantization_mode=self.qmode, @@ -105,7 +105,7 @@ def _test_model(self, model, num_samples=1, mode='random', delta=1e-2, full_precision_model_spec = coreml_spec # Quantize model from another copy - quantized_model_spec = quantization_utils.quantize_spec_weights( + quantized_model_spec = quantization_utils._quantize_spec_weights( spec=coreml_model.get_spec(), nbits=self.qbits, quantization_mode=self.qmode,