当前位置: 首页 > news >正文

pytorch 对图片进行归一化处理

如题,神经网络通常使用浮点数张量作为输入,我们要做的第一件事情就是将图片转化为浮点数,并且做归一化操作。

import torch
import imageio
import osdata_dir='F:\\work\\deep_learning\\pytorch\\dlwpt-code-master\\data\\p1ch4\\image-cats'
print(data_dir)
file_names=[name for name in os.listdir(data_dir) ] #if os.path.splitext(name)[-1]=='png'batch_size=3
batch=torch.zeros(batch_size,3,256,256,dtype=torch.uint8)for i ,file_name in enumerate(file_names):img_arr=imageio.imread(os.path.join(data_dir,file_name))img_t=torch.from_numpy(img_arr)img_t=img_t.permute(2,0,1)img_t=img_t[:3]batch[i]=img_t#获取通道数量
n_channels=batch.shape[1]#print(n_channels)#一般为3#将图片转为float类型
batch=batch.float()for c in range(n_channels):print(batch[:,c].shape)#对3张图片所有的c通道求平均值和方差mean=torch.mean(batch[:,c])std=torch.std(batch[:,c])batch[:,c]=(batch[:,c]-mean)/stdprint(mean)print(std)print(batch.shape)print(batch)

结果如下:

torch.Size([3, 256, 256])
tensor(147.8829)
tensor(56.4011)
torch.Size([3, 256, 256])
tensor(114.5765)
tensor(52.7250)
torch.Size([3, 256, 256])
tensor(90.6373)
tensor(49.2352)torch.Size([3, 3, 256, 256])
http://www.lryc.cn/news/194844.html

相关文章:

  • 零售数据分析师熬夜整理:人、货、场、供、财这样做
  • 基于SSM的学生选课管理系统
  • SQL注入漏洞
  • C++ wpf自制软件打包安装更新源码实例
  • 8月19日PMP成绩,预计10月16日公布!附查询入口、流程
  • 简易LDO设计(包含原理图、PCB和实验)
  • SpringBoot面试题5:SpringBoot Starter的工作原理是什么?
  • Leetcode 2902. Count of Sub-Multisets With Bounded Sum
  • ARP协议(地址解析协议) 的作用和操作过程
  • 轻游戏风格虚拟资源付费下载模板Discuz论坛模板
  • MongoDB索引操作
  • AMEYA360:君正低功耗AIoT图像识别处理器—X1600/X1600E
  • EM@圆和圆锥曲线的参数方程
  • uniapp 微信小程序 vue3.0+TS手写自定义封装步骤条(setup)
  • Python 金融大数据分析
  • 初识C++入门(1)
  • 使用Selenium的WebDriver进行长截图
  • python+大数据校园卡数据分析 计算机竞赛
  • 【机器学习】sklearn降维算法PCA
  • 华为云云耀云服务器L实例评测|企业项目最佳实践之评测用例(五)
  • Xcode升级到15.0 解决DT_TOOLCHAIN_DIR问题
  • 小谈设计模式(29)—访问者模式
  • 【25】c++设计模式——>责任链模式
  • GlobalTransactional
  • Android Studio运行kotlin项目,一直Read timed out
  • Excel 的单元格内容和单元格格式
  • 4大软件测试策略的特点和区别(单元测试、集成测试、确认测试和系统测试)
  • armbian 安装mysql
  • Ubuntu22常用软件
  • 【CFD小工坊】浅水模型的边界条件