Custom Yolov5s TorchScript model does not work on Android Object Detection Demo App

I have custom model trained on yolov5s v5 and I converted it to torchscript.ptl using ultralytics export.py with code modification as told here.
When I build on android i get the following error:

error that appears in debugging:
E/AndroidRuntime: FATAL EXCEPTION: Thread-2
Process: org.pytorch.demo.objectdetection, PID: 27891
java.lang.ArrayIndexOutOfBoundsException: length=1310400; index=1310449
at org.pytorch.demo.objectdetection.PrePostProcessor.outputsToNMSPredictions(PrePostProcessor.java:123)
at org.pytorch.demo.objectdetection.MainActivity.run(MainActivity.java:248)

I changed my torch version also, I am still facing the same issue.

yolov5s.torchscript.ptl model file provided in the repo. works fine. I’m facing this issue when I use my custom model.

cc: @IvanKobzarev @Linbin

there are some predefined parameters: https://github.com/pytorch/android-demo-app/blob/76ba0e04a423acbfe960dcd8dd9a0bc47c3893e7/ObjectDetection/app/src/main/java/org/pytorch/demo/objectdetection/PrePostProcessor.java#L38-L39

Please check if it fits your new model.

Thank you for your reply. I changed the value as calculated by 25200*(num_of_class+5) but I’m still facing the same issue.
Do I need to change this value also or any other ?

“private static int mOutputColumn = 85; // left, top, right, bottom, score and 80 class probability”

I changed only " private static int mOutputRow = 25200; // as decided by the YOLOv5 model for input image of size 640*640" this value.

Update:
I even changed mOutputColumn = 85 to my custom class number and class number + 5 also but I am still facing the same issue.

So I solved this issue.
You need to change only mOutputColumn value with you number of classes+5. I was changing mOutputRow = 25200 with value calculated by 25200*(num_of_class+5) which caused this issue.

// model output is of size 25200*(num_of_class+5)
private static int mOutputRow = 25200; // as decided by the YOLOv5 model for input image of size 640*640
private static int mOutputColumn = 52; // left, top, right, bottom, score and 80 class probability
private static float mThreshold = 0.50f; // score above which a detection is generated
//

1 Like

02/26 18:59:09: Launching ‘app’ on Pixel 6 API 32.
Install successfully finished in 76 ms.
$ adb shell am start -n “org.pytorch.demo.objectdetection/org.pytorch.demo.objectdetection.MainActivity” -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 16102 on device ‘Pixel_6_API_32 [emulator-5554]’.
Capturing and displaying logcat messages from application. This behavior can be disabled in the “Logcat output” section of the “Debugger” settings page.
I/objectdetectio: Late-enabling -Xcheck:jni
W/objectdetectio: Unexpected CPU variant for X86 using defaults: x86_64
V/studio.deploy: Startup agent attached to VM
V/studio.deploy: No existing instrumentation found. Loading instrumentation from instruments-b5380c1f.jar
W/objectdetectio: DexFile /data/data/org.pytorch.demo.objectdetection/code_cache/.studio/instruments-b5380c1f.jar is in boot class path but is not in a known location
V/studio.deploy: Applying transforms with cached classes
W/re-initialized>: type=1400 audit(0.0:66): avc: granted { execute } for path=“/data/data/org.pytorch.demo.objectdetection/code_cache/startup_agents/8dee6a62-agent.so” dev=“dm-30” ino=139518 scontext=u:r:untrusted_app:s0:c148,c256,c512,c768 tcontext=u:object_r:app_data_file:s0:c148,c256,c512,c768 tclass=file app=org.pytorch.demo.objectdetection
W/objectdetectio: Redefining intrinsic method java.lang.Thread java.lang.Thread.currentThread(). This may cause the unexpected use of the original definition of java.lang.Thread java.lang.Thread.currentThread()in methods that have already been compiled.
W/objectdetectio: Redefining intrinsic method boolean java.lang.Thread.interrupted(). This may cause the unexpected use of the original definition of boolean java.lang.Thread.interrupted()in methods that have already been compiled.
D/CompatibilityChangeReporter: Compat change id reported: 171979766; UID 10148; state: ENABLED
V/GraphicsEnvironment: ANGLE Developer option for ‘org.pytorch.demo.objectdetection’ set to: ‘default’
V/GraphicsEnvironment: ANGLE GameManagerService for org.pytorch.demo.objectdetection: false
V/GraphicsEnvironment: Neither updatable production driver nor prerelease driver is supported.
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/Camera2Initializer: CameraX initializing with Camera2 …
I/CameraManagerGlobal: Connecting to camera service
D/CameraRepository: Added camera: 0
D/UseCaseAttachState: Active and online use case: [] for camera: 0
D/CameraRepository: Added camera: 1
D/UseCaseAttachState: Active and online use case: [] for camera: 1
D/libEGL: loaded /vendor/lib64/egl/libEGL_emulation.so
D/libEGL: loaded /vendor/lib64/egl/libGLESv1_CM_emulation.so
D/libEGL: loaded /vendor/lib64/egl/libGLESv2_emulation.so
W/objectdetectio: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (unsupported, reflection, allowed)
W/objectdetectio: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (unsupported, reflection, allowed)
W/native: [W LegacyTypeDispatch.h:79] Warning: AutoNonVariableTypeMode is deprecated and will be removed in 1.10 release. For kernel implementations please use AutoDispatchBelowADInplaceOrView instead, If you are looking for a user facing API to enable running your inference-only workload, please use c10::InferenceMode. Using AutoDispatchBelowADInplaceOrView in user code is under risk of producing silent wrong result in some edge cases. See Note [AutoDispatchBelowAutograd] for more details. (function operator())
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.pytorch.demo.objectdetection, PID: 16102
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pytorch.demo.objectdetection/org.pytorch.demo.objectdetection.MainActivity}: com.facebook.jni.CppException: PytorchStreamReader failed locating file bytecode.pkl: file not found ()
Exception raised from valid at …/caffe2/serialize/inline_container.cc:158 (most recent call first):
(no backtrace available)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3707)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3864)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2253)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7870)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: com.facebook.jni.CppException: PytorchStreamReader failed locating file bytecode.pkl: file not found ()
Exception raised from valid at …/caffe2/serialize/inline_container.cc:158 (most recent call first):
(no backtrace available)
at org.pytorch.LiteNativePeer.initHybrid(Native Method)
at org.pytorch.LiteNativePeer.(LiteNativePeer.java:28)
at org.pytorch.LiteModuleLoader.load(LiteModuleLoader.java:30)
at org.pytorch.demo.objectdetection.MainActivity.onCreate(MainActivity.java:201)
at android.app.Activity.performCreate(Activity.java:8057)
at android.app.Activity.performCreate(Activity.java:8037)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1341)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3688)
… 12 more
W/System: A resource failed to call close
i used custom ptl mode after runing sometime app close automatically without running the app or sometime it will show this error. ( implementation ‘org.pytorch:pytorch_android_lite:1.10.0’
implementation ‘org.pytorch:pytorch_android_torchvision_lite:1.10.0’ Used) can anyone help me to find the solution?