[Novice] images and labels are different channels

username: chanakin
cooked: <p>Dear all,</p>
<p>This is first my end2end deep learning project and I have trying to train a U-Net with images with 3 channels and labels with 1 channel respectively. However, since I need to specify the in_channels as part of my training loop in the network, I am not sure where and how the network can take in 1 channel input label.</p>
<p>Here is the training_loop and Dataloader for reference:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/3/e34c20138fab17ec1b59dc4d154babe84947b5f9.png" data-download-href="https://discuss.pytorch.org/uploads/default/e34c20138fab17ec1b59dc4d154babe84947b5f9" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/3/e34c20138fab17ec1b59dc4d154babe84947b5f9_2_373x500.png" alt="image" data-base62-sha1="wqLtYxCgjFtn332XIaJJyeMQDnH" width="373" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/3/e34c20138fab17ec1b59dc4d154babe84947b5f9_2_373x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/3/e34c20138fab17ec1b59dc4d154babe84947b5f9_2_559x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/3/e34c20138fab17ec1b59dc4d154babe84947b5f9.png 2x" data-dominant-color="272722"><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">623×834 82 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: chanakin
cooked: <p><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/9/b9aa4276207d778f7be3260a816242b84c5635bb.png" alt="image" data-base62-sha1="qut6UTDFpDepa2cqCdrfD3Bd9s7" width="643" height="234"></p>

username: chanakin
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/7/a766c0dba2654b79e3a78a45fc5816f361eb4965.png" data-download-href="https://discuss.pytorch.org/uploads/default/a766c0dba2654b79e3a78a45fc5816f361eb4965" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/7/a766c0dba2654b79e3a78a45fc5816f361eb4965_2_690x150.png" alt="image" data-base62-sha1="nSTSUgsoXxggRLcb96OKQCVxJRj" width="690" height="150" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/7/a766c0dba2654b79e3a78a45fc5816f361eb4965_2_690x150.png, https://discuss.pytorch.org/uploads/default/original/3X/a/7/a766c0dba2654b79e3a78a45fc5816f361eb4965.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/a/7/a766c0dba2654b79e3a78a45fc5816f361eb4965.png 2x" data-dominant-color="27271F"><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">789×172 23.6 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: chanakin
cooked: <p>And lastly, the subsequent Dataset module, sorry for spamming, many thanks in advance for your help <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/5/b51a676891e1a0e07adbdc6ef4321448178828e5.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/b51a676891e1a0e07adbdc6ef4321448178828e5" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/5/b51a676891e1a0e07adbdc6ef4321448178828e5_2_314x500.jpeg" alt="image" data-base62-sha1="pQ6Zww8fN72GPWa5jMLmhrZy5xP" width="314" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/5/b51a676891e1a0e07adbdc6ef4321448178828e5_2_314x500.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/b/5/b51a676891e1a0e07adbdc6ef4321448178828e5.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/5/b51a676891e1a0e07adbdc6ef4321448178828e5.jpeg 2x" data-dominant-color="504E4A"><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">430×684 122 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: chanakin
cooked: <p>And this is the runtime error, after it was successfully loaded the 3 channels images, but the loop then encounter the 1 channel label:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/3/2319ffcde69a42ef8e282f1f454029711a41be9e.png" data-download-href="https://discuss.pytorch.org/uploads/default/2319ffcde69a42ef8e282f1f454029711a41be9e" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/3/2319ffcde69a42ef8e282f1f454029711a41be9e_2_690x48.png" alt="image" data-base62-sha1="50wpHBS26IVeDQemiYtxVnxks3A" width="690" height="48" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/3/2319ffcde69a42ef8e282f1f454029711a41be9e_2_690x48.png, https://discuss.pytorch.org/uploads/default/original/3X/2/3/2319ffcde69a42ef8e282f1f454029711a41be9e.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/2/3/2319ffcde69a42ef8e282f1f454029711a41be9e.png 2x" data-dominant-color="30302B"><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">929×65 9.61 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: ptrblck
cooked: <aside class="quote no-group" data-username="chanakin" data-post="5" data-topic="144658">
<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/chanakin/48/47257_2.png" class="avatar"> chanakin:</div>
<blockquote>
<p>after it was successfully loaded the 3 channels images</p>
</blockquote>
</aside>
<p>Based on the error message that’s not the case as at least the current input image has a single channel only.<br>
Iterate the dataset once and check the shape of all input tensors.<br>
Once you’ve found the samples with a single channel, add a transformation to the <code>__getitem__</code> and e.g. repeat the channel dimension to create 3 channels.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>