当前位置:网站首页>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])
如果我的这篇文章帮助到了你,那我也会感到很高兴,一个人能走多远,在于与谁同行。
参考
边栏推荐
- QT add mouse event to control
- QT string operation
- Solution of phpstudy service environment 80 port occupied by process system under Windows
- Network Security Learning (XII) OSI and TCP
- Network Security Learning (XIII) data link layer
- [opencv] edge detection [API and source code implementation]
- 互联网协议之 IPFS
- CSV generated by PHP cannot completely display the number with leading 0
- 内存分页与调优,内核与用户空间
- Network Security Learning (XIV) IP protocol
猜你喜欢

File contains vulnerability

向下扎根,向上生长,探寻华为云AI的“根”力量

Take root downward, grow upward, and explore the "root" power of Huawei cloud AI

About using NPM command under the terminal, the installation error problem is solved (my own experience)

Secure code warrior learning record (IV)

Analysis of the influence of ESM direction finding error on positioning error

Zero crossing position search of discrete data (array)

WordPress removes the website publishing time
![[paper notes] a meta reinforcement learning algorithm for causal discovery](/img/03/84462b38551c41173f7a9734cb0e99.png)
[paper notes] a meta reinforcement learning algorithm for causal discovery

General paging function
随机推荐
Ffmpeg first learning (only for coding)
赋能合作伙伴,亚马逊云科技如何落地“扶上马,送一程”?
QT operation to solve large amount of duplicate data
OASYS system of code audit
Analysis of the influence of ESM direction finding error on positioning error
Analysis of direction finding error of multi baseline interferometer system
Network Security Learning (11) scanning and blasting
技术美术百人计划学习笔记(1)--基础渲染管线
HJ9 提取不重复的整数
Memory paging and tuning, kernel and user space
网格参数化Least Squares Conformal Maps实现(3D网格映射到2D平面)
Secure code warrior learning record (II)
Mongodb的特点、与MySQL的差别、以及应用场景
[paper notes] a meta reinforcement learning algorithm for causal discovery
Longitude and latitude and its transformation with coordinate system
Week 2: convolutional neural network
File contains vulnerability
General paging function
PCL basic operation Encyclopedia
The small icon of notification setting shows a small square