How to use the coremltools.utils.load_spec function in coremltools

To help you get started, we’ve selected a few coremltools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github onnx / onnxmltools / tests / coreml / test_cml_OneHotEncoderConverter.py View on Github external
def test_one_hot_encoder(self):
        script_dir = os.path.dirname(__file__)
        relative_path = "../data/onehot_simple.mlmodel"
        abs_file = os.path.join(script_dir, relative_path)
        model_coreml = coremltools.utils.load_spec(abs_file)
        model_onnx = convert(model_coreml)
        self.assertTrue(model_onnx is not None)
github microsoft / Windows-Machine-Learning / Tools / WinMLDashboard / public / convert.py View on Github external
def coreml_converter(args):
    # When imported, CoreML tools checks for the current version of Keras and TF and prints warnings if they are
    # outside its expected range. We don't want it to import these packages (since they are big and take seconds to
    # load) and we don't want to clutter the console with unrelated Keras warnings when converting from CoreML.
    import sys
    sys.modules['keras'] = None
    import coremltools
    source_model = coremltools.utils.load_spec(args.source)
    onnx_model = winmltools.convert_coreml(source_model, get_opset(args.ONNXVersion), args.name)
    return onnx_model
github skafos / boot-finder-model / zappos.py View on Github external
new_dataset = upload_boots_to_s3(
            s3=S3_CNX,
            meta_data=new_meta_data
        )

        # Retrain model
        new_model = retrain_image_similarity_model(
            s3=S3_CNX,
            dataset=new_dataset
        )

        # Convert model to coreml
        print("\nConverting model to coreml format and coercing to half precision", flush=True)
        new_model.export_coreml(COREML_MODEL_NAME)
        # Create half precision version of this model and overwrite the other one
        model_spec = coremltools.utils.load_spec(COREML_MODEL_NAME)
        model_fp16_spec = coremltools.utils.convert_neural_network_spec_weights_to_fp16(model_spec)
        coremltools.utils.save_spec(model_fp16_spec, COREML_MODEL_NAME)

        # Upload model to s3
        upload_model_to_s3(
            s3=S3_CNX,
            dataset=new_dataset,
            new_skus=new_skus
        )

        # Upload model to Skafos
        res = upload_model_to_skafos(
            dataset=new_dataset,
            new_skus_count=total_new_skus,
            boots_count=total_boots
        )
github lutzroeder / netron / tools / converter / onnx-converter.py View on Github external
#!/usr/bin/env python

import os
import sys

file = sys.argv[1];
base, extension = os.path.splitext(file)

if extension == '.mlmodel':
	import coremltools
	import onnxmltools
	coreml_model = coremltools.utils.load_spec(file)
	onnx_model = onnxmltools.convert.convert_coreml(coreml_model)
	onnxmltools.utils.save_model(onnx_model, base + '.onnx')
elif extension == '.h5':
	import keras
	import onnxmltools
	keras_model = keras.models.load_model(file)
	onnx_model = onnxmltools.convert.convert_keras(keras_model)
	onnxmltools.utils.save_model(onnx_model, base + '.onnx')
elif extension == '.pkl':
	from sklearn.externals import joblib
	import onnxmltools
	sklearn_model = joblib.load(file)
	onnx_model = onnxmltools.convert.convert_sklearn(sklearn_model)
	onnxmltools.utils.save_model(onnx_model, base + '.onnx')
github GantMan / nicornot / datagrab / downsize.py View on Github external
import coremltools

# Load a model
print("Loading Model")
print("-------------------------------")
model_spec = coremltools.utils.load_spec('./MegaNic.mlmodel')

# Lower precision
print("Lowering Precision of Loaded Model")
print("-------------------------------")
model_fp16_spec = coremltools.utils.convert_neural_network_spec_weights_to_fp16(model_spec)

# Save new smaller model
print("Saving NEW Model")
print("-------------------------------")
coremltools.utils.save_spec(model_fp16_spec, './MegaNicFP16.mlmodel')
github tf-coreml / tf-coreml / utils / _infer_shapes_nn_mlmodel.py View on Github external
input_shape_dict: dictionary of  string --> tuple
                      string:  input name
                      tuple: input shape as a 5 length tuple in order (Seq, Batch, C, H, W)

        If input_shape_dict is not provided, input shapes are inferred from the input description in the mlmodel.
        Since the description in the specification only contains values of C,H,W; Seq and Batch dimensions are set to 1.

    Output:

        shape_dict:  dictionary containing all the blobs in the neural network and their shapes, expressed as length 5 tuples,
                     to be interpreted in order (Seq, Batch, C, H, W).
    """
    #Load the spec
    print('Loading spec. This might take a while.....')
    t = time.time()
    spec = coremltools.utils.load_spec(model_path)
    print('Spec loaded. Time taken = %f secs' % (time.time() - t))

    shape_dict = {}
    if input_shape_dict:
        for key, value in input_shape_dict.items():
            assert len(value) == 5, 'Shape of the input must be of length 5'
            shape_dict[key] = value

    # construct input_shape_dict from the model description
    else:
        for inp in spec.description.input:
            input_name = inp.name
            C = H = W = 1
            if inp.type.WhichOneof('Type') == 'imageType':
                W = int(inp.type.imageType.width)
                H = int(inp.type.imageType.height)
github tf-coreml / tf-coreml / utils / inspect_mlmodel.py View on Github external
def inspect(model_path, output_txt_file):
    spec = coremltools.utils.load_spec(model_path)
    sys.stdout = open(os.devnull, "w")
    shape_dict = _infer_shapes(model_path)
    sys.stdout = sys.__stdout__

    types_dict = {}

    sys.stdout = open(output_txt_file, 'w')

    nn = spec.neuralNetwork
    if spec.WhichOneof('Type') == 'neuralNetwork':
        nn = spec.neuralNetwork
    elif spec.WhichOneof('Type') == 'neuralNetworkRegressor':
        nn = spec.neuralNetworkRegressor
    elif spec.WhichOneof('Type') == 'neuralNetworkClassifier':
        nn = spec.neuralNetworkClassifier
    else: