How to use module.foward(Ivalue).toTensor()

gohna: <p>My code:</p>
<pre><code class="lang-auto">final Tensor img1 = TensorImageUtils.bitmapToFloat32Tensor(bitmap1,
                    TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_STD_RGB);
            final Tensor img2 = TensorImageUtils.bitmapToFloat32Tensor(bitmap2,
                    TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_STD_RGB);

            IValue img = IValue.listFrom(img1, img2);
            final Tensor outputTensor = module.forward(img).toTensor();
</code></pre>
<p>error:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/7/37c6e85a66e965b67664a30d282ceb8888a709fe.png" data-download-href="https://discuss.pytorch.org/uploads/default/37c6e85a66e965b67664a30d282ceb8888a709fe" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/7/37c6e85a66e965b67664a30d282ceb8888a709fe_2_690x197.png" alt="image" data-base62-sha1="7XqpWJxgTlyrm3E6SaBLgJvkFBQ" width="690" height="197" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/7/37c6e85a66e965b67664a30d282ceb8888a709fe_2_690x197.png, https://discuss.pytorch.org/uploads/default/original/3X/3/7/37c6e85a66e965b67664a30d282ceb8888a709fe.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/3/7/37c6e85a66e965b67664a30d282ceb8888a709fe.png 2x" data-dominant-color="EEEFF7"><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">878×251 39.3 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>In fact, there are two pictures in my inputs. At first, I used<code> torch.cat</code> to splice on dim = 0 in pytorch. Later, I found that cat operation is not supported in Android. Instead, I changed the model to receive a list [tensor], read list [0] and list [1] respectively in the model, and then splice in the model. I noticed that the input type of forward can be an Ivalue, and the listfrom of Ivalue seems to support the conversion of list [tensor] to Ivalue, so I don’t understand what the problem is.My model input is t<strong>wo pictures</strong> . How should I implement this in Android, or how can I implement the <code>torch.cat</code> operation in Android？</p>

Linbin: <p>from the error message your model expects a single Tensor as input, instead of a list of tensors. Try</p>
<pre><code>module.forward(img1)</code></pre>

gohna: <p>In fact, there are two pictures in my inputs. At first, I used<code> torch.cat</code> to splice on dim = 0 in pytorch. Later, I found that cat operation is not supported in Android. Instead, I changed the model to receive a list [tensor], read list [0] and list [1] respectively in the model, and then splice in the model. I noticed that the input type of forward can be an Ivalue, and the listfrom of Ivalue seems to support the conversion of list [tensor] to Ivalue, so I don’t understand what the problem is.My model input is t<strong>wo pictures</strong>. How should I implement this in Android, or how can I implement the <code>torch.cat</code> operation in Android</p>

Linbin: <p>What is the output of your model? Maybe the type error is caused by output. Try</p>
<pre><code class="lang-auto">auto output = module.forward(img);
IValue[] outputTuple = output.toTuple(); 
Tensor outputTensor = outputTuple[0].toTensor();
...
</code></pre>
<p>Break the forward statement into two lines for easier debugging.</p>

gohna: <p>I have solved this problem. Thank you.</p>
<p>Do you know some methods similar to <code>torch. Matmul</code> in Android?</p>

Linbin: <p>I don’t think there’s Java API for that. But you can use C++ frontend: <a href="https://pytorch.org/cppdocs/api/namespace_torch__linalg.html#functions" class="inline-onebox" rel="noopener nofollow ugc">Namespace torch::linalg — PyTorch master documentation</a></p>

Virgilio_Magalhaes: <p><a class="mention" href="/u/gohna">@gohna</a> i have the same problem, my model accept 2 images, could you help me?</p>

Linbin: <p>This is how to use a model taking a list of images</p>
<aside class="quote no-group" data-username="gohna" data-post="1" data-topic="135048">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gohna/48/43300_2.png" class="avatar"> gohna:</div>
<blockquote>
<pre><code class="lang-auto">            IValue img = IValue.listFrom(img1, img2);
            final Tensor outputTensor = module.forward(img).toTensor();
</code></pre>
</blockquote>
</aside>

1702609: <p>I know the solution.</p>
<p>You do:</p>
<p>float<span class="chcklst-box fa fa-square-o fa-fw"></span> img1_float = img1.getDataAsFloatArray();<br>
float<span class="chcklst-box fa fa-square-o fa-fw"></span> img2_float = img2.getDataAsFloatArray();</p>
<p>int totalLength = img1_float.length + img2_float.length;<br>
float<span class="chcklst-box fa fa-square-o fa-fw"></span> inputData = new float[totalLength];</p>
<p>System.arraycopy(img1_float, 0, inputData , 0, img1_float.length);<br>
System.arraycopy(img2_float, 0, inputData , img1_float.length, img2_float.length);<br>
FloatBuffer inTensorBuffer = Tensor.allocateFloatBuffer(CHANNEL * HEIGHT* WIDTH);<br>
for (float val : inputData )<br>
inTensorBuffer.put(val);<br>
Tensor inTensor = Tensor.fromBlob(inTensorBuffer, new long<span class="chcklst-box fa fa-square-o fa-fw"></span>{BATCH ,CHANNEL, HEIGHT, WIDTH});<br>
Tensor outputTensor = mModule.forward(IValue.from(inTensor)).toTensor();</p>