当前位置:网站首页>计算图像数据集均值和方差
计算图像数据集均值和方差
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)结果:

边栏推荐
- quick lua加密
- Notes on creating a new virtual machine in Hyper-V
- What is the difference between NFT and digital collection?
- MySQL面试题大全(陆续更新)
- Powershell中UTF-8环境中文乱码解决办法
- cocoscreator 显示刘海内容
- [Cloud Native] What should I do if SQL (and stored procedures) run too slowly?
- softmax函数详解
- Understanding of js arrays
- 场效应管 | N-mos内部结构详解
猜你喜欢

Xiaomi mobile phone SMS location service activation failed

对js的数组的理解

MySQL高级语句(一)
![[Cloud native] Ribbon is no longer used at the bottom layer of OpenFeign starting from the 2020.0.X version](/img/7e/1d27e3f1856ab8c6cbfc5221c717bb.png)
[Cloud native] Ribbon is no longer used at the bottom layer of OpenFeign starting from the 2020.0.X version

安装Multisim出现 No software will be installed or removed解决方法

Why does read in bash need to cooperate with while to read the contents of /dev/stdin

js中的this指向与原型对象

MySQL高级学习笔记

For penetration testing methods where the output point is a timestamp (take Oracle database as an example)

VS connects to MYSQL through ODBC (2)
随机推荐
quick-3.5 无法正常显示有混合纹理的csb文件
对js的数组的理解
cocos2d-x-3.2 不能混合颜色修改
[Cloud Native] What should I do if SQL (and stored procedures) run too slowly?
MySQL面试题大全(陆续更新)
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.
Global scope and function scope in js
How MySQL - depots table?A look at will understand
break and continue exit in js
SQLite 查询表中每天插入的数量
ERROR Error: No module factory availabl at Object.PROJECT_CONFIG_JSON_NOT_VALID_OR_NOT_EXIST ‘Error
flutter 混合开发 module 依赖
微信小程序启动优化
安装Multisim出现 No software will be installed or removed解决方法
js中流程控制语句
Markdown 帮助文档
unicloud 云开发记录
What is the difference between NFT and digital collection?
Tencent Cloud GPU Desktop Server Driver Installation
动态规划(一)| 斐波那契数列和归递