Usage of U-2-Net Model in android

fahad: <p>I converted the orignal model u2net.pth to u2net.p like follows :</p>
<blockquote>
<p>scripted = torch.jit.script(net)<br>
torch.jit.save(scripted, “u2net.p”)</p>
</blockquote>
<p>and also converted it to light version :</p>
<blockquote>
<p>example = torch.rand(1, 3, 320, 320)<br>
traced_script_module = torch.jit.trace(model, example)<br>
traced_script_module_optimized = optimize_for_mobile(traced_script_module)<br>
traced_script_module_optimized._save_for_lite_interpreter(“saved_models/u2net.ptl”)</p>
</blockquote>
<p>However, I’m having some trouble using this model in android (following the instructions from here <a href="https://pytorch.org/mobile/android/" rel="noopener nofollow ugc">PyTorch Android</a> ), precisely in giving input and taking the output, any help would be much appreciated …</p>

guangy10: <p>Hey <a class="mention" href="/u/fahad">@fahad</a>, could you share what kind of issues you’re having? logs and code snippet would be helpful for us to give suggestions. CC: <a class="mention" href="/u/martin_yuan">@Martin_Yuan</a> <a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a></p>

fahad: <p>Sure <a class="mention" href="/u/guangy10">@guangy10</a> , Acutally I’m using the demo app (provided by PyTorch) for Image segmentation<br>
provided <a href="https://github.com/pytorch/android-demo-app/tree/master/ImageSegmentation" rel="noopener nofollow ugc">here</a> on GitHub, which is using deeplab v3, I replaced my u2net.ptl model with deeplab v3 model and its giving me errors like follows:</p>
<blockquote>
<p>java.lang.IllegalStateException: Expected IValue type 13, actual type 7<br>
at org.pytorch.IValue.preconditionType(IValue.java:314)<br>
at org.pytorch.IValue.toDictStringKey(IValue.java:301)<br>
at org.pytorch.imagesegmentation.MainActivity.run(MainActivity.java:124)<br>
at java.lang.Thread.run(Thread.java:919)</p>
</blockquote>

iseeyuan: <p><a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a> , could you help on the demo app issue?</p>

Linbin: <p>Hi <a class="mention" href="/u/fahad">@fahad</a>, it failed at this line</p>
<pre><code>    Map&lt;String, IValue&gt; outTensors = mModule.forward(IValue.from(inputTensor)).toDictStringKey();
</code></pre>
<p>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 <strong>toTuple()</strong> instead.</p>
<pre><code>    IValue[] outputTuple = mModule.forward(IValue.from(inputTensor)).toTuple();</code></pre>

fahad: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/6/76f5903da35a20762ac8adad7a87d651eb34516d.png" data-download-href="https://discuss.pytorch.org/uploads/default/76f5903da35a20762ac8adad7a87d651eb34516d" title="TupleError"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/6/76f5903da35a20762ac8adad7a87d651eb34516d_2_690x356.png" alt="TupleError" data-base62-sha1="gYmsrsQB41tmKh1zLWNlk1kODbn" width="690" height="356" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/6/76f5903da35a20762ac8adad7a87d651eb34516d_2_690x356.png, https://discuss.pytorch.org/uploads/default/optimized/3X/7/6/76f5903da35a20762ac8adad7a87d651eb34516d_2_1035x534.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/7/6/76f5903da35a20762ac8adad7a87d651eb34516d_2_1380x712.png 2x" data-dominant-color="F9F8F8"><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">TupleError</span><span class="informations">1521×785 45 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><br>
Hey <a class="mention" href="/u/linbin">@Linbin</a>, 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 …<br>
I’m attaching an image I know its a little odd to attach images written code but I want to show you the error …</p>

Linbin: <p>it’s an array in Java so just use index:</p>
<pre><code>          Tensor outputTensor = outTensors[0].toTensor()</code></pre>

fahad: <p><a class="mention" href="/u/linbin">@Linbin</a> 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 …<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/7/5711864ce45b000cfdc2947e1082b89ee03361a8.png" data-download-href="https://discuss.pytorch.org/uploads/default/5711864ce45b000cfdc2947e1082b89ee03361a8" title="error2"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/5711864ce45b000cfdc2947e1082b89ee03361a8_2_690x339.png" alt="error2" data-base62-sha1="cqf5lu5WDT844JkOy3g9RYoeRv2" width="690" height="339" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/5711864ce45b000cfdc2947e1082b89ee03361a8_2_690x339.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/5711864ce45b000cfdc2947e1082b89ee03361a8_2_1035x508.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/5711864ce45b000cfdc2947e1082b89ee03361a8_2_1380x678.png 2x" data-dominant-color="F7F7F7"><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">error2</span><span class="informations">1892×932 74.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>

Linbin: <p>You may want to print the shape of the output tensor</p>
<pre><code>     long[] shape = outputTensor.shape();</code></pre>