資源簡介
在利用深度學(xué)習(xí)開源代碼測試自己數(shù)據(jù)時(shí),會(huì)遇到對自己構(gòu)建數(shù)據(jù)集進(jìn)行均值和方差計(jì)算的問題。本資源有兩個(gè)python腳本,在python3下編寫,一個(gè)是直接求取原始數(shù)據(jù)集均值,一個(gè)是對拉成張量后的數(shù)據(jù)集進(jìn)行均值和方差求取處理,用于transforms.Normalize()函數(shù)設(shè)置。

代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
“““
Created?on?Mon?Apr?22?22:02:51?2019
@author:?MSIK
“““
import?os
import?numpy?as?np
import?cv2
from?tqdm?import?tqdm
?
#?calculate?means?and?std
filepath?=?‘D:/SemanticSegmentation/pytorch-semseg/datasets/VOC/VOC2019/JPEGImages‘
pathDir?=?os.listdir(filepath)
CNum?=?1000?????#?挑選多少圖片進(jìn)行計(jì)算
#random.randint(lowhighsize)
filenameArray?=?np.random.randint(1len(pathDir)CNum)
?
img_h?img_w?=?256?256
imgs?=?np.zeros([img_w?img_h?3?1])
means?stdevs?=?[]?[]
?
#for?idx?in?tqdm(range(len(pathDir))):
for?idx?in?tqdm(range(CNum)):
????filename?=?pathDir[idx]
????img_path?=?os.path.join(filepath?filename)
?
????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(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))
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????1349??2019-04-24?20:39??main_GetMeanandStd.py
?????文件?????????835??2019-04-22?00:07??main_GetMeanValue.py
- 上一篇:NAO機(jī)器人抓紅球
- 下一篇:face_detect1.0.py
評論
共有 條評論