Clipping input data to the valid range for imshow with RGB data

I have calculated the mean and std for normalization using following code

mean = 0.

std = 0.

nb_samples = 0.

for data,_ in train_loader:

    batch_samples = data.size(0)

    data = data.view(batch_samples, data.size(1), -1)

    mean += data.mean(2).sum(0)

    std += data.std(2).sum(0)

    nb_samples += batch_samples

mean /= nb_samples

std /= nb_samples

I got these values

[0.5820, 0.4512, 0.4023], [0.2217, 0.1858, 0.1705]

when I put the values, it gives me this warning

Clipping input data to the valid range for imshow with RGB data ([0…1] for floats or [0…255] for integers).

But if I used the values calculated for imagenet dataset subset, it does not give warning.
So which values should I use?

Hi,

By imshow I assume you are using matplotlib.pyplot. In this case, imshow expects inputs in range of [0, 1] or [0, 255] so if the input array does not have this range, it will clip input.
Concerning the issue that ImageNet values do not create this warning, it is possible but does not matter. The point is that you are using mean and std in z-score ((x-mean) / std) so the output values can out of [-1, 1]. So, for visualization, you need first to unnormalize your input then clipping input data would be ok.

Bests