Different result for the same pytorch model on PC and android mobilephone?

username: btdan
cooked: <p>Hello, I have trained a mobilenetv2 model with the help of pytorch(version: 1.9.0)<br>
and convert to .pt format， deploy the .pt model on android mobilephone.<br>
However, I get very different result from the result on PC.<br>
Can anyone help me to solve this problem?<br>
The  details are as follows:<br>
First，train  the new model(named result.ckpt) from mobilenetv2(the same problem with mobilenetv3 ) with the help of pytorch.<br>
Secondly, convert  the model result.ckpt  to result.pt  the key  source codes is as follows:</p>
<pre><code class="lang-auto">import torch
import torchvision
from torch.utils.mobile_optimizer import optimize_for_mobile
import torchvision.models as models

model= torch.load('result.ckpt', map_location=torch.device('cpu'))
print(model)
model.eval()
example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)
traced_script_module.save("result.pt")

</code></pre>
<p>Thirdly, deploy the result.pt model inside app source code. And then compile to get the apk<br>
the key source codes are as follows:</p>
<pre><code class="lang-auto">module = LiteModuleLoader.load(assetFilePath(this, "largeMbv3.pt"));
bitmap = decodeFile(imgPath);
newbitmap = compressImageFromBitmap(bitmap,224,224);
final Tensor inputTensor = TensorImageUtils.bitmapToFloat32Tensor(newbitmap,
                TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_STD_RGB, MemoryFormat.CHANNELS_LAST);
      
        // running the model
        final Tensor outputTensor = module.forward(IValue.from(inputTensor)).toTensor();
        final float[] scores = outputTensor.getDataAsFloatArray();
</code></pre>
<p>And the function compressImageFromBitmap is as follows:</p>
<pre><code class="lang-auto">    public static Bitmap compressImageFromBitmap(Bitmap bitmap, int pixelW, int pixelH) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
        if( os.toByteArray().length / 1024&gt;512) {//判断如果图片大于0.5M,进行压缩避免在生成图片（BitmapFactory.decodeStream）时溢出
            os.reset();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 50, os);
        }
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        BitmapFactory.decodeStream(is, null, options);
        options.inJustDecodeBounds = false;
        options.inSampleSize = computeSampleSize(options , pixelH &gt; pixelW ? pixelW : pixelH ,pixelW * pixelH );
        is = new ByteArrayInputStream(os.toByteArray());
        Bitmap newBitmap = BitmapFactory.decodeStream(is, null, options);
        return newBitmap;
    }

</code></pre>
<p>However, I get quiet different result for same image to identify on PC and android<br>
Can anyone help me to solve this problem? Thans  very much.</p>
<p>By the way, I have set random to  a fix number when training the model.Just as follows:</p>
<pre><code class="lang-auto">seed=42
random.seed(seed)
numpy.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)  # all gpus
    torch.backends.cudnn.benchmark = False
    torch.backends.cudnn.deterministic = True
torch.use_deterministic_algorithms(True)
torch.set_default_tensor_type(torch.float32)
</code></pre>
<p>But it does  not  have any  change at all.<br>
Thans very much  for your  help .<br>
Best wishes to you.</p>