当前位置:网站首页>PyTorch的数据输入格式要求及转换
PyTorch的数据输入格式要求及转换
2022-07-25 23:12:00 【哈哈哈哈哈嗝哈哈哈】
数据格式要求
The 4 dimensions of input_patch are <batch size, image height, image width, image channel> respectively. In Pytorch, the input channel should be in the second dimension. That’s why the permutation is required.
After the permutation, the 4 dimensions of in_img will be <batch size, image channel, image height, image width>1.
将 numpy格式图像转化为相应的tensor格式
在喂入深度学习网络之前,需要对数据格式进行转换,需要将 N x H x W x C 的 numpy格式图像转化为相应的 tensor格式 N x C x H x W2:
def toTensor(img):
img = torch.from_numpy(img.transpose((0, 3, 1, 2)))
return img.float().div(255).unsqueeze(0)
按要求对输入数据进行格式转换
使用.permute()函数对数据的各维度进行调整,并使用.unsqueeze()函数对其进行升维,代码及演示如下:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2022/7/12 12:26
# @FileName: unsqueeze.py
# @Software: PyCharm
import torch
x = torch.rand(8,128,192)
print(x.shape) # torch.Size([8, 128, 192])
x1 = torch.unsqueeze(x, -1)
print(x1.shape) # torch.Size([8, 128, 192, 1])
print(x1[0].shape) # torch.Size([128, 192, 1])
print(x1[1].shape) # torch.Size([128, 192, 1])
print(x1[-1].shape) # torch.Size([128, 192, 1])
如果我的这篇文章帮助到了你,那我也会感到很高兴,一个人能走多远,在于与谁同行。
参考
边栏推荐
- [opencv] edge detection [API and source code implementation]
- Simulink learning notes (III) - Simulink automatic code generation (II) "suggestions collection"
- Secure code warrior learning record (IV)
- Secure code warrior learning record (II)
- Family relationship calculator wechat applet source code
- [tensorflow] about seed
- Analysis of direction finding error of multi baseline interferometer system
- Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants
- 【接口性能优化】索引失效的原因以及如何进行SQL优化
- Design of Butterworth filter and drawing of amplitude frequency characteristic curve
猜你喜欢

Unity 使用宏

Network Security Learning (11) scanning and blasting

5 ROS仿真建模(3- rviz+gazebo+控制仿真机器人)

Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions

如何获取广告服务流量变现数据,助力广告效果分析?

Secure code warrior learning record (IV)

PHP JSON variable array problem

Apple CMS V10 template /mxone Pro adaptive film and television website template

Expression of directional signal -- complex exponential signal

自定义mvc原理
随机推荐
[paper notes] robot dynamic tracking and grasping method based on online prediction and planning
Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download
WordPress function encyclopedia, you can do the theme after learning it
uvm_ HDL -- implementation of DPI in UVM (4)
Ffmpeg first learning (only for coding)
[PTA] 7-24 minimum fraction (15 points)
serialization and deserialization
Source code of YY music wechat applet imitating Netease cloud music
Tencent map API request source is not authorized, this request source domain name
四旋翼飞行器的飞控实现「建议收藏」
动态内存管理
Data filtering of MATLAB
类和对象(3)
ETL工具(数据同步) 二
Simulink学习笔记(三)——Simulink自动代码生成(二)「建议收藏」
Analysis of direction finding error of multi baseline interferometer system
赋能合作伙伴,亚马逊云科技如何落地“扶上马,送一程”?
Network Security Learning (XIV) IP protocol
Network Security Learning notes-1 file upload
HJ7 取近似值