Pytorch: Normalize Image data set
I want to normalize custom dataset of images. For that i need to compute mean and standard deviation by iterating over the dataset. How can I normalize my entire dataset before creating the data set?

Well, let's take this image as an example:



The first thing you need to do is decide which library you want to use: Pillow or OpenCV. In this example I'll use Pillow:

from PIL import Image
import numpy as np

img = Image.open("test.jpg")
pix = np.asarray(img.convert("RGB")) # Open the image as RGB

Rchan = pix[:,:,0]  # Red color channel
Gchan = pix[:,:,1]  # Green color channel
Bchan = pix[:,:,2]  # Blue color channel

Rchan_mean = Rchan.mean()
Gchan_mean = Gchan.mean()
Bchan_mean = Bchan.mean()

Rchan_var = Rchan.var()
Gchan_var = Gchan.var()
Bchan_var = Bchan.var()


And the results are:


Red Channel Mean: 134.80585625 
Red Channel Variance: 3211.35843945
Green Channel Mean: 81.0884125
Green Channel Variance: 1672.63200823
Blue Channel Mean: 68.1831375
Blue Channel Variance: 1166.20433566


Hope it helps for your needs.
