pythonpytorchgenerative-adversarial-network

GAN Model Code Modification — 3 Channels to 1 Channel


This model is designed for processing 3-channel images (RGB) while I need to handle some black and white image data (grayscale), so I’d like to change the “ch” parameter to “1” instead of “3.”

The full code is available here — https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html

If we just change this parameter — “nc = 3” --> “nc = 1” — without adjusting generator’s and discriminator’s code blocks, executing just gives an error message:

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

Is there a guide on how to modify this or, perhaps, calculate these values manually using this formula (shape section)?

Please advise.


Solution

  • A grayscale image is a "special case" of a color image: a pixel has a gray color iff the red channel equals the green equals the blue. Thus a pixel with values [200, 10, 30] will be green-ish in color, while a pixel with values [180, 180, 180] will have a gray color.
    Therefore, the simplest way to process gray scale images using a pre-trained RGB model is to duplicate the single channel of the grayscale image 3 times to generate RGB-like image with three channels that has gray colors.