Search code examples
swiftcoremlimage-sizecoremltoolsmlmodel

CoreML output image size is not the same as the model prediction output image size


I tried to change the existing MLModel (https://drive.google.com/file/d/16JEWh48fgQc8az7avROePOd-PYda0Yi2/view?usp=sharing) output size from 2048x2048 (existing output size) to 1024x1024.

I used this script to change the output image size:

spec = ct.utils.load_spec("myModel.mlmodel")
output = spec.description.output[0]
output.type.imageType.height = 1024
output.type.imageType.width = 1024
ct.utils.save_spec(spec, "myModelNew.mlmodel")

The new model is saved correctly, with the expected output size in the prediction tab:

enter image description here

But when I run it with the new model it generates 2048x2048 like the original model.
Any idea why will it behave like this? Thank you for the help!


Solution

  • Core ML does not automatically resize the output based on the dimensions you provide. Those dimensions just tell the user of the model what to expect. For this particular model, the output size really is 2048x2048, not 1024x1024.

    The output size of the model is determined by its architecture. If you want a 1024x1024 output, you may need to remove certain layers from the architecture. Or you can add a downsampling layer to the end of the model that converts 2048x2048 into 1024x1024.

    What you did is change the model description, but what you need to do is change the model itself.