coremltools: Create MLModel from Spec gives segmentation fault
I have a CoreML Model (Neural Network Classifier) that works well. I want to create updatable model from that one. For this, I wrote the Python script below. But I get zsh: segmentation fault python script.py
error when I run script.
import coremltools
from coremltools.models.neural_network.update_optimizer_utils import AdamParams
# File paths:
old_coreml_file_path = 'AModel.mlmodel'
updatable_coreml_file_path = 'updatableAModel.mlmodel'
# Get old CoreML Model:
old_model = coremltools.models.MLModel(old_coreml_file_path)
spec = old_model.get_spec()
builder = coremltools.models.neural_network.NeuralNetworkBuilder(spec=spec)
# CoreML Model to Updatable CoreML Model:
builder.make_updatable(['dense_1', 'dense_2'])
# Set update params:
builder.set_categorical_cross_entropy_loss(name='lossLayer', input='class')
builder.set_adam_optimizer(AdamParams(lr=0.01, batch=8))
builder.set_epochs(allowed_set=[1,2,4,8,16])
# Save Updatable Model:
mlmodel_updatable = coremltools.models.MLModel(spec)
mlmodel_updatable.save(updatable_coreml_file_path)
The error comes in the mlmodel_updatable = coremltools.models.MLModel(spec)
line.
If I give incorrect input and get a warning in builder.set_epochs
section like builder.set_epochs(8, [1,2,4,10,16])
, the model is saved without any error, but in my Xcode project, it gives the error about determining the epoch number for the ML Model.
I tried another way to save:
When I try to save with
coremltools.utils.save_spec(spec, updatable_coreml_file_path)
instead of
mlmodel_updatable = coremltools.models.MLModel(spec)
mlmodel_updatable.save(updatable_coreml_file_path)
at the end of the script, the script runs smoothly, when I drag the saved model to Xcode, MLModel looks good in Xcode and the class is created automatically, but when I build the project, I get the Build Error: Command CoreMLModelCompile failed with a nonzero exit code
error.
I tried to clean the build folder and rebuild it, but it did’t work.
System environment:
- coremltools version 3.2 (Version 3.0b6 gives same problems.)
- macOS 10.15.3
- XCode 11.3.1
- Python 3.6.8 (anaconda)
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 19
Only the layers marked as updatable need to have un-quantized weights.
You can have a model with, say 10 conv/dense layers (layers with weights), such that the first 9 layers are quantized and not marked as updatable and the last layer is marked as updatable and unquantized. This is totally fine and should work.
Quantized models are not supported for update unfortunately. You might want to de-quantize your model before making it updatable.