当前位置:网站首页>计算图像数据集均值和方差
计算图像数据集均值和方差
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)
结果:
边栏推荐
猜你喜欢
浏览器查找js绑定或者监听的事件
Hyper-V新建虚拟机注意事项
MySQL高级语句(一)
RuntimeError: CUDA error: no kernel image is available for execution on the device问题记录
Artifact SSMwar exploded Error deploying artifact.See server log for details
js中的this指向与原型对象
MySQL高级SQL语句(二)
多元线性回归方程原理及其推导
VS2017连接MYSQL
qt:cannot open C:\Users\XX\AppData\Local\Temp\main.obj.15576.16.jom for write
随机推荐
Nmap的下载与安装
Talking about the understanding of CAP in distributed mode
DeFi Token in the project management
unicloud cloud development record
Sqlite column A data is copied to column B
NFTs: The Heart of Digital Ownership
quick-3.5 无法正常显示有混合纹理的csb文件
The browser looks for events bound or listened to by js
quick-3.5 ActionTimeline的setLastFrameCallFunc调用会崩溃问题
一文速学-玩转MySQL获取时间、格式转换各类操作方法详解
Hyper-V新建虚拟机注意事项
Judgment of database in SQL injection
Why does read in bash need to cooperate with while to read the contents of /dev/stdin
What is the difference between NFT and digital collection?
For penetration testing methods where the output point is a timestamp (take Oracle database as an example)
使用ps | egrep时过滤排除掉egrep自身
Gradle sync failed: Uninitialized object exists on backward branch 142
What is an EVM Compatible Chain?
MySQL高级SQL语句(二)
understand js operators