apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.34k stars 626 forks source link

Converted classification model returns invalid confidence scores (e.g. negative numbers and Nan) #1015

Open Johnson145 opened 3 years ago

Johnson145 commented 3 years ago

🐞Describe the bug

I converted a TensorFlow graph into a mlmodel. I can correctly deploy the mlmodel file except for the fact that the values it returns do not make any sense. It's a classification model supposed to return confidence scores in [0,1], but it returns arbitrary large and/or small numbers. Once, I even got a NaN value.

The original TensorFlow graph works just fine. It feels like maybe the preprocessing isn't applied correctly? I have configured the preprocessing during the conversion though.

To Reproduce

This is how I converted the model:


    bias = -mean / std
    image_input = ct.ImageType(shape=(1, input_size, input_size, 3,),
                               bias=[bias, bias, bias],
                               scale=1 / std,
                               name=input_feature_name,
                               )

    ml_model = ct.convert(
        model=model_path,
        inputs=[image_input],
        outputs=[output_feature_name],
        minimum_deployment_target=ct.target.iOS13,
    )

    ml_model.save(output_path)

I'm sorry, I don't have model file which I'm allowed to share. However, I could reproduce this with multiple models (e.g. Inceptionv3 and MobileNetv2).

System environment (please complete the following information):

Additional context

My mlmodel integration within my iOS app isn't a new one. I originally set it up to target iOS 11 along with the following successful setup:

That's working as it should. It works with older versions of the same graphs, which are failing now, too. However, before retraining my model I migrated from TensorFlow 1.14 to 1.15 and so I couldn't use that setup anymore.

As an intermediate solution I also tried to use tfcoreml 1.1 with coremltools 3.4 to target iOS 13. It suffers the same issue as the finally used setup of coremltools 4.0.

I got no clue how to continue debugging this. Any help is appreciated very much!

TobyRoseman commented 1 year ago

@Johnson145 - Can you give us self contained steps to reproduce the problem? Without model_path we can not run your code.