当前位置:网站首页>Pytorch data input format requirements and conversion
Pytorch data input format requirements and conversion
2022-07-25 23:19:00 【Ha ha ha ha ha ha ha ha】
Index indexes
Data format requirements
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.
take numpy Format image into corresponding tensor Format
Before feeding into the deep learning network , The data format needs to be converted , Need to put N x H x W x C Of numpy Format image into corresponding tensor Format 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)
Format conversion of input data as required
Use .permute() Function to adjust the dimensions of data , And use .unsqueeze() Function to increase its dimension , The code and demonstration are as follows :
#!/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])
If my article helps you , Then I'll be happy too , How far can one go , It's about who to walk with .
Reference resources
边栏推荐
- Simulink learning notes (III) - Simulink automatic code generation (II) "suggestions collection"
- [paper notes] robot dynamic tracking and grasping method based on online prediction and planning
- Design of Butterworth filter and drawing of amplitude frequency characteristic curve
- Mongodb的特点、与MySQL的差别、以及应用场景
- CSV generated by PHP cannot completely display the number with leading 0
- Vscode shortcut key: collapse and expand code
- Take root downward, grow upward, and explore the "root" power of Huawei cloud AI
- QT string operation
- 动态内存管理
- 谷粒学苑P98踩坑 e.GlobalExceptionHandler : null
猜你喜欢

Network Security Learning notes-1 file upload

Solution of phpstudy service environment 80 port occupied by process system under Windows

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

Take away applet with main version of traffic / repair to add main access function of traffic
![[interface performance optimization] reasons for index failure and how to optimize SQL](/img/b9/64058c823c4497ac36bfb62a101816.jpg)
[interface performance optimization] reasons for index failure and how to optimize SQL

Solve the problem phpstudy failed to import the database

Unity uses macros

POI special effects Market Research

@Import

Longitude and latitude and its transformation with coordinate system
随机推荐
EasyExcel实用技巧
firewall 命令简单操作
Which securities firm is the best and safest for beginners to open an account
Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download
Vscode shortcut key: collapse and expand code
推荐系统——An Embedding Learning Framework for Numerical Features in CTR Prediction
网格参数化Least Squares Conformal Maps实现(3D网格映射到2D平面)
Unity 使用宏
策略模式_
单模型常识推理首超人类!HFL登顶OpenBookQA挑战赛
Take away applet with main version of traffic / repair to add main access function of traffic
Analysis of the influence of ESM direction finding error on positioning error
Multimodal deep multi modal sets
WordPress controls the minimum and maximum number of words of article comments
Mongodb的特点、与MySQL的差别、以及应用场景
The new UI people help task help PHP source code with a value of 1500 / reward task Tiktok Kwai headline like source code / with three-level distribution can be packaged applet
[paper notes] robot dynamic tracking and grasping method based on online prediction and planning
Servlet overview
ETL工具(数据同步) 二
Tencent map API request source is not authorized, this request source domain name