Output with shape [1, 28, 28] doesn't match the broadcast shape [3, 28, 28]

username: subhadeep
cooked: <p>output with shape [1, 28, 28] doesn’t match the broadcast shape [3, 28, 28], this is the error how can I resolve this??</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/6/6dadd5c6fd8bda5ab99cdc4b553051e0e3b12b5e.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/6dadd5c6fd8bda5ab99cdc4b553051e0e3b12b5e" title="Screenshot_1"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/6/6dadd5c6fd8bda5ab99cdc4b553051e0e3b12b5e_2_690x297.jpeg" alt="Screenshot_1" data-base62-sha1="fEgu1eTuj6gDoqdjSbmsO5Dmqui" width="690" height="297" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/6/6dadd5c6fd8bda5ab99cdc4b553051e0e3b12b5e_2_690x297.jpeg, https://discuss.pytorch.org/uploads/default/optimized/2X/6/6dadd5c6fd8bda5ab99cdc4b553051e0e3b12b5e_2_1035x445.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/6/6dadd5c6fd8bda5ab99cdc4b553051e0e3b12b5e.jpeg 2x" data-dominant-color="323333"><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">Screenshot_1</span><span class="informations">1365×589 97.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: ptrblck
cooked: <p>Based on the error message it looks like (some) images might be grayscale images and thus only contain one channel.<br>
If that’s the case for all images, just use a single value for the mean and std arguments:</p>
<pre><code class="lang-python">transforms.Normalize((0.5,), (0.5,))
</code></pre>
<p>Alternatively, if just a few images are gray while others are RGB images, you could convert these images after loading using:</p>
<pre><code class="lang-python">img = Image.open(path).convert('RGB')
</code></pre>

username: Geoffrey_Payne
cooked: <p>I have the same problem, and I am converting the image to RGB as you suggest. Although really I want a grayscale image.</p>
<pre><code class="lang-auto">t_ = None
    if Hyper.is_grayscale:
        t_ = transforms.Compose([
            transforms.Grayscale(num_output_channels=1),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 5])
            ])
    else:
        t_ = transforms.Compose([
            transforms.ToTensor(),
             transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
            ])
    test_img1 = Image.open("test_examples/fast_train.jpg").convert("RGB")
    test_img1 =  t_(test_img1).unsqueeze(0)
</code></pre>

username: ptrblck
cooked: <p>If you want to keep the image tensors in the grayscale format, you could use the <code>Normalize</code> transformation from my previous post with a single <code>mean</code> and <code>std</code> value and it should work.<br>
Currently you are passing 3 values for these stats, which will most likely yield a shape mismatch error.</p>

username: Geoffrey_Payne
cooked: <p>Yes, I have since noticed that for Grayscale images I need only 1 value for the mean and 1 value for the std in the normalisation transformation, so I have fixed that now</p>