728x90
반응형
https://engineer-mole.tistory.com/12
-My Code(Calc mean, std for Normalize)-
import torch
import numpy as np
import os
import os.path as osp
from mmcv import Config
from glob import glob
from PIL import Image
torch.manual_seed(123456)
torch.cuda.manual_seed(123456)
np.random.seed(123456)
random.seed(123456)
def get_img(img_path, read_type='pil'):
try:
if read_type == 'cv2':
img = cv2.imread(img_path)
img = img[:, :, [2, 1, 0]]
elif read_type == 'pil':
img = np.array(Image.open(img_path))
except Exception as e:
print(img_path)
raise
return img
def get_dir(path):
tt_train_data_dir = []
tt_train_gt_dir = []
dir_names = os.listdir(path)
for dir_name in dir_names:
tt_train_data_dir.append(path+dir_name+'/Train_images/')
tt_train_gt_dir.append(path+dir_name+ '/Train_gts/')
return tt_train_data_dir, tt_train_gt_dir
if __name__ == '__main__':
tt_root_dir = './data'
tt_train_data_dir, tt_train_gt_dir = get_dir(tt_root_dir)
avg_mean = []
avg_std = []
for i in range(len(tt_train_data_dir)):
print('[%d / %s]'%(i, tt_train_data_dir[i]))
img_names = [img_name for img_name in mmcv.utils.scandir(tt_train_data_dir[i], '.jpg')]
img_names.extend([img_name for img_name in mmcv.utils.scandir(tt_train_data_dir[i], '.png')])
for idx, img_name in enumerate(img_names):
print('[%d/%d]'%(idx,len(img_names)))
img_path = tt_train_data_dir[i] + img_name
img = get_img(img_path)
img = Image.fromarray(img)
img = img.convert('L')
img = transforms.ToTensor()(img)
np_img=np.asarray(img)
avg_mean.append(np.mean(np_img))
avg_std.append(np.std(np_img))
print('whole data: %d'%len(avg_mean))
avg_mean = np.array(avg_mean)
avg_std = np.array(avg_std)
print('Average mean : %f'%np.mean(avg_mean))
print('Average std : %f'%np.mean(avg_std))
# whole data: 18399
# Average mean : 0.558607
# Average std : 0.203590
# Calc Hist
# input_img = img.numpy().astype(np.float32)
# plt.hist(input_img[0].flatten(), bins=256, range=(0.0, 1.0), fc='k', ec='k')
# image_name = img_path.split('/')[-1]
# histo_path = "./histogram/out_his_%s.png"%(image_name)
# plt.savefig(histo_path)
728x90
반응형
'머신러닝 > Computer Vision' 카테고리의 다른 글
[PIL] 함수 모음 - 지속적으로 작성 진행 중 (0) | 2022.01.21 |
---|---|
[PIL] Crop / Clip image by Polygon points. / PIL로 이미지 자르기 (0) | 2021.12.21 |
Tensor to image / numpy array to image / PIL image save (Save feature as image) (0) | 2021.10.29 |
[Computer Vision 기초] 3. 현대 신경망(Convolution, Pooling) (0) | 2021.07.18 |
[Computer Vision 기초] 2. 텐서플로 기초와 모델 훈련 (0) | 2021.07.14 |