CoreML model input & output needs Image type

Dear Developers:


I transform a Keras model of input a gray single-channel image and output a gray single-channel image.


In using a "coremltools", coremltools.converters.keras.convert , setting

----------------------

coreml_model = coremltools.converters.keras.convert(model, input_names = 'data',

image_input_names='data',

output_names='outputImage',

image_scale= 1/255.0)

------------------------

coreml_model only sets Inputs data: Image(Grayscale 256x256)

Outputs outputImage:MutliArray( Double 1x256x256)


if writing codes below,

--------------------------

def convert_multiarray_output_to_image(spec, feature_name, is_bgr=False):

for output in spec.description.output:

if output.name != feature_name:

continue

if output.type.WhichOneof('Type') != 'multiArrayType':

raise ValueError("%s is not a multiarray type" % output.name)

array_shape = tuple(output.type.multiArrayType.shape)

channels, height, width = array_shape

from coremltools.proto import FeatureTypes_pb2 as ft

if channels == 1:

output.type.imageType.colorSpace = ft.ImageFeatureType.ColorSpace.Value('GRAYSCALE')

elif channels == 3:


spec = coreml_model.get_spec()

convert_multiarray_output_to_image( spec, 'outputImage', is_bgr=False)

newModel = coremltools.models.MLModel(spec)

------------------------

coreml_model only sets Inputs inputImage: MutliArray( Double 1x256x256 )

Outputs outputImage:Image( Grayscale 256x256)



Question:

Is there any way to output coreml_model as below ?

Inputs inputImage: image( Grayscale 256x256 )

Outputs outputImage:Image( Grayscale 256x256)



Thanks


Replies

Hi Bruce,


You also need to convert the input into an image. You can do that using the same method as for outputs. Just replace all instances of "output" with "input" in your convert_multiarray_output_to_image method and you got yourself a convert_multiarray_input_to_image method. Then you just need to apply that to the spec as well before creating your model.