import coremltools
# Load MLModel
mlmodel = coremltools.models.MLModel('path/to/the/model.mlmodel')
# use model for prediction
mlmodel.predict(...)
# save the model
mlmodel.save('path/to/the/saved/model.mlmodel')
# Get spec from the model
spec = mlmodel.get_spec()
# print input/output description for the model
print(spec.description)
# save out the model directly from the spec
coremltools.models.utils.save_spec(spec,'path/to/the/saved/model.mlmodel')
# convert spec to MLModel, this step compiles the model as well
mlmodel = coremltools.models.MLModel(spec)
# Load the spec from the saved .mlmodel file directly
spec = coremltools.models.utils.load_spec('path/to/the/model.mlmodel')
import coremltools
mlmodel = coremltools.models.MLModel('path/to/the/model.mlmodel')
mlmodel.visualize_spec()
Another useful tool for visualizing CoreML models and models from other frameworks: Netron
This is useful for image based neural network models
import coremltools
spec = coremltools.models.utils.load_spec('path/to/the/saved/model.mlmodel')
# Get neural network portion of the spec
if spec.WhichOneof('Type') == 'neuralNetworkClassifier':
nn = spec.neuralNetworkClassifier
if spec.WhichOneof('Type') == 'neuralNetwork':
nn = spec.neuralNetwork
elif spec.WhichOneof('Type') == 'neuralNetworkRegressor':
nn = spec.neuralNetworkRegressor
else:
raise ValueError('MLModel must have a neural network')
print(nn.preprocessing)