計(jì)算pytorch標(biāo)準(zhǔn)化(Normalize)所需要數(shù)據(jù)集的均值和方差實(shí)例
pytorch做標(biāo)準(zhǔn)化利用transforms.Normalize(mean_vals, std_vals),其中常用數(shù)據(jù)集的均值方差有:
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]
計(jì)算自己數(shù)據(jù)集圖像像素的均值方差:
import numpy as np
import cv2
import random
# calculate means and std
train_txt_path = './train_val_list.txt'
CNum = 10000 # 挑選多少圖片進(jìn)行計(jì)算
img_h, img_w = 32, 32
imgs = 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 , 隨機(jī)挑選圖片
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[:,:,i,:].ravel() # 拉成一行
means.append(np.mean(pixels))
stdevs.append(np.std(pixels))
# cv2 讀取的圖像格式為BGR,PIL/Skimage讀取到的都是RGB不用轉(zhuǎn)
means.reverse() # BGR --> RGB
stdevs.reverse()
print("normMean = {}".format(means))
print("normStd = {}".format(stdevs))
print('transforms.Normalize(normMean = {}, normStd = {})'.format(means, stdevs))
以上這篇計(jì)算pytorch標(biāo)準(zhǔn)化(Normalize)所需要數(shù)據(jù)集的均值和方差實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何解決cmd運(yùn)行python提示不是內(nèi)部命令
在本篇文章里小編給大家整理了關(guān)于如何解決cmd運(yùn)行python提示不是內(nèi)部命令的相關(guān)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2020-07-07
解決python的空格和tab混淆而報(bào)錯(cuò)的問(wèn)題
這篇文章主要介紹了解決python的空格和tab混淆而報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
Python實(shí)現(xiàn)Linux命令xxd -i功能
這篇文章主要介紹了Python實(shí)現(xiàn)Linux命令xxd -i功能的相關(guān)資料,需要的朋友可以參考下2016-03-03
Pytest使用fixture實(shí)現(xiàn)token共享的方法
同學(xué)們?cè)谧鰌ytest接口自動(dòng)化時(shí),會(huì)遇到一個(gè)場(chǎng)景就是不同的測(cè)試用例需要有一個(gè)登錄的前置步驟,登錄完成后會(huì)獲取到token,用于之后的代碼中,本文給大家介紹Pytest使用fixture實(shí)現(xiàn)token共享的方法,感興趣的朋友一起看看吧2023-11-11
用Python進(jìn)行數(shù)據(jù)清洗以及值處理
這篇文章主要介紹了用Python進(jìn)行數(shù)據(jù)清洗以及值處理,數(shù)據(jù)分析中,數(shù)據(jù)清洗是一個(gè)必備階段。數(shù)據(jù)分析所使用的數(shù)據(jù)一般都很龐大,致使數(shù)據(jù)不可避免的出現(xiàn)重復(fù)、缺失、異常值等異常數(shù)據(jù),如果忽視這些異常數(shù)據(jù),可能導(dǎo)致分析結(jié)果的準(zhǔn)確性,需要的朋友可以參考下2023-07-07

