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

边栏推荐
- A simple bash to powershell case
- 禅道安装及使用教程
- Gradle sync failed: Uninitialized object exists on backward branch 142
- kotlin 插件更新到1.3.21
- [Cloud native] Open source data analysis SPL easily copes with T+0
- 5 methods of MySQL paging query
- Understanding of objects and functions in js
- powershell statistics folder size
- quick lua加密
- 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.
猜你喜欢

通信原理——纠错编码 | 汉明码(海明码)手算详解

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.

jenkins +miniprogram-ci 一键上传微信小程序

Notes on creating a new virtual machine in Hyper-V

一文速学-玩转MySQL获取时间、格式转换各类操作方法详解

How MySQL - depots table?A look at will understand

MySQL面试题大全(陆续更新)

DC-CDN学习笔记

This in js points to the prototype object

计网 Packet Tracer仿真 | 简单易懂集线器和交换机对比(理论+仿真)
随机推荐
configure:error no SDL library found
Using IIS10 to build an asp website in win11
VTK:Could not locate vtkTextRenderer object.
Flow control statement in js
Talking about the understanding of CAP in distributed mode
unicloud 云开发记录
C语言 | 获取字符串里逗号间隔的内容
Tencent Cloud GPU Desktop Server Driver Installation
Pure shell implementation of text replacement
TransactionTemplate transaction programmatic way
VS2017连接MYSQL
Filter out egrep itself when using ps | egrep
TransactionTemplate 事务编程式写法
Tencent Cloud Lightweight Server deletes all firewall rules
浅谈对分布式模式下CAP的理解
cocos2d-x-3.2 不能混合颜色修改
Access database query
cocos2d-x-3.x 修改和纪录
[swagger close] The production environment closes the swagger method
UiBot存在已打开的MicrosoftEdge浏览器,无法执行安装