Torchvision.ops.nms on Android Mobile

I try to use torchvision.ops.nms in my model. and i have used the following to save my model using torch.jit.trace.

example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)
traced_script_module.save("model.pt")

Everything works well. But when load model.pt on mobile,came across error.

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)
                          ~~~~~~~~~~~~~~~~~~~ <--- 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)
    

Can anyone please help me? Thank you

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.

The log message explaining that torchvision::nms op may not be currently supported seems to be contradicting the up-to-date Pytorch 1.7.0 doc

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 (<= 1.7.0)

I opened an issue in vision repo a few weeks ago. It didn’t get attention :confused:

same error in torch1.6.0 / torchvisoin 0.7.0.

Any updates on this?

Got the same error with torch 1.8 and vision 0.9 …

I was able to successfully use the operations by loading them with facebooks native loading. Here’s some code to load torchvision_ops:

import com.facebook.soloader.nativeloader.NativeLoader;
import com.facebook.soloader.nativeloader.SystemDelegate;

With this in main:

static {    
    if (!NativeLoader.isInitialized()) {
        NativeLoader.init(new SystemDelegate());
    }
    NativeLoader.loadLibrary("pytorch_jni");
    NativeLoader.loadLibrary("torchvision_ops");
}
1 Like

Please checkout the tutorial for d2go how to use torchvision_ops:

Does this works now , still I am facing error