Problem converting MNIST images to RGB

Vendrick17: <p>Hello, I am trying to convert the images form MNIST to RGB using torch.transforms, my code is as follows:</p>
<pre><code class="lang-auto">IMG_SIZE = 32 
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'

BATCH_SIZE = 256

TRANSFORM = transforms.Compose([
    #transforms.Lambda(lambda x: x.convert('RGB')),
    transforms.Resize(IMG_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))
])

mnist_train = datasets.MNIST(root='./data', train=True, download=True, transform=TRANSFORM)
usps_train = datasets.USPS(root='./data', train=True, download=True, transform=TRANSFORM)

mnist_loader = torch.utils.data.DataLoader(mnist_train, batch_size=BATCH_SIZE, shuffle=True)
usps_loader = torch.utils.data.DataLoader(usps_train, batch_size=BATCH_SIZE, shuffle=True)

mnist_test = datasets.MNIST(root='./data', train=False, download=True, transform=TRANSFORM)
usps_test = datasets.USPS(root='./data', train=False, download=True, transform=TRANSFORM)

mnist_test_loader = torch.utils.data.DataLoader(mnist_test, batch_size=BATCH_SIZE)
usps_test_loader = torch.utils.data.DataLoader(usps_test, batch_size=BATCH_SIZE)

X_test_fixed = next(iter(mnist_test_loader))[0]
Y_test_fixed = next(iter(usps_test_loader))[0]
</code></pre>
<p>The code works fine and I get RGB images, but when I try to visualize them I get these weird results.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/b/5/b542f4e757f410142bf820e7b5dd60692e690152.png" alt="image" data-base62-sha1="pRvSjOEK3kDloJjTgeuV9Bd7UMq" width="321" height="321"><br>
To visualize the image I used</p>
<pre><code class="lang-auto">plt.imshow(X_test_fixed[1].reshape(32,32,3))
plt.show()
</code></pre>
<p>I would appreciate any help, I even tried to use the convert function from PIL and the result is the same.</p>

eqy: <p>I believe this might be because you are resizing after converting to RGB, which could introduce artifacts due to the interpolation method. Could you try converting to RGB <strong>after</strong> resizing and see if that improves the results?</p>

Vendrick17: <p>That is what I did, the transforms.Lambda function is the one that make the conversion to RGB, if you mean using the .convert(‘RGB’), I also changed the position as you said and I got the same result.</p>

eqy: <p>Sorry, I didn’t see the use of repeat to implement the RGB transform. The reason you are seeing the strange output is because you are visualizing the data after normalization (which by the way seems to be using strange values as MNIST mean and standard deviation are both &lt; 0.5 <a href="https://datascience.stackexchange.com/questions/46228/how-mean-and-deviation-come-out-with-mnist-datase" class="inline-onebox" rel="noopener nofollow ugc">neural network - How mean and deviation come out with MNIST dataset? - Data Science Stack Exchange</a>). This mean subtraction will cause shifts in how the colors are displayed because <code>pyplot.imshow()</code> will clamp the values to <code>[0,1]</code> (you will see a warning about this).</p>
<p>If you remove the normalization the image looks as expected. In other words normalization is fine as a preprocessing step, but I would be cautious in applying it before trying to display the image.</p>

Vendrick17: <p>Thanks for your response, I also thought the same, but no, the problem is still there if I remove the normalization.</p>

Vendrick17: <p>Well, just in case, I could solve the problem and it was not about the transformers but the reshape I did to the image, the proper way to do it is by using np.transpose() and switching the indices.</p>