How to calculate the mean and Variance of data sets required by pytorch standardized Normalize
This article will explain in detail how to calculate the mean and variance of data sets needed for pytorch standardized Normalize. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Pytorch uses transforms.Normalize (mean_vals, std_vals) for standardization, in which the means and variances of commonly used data sets are:
If 'coco' in args.dataset: mean_vals = [0.471, 0.448, 0.408] std_vals = [0.234, 0.239, 0.242] elif' imagenet' in args.dataset: mean_vals = [0.485, 0.456, 0.406] std_vals = [0.229, 0.224,0.225]
Calculate the mean and variance of image pixels in your own dataset:
Import numpy as npimport cv2import random # calculate means and stdtrain_txt_path ='. / train_val_list.txt' CNum = 10000 # number of pictures selected to calculate img_h, img_w = 32, 32imgs = np.zeros ([img_w, img_h, 3,1]) means, stdevs = [], [] with open (train_txt_path,'r') as f: lines = f.readlines () random.shuffle (lines) # shuffle For i in tqdm_notebook (range (CNum)): img_path = os.path.join ('. / train', lines [I] .rstrip (). Split () [0]) img = cv2.imread (img_path) img = cv2.resize (img, (img_h, img_w)) img = img [:, np.newaxis] imgs = np.concatenate ((imgs, img)) Axis=3) # print (I) imgs = imgs.astype (np.float32) / 255. For i in tqdm_notebook (range (3)): pixels = imgs [:,:, iMagne:] .ravel () # pulled into a line means.append (np.mean (pixels)) stdevs.append (np.std (pixels)) # the image format read by cv2 is BGR All PIL/Skimage reads is that RGB does not need to transfer means.reverse () # BGR-> RGBstdevs.reverse () print ("normMean = {}" .format (means)) print ("normStd = {}" .format (stdevs)) print ('transforms.Normalize (normMean = {}, normStd = {})' .format (means, stdevs)) about "how to calculate the mean and variance of datasets required for pytorch standardization Normalize". Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.