1

I tried to modify this example code enter link description here of DCGAN for my own dataset. The exmple code is for RGB image, but my data is grayscale image. Therefore, I set the value of number of channels ‘nc’ as 1. However, when I tried running the program, i got thid error: RuntimeError: Given groups=1, weight of size 64 1 4 4, expected input[128, 3, 64, 64] to have 1 channels, but got 3 channels instead. I do know that its a problem about number of channels but I have no idea what else should I change to solve this error.

Any help would be appreciated!

  • Does this answer your question? [RuntimeError: Given groups=1, weight of size \[64, 3, 7, 7\], expected input\[3, 1, 224, 224\] to have 3 channels, but got 1 channels instead](https://stackoverflow.com/questions/53416833/runtimeerror-given-groups-1-weight-of-size-64-3-7-7-expected-input3-1) – Guga Nemsitsveridze May 10 '20 at 07:35

2 Answers2

0

You are passing RGB images with 3 channels. You should pass tensor of shape [batch, 1, width, height] while yours is apparently [128, 3, 64, 64] (and should be [128, 1, 64, 64]).

Verify you input image shape via print(image.shape) attribute`

If you use torchvision (with pillow under the hood), you can use torchvision.transforms.Grayscale() (see docs). Also verify your images are indeed grayscale.

Szymon Maszke
  • 22,747
  • 4
  • 43
  • 83
0

What's the actual shape of your input? My guess is that you're loading images as standard RGBs, which will load them as 3 channel images even though they're really greyscale.

Karl
  • 961
  • 6
  • 10