Ho to export a PyTorch model to CoreML model for usage in a iOS App

Hi,

as showed in the course I created the PyTorch model sample and want to export / convert this model o a CoreML iOS Model using the coremltools. Input is a 224x224 image and output is a image classification (3 different classes)

I am using coremltools for this with this code:

import coremltools as ct

modelml = ct.convert( scripted_model, inputs=[ct.ImageType(shape=(1,3,224,244))] )

I have a working iOS App code which performs with another model which was created using Microsoft Azure Vision. The PyTorch exported model is loaded and a prediction is performed, but I am getting this error:

Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[VNCoreMLFeatureValueObservation identifier]: unrecognized selector sent to instance 0x2805dd3b0

When I check the exported model with Xcode and compare it with another model which is working with the sample iOS App code (created and exported from Microsoft Azure) I can see that the input (for image classification using the device camera) seems ok and is equal, but the output is totally different. (see screenshots)

The working model has two outputs:

loss => Dictionary (String => Double) classLabel => String

My exported model using coremltools just has one export: MultiArray(Float32) (name var_1620, I think this is the last feature layer output of the EfficentNetB2)

How do I change my model or my coremltools export to get the correct output for the prediction ? I read the coreml documentation (https://coremltools.readme.io/docs/pytorch-conversion) and tried some GitHub samples. But I never get the correct output.

How do I export the PyTorch model so that the output is correct and the prediction will work ?

Best

Marco

I have the same question in FB16079804 and posted here.

[@Marco Seraphin](https://developer.apple.com/forums/profile/Marco Seraphin) have you found the solution ?

Ho to export a PyTorch model to CoreML model for usage in a iOS App
 
 
Q