Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Failed to Load Custom Model in Android #19

Closed
junjuew opened this issue Oct 16, 2019 · 14 comments
Closed

Failed to Load Custom Model in Android #19

junjuew opened this issue Oct 16, 2019 · 14 comments

Comments

@junjuew
Copy link

junjuew commented Oct 16, 2019

Hello,

I'm trying to deploy a custom model in python to Android using pytorch mobile. I used torch.jit.trace to trace and then save the model. The saved model is working correctly when I load it in both C++ and python. However, when using pytorch_android, the following error occurred when loading the module (inside Module.load(modelFileAbsolutePath) function). The only thing different from the PyTorchDemoApp is the model file path. And I've confirmed that the model file path is correct and the file exists. I'm using pytorch 1.3.0 in both python and android.

Any suggestions on why the model loading would fail? Would this be related to this issue? Any suggestions on how to get more debugging information? Thank you!

2019-10-15 21:25:36.585 24959-25880/org.pytorch.demo E/PyTorchDemo: Error during image analysis
    com.facebook.jni.CppException: false CHECK FAILED at ../c10/core/Backend.h (tensorTypeIdToBackend at ../c10/core/Backend.h:106)
    (no backtrace available)
        at org.pytorch.Module$NativePeer.initHybrid(Native Method)
        at org.pytorch.Module$NativePeer.<init>(Module.java:70)
        at org.pytorch.Module.<init>(Module.java:25)
        at org.pytorch.Module.load(Module.java:21)
        at org.pytorch.demo.vision.ImageClassificationActivity.analyzeImage(ImageClassificationActivity.java:168)
        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(lambda)
        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:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.os.HandlerThread.run(HandlerThread.java:61)
@ZhangWen0301
Copy link

same question

1 similar comment
@hmzjwhmzjw
Copy link

same question

@ljk53
Copy link

ljk53 commented Oct 16, 2019

@junjuew thanks for your feedback and it does look similar as #26764.
Are you able to repro this with some open source model? It would be helpful if I can read the model code and see what it does differently during model loading phase.

@junjuew
Copy link
Author

junjuew commented Oct 16, 2019

@ljk53 I reproduced this error using an open-sourced neural style transfer network from here. The original repo is a bit convoluted, so I created a simple repo here to demonstrate the problem. The DNN definition is here. The README.md shows the steps of tracing in python and loading successfully in C++. However, when using the saved model in the android app, the error mentioned above occurs.

@ljk53
Copy link

ljk53 commented Oct 16, 2019

@junjuew thanks for creating the easy repro steps. We are working on a fix now. I reopened #26764 and will keep updating there.

@ljk53
Copy link

ljk53 commented Oct 23, 2019

@junjuew my fixes were landed and available in 1.4-snapshot build. You can update your build.gradle to try 1.4-snapshot, like this: ljk53@443b524

Please let me know if it fixes your problem, thanks!

cc: @ZhangWen0301 @hmzjwhmzjw

@junjuew
Copy link
Author

junjuew commented Oct 24, 2019

@ljk53 It fixed my problem. Thank you. I'll go ahead and close this issue then.

@junjuew junjuew closed this as completed Oct 24, 2019
@IsaacBerman
Copy link

@ljk53 When I switch my dependencies to those found in the changes you linked "1.4.0-SNAPSHOT," i get the following errors "ERROR: Unable to resolve dependency for ':app@debug/compileClasspath': Failed to transform artifact 'pytorch_android.aar (org.pytorch:pytorch_android:1.4.0-SNAPSHOT:20191115.103440-37)' to match attributes {artifactType=jar}"

@FilipJergleAlmquist
Copy link

I also get those errors when using 1.4.0-SNAPSHOT.

@Joyako
Copy link

Joyako commented Nov 18, 2019

@ljk53 The first few weeks, my project is OK when I used the "1.4.0-SNAPSHOT", but now it go wrong.The error is same to @IsaacBerman .

@Joyako
Copy link

Joyako commented Nov 18, 2019

@IsaacBerman You find any method to solve your question, if you have solved it , please tell me, thanks.

@IsaacBerman
Copy link

@Joyako No fix as of yet, will let you know if I find something.

@IsaacBerman
Copy link

@Joyako A project clean fixed the "Unable to resolve dependency error," but the original error still stands for me.

@dreiss
Copy link

dreiss commented Nov 20, 2019

We had some issues with snapshot uploads that should be resolved now. I just downloaded this repo, made these changes, and was able to run ./gradlew assembleDebug in the PyTorchDemoApp directory. If you're still experiencing this, please let us know. And please try to reproduce by checking out a clean copy of the repository and making these changes locally before building, and make sure that you're in the right directory (PyTorchDemoApp vs HelloWorldApp).

diff --git i/PyTorchDemoApp/app/build.gradle w/PyTorchDemoApp/app/build.gradle
index c0226ed..8ca451e 100644
--- i/PyTorchDemoApp/app/build.gradle
+++ w/PyTorchDemoApp/app/build.gradle
@@ -2,6 +2,9 @@ apply plugin: 'com.android.application'
 
 repositories {
     jcenter()
+    maven {
+        url "https://oss.sonatype.org/content/repositories/snapshots"
+    }
 }
 
 android {
@@ -35,6 +38,6 @@ dependencies {
     implementation "androidx.camera:camera-camera2:$camerax_version"
     implementation 'com.google.android.material:material:1.0.0-beta01'
 
-    implementation 'org.pytorch:pytorch_android:1.3.0'
-    implementation 'org.pytorch:pytorch_android_torchvision:1.3.0'
+    implementation 'org.pytorch:pytorch_android:1.4.0-SNAPSHOT'
+    implementation 'org.pytorch:pytorch_android_torchvision:1.4.0-SNAPSHOT'
 }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants