RuntimeError: output with shape doesn't match the broadcast shape

simongeek: <p>I want to train RGB images on ResNet50 using transfer learning method.<br>
I was resized images to 224x224 and I got error using GPU:</p>
<p><code>RuntimeError: output with shape [1, 224, 224] doesn't match the broadcast shape [3, 224, 224]</code></p>
<p>On CPU all works fine.</p>
<p>How to deal with it?</p>

ptrblck: <p>Could you post the line of code throwing this error?<br>
It seems strange that your code is running fine on the CPU.</p>

simongeek: <p>I get error on filepath line and image = self.transform</p>
<pre><code class="lang-auto">    def __getitem__(self, index):
        """Generate one sample of data."""
        filepath = os.path.join(self.root + "/img/" + self.filenames[index])
        bbox = self.bbox[index]
        image = cv2.imread(filepath, cv2.IMREAD_COLOR)
        image = image[bbox[1]:bbox[3], bbox[0]:bbox[2]]
        image = cv2.copyMakeBorder(image, bbox[1], bbox[3], bbox[0], bbox[2], cv2.BORDER_CONSTANT, value=[255, 255, 255])
        attribute_label = self.attributes[index]
        #category_label = self.categories[index]

        if self.transform is not None:
            image = self.transform(image)

        return image, attribute_label
</code></pre>

ptrblck: <p>This line of code should also throw this error if you run the code in the CPU, so that’s a bit strange.<br>
Anyway, how did you define <code>self.transform</code>?</p>

simongeek: <p>My transform:</p>
<pre><code class="lang-auto">transform = transforms.Compose([
    transforms.ToPILImage(),
    transforms.Resize(size=(224, 224)),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
</code></pre>

ptrblck: <p>Could you print the shape of the image before passing it to the transformation.<br>
I’m not sure, if OpenCV creates pseudo-color channels if you are loading a grayscale image, but if not that could yield this error.<br>
If that’s the case, you could extend your tensor in that dimension to create 3 identical color channels.</p>

simongeek: <p>Few examples:</p>
<p>(432, 512, 3)<br>
(346, 212, 3)<br>
(600, 596, 3)<br>
(298, 454, 3)<br>
(472, 348, 3)<br>
(516, 380, 3)<br>
(458, 362, 3)<br>
(530, 598, 3)<br>
(478, 384, 3)</p>

simongeek: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Here is all errors</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/4/46193a8e3baa880eea182e3f318f9199ffbc9dc7.png" data-download-href="https://discuss.pytorch.org/uploads/default/46193a8e3baa880eea182e3f318f9199ffbc9dc7" title="08"><img src="https://discuss.pytorch.org/uploads/default/original/2X/4/46193a8e3baa880eea182e3f318f9199ffbc9dc7.png" alt="08" data-base62-sha1="a07tsPw8NzL5Bc90bI2GqlFn0p1" width="690" height="303" data-dominant-color="313131"><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">08</span><span class="informations">701×308 14.5 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>

ptrblck: <p>This error was usually thrown, if you’ve passed a grayscale image to the normalization with three values.<br>
Could you add a check to see, if all images contain three channels?</p>

simongeek: <p>I was checked, and I don’t have images with single channels…</p>

maomao: <p>Let me clarify, if the img has three channels, you should have three number for mean, just for example, img is RGB, mean is [0.5, 0.5, 0.5], the normalize result is R * 0.5, G * 0.5, B * 0.5. If img is gray type that only one channel, so mean should be [0.5], the normalize result is R * 0.5</p>

MohanaMeher: <p>I’m working on Ubuntu 16.04 version and using torch version 1.3.1<br>
It’s that MNIST data set consists of grey images.</p>
<p><code>transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)), ])</code></p>
<p><strong>can be changed to</strong></p>
<p><code>transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])</code></p>