turicreate.image_similarity.ImageSimilarityModel.export_coreml

ImageSimilarityModel.export_coreml(filename)

Save the model in Core ML format. The exported model calculates the distance between a query image and each row of the model’s stored data. It does not sort and retrieve the k nearest neighbors of the query image.

See also

save

Examples

>>> # Train an image similarity model
>>> model = turicreate.image_similarity.create(data)
>>>
>>> # Query the model for similar images
>>> similar_images = model.query(data)
+-------------+-----------------+---------------+------+
| query_label | reference_label |    distance   | rank |
+-------------+-----------------+---------------+------+
|      0      |        0        |      0.0      |  1   |
|      0      |        2        | 24.9664942809 |  2   |
|      0      |        1        | 28.4416069428 |  3   |
|      1      |        1        |      0.0      |  1   |
|      1      |        2        | 21.8715131191 |  2   |
|      1      |        0        | 28.4416069428 |  3   |
|      2      |        2        |      0.0      |  1   |
|      2      |        1        | 21.8715131191 |  2   |
|      2      |        0        | 24.9664942809 |  3   |
+-------------+-----------------+---------------+------+
[9 rows x 4 columns]
>>>
>>> # Export the model to Core ML format
>>> model.export_coreml('myModel.mlmodel')
>>>
>>> # Load the Core ML model
>>> import coremltools
>>> ml_model = coremltools.models.MLModel('myModel.mlmodel')
>>>
>>> # Prepare the first image of reference data for consumption
>>> # by the Core ML model
>>> import PIL
>>> image = tc.image_analysis.resize(data['image'][0], *reversed(model.input_image_shape))
>>> image = PIL.Image.fromarray(image.pixel_data)
>>>
>>> # Calculate distances using the Core ML model
>>> ml_model.predict(data={'image': image})
{'distance': array([ 0., 28.453125, 24.96875 ])}