Skip to content

Commit a63319b

Browse files
authored
add deprecation warnings (#705)
address review comments update the warning message in the decorator to work with classes
1 parent 53106d8 commit a63319b

File tree

14 files changed

+145
-70
lines changed

14 files changed

+145
-70
lines changed

coremltools/converters/caffe/_caffe_converter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def convert(model, image_input_names=[], is_bgr=False,
178178
179179
"""
180180
from ...models import MLModel
181-
from ...models.utils import convert_neural_network_weights_to_fp16 as convert_neural_network_weights_to_fp16
181+
from ...models.utils import _convert_neural_network_weights_to_fp16
182182

183183
if model_precision not in _VALID_MLMODEL_PRECISION_TYPES:
184184
raise RuntimeError('Model precision {} is not valid'.format(model_precision))
@@ -197,7 +197,7 @@ def convert(model, image_input_names=[], is_bgr=False,
197197
pass
198198

199199
if model_precision == _MLMODEL_HALF_PRECISION and model is not None:
200-
model = convert_neural_network_weights_to_fp16(model)
200+
model = _convert_neural_network_weights_to_fp16(model)
201201

202202
return model
203203

coremltools/converters/keras/_keras_converter.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
from ...models import datatypes
1212
from ...models import MLModel as _MLModel
1313
from ...models import _MLMODEL_FULL_PRECISION, _MLMODEL_HALF_PRECISION, _VALID_MLMODEL_PRECISION_TYPES
14-
from ...models.utils import convert_neural_network_weights_to_fp16 as convert_neural_network_weights_to_fp16
15-
from ...models.utils import convert_neural_network_spec_weights_to_fp16 as convert_neural_network_spec_weights_to_fp16
14+
from ...models.utils import _convert_neural_network_spec_weights_to_fp16
1615

1716
from ..._deps import HAS_KERAS_TF as _HAS_KERAS_TF
1817
from ..._deps import HAS_KERAS2_TF as _HAS_KERAS2_TF
@@ -588,7 +587,7 @@ def convertToSpec(model,
588587
'Keras not found or unsupported version or backend found. keras conversion API is disabled.')
589588

590589
if model_precision == _MLMODEL_HALF_PRECISION and model is not None:
591-
spec = convert_neural_network_spec_weights_to_fp16(spec)
590+
spec = _convert_neural_network_spec_weights_to_fp16(spec)
592591

593592
return spec
594593

coremltools/models/_deprecation.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import warnings
2+
import functools
3+
4+
def deprecated(obj=None, suffix=""):
5+
'''
6+
Decorator to mark a function or a class as deprecated
7+
'''
8+
9+
def decorator_deprecation_warning(obj):
10+
@functools.wraps(obj)
11+
def wrapped(*args, **kwargs):
12+
if isinstance(obj, type):
13+
msg = "Class \"%s\" is deprecated and will be removed in the next release" % obj.__name__
14+
else:
15+
msg = "Function \"%s\" is deprecated and will be removed in the next release" % obj.__name__
16+
if suffix:
17+
msg += "; %s" % suffix
18+
warnings.warn(msg, category=FutureWarning)
19+
return obj(*args, **kwargs)
20+
return wrapped
21+
22+
if obj is None:
23+
return decorator_deprecation_warning
24+
25+
return decorator_deprecation_warning(obj)
26+

coremltools/models/model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .utils import macos_version as _macos_version
1717
from .utils import save_spec as _save_spec
1818
from ..proto import Model_pb2 as _Model_pb2
19+
from coremltools.models._deprecation import deprecated
1920

2021
_MLMODEL_FULL_PRECISION = 'float32'
2122
_MLMODEL_HALF_PRECISION = 'float16'
@@ -113,7 +114,7 @@ def _get_proxy_and_spec(filename, use_cpu_only=False):
113114

114115
return None, specification
115116

116-
117+
@deprecated
117118
class NeuralNetworkShaper(object):
118119
"""
119120
This class computes the intermediate tensor shapes for a neural network model.
@@ -358,6 +359,7 @@ def predict(self, data, useCPUOnly=False, **kwargs):
358359
else:
359360
raise Exception('Unable to load CoreML.framework. Cannot make predictions.')
360361

362+
@deprecated
361363
def visualize_spec(self, port=None, input_shape_dict=None, title='CoreML Graph Visualization'):
362364
"""
363365
Visualize the model.

coremltools/models/neural_network/builder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ def inspect_layers(self, last=-1, verbose=False):
862862
for i, alayer in enumerate(self.nn_spec.layers[::-1]):
863863
if i >= last:
864864
break
865-
layer_type, name, in_blobs, out_blobs, params_info = summarize_network_layer_info(alayer)
865+
layer_type, name, in_blobs, out_blobs, params_info = _summarize_network_layer_info(alayer)
866866
print('[Id: {}], Name: {} (Type: {})'.format(n_layers - i - 1, name, layer_type))
867867
print(' ' * 10 + 'Updatable: {}'.format(alayer.isUpdatable))
868868
print(' ' * 10 + 'Input blobs: {}'.format(in_blobs))
@@ -924,7 +924,7 @@ def inspect_updatable_layers(self):
924924
"""
925925
for _, layer in enumerate(self.nn_spec.layers[::-1]):
926926
if layer.isUpdatable:
927-
layer_type, name, in_blobs, out_blobs, _ = summarize_network_layer_info(layer)
927+
layer_type, name, in_blobs, out_blobs, _ = _summarize_network_layer_info(layer)
928928
print('Name: {} (Type: {})'.format(name, layer_type))
929929
print(' ' * 10 + 'Input blobs: {}'.format(in_blobs))
930930
print(' ' * 10 + 'Output blobs: {}'.format(out_blobs))

coremltools/models/neural_network/flexible_shape_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from ... import _MINIMUM_FLEXIBLE_SHAPES_SPEC_VERSION
1313
from ... import _MINIMUM_NDARRAY_SPEC_VERSION
1414
from ..model import NeuralNetworkShaper
15+
from coremltools.models._deprecation import deprecated
1516

1617
_SEQUENCE_KEY = 'S'
1718
_BATCH_KEY = 'B'
@@ -701,7 +702,7 @@ def add_multiarray_ndshape_enumeration(spec, feature_name, enumerated_shapes):
701702
spec.specificationVersion = max(_MINIMUM_NDARRAY_SPEC_VERSION,
702703
spec.specificationVersion)
703704

704-
705+
@deprecated
705706
def get_allowed_shape_ranges(spec):
706707
"""
707708
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):
717718
return output
718719

719720

720-
721+
@deprecated
721722
def can_allow_multiple_input_shapes(spec):
722723
"""
723724
Examines a model specification and determines if it can compute results for more than one output shape.

coremltools/models/neural_network/printer.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
# Use of this source code is governed by a BSD-3-clause license that can be
44
# found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
55

6-
from .spec_inspection_utils import *
6+
from .spec_inspection_utils import _get_feature_description_summary, \
7+
_summarize_neural_network_spec_code_style, \
8+
_summarize_neural_network_spec
79

10+
from coremltools.models._deprecation import deprecated
811

12+
@deprecated
913
def print_network_spec_parameter_info_style(mlmodel_spec, interface_only=False):
14+
return _print_network_spec_parameter_info_style(mlmodel_spec, interface_only=interface_only)
15+
16+
def _print_network_spec_parameter_info_style(mlmodel_spec, interface_only=False):
1017
""" Print the network information summary.
1118
Args:
1219
mlmodel_spec : the mlmodel spec
1320
interface_only : Shows only the input and output of the network
1421
"""
15-
inputs, outputs, layers_info = summarize_neural_network_spec(mlmodel_spec)
22+
inputs, outputs, layers_info = _summarize_neural_network_spec(mlmodel_spec)
1623

1724
print('Inputs:')
1825
for i in inputs:
@@ -41,16 +48,19 @@ def print_network_spec_parameter_info_style(mlmodel_spec, interface_only=False):
4148

4249
print('\n')
4350

44-
51+
@deprecated
4552
def print_network_spec_coding_style(mlmodel_spec, interface_only=False):
53+
return _print_network_spec_coding_style(mlmodel_spec, interface_only=interface_only)
54+
55+
def _print_network_spec_coding_style(mlmodel_spec, interface_only=False):
4656
"""
4757
Args:
4858
mlmodel_spec : the mlmodel spec
4959
interface_only : Shows only the input and output of the network
5060
"""
5161

52-
inputs = [(blob.name, get_feature_description_summary(blob)) for blob in mlmodel_spec.description.input]
53-
outputs = [(blob.name, get_feature_description_summary(blob)) for blob in mlmodel_spec.description.output]
62+
inputs = [(blob.name, _get_feature_description_summary(blob)) for blob in mlmodel_spec.description.input]
63+
outputs = [(blob.name, _get_feature_description_summary(blob)) for blob in mlmodel_spec.description.output]
5464

5565
input_names = []
5666
print('Inputs:')
@@ -83,7 +93,7 @@ def print_network_spec_coding_style(mlmodel_spec, interface_only=False):
8393
return
8494

8595
print('\n')
86-
summarize_neural_network_spec_code_style(nn_spec, input_names=input_names, output_names=output_names)
96+
_summarize_neural_network_spec_code_style(nn_spec, input_names=input_names, output_names=output_names)
8797

8898

8999
def print_network_spec(mlmodel_spec, interface_only=False, style=''):
@@ -95,6 +105,6 @@ def print_network_spec(mlmodel_spec, interface_only=False, style=''):
95105
"""
96106

97107
if style == 'coding':
98-
print_network_spec_coding_style(mlmodel_spec, interface_only=interface_only)
108+
_print_network_spec_coding_style(mlmodel_spec, interface_only=interface_only)
99109
else:
100-
print_network_spec_parameter_info_style(mlmodel_spec, interface_only=interface_only)
110+
_print_network_spec_parameter_info_style(mlmodel_spec, interface_only=interface_only)

coremltools/models/neural_network/quantization_utils.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
from ... import (_MINIMUM_QUANTIZED_MODEL_SPEC_VERSION,
3232
_MINIMUM_FP16_SPEC_VERSION)
3333

34+
from coremltools.models._deprecation import deprecated
35+
3436

3537
class QuantizedLayerSelector(object):
3638
""" 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
752754
raise Exception('Unknown layer ' + layer_type + ' to be quantized')
753755

754756

757+
@deprecated(suffix="instead use 'quantize_weights'.")
755758
def quantize_spec_weights(spec, nbits, quantization_mode, **kwargs):
759+
return _quantize_spec_weights(spec, nbits, quantization_mode, **kwargs)
760+
761+
762+
def _quantize_spec_weights(spec, nbits, quantization_mode, **kwargs):
756763

757764
nn_model_types = ['neuralNetwork', 'neuralNetworkClassifier',
758765
'neuralNetworkRegressor']
@@ -784,10 +791,10 @@ def quantize_spec_weights(spec, nbits, quantization_mode, **kwargs):
784791
# Recursively convert all pipeline models
785792
elif spec.WhichOneof('Type') == 'pipeline':
786793
for model_spec in spec.pipeline.models:
787-
quantize_spec_weights(model_spec, nbits, quantization_mode, **kwargs)
794+
_quantize_spec_weights(model_spec, nbits, quantization_mode, **kwargs)
788795

789796
elif spec.WhichOneof('Type') in ['pipelineClassifier', 'pipelineRegressor']:
790-
quantize_spec_weights(spec.pipeline, nbits, quantization_mode, **kwargs)
797+
_quantize_spec_weights(spec.pipeline, nbits, quantization_mode, **kwargs)
791798

792799
return spec
793800

@@ -1149,7 +1156,7 @@ def quantize_weights(full_precision_model,
11491156

11501157
print("Quantizing using {} quantization".format(quantization_mode))
11511158
spec = full_precision_model.get_spec()
1152-
qspec = quantize_spec_weights(spec, nbits, qmode, **kwargs)
1159+
qspec = _quantize_spec_weights(spec, nbits, qmode, **kwargs)
11531160

11541161
if macos_version() < (10, 14):
11551162
print("WARNING! Unable to return a quantized MLModel instance since"

0 commit comments

Comments
 (0)