• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vanvalenlab / deepcell-tf / 5537313512

pending completion
5537313512

push

github-actions

web-flow
Bump version for 0.12.6 release (#674)

1 of 1 new or added line in 1 file covered. (100.0%)

4820 of 5520 relevant lines covered (87.32%)

3.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/deepcell/utils/export_utils.py
1
# Copyright 2016-2023 The Van Valen Lab at the California Institute of
2
# Technology (Caltech), with support from the Paul Allen Family Foundation,
3
# Google, & National Institutes of Health (NIH) under Grant U24CA224309-01.
4
# All rights reserved.
5
#
6
# Licensed under a modified Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
9
#
10
#     http://www.github.com/vanvalenlab/deepcell-tf/LICENSE
11
#
12
# The Work provided may be used for non-commercial academic purposes only.
13
# For any other use of the Work, including commercial use, please contact:
14
# vanvalenlab@gmail.com
15
#
16
# Neither the name of Caltech nor the names of its contributors may be used
17
# to endorse or promote products derived from this software without specific
18
# prior written permission.
19
#
20
# Unless required by applicable law or agreed to in writing, software
21
# distributed under the License is distributed on an "AS IS" BASIS,
22
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
# See the License for the specific language governing permissions and
24
# limitations under the License.
25
# ==============================================================================
26
"""Save Keras models as a SavedModel for TensorFlow Serving"""
×
27

28

29
import os
×
30
import numpy as np
×
31
import tensorflow as tf
×
32

33
from tensorflow.python.platform import tf_logging
×
34

35

36
def export_model(keras_model, export_path, model_version=0, weights_path=None,
×
37
                 include_optimizer=True, overwrite=True, save_format='tf'):
38
    """Export a model for use with TensorFlow Serving.
39

40
    DEPRECATED: ``tf.keras.models.save_model`` is preferred.
41

42
    Args:
43
        keras_model (tensorflow.keras.Model): Instantiated Keras model.
44
        export_path (str): Destination to save the exported model files.
45
        model_version (int): Integer version of the model.
46
        weights_path (str): Path to a ``.h5`` or ``.tf`` weights file.
47
        include_optimizer (bool): Whether to export the optimizer.
48
        overwrite (bool): Whether to overwrite any existing files in
49
            ``export_path``.
50
        save_format (str): Saved model format, one of ``'tf'`` or ``'h5'``.
51
    """
52
    tf_logging.warn('`export_model` is deprecated. '
×
53
                    'Please use `tf.keras.models.save_model` instead.')
54

55
    if weights_path:
×
56
        keras_model.load_weights(weights_path, by_name=True)
×
57

58
    tf.keras.models.save_model(
×
59
        keras_model,
60
        os.path.join(export_path, str(int(model_version))),
61
        overwrite=overwrite,
62
        include_optimizer=include_optimizer,
63
        save_format=save_format,
64
        signatures=None,
65
    )
66

67

68
def export_model_to_tflite(model_file, export_path, calibration_images,
×
69
                           norm=True, location=True, file_name='model.tflite'):
70
    """Export a saved keras model to tensorflow-lite with int8 precision.
71

72
    .. deprecated:: 0.12.4
73

74
       The ``export_model_to_tflite`` function is deprecated and will be
75
       removed in 0.13. Use ``tf.keras.models.save_model`` instead.
76

77
    This export function has only been tested with ``PanopticNet`` models.
78
    For the export to be successful, the ``PanopticNet`` model must have
79
    ``norm_method`` set to ``None``, ``location`` set to ``False``,
80
    and the upsampling layers must use ``bilinear`` interpolation.
81

82
    Args:
83
        model_file (str): Path to saved model file
84
        export_path (str): Directory to save the exported tflite model
85
        calibration_images (numpy.array): Array of images used for calibration
86
            during model quantization
87
        norm (bool): Whether to normalize calibration images.
88
        location (bool): Whether to append a location image
89
            to calibration images.
90
        file_name (str): File name for the exported model. Defaults to
91
            'model.tflite'
92
    """
93
    import warnings
×
94

95
    warnings.warn(
×
96
        (
97
            "\n\nexport_model_to_tflite is deprecated and will be removed.\n"
98
            "Use tf.keras.models.save_model instead.\n"
99
        ),
100
        DeprecationWarning,
101
        stacklevel=2,
102
    )
103

104
    # Define helper function - normalization
105
    def norm_images(images):
×
106
        mean = np.mean(images, axis=(1, 2), keepdims=True)
×
107
        std = np.std(images, axis=(1, 2), keepdims=True)
×
108
        norm = (images - mean) / std
×
109
        return norm
×
110

111
    # Define helper function - add location layer
112
    def add_location(images):
×
113
        x = np.arange(0, images.shape[1], dtype='float32')
×
114
        y = np.arange(0, images.shape[2], dtype='float32')
×
115

116
        x = x / max(x)
×
117
        y = y / max(y)
×
118

119
        loc_x, loc_y = np.meshgrid(x, y, indexing='ij')
×
120
        loc = np.stack([loc_x, loc_y], axis=-1)
×
121
        loc = np.expand_dims(loc, axis=0)
×
122
        loc = np.tile(loc, (images.shape[0], 1, 1, 1))
×
123
        images_with_loc = np.concatenate([images, loc], axis=-1)
×
124
        return images_with_loc
×
125

126
    # Create generator to calibrate model quantization
127
    calibration_images = calibration_images.astype('float32')
×
128
    if norm:
×
129
        calibration_images = norm_images(calibration_images)
×
130
    if location:
×
131
        calibration_images = add_location(calibration_images)
×
132

133
    def representative_data_gen():
×
134
        for image in calibration_images:
×
135
            data = [np.expand_dims(image, axis=0)]
×
136
            yield data
×
137

138
    converter = tf.compat.v1.lite.TFLiteConverter.from_keras_model_file(model_file)
×
139
    converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
×
140
    converter.inference_input_type = tf.uint8
×
141
    converter.inference_output_type = tf.uint8
×
142
    converter.optimizations = [tf.lite.Optimize.DEFAULT]
×
143
    converter.representative_dataset = representative_data_gen
×
144
    tflite_model = converter.convert()
×
145

146
    # Save converted model
147
    save_path = os.path.join(export_path, file_name)
×
148
    open(save_path, "wb").write(tflite_quant_model)
×
149

150
    return tflite_model
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc