Torchvision.ops.nms on Android Mobile

AmrAhmed: <p>I try to use torchvision.ops.nms in my model. and i have used the following to save my model using torch.jit.trace.</p>
<pre><code class="lang-auto">example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)
traced_script_module.save("model.pt")
</code></pre>
<p>Everything works well. But when load model.pt on mobile,came across error.</p>
<pre><code class="lang-auto">E/AndroidRuntime: Caused by: com.facebook.jni.CppException: 
    Unknown builtin op: torchvision::nms.
    Could not find any similar ops to torchvision::nms. This op may not exist or may not be currently supported in TorchScript.
    :
.................
/usr/lib/python3.6/runpy.py(85): _run_code
    /usr/lib/python3.6/runpy.py(193): _run_module_as_main
    Serialized   File "code/__torch__/resnet.py", line 68
        _39 = torch.slice(torch.select(scores0, 0, 0), 0, 0, 9223372036854775807, 1)
        _40 = torch.slice(_38, 1, 0, 9223372036854775807, 1)
        anchors_nms_idx = ops.torchvision.nms(_40, torch.select(_39, 1, 0), 0.5)
                          ~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
        _41 = torch.slice(torch.select(classification0, 0, 0), 1, 0, 9223372036854775807, 1)
        anchors_nms_idx0 = torch.to(anchors_nms_idx, dtype=4, layout=0, device=torch.device("cpu"), pin_memory=False, non_blocking=False, copy=False, memory_format=None)
    
</code></pre>
<p>Can anyone please help me? Thank you</p>

valerianG: <p>Same error using Pytorch 1.7.0 / torchvision 0.8.1 packages in Python, and ‘org.pytorch:pytorch_android:1.7.0’ / ‘org.pytorch:pytorch_android_torchvision:1.7.0’ as dependencies in Android.</p>
<p>The log message explaining that torchvision::nms op may not be currently supported seems to be contradicting the  <a href="https://pytorch.org/docs/stable/torchvision/ops.html" rel="noopener nofollow ugc">up-to-date Pytorch 1.7.0 doc</a></p>
<p>EDIT: Torchscript op sets are not the same on PC and android. Nms is indeed included in torchvision and supports scripting but is unavailable in the android version of pytorch (&lt;= 1.7.0)</p>

vferrer: <p>I opened an issue in <a href="https://github.com/pytorch/vision/issues/2581" rel="noopener nofollow ugc">vision repo</a> a few weeks ago. It didn’t get attention <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"></p>

BobChen: <p>same error in torch1.6.0 / torchvisoin 0.7.0.</p>

Ludwig_Friborg: <p>Any updates on this?</p>
<p>Got the same error with torch 1.8 and vision 0.9 …</p>

Ludwig_Friborg: <p>I was able to successfully use the operations by loading them with facebooks native loading. Here’s some code to load <code>torchvision_ops</code>:</p>
<pre><code class="lang-auto">import com.facebook.soloader.nativeloader.NativeLoader;
import com.facebook.soloader.nativeloader.SystemDelegate;
</code></pre>
<p>With this in main:</p>
<pre><code class="lang-auto">static {    
    if (!NativeLoader.isInitialized()) {
        NativeLoader.init(new SystemDelegate());
    }
    NativeLoader.loadLibrary("pytorch_jni");
    NativeLoader.loadLibrary("torchvision_ops");
}
</code></pre>

IvanKobzarev: <p>Please checkout the tutorial for d2go how to use torchvision_ops:</p>
<aside class="onebox githubfolder">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="16" height="16">

      <a href="https://github.com/pytorch/android-demo-app/tree/master/D2Go" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h3><a href="https://github.com/pytorch/android-demo-app/tree/master/D2Go" target="_blank" rel="noopener nofollow ugc">pytorch/android-demo-app</a></h3>

  <p><a href="https://github.com/pytorch/android-demo-app/tree/master/D2Go" target="_blank" rel="noopener nofollow ugc">master/D2Go</a></p>

  <p><span class="label1">PyTorch android examples of usage in applications. Contribute to pytorch/android-demo-app development by creating an account on GitHub.</span></p>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


yogesh_vasu: <p>Does this works now , still I am facing error</p>

Osman: <p>replace:</p>
<pre><code>NativeLoader.loadLibrary("pytorch_jni");
</code></pre>
<p>with:</p>
<pre><code>NativeLoader.loadLibrary("pytorch_jni_lite");
</code></pre>

Cagatay_Bilgin: <p>You landed on this page from a web search? Please visit ExecuTorch which replaced the older stack! <a href="https://github.com/pytorch/executorch" class="inline-onebox" rel="noopener nofollow ugc">GitHub - pytorch/executorch: On-device AI across mobile, embedded and edge for PyTorch</a>. If you have any questions or want to stay in the loop, join our Discord server at <a href="https://discord.gg/Wyn9PWqP" class="inline-onebox" rel="noopener nofollow ugc">PyTorch Edge</a>.</p>