If you can’t convert with CoreMLTools, rewrite the unsupported operation and convert it. Pytorch convert function for op’’’not implemented.

MLBoy
3 min readDec 27, 2021

What to do if there is an operation that cannot be converted by CoreML Tools

CoreMLTools converts machine learning models for iOS

This is a method to try conversion somehow when conversion is not possible.

We want to convert even if an error occurs

When converting a machine learning model, we may get an error.

pytorch convert function for op’var’ not implemented.

But we want to convert it somehow.
There are quite a few models that the conversion script doesn’t support, but if we can somehow convert them, we can use those models on iOS.

Caused by some operations that CoreML Tools does not support

For example, in the above case, CoreMLTools does not have a function to change the pytorch function that asks for the variance called’var’, so it cannot be converted.

Manually write the calculation and solve it

If the function is not supported, we can substitute the function with the functions that are supported.
In the above case, it can be converted by CoreMLTools by writing a calculation to find the variance.

Because
1. The variance is calculated from the average.
2. The average can be calculated by the function torch.mean.
3. torch.mean can be converted with CoreMLTools.

Therefore, it can be replaced with handwritten calculation and converted with CoreML Tools.

The above uses the mean function to find the variance.

Find the variance
1. Calculate the mean of value
2. Calculate the deviation (value-mean value)
3. Calculate the squared mean of the deviation (= variance)

Now you can convert.

By rewriting the model as above, the conversion was successful.

[Output of the converted Photo2 Cartoon]

Points to note

However, the var function of pytorch requires an unbiased variance, which is not exactly the variance.
So the output is a little different.

[Slightly different output. Left original / right manually converted Core ML model]

However, it is better than nothing, so it will be converted.
Or rather, should I have included a calculation to find the unbiased variance? .. ..

Various models can be converted

We may encounter unsupported operations and get stuck, but
we can convert various models to mobile by replacing them with convertible forms as described above .

🐣

I’m a freelance engineer.
Work consultation
Please feel free to contact us with a brief development description.
rockyshikoku@gmail.com

I am making an app that uses Core ML and ARKit.
We send machine learning / AR related information.

GitHub

Twitter
Medium

--

--