计算pytorch标准化(Normalize)所需要数据集的均值和方差实例

作者:菜鸟川 时间:2022-08-24 22:34:00 

pytorch做标准化利用transforms.Normalize(mean_vals, std_vals),其中常用数据集的均值方差有:


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]

计算自己数据集图像像素的均值方差:


import numpy as np
import cv2
import random

# calculate means and std
train_txt_path = './train_val_list.txt'

CNum = 10000   # 挑选多少图片进行计算

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 , 随机挑选图片

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不用转
means.reverse() # BGR --> RGB
stdevs.reverse()

print("normMean = {}".format(means))
print("normStd = {}".format(stdevs))
print('transforms.Normalize(normMean = {}, normStd = {})'.format(means, stdevs))

来源:https://blog.csdn.net/weixin_38533896/article/details/85951903

标签:pytorch,标准化,数据集,均值,方差
0
投稿

猜你喜欢

  • Pycharm运行加载文本出现错误的解决方法

    2021-02-01 09:07:18
  • php析构函数的具体用法小结

    2024-04-23 09:20:31
  • asp如何用JMail同时给多人发信?

    2010-06-12 12:52:00
  • PyQt5-QDateEdit的简单使用操作

    2022-09-16 17:35:51
  • Pytorch 使用不同版本的cuda的方法步骤

    2023-02-06 07:52:23
  • JavaScript 扩展运算符用法实例小结【基于ES6】

    2024-04-22 13:06:03
  • mysql 判断是否为子集的方法步骤

    2024-01-26 03:53:11
  • Pytorch之finetune使用详解

    2021-08-31 20:41:44
  • PHP清除缓存的几种方法总结

    2024-06-05 15:32:34
  • Python3使用TCP编写一个简易的文件下载器功能

    2021-02-20 09:58:07
  • 一个简单的SQL 行列转换语句

    2024-01-24 01:57:08
  • 解决mysql 1040错误Too many connections的方法

    2024-01-24 06:56:58
  • Python 页面解析Beautiful Soup库的使用方法

    2022-02-26 07:57:15
  • ChatGpt无法访问或错误码1020的几种解决方案

    2023-03-03 05:58:36
  • Python开发之pip安装及使用方法详解

    2022-11-27 06:22:48
  • Django model反向关联名称的方法

    2021-03-06 17:21:28
  • Python10行代码实现模拟百度搜索的示例

    2022-07-19 17:10:57
  • python urllib库的使用详解

    2021-06-12 14:42:04
  • 在Python程序中操作文件之flush()方法的使用教程

    2023-12-02 16:41:31
  • Django查找网站项目根目录和对正则表达式的支持

    2023-04-09 15:50:37
  • asp之家 网络编程 m.aspxhome.com