1 channel input on Pytorch Mobile - Android

username: cansozbir
cooked: <p>Hi, I’m trying to do a mnist classifier with pytorch-mobile.<br>
I’ve export my model with jit in python.</p>
<pre><code class="lang-auto">example = torch.rand(1, 1, 28, 28)
trace = torch.jit.trace(net, example)
trace.save("mymodel.pt")
</code></pre>
<p>And this is how I use it in my kotlin project.</p>
<pre><code class="lang-auto">        fun assetFilePath(context: Context, assetName: String): String {
            val file = File(context.filesDir, assetName)
            if (file.exists() &amp;&amp; file.length() &gt; 0) {
                return file.absolutePath
            }
            context.assets.open(assetName).use { inputStream -&gt;
                FileOutputStream(file).use { outputStream -&gt;
                    val buffer = ByteArray(4 * 1024)
                    var read: Int
                    while (inputStream.read(buffer).also { read = it } != -1) {
                        outputStream.write(buffer, 0, read)
                    }
                    outputStream.flush()
                }
                return file.absolutePath
            }
        }

        val module:Module = Module.load(assetFilePath(this, "mymodel.pt"))

        val resizedBitmap:Bitmap = Bitmap.createScaledBitmap(
            MyCanvasView.extraBitmap,
            28,
            28,
            true
        )

        val inputTensor: Tensor = TensorImageUtils.bitmapToFloat32Tensor(
            resizedBitmap,
            TensorImageUtils.TORCHVISION_NORM_MEAN_RGB,
            TensorImageUtils.TORCHVISION_NORM_STD_RGB
        )

        val outputTensor = module.forward(IValue.from(inputTensor)).toTensor()
</code></pre>
<p>the problem is inputTensor’s shape is [1, 3, 28, 28].  How can I create a single channel tensor from a bitmap?</p>
<pre><code class="lang-auto">java.lang.RuntimeException: Given groups=1, weight of size 8 1 3 3, expected input[1, 3, 28, 28] to have 1 channels, but got 3 channels instead
    The above operation failed in interpreter.
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/b/3b180dbc4992c1bacfaf971fb98b82008f08932b.png" data-download-href="https://discuss.pytorch.org/uploads/default/3b180dbc4992c1bacfaf971fb98b82008f08932b" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3b180dbc4992c1bacfaf971fb98b82008f08932b_2_257x500.png" alt="image" data-base62-sha1="8qLHJQ00lJIjytV4wc1b9vIle8b" width="257" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3b180dbc4992c1bacfaf971fb98b82008f08932b_2_257x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3b180dbc4992c1bacfaf971fb98b82008f08932b_2_385x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/3/b/3b180dbc4992c1bacfaf971fb98b82008f08932b.png 2x" data-dominant-color="27292F"><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">423×820 14.1 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>

username: NAVEEN_PALURU
cooked: <p>Hi, I am also facing a similar issue of using 1 channel images in the PyTorch Android. Did you find any way out?</p>

username: NAVEEN_PALURU
cooked: <p>Hi, you can try this way.</p>
<p>Make sure that, x = torch.mean(x,dim=1,keepdim=True), is the first statement in the forward method of pytorch model.</p>
<p>Then,</p>
<p>example = torch.rand(1, 3, 28, 28)<br>
trace = torch.jit.trace(net, example)<br>
trace.save(“mymodel.pt”)</p>