Search code examples
coremlcoremltools

Split a pre-trained CoreML model into two


I have a Sound Classification model from turicreate example here: https://apple.github.io/turicreate/docs/userguide/sound_classifier/

I am trying to split this model into two and save the two parts as separate CoreML Models using coremltools library. Can anyone please guide me on how to do this?

I am able to load the model and even print out the spec of the model. But don't know where to go from here.

import coremltools

mlmodel = coremltools.models.MLModel('./EnvSceneClassification.mlmodel')

# Get spec from the model
spec = mlmodel.get_spec()

Output should be two CoreML Models i.e. the above model split into two parts.


Solution

  • I'm not 100% sure on what the sound classifier model looks like. If it's a pipeline, you can just save each sub-model from the pipeline as its own separate mlmodel file.

    If it's not a pipeline, it requires some model surgery. You will need to delete layers from the spec (with del spec.neuralNetworkClassifier.layers[a:b]).

    You'll also need to change the inputs of the first model and the outputs of the second model to account for the deleted layers.