Increasing the number of channels with transform

username: learningpytorch
cooked: <p>I am using the EMNIST dataset which is in grayscale. I would like to include a line using transforms to get 3 channels.</p>
<pre><code class="lang-auto">transform=transforms.Compose([
                               transforms.Resize(image_size),
                               transforms.CenterCrop(image_size),
                               transforms.ToTensor(),
                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                              ])
</code></pre>
<p>When I am using the dataloader I get this error:<br>
RuntimeError: output with shape [1, 64, 64] doesn’t match the broadcast shape [3, 64, 64]</p>
<p>The code that generates this error is:</p>
<pre><code class="lang-auto">dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,
                                         shuffle=True, num_workers=1)

device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu &gt; 0) else "cpu")

# Plot some training images
real_batch = next(iter(dataloader))
print(real_batch[0].shape)
plt.figure(figsize=(8,8))
plt.axis("off")
plt.title("Training Images")
plt.imshow(np.transpose(vutils.make_grid(real_batch[0].to(device)[:64], padding=2, normalize=True).cpu(),(1,2,0)))
</code></pre>
<p>Thanks in advance!</p>

username: Dwight_Foster
cooked: <p>You cannot really autocolorize an image. Is there a way for you to change your first cnn to only have 1 input channel? If not you could look into making a custom transform and just copy the image and stack them. <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html" rel="noopener nofollow ugc">Here</a> is a tutorial where it shows what a custom transform looks like.</p>

username: learningpytorch
cooked: <p>Thanks for your message. I solved with this (see below), but the link you provided it is very useful since I have to rotate the image and do a flip. Any suggestion to avoid creating a whole custom transform?</p>
<pre><code class="lang-auto">transform=transforms.Compose([
                               transforms.Resize(image_size),
                               transforms.CenterCrop(image_size),
                               transforms.ToTensor(),
                               transforms.Lambda(lambda x: x.repeat(3,1,1)),
                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                              
                              ])
</code></pre>

username: Dwight_Foster
cooked: <p>Yes you can look at the transforms <a href="https://pytorch.org/vision/stable/transforms.html" rel="noopener nofollow ugc">here</a> but there are some that rotate and flip an image. Examples are random rotation:</p>
<pre><code class="lang-auto">torchvision.transforms.RandomRotation(degrees, interpolation=&lt;InterpolationMode.NEAREST: 'nearest'&gt;, expand=False, center=None, fill=0, resample=None)
</code></pre>
<p>and random horizontal flip:</p>
<pre><code class="lang-auto">torchvision.transforms.RandomHorizontalFlip(p=0.5)
</code></pre>

username: learningpytorch
cooked: <p>Thank you very much <a class="mention" href="/u/dwight_foster">@Dwight_Foster</a> for your help. Just one last question. Where can I set the likelihood of the rotation to 100% to perform always a rotation? It hasn’t got a p parameter like the flip.</p>

username: Dwight_Foster
cooked: <p>So basically how it works is that you give it a range of degrees and it picks a random degree in that range and rotates it to that degree. If you just want it to rotate to a specific degree each time you can just set the angle equal to that degree and it should always rotate there.</p>

username: learningpytorch
cooked: <p>It doesn’t seem to work. I already tried it without success. The original grid is shown below.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/e/cefaa0ebf0a2883da409f94948f5dfee000b4f56.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/cefaa0ebf0a2883da409f94948f5dfee000b4f56" title="A"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/e/cefaa0ebf0a2883da409f94948f5dfee000b4f56_2_470x500.jpeg" alt="A" data-base62-sha1="tx1klE4QuFkMj9h1W0renOhaZBI" width="470" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/e/cefaa0ebf0a2883da409f94948f5dfee000b4f56_2_470x500.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/c/e/cefaa0ebf0a2883da409f94948f5dfee000b4f56.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/c/e/cefaa0ebf0a2883da409f94948f5dfee000b4f56.jpeg 2x" data-dominant-color="434343"><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">A</span><span class="informations">502×533 33.8 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>
<p>I want to display it in normal handwriting position. But after rotating 90 degrees this is what I get.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/a/caddbcf2f620ee761358ab38e4d09e46907018e5.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/caddbcf2f620ee761358ab38e4d09e46907018e5" title="A2"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/a/caddbcf2f620ee761358ab38e4d09e46907018e5_2_452x500.jpeg" alt="A2" data-base62-sha1="sWDwhuRKWg8QghMrqlEhcFzg7YN" width="452" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/a/caddbcf2f620ee761358ab38e4d09e46907018e5_2_452x500.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/c/a/caddbcf2f620ee761358ab38e4d09e46907018e5.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/c/a/caddbcf2f620ee761358ab38e4d09e46907018e5.jpeg 2x" data-dominant-color="555555"><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">A2</span><span class="informations">573×633 67.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>

username: Dwight_Foster
cooked: <p>Can you send your new transforms. You might have to set the angle to a tuple like (90, 90) because if it just says 90 it will interpret that as 0 to 90.</p>

username: learningpytorch
cooked: <p>Problem solved. Thanks a million. The transform that solved the problem was this:</p>
<pre><code class="lang-auto">transform=transforms.Compose([
                               transforms.Resize(image_size),
                               transforms.CenterCrop(image_size),
                               transforms.ToTensor(),
                               transforms.Lambda(lambda x: x.repeat(3,1,1)),
                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                               transforms.RandomHorizontalFlip(p=1),
                               transforms.RandomRotation((90,90),expand=False, center=None, fill=0, resample=None),
                               
                              ])
</code></pre>