Usage of U-2-Net Model in android

I converted the orignal model u2net.pth to u2net.p like follows :

scripted = torch.jit.script(net), “u2net.p”)

and also converted it to light version :

example = torch.rand(1, 3, 320, 320)
traced_script_module = torch.jit.trace(model, example)
traced_script_module_optimized = optimize_for_mobile(traced_script_module)

However, I’m having some trouble using this model in android (following the instructions from here PyTorch Android ), precisely in giving input and taking the output, any help would be much appreciated …

Hey @fahad, could you share what kind of issues you’re having? logs and code snippet would be helpful for us to give suggestions. CC: @Martin_Yuan @IvanKobzarev

Sure @guangy10 , Acutally I’m using the demo app (provided by PyTorch) for Image segmentation
provided here on GitHub, which is using deeplab v3, I replaced my u2net.ptl model with deeplab v3 model and its giving me errors like follows:

java.lang.IllegalStateException: Expected IValue type 13, actual type 7
at org.pytorch.IValue.preconditionType(
at org.pytorch.IValue.toDictStringKey(

@IvanKobzarev , could you help on the demo app issue?

Hi @fahad, it failed at this line

    Map<String, IValue> outTensors = mModule.forward(IValue.from(inputTensor)).toDictStringKey();

The error message said that it expects DICT_STRING_KEY (type 13) but got TUPLE (type 7). That means your model returns a tuple instead of dict. You need to call toTuple() instead.

    IValue[] outputTuple = mModule.forward(IValue.from(inputTensor)).toTuple();

Hey @Linbin, I tried what you suggested, but there’s another error that get raised due to that and that is in the outputTensor, it becomes unable to get the values from outTensors …
I’m attaching an image I know its a little odd to attach images written code but I want to show you the error …

it’s an array in Java so just use index:

          Tensor outputTensor = outTensors[0].toTensor()

@Linbin sorry for the late reply, I tried that with the array but now there is an index out of bound exception is raised due to that …

You may want to print the shape of the output tensor

     long[] shape = outputTensor.shape();