1
votes

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
I think currently Core ML does not support images as possible output type (although this may have changed in the latest betas). I'd published some code that can convert the MLMultiArray back into an image: github.com/hollance/CoreMLHelpers (although your image appears to be grayscale, so you'll have to tweak the code a little since it currently only supports RGB images). - Matthijs Hollemans
Thank you I'll give it a look ! Hopefully, CoreML will support images in the next releases - Nathan Hubens
I just pushed an update that can also handle grayscale images. - Matthijs Hollemans
By the way, also check out this thread on the Apple Dev Forums, it shows how to modify the mlmodel so that it outputs images instead of a multi-array: forums.developer.apple.com/thread/81571 - Matthijs Hollemans
@NathanHubens i am developing a .mlmodel file using python for this i am using linear-regression What i want,i want to use xls file(or it's raw data) as input of .mlmodel and want to use this data in py file and it should return a array i.e (1) How to pass xlx or csv or it's raw data as input in .mlmodel (2) how to use this input data in py file and do calculation (3) mlmodel should return array How i can implement this type of mlmodel ? - hitesh landge

1 Answers

1
votes

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.