pytorch / android-demo-app

PyTorch android examples of usage in applications
1.47k stars 606 forks source link

com.facebook.jni.CppException: isTensor() INTERNAL ASSERT FAILED in PytorchDemo when using a customized model #163

Closed ch1nlu closed 3 years ago

ch1nlu commented 3 years ago

Hello, I came into this issue when I was trying to test the Image Classification in PytorchDemo with my own model mobilenet.pt, which is a binary classification model based on mobilenet_v2.

I altered the original code to accomondate my demand: in VisionListActivity.java:

//      intent.putExtra(ImageClassificationActivity.INTENT_MODULE_ASSET_NAME,
//          "mobilenet_v2.pt");
      intent.putExtra(ImageClassificationActivity.INTENT_MODULE_ASSET_NAME,
              "mobilenet.pt");

I also altered TOP_K=2 in ImageClassificationActivity.java.

However, when I run Image Classification module, I got the following error message:

E/PyTorchDemo: Error during image analysis com.facebook.jni.CppException: isTensor() INTERNAL ASSERT FAILED at "../../../../src/main/cpp/libtorch_include/x86/ATen/core/ivalue_inl.h":111, please report a bug to PyTorch. Expected Tensor but got Object Exception raised from toTensor at ../../../../src/main/cpp/libtorch_include/x86/ATen/core/ivalue_inl.h:111 (most recent call first): (no backtrace available) at org.pytorch.NativePeer.initHybrid(Native Method) at org.pytorch.NativePeer.(NativePeer.java:24) at org.pytorch.Module.load(Module.java:23) at org.pytorch.demo.vision.ImageClassificationActivity.analyzeImage(ImageClassificationActivity.java:166) at org.pytorch.demo.vision.ImageClassificationActivity.analyzeImage(ImageClassificationActivity.java:31) at org.pytorch.demo.vision.AbstractCameraXActivity.lambda$setupCameraX$2$AbstractCameraXActivity(AbstractCameraXActivity.java:90) at org.pytorch.demo.vision.-$$Lambda$AbstractCameraXActivity$t0OjLr-l_M0-_0_dUqVE4yqEYnE.analyze(Unknown Source:2) at androidx.camera.core.ImageAnalysisAbstractAnalyzer.analyzeImage(ImageAnalysisAbstractAnalyzer.java:57) at androidx.camera.core.ImageAnalysisNonBlockingAnalyzer$1.run(ImageAnalysisNonBlockingAnalyzer.java:135) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.os.HandlerThread.run(HandlerThread.java:67)

I had a hard time to debug the error message above.

It worked fine when I use the original mobilenet_v2.pt, so I've suspected the problem is caused by my own model. But turns out that my own model mobilenet.pt also works well when I plug it into HelloWorldApp project.

Could you kindly help me?

ch1nlu commented 3 years ago

Issue fixed. We change the pytorch dependencies version in build.gradle to match the one in HelloWorldApp and it worked!

//    implementation 'org.pytorch:pytorch_android:1.6.0-SNAPSHOT'
//    implementation 'org.pytorch:pytorch_android_torchvision:1.6.0-SNAPSHOT'
    implementation 'org.pytorch:pytorch_android:1.8.0'
    implementation 'org.pytorch:pytorch_android_torchvision:1.8.0'