Convert to CoreML but predict wrong

MarcSteven: <p>I convert the PyTorch model to CoreML model , but it always predict wrongly, I don’t know how to solve this issue.<br>
The model input type is multiArray 1<em>3</em>224<em>224, and output is multiArray 1</em> 7.<br>
When predicted , it always show the same results.<br>
I ever add preprocess_args when converted  onnx to CoreML , but it cannot get  the same results.</p>

ptrblck: <p>It seems like a dublicate from your other threads on this topic.</p>
<p>Based on all descriptions so far, it seems that you don’t know:</p>
<ul>
<li>how the PyTorch training was done</li>
<li>what preprocessing steps were performed</li>
<li>how to match the CoreML output to the PyTorch model output</li>
</ul>
<p>If that’s still the case, I doubt we can help you any further.</p>
<p>My best suggestion was:<br>
Try to add a preprocessing pipeline manually, retrain the model, and export this <em>known</em> workflow to CoreML.<br>
We are mainly focused on PyTorch so most likely you won’t get much help regarding the CoreML deployment and you should ask in their discussion board or in their GitHub repo, if you encounter any issues with CoreML.</p>

MarcSteven: <p><a href="https://discuss.pytorch.org/t/how-to-add-preprocess-into-the-model/66154">preprocess in the PyTorch</a><br>
In their you said if you never did image preprocess , in my CoreML model I should ignore the image preprocess. But CoreML only know the pixel 0- 255, but if model of pytorch never did image preprocess, how to deal with the model to get the right answer.</p>
<p>So I want to ensure three cases about it</p>
<ul>
<li>1， Is it possible to change the input type to image_ from multiArray to image</li>
<li>2,  is it possible to add image preprocess   in the model.</li>
<li>3, How to get the preprocess steps about it.<br>
I don’t know how the model trained the preprocess args, I asked the engineer, he replied he didn’t know.<br>
Convert onnx to CoreML , i add preprocess args about it.<br>
I only add image_scale:1/255.0, red_bias/blue_bias/green_bias (never added it)<br>
I know even though in the pytorch model added the preprocess but when it converted  to onnx, it doesn’t support image preprocess, so when convert to CoreML, I must do it manually. But perhaps they are same.<br>
By the way, if never did image preprocess in PyTorch model, what  will be happened? Or MultiArray never need to image preprocess.<br>
What’s the difference between multiArray and image in the field of preprocess?<br>
Thank for your info. It’s so painful.<br>
<strong>Actually in Python training code, it should have preprocess_input or normalize function, but if the training model doesn’t have the step, does it influence the result?</strong>
</li>
</ul>

ptrblck: <ol>
<li>
<p>I don’t know, what <code>multiArray</code> is and I assume it’s part of the CoreML model? If so, I would recommend to ask in their board or github.</p>
</li>
<li>
<p>Yes, as answered <a href="https://discuss.pytorch.org/t/how-to-add-preprocess-into-the-model/66154/4">here</a> with an example.</p>
</li>
<li>
<p>If neither you nor the engineer who was working on the model knows, which preprocessing was done, unfortunately we cannot help. My best shot is still to retrain the model with a <em>known</em> preprocessing pipeline.<br>
Depending what preprocessing steps were done, ONNX might support them. E.g. normalization is a simple subtraction and division, which is supported by ONNX. More complicated image transformation might not be supported and you should implement them in CoreML or any other library supported by your deployment platform. Since we do not know which preprocessing was done, it won’t really help discussing, which methods are implemented in ONNX etc.</p>
</li>
</ol>
<p><code>MultiArray</code> doesn’t seem to be a PyTorch class and it seem your question was already answered in the <a href="https://github.com/onnx/onnx-coreml/issues/515">onnx-coreml github</a>.</p>
<aside class="quote no-group" data-username="MarcSteven" data-post="3" data-topic="66355">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/a6a055/48.png" class="avatar"> MarcSteven:</div>
<blockquote>
<p>Actually in Python training code, it should have preprocess_input or normalize function, but if the training model doesn’t have the step, does it influence the result?</p>
</blockquote>
</aside>
<p>Normalization usually speeds up the training or makes it possible in the first place.</p>
<p>If normalization or any other preprocessing technique <em>was already</em> used to train your model, you <em>have to</em> add these steps to your deployment pipeline. Otherwise your model will just output random values and will most likely yield a bad accuracy.</p>

MarcSteven: <blockquote>
<p>MultiArray should be from Numpy<br>
for Image classifier image preprocess is necessary, if you don’t do the task, the model never know what it is.So that’s why my model predicts wrongly.</p>
</blockquote>