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

Mji: <p>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.<br>
When I build on android i get the following error:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/2/128f23b8aec5bb80a989ed90135e5f8b6b9e76e5.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/128f23b8aec5bb80a989ed90135e5f8b6b9e76e5" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/2/128f23b8aec5bb80a989ed90135e5f8b6b9e76e5.jpeg" alt="image" data-base62-sha1="2EbgI058eeLyC9ne2N64AQFy3Vb" width="690" height="204" data-dominant-color="747C85"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">892×265 30.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>error that appears in debugging:<br>
E/AndroidRuntime: FATAL EXCEPTION: Thread-2<br>
Process: org.pytorch.demo.objectdetection, PID: 27891<br>
java.lang.ArrayIndexOutOfBoundsException: length=1310400; index=1310449<br>
at org.pytorch.demo.objectdetection.PrePostProcessor.outputsToNMSPredictions(PrePostProcessor.java:123)<br>
at org.pytorch.demo.objectdetection.MainActivity.run(MainActivity.java:248)</p>
<p>I changed my torch version also, I am still facing the same issue.</p>
<p>yolov5s.torchscript.ptl model file provided in the repo. works fine. I’m facing this issue when I use my custom model.</p>

hanton: <p>cc: <a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a> <a class="mention" href="/u/linbin">@Linbin</a></p>

Linbin: <aside class="quote no-group" data-username="Mji" data-post="1" data-topic="140439">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/a88e57/48.png" class="avatar"> Mji:</div>
<blockquote>
<p>1310400</p>
</blockquote>
</aside>
<p>there are some predefined parameters: <a href="https://github.com/pytorch/android-demo-app/blob/76ba0e04a423acbfe960dcd8dd9a0bc47c3893e7/ObjectDetection/app/src/main/java/org/pytorch/demo/objectdetection/PrePostProcessor.java#L38-L39" rel="noopener nofollow ugc">https://github.com/pytorch/android-demo-app/blob/76ba0e04a423acbfe960dcd8dd9a0bc47c3893e7/ObjectDetection/app/src/main/java/org/pytorch/demo/objectdetection/PrePostProcessor.java#L38-L39</a></p>
<p>Please check if it fits your new model.</p>

Mji: <p>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.<br>
Do I need to change this value also or any other ?</p>
<p>“private static int mOutputColumn = 85; // left, top, right, bottom, score and 80 class probability”</p>
<p>I changed only " private static int mOutputRow = 25200; // as decided by the YOLOv5 model for input image of size 640*640" this value.</p>
<p>Update:<br>
I even changed mOutputColumn = 85 to my custom class number and class number + 5 also but I am still facing the same issue.</p>

Mji: <p>So I solved this issue.<br>
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.</p>
<p>// model output is of size 25200*(num_of_class+5)<br>
private static int mOutputRow = 25200; // as decided by the YOLOv5 model for input image of size 640*640<br>
private static int mOutputColumn = 52; // left, top, right, bottom, score and 80 class probability<br>
private static float mThreshold = 0.50f; // score above which a detection is generated<br>
//</p>

Istiak_Ahmed: <p>02/26 18:59:09: Launching ‘app’ on Pixel 6 API 32.<br>
Install successfully finished in 76 ms.<br>
$ adb shell am start -n “org.pytorch.demo.objectdetection/org.pytorch.demo.objectdetection.MainActivity” -a android.intent.action.MAIN -c android.intent.category.LAUNCHER<br>
Connected to process 16102 on device ‘Pixel_6_API_32 [emulator-5554]’.<br>
Capturing and displaying logcat messages from application. This behavior can be disabled in the “Logcat output” section of the “Debugger” settings page.<br>
I/objectdetectio: Late-enabling -Xcheck:jni<br>
W/objectdetectio: Unexpected CPU variant for X86 using defaults: x86_64<br>
V/studio.deploy: Startup agent attached to VM<br>
V/studio.deploy: No existing instrumentation found. Loading instrumentation from instruments-b5380c1f.jar<br>
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<br>
V/studio.deploy: Applying transforms with cached classes<br>
W/re-initialized&gt;: 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<br>
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.<br>
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.<br>
D/CompatibilityChangeReporter: Compat change id reported: 171979766; UID 10148; state: ENABLED<br>
V/GraphicsEnvironment: ANGLE Developer option for ‘org.pytorch.demo.objectdetection’ set to: ‘default’<br>
V/GraphicsEnvironment: ANGLE GameManagerService for org.pytorch.demo.objectdetection: false<br>
V/GraphicsEnvironment: Neither updatable production driver nor prerelease driver is supported.<br>
D/NetworkSecurityConfig: No Network Security Config specified, using platform default<br>
D/NetworkSecurityConfig: No Network Security Config specified, using platform default<br>
D/Camera2Initializer: CameraX initializing with Camera2 …<br>
I/CameraManagerGlobal: Connecting to camera service<br>
D/CameraRepository: Added camera: 0<br>
D/UseCaseAttachState: Active and online use case: [] for camera: 0<br>
D/CameraRepository: Added camera: 1<br>
D/UseCaseAttachState: Active and online use case: [] for camera: 1<br>
D/libEGL: loaded /vendor/lib64/egl/libEGL_emulation.so<br>
D/libEGL: loaded /vendor/lib64/egl/libGLESv1_CM_emulation.so<br>
D/libEGL: loaded /vendor/lib64/egl/libGLESv2_emulation.so<br>
W/objectdetectio: Accessing hidden method Landroid/view/View;-&gt;computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (unsupported, reflection, allowed)<br>
W/objectdetectio: Accessing hidden method Landroid/view/ViewGroup;-&gt;makeOptionalFitsSystemWindows()V (unsupported, reflection, allowed)<br>
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())<br>
D/AndroidRuntime: Shutting down VM<br>
E/AndroidRuntime: FATAL EXCEPTION: main<br>
Process: org.pytorch.demo.objectdetection, PID: 16102<br>
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 ()<br>
Exception raised from valid at …/caffe2/serialize/inline_container.cc:158 (most recent call first):<br>
(no backtrace available)<br>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3707)<br>
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3864)<br>
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)<br>
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)<br>
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)<br>
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2253)<br>
at android.os.Handler.dispatchMessage(Handler.java:106)<br>
at android.os.Looper.loopOnce(Looper.java:201)<br>
at android.os.Looper.loop(Looper.java:288)<br>
at android.app.ActivityThread.main(ActivityThread.java:7870)<br>
at java.lang.reflect.Method.invoke(Native Method)<br>
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)<br>
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)<br>
Caused by: com.facebook.jni.CppException: PytorchStreamReader failed locating file bytecode.pkl: file not found ()<br>
Exception raised from valid at …/caffe2/serialize/inline_container.cc:158 (most recent call first):<br>
(no backtrace available)<br>
at org.pytorch.LiteNativePeer.initHybrid(Native Method)<br>
at org.pytorch.LiteNativePeer.(LiteNativePeer.java:28)<br>
at org.pytorch.LiteModuleLoader.load(LiteModuleLoader.java:30)<br>
at org.pytorch.demo.objectdetection.MainActivity.onCreate(MainActivity.java:201)<br>
at android.app.Activity.performCreate(Activity.java:8057)<br>
at android.app.Activity.performCreate(Activity.java:8037)<br>
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1341)<br>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3688)<br>
… 12 more<br>
W/System: A resource failed to call close<br>
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’<br>
implementation ‘org.pytorch:pytorch_android_torchvision_lite:1.10.0’ Used) can anyone help me to find the solution?</p>