CoreML output type

2k views Asked by At

Is it possible to change the output type of the CoreML model ? My model takes images as inputs and images as outputs but when I convert my Keras model into a mlmodel, I get :

coreml_model = coremltools.converters.keras.convert('/Users/user/Desktop/model.h5',input_names='input_img',image_input_names='input_img',output_names='image')
coreml_model.save('/Users/user/Desktop/model.mlmodel')

enter image description here

The output is an MultiArray type but I want an Image type, how can I change it ?

1

There are 1 answers

0
adib On

Yes it's possible. However you would need to manually alter the converted Core ML model afterwards, since coremltools as of version 2.1 does not provide any conversion option for this.

In a nutshell, here's what you need to do after converting the model to Core ML format. These should be done on the Python side by calling the lower-level APIs of coremltools.

  1. Load the converted CoreML model into Python using coremltools
  2. Append a new ActivationLinear layer at the end of the chain, just after your original model's output layer. You can also perform linear transformations using this layer, such as converting ranges from 0..1 to 0..255 and/or adding a bias.
  3. Configure that new layer as an image output layer by setting its type property.
  4. Save the updated model into a new Core ML model.
  5. Load it back in and test using a sample from the training data set, as a sanity check.

For Step 5 to work, you'll need to run the Python script on a Mac, since it uses the native Core ML libraries to run the model.

For details, you can read my post on getting Core ML to produce images as output.