Data transform for single channel images

username: somghosh
cooked: <p>When using RGB images i wrote the transform like</p>
<p>transform_list = [transforms.ToTensor(),<br>
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]</p>
<p>And it worked perfectly.</p>
<p>But for grayscale images when I writing it like</p>
<p>transform_list = [transforms.ToTensor(),<br>
transforms.Normalize(0.5, 0.5)]<br>
I am getting an error:</p>
<p>File “train_2.py”, line 171, in <br>
train(epoch)<br>
File “train_2.py”, line 90, in train<br>
for iteration, batch in enumerate(training_data_loader, 1):<br>
File “/home/iiitd/.local/lib/python2.7/site-packages/torch/utils/data/dataloader.py”, line 201, in <strong>next</strong><br>
return self._process_next_batch(batch)<br>
File “/home/iiitd/.local/lib/python2.7/site-packages/torch/utils/data/dataloader.py”, line 221, in _process_next_batch<br>
raise batch.exc_type(batch.exc_msg)<br>
TypeError: Traceback (most recent call last):<br>
File “/home/iiitd/.local/lib/python2.7/site-packages/torch/utils/data/dataloader.py”, line 40, in _worker_loop<br>
samples = collate_fn([dataset[i] for i in batch_indices])<br>
File “/home/iiitd/soumyadeep/pix2pix-pytorch_2/dataset.py”, line 29, in <strong>getitem</strong><br>
input = self.transform(input)<br>
File “/usr/local/lib/python2.7/dist-packages/torchvision/transforms.py”, line 34, in <strong>call</strong><br>
img = t(img)<br>
File “/usr/local/lib/python2.7/dist-packages/torchvision/transforms.py”, line 155, in <strong>call</strong><br>
for t, m, s in zip(tensor, self.mean, self.std):<br>
TypeError: zip argument <span class="hashtag">#2</span> must support iteration</p>
<p>How should I write the transform for grayscale images ??</p>

username: richard
cooked: <p>Could you post a minimal example (or just an example) that I can run to see how the error happens?</p>

username: qi_chang
cooked: <p>This should be correct:</p>
<blockquote>
<p>transform_list = [transforms.ToTensor(),<br>
transforms.Normalize([0.5], [0.5])]</p>
</blockquote>

username: somghosh
cooked: <p>Hi <a class="mention" href="/u/richard">@richard</a>,</p>
<p>This is my code, where ‘a’ and ‘b’ are two folders containing grayscale images. For RGB images i have commented out the line for the transform.</p>
<pre><code class="lang-auto"> class DatasetFromFolder(data.Dataset):
    def __init__(self, image_dir):
        super(DatasetFromFolder, self).__init__()
        self.photo_path = join(image_dir, "a")
        self.sketch_path = join(image_dir, "b")
        self.image_filenames = [x for x in listdir(self.photo_path) if is_image_file(x)]

        transform_list = [transforms.ToTensor(),
                          transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]   
	#the above line was there for RGB #images


   
        self.transform = transforms.Compose(transform_list)

    def __getitem__(self, index):
        # Load Image
        input = load_img(join(self.photo_path, self.image_filenames[index]))
        input = self.transform(input)
        target = load_img(join(self.sketch_path, self.image_filenames[index]))
        target = self.transform(target)

        return input, target
</code></pre>

username: somghosh
cooked: <p>Hi <a class="mention" href="/u/qi_chang">@qi_chang</a></p>
<p>I tried this but its giving me the following error:</p>
<p>RuntimeError: CUDNN_STATUS_INTERNAL_ERROR</p>

username: smth
cooked: <p>it’s possible that torchvision’s normalize doesn’t support 1-channel images.<br>
Open an issue here <a href="https://github.com/pytorch/vision">https://github.com/pytorch/vision</a></p>

username: gt_tugsuu
cooked: <p>I had same issue, then adding brackets to the <code>mean</code> and <code>std</code> value inside <code>transform.Normalize</code> fixed this.</p>
<p>As <a class="mention" href="/u/qi_chang">@qi_chang</a> replied this should work:</p>
<aside class="quote no-group" data-username="qi_chang" data-post="3" data-topic="8429">
<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/qi_chang/48/11739_2.png" class="avatar"> qi_chang:</div>
<blockquote>
<p>transform_list = [transforms.ToTensor(),<br>
transforms.Normalize([0.5], [0.5])]</p>
</blockquote>
</aside>

username: ranklord
cooked: <p>Add to your transforms list this transformation:</p>
<p><code>transforms.Grayscale(num_output_channels=1)</code></p>

username: mayurat22
cooked: <p>I am loading gray scale image using ImageFolder and using transform.Normalize((0.5, ),(0.5, )). But i am still receiving the images as 3 channel images.</p>