当前位置:网站首页>计算图像数据集均值和方差
计算图像数据集均值和方差
2022-07-31 05:16:00 【王大队长】
相信我们都见到过这样一段代码:

这里的transforms.Normalize函数里的mean和std的值就是我们的ImageNet的均值和方差。但是我也见到过不采用这些值得均值与方差,于是我思考怎么计算自己数据集图片的均值与方差。下面将会给出计算过程的代码,代码实现部分出自这本书:

这里我以一个小的奥特曼数据集为例计算其均值方差:

代码实现:
import os
import torch
import imageio
import numpy as np
batch_size = 138 #你数据集里的图片个数
h,w = 256, 256 #因为我们的transforms里normalize前往往跟着一个裁减,所以我们这里也要先裁减再计算
batch = torch.zeros(batch_size, 3, h, w, dtype=torch.uint8)
data_dir = r'D:\Dataset\AoteMan\train\奥特曼'
print('图片个数:',len(os.listdir(data_dir)))
filenames = [name for name in os.listdir(data_dir)]
for i, filename in enumerate(filenames):
img_arr = imageio.imread(os.path.join(data_dir, filename))
img_arr = np.resize(img_arr,(h,w,3)) #裁减
img_t = torch.from_numpy(img_arr)
img_t = img_t.permute(2,0,1) # h,w,c -> c,h,w
img_t = img_t[:3] #取前三个通道
batch[i] = img_t
batch = batch.float()
batch /= 255.
n_channels = batch.shape[1]
means, stds = [], []
for c in range(n_channels):
mean = torch.mean(batch[:,c])
means.append(mean)
std = torch.std(batch[:,c])
stds.append(std)
batch[:,c] = (batch[:,c] - mean) / std
print(means)
print(stds)结果:

边栏推荐
- sql add default constraint
- quick-3.5 lua调用c++
- configure:error no SDL library found
- quick-3.6源码修改纪录
- kotlin 插件更新到1.3.21
- MYSQL事务与锁问题处理
- 数据库 | SQL增删改查基础语法
- After unicloud is released, the applet prompts that the connection to the local debugging service failed. Please check whether the client and the host are under the same local area network.
- VS connects to MYSQL through ODBC (2)
- SSH自动重连脚本
猜你喜欢
随机推荐
flutter arr dependencies
为数学而歌之伯努利家族
ERROR Error: No module factory availabl at Object.PROJECT_CONFIG_JSON_NOT_VALID_OR_NOT_EXIST ‘Error
Understanding of js arrays
sql add default constraint
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
js中的函数
SSH automatic reconnection script
Gradle sync failed: Uninitialized object exists on backward branch 142
自定dialog 布局没有居中解决方案
Take you to understand the MySQL isolation level, what happens when two transactions operate on the same row of data at the same time?
jenkins +miniprogram-ci upload WeChat applet with one click
cocoscreator3.5.2打包微信小游戏发布到QQ小游戏修改
quick-3.6源码修改纪录
禅道安装及使用教程
cocos2d-x-3.2创建项目方法
Filter out egrep itself when using ps | egrep
[swagger close] The production environment closes the swagger method
DC-CDN学习笔记
The server time zone value ‘й‘ is unrecognized or represents more than one time zone









