Crash when running my TorchScript model from JAVA

Hi,
I’m trying to run my TorchScript model in JAVA, but I get an error and my application crashes. I based on the code in the “HelloWorld” example application, which worked fine on my Android device.

code lines:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Bitmap bitmap = null;
Module module = null;
try {
  // creating bitmap from packaged into app android asset 'image.jpg',
  Log.d("PytorchHelloWorld", "Loading input image");
  bitmap = BitmapFactory.decodeStream(getAssets().open("image2.jpg"));

  // loading serialized torchscript module from packaged into app android asset model.pt,
  // app/src/model/assets/model.pt
  Log.d("PytorchHelloWorld", "Loading pytorch model");
  module = Module.load(assetFilePath(this, "torchScript_model.pt"));
} catch (IOException e) {
  Log.e("PytorchHelloWorld", "Error reading assets", e);
  finish();
}

// showing image on UI
ImageView imageView = findViewById(R.id.image);
imageView.setImageBitmap(bitmap);

// preparing input tensor
Log.d("PytorchHelloWorld", "Preparing input tensor");
final Tensor inputTensor = TensorImageUtils.bitmapToFloat32Tensor(bitmap,
    TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_STD_RGB);

// running the model
Log.d("PytorchHelloWorld", "Running the model");
final Tensor outputTensor = module.forward(IValue.from(inputTensor)).toTensor();
TextView textView = findViewById(R.id.text);
textView.setText("ok");

}

logs
PytorchHelloWorld: Loading input image
PytorchHelloWorld: Loading pytorch model
AndroidRuntime: Shutting down VM
AndroidRuntime: FATAL EXCEPTION: main
AndroidRuntime: Process: org.pytorch.helloworld, PID: 8625
AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pytorch.helloworld/org.pytorch.helloworld.MainActivity}: com.facebook.jni.CppException:
AndroidRuntime: Arguments for call are not valid.
AndroidRuntime: The following variants are available:
AndroidRuntime:
AndroidRuntime: aten::upsample_bilinear2d(Tensor self, int[2] output_size, bool align_corners) -> (Tensor):
AndroidRuntime: Expected at most 3 arguments but found 5 positional arguments.
AndroidRuntime:
AndroidRuntime: aten::upsample_bilinear2d.out(Tensor self, int[2] output_size, bool align_corners, *, Tensor(a!) out) -> (Tensor(a!)):
AndroidRuntime: Argument out not provided.

Can you please help me understand what’s the issue?

Hello, @Aviais
Which version of pytorch did you use to get your model.pt file?
(To check the version: python -c "import torch; print(torch.version.__version) )

Which version of gradle dependencies do you use?

It could be that the model was traced/scripted with different version from gradle dependencies, while between those versions aten::upsample_bilinear2d.out maybe changed.

If you trace/script with nightly build - its better to use android dependencies from nightly channel (https://github.com/pytorch/pytorch/tree/master/android#nightly)

And matching version of gradle dependencies and installed pytorch.

Hi @IvanKobzarev,
Thank you! You solved my problem!
I used PyTorch 1.5.0 to get my ‘model.pt’ and gradle dependencies of 1.4.0. When I changed my dependencies to 1.5.0, the issues was solved!

Thanks!