当前位置:网站首页>Intuitive understanding of torch nn. Unfold
Intuitive understanding of torch nn. Unfold
2022-04-23 07:27:00 【wujpbb7】
torch.nn.Unfold It's a batch Data in press C、Kernel_W、Kernel_H pack , Detailed explanation reference :
PyTorch in torch.nn.functional.unfold Function usage details
This article is mainly about Unfold Back to tensor The middle part of the is restored to patches.
# -*- coding:utf-8 -*-
import cv2
import torch
import numpy as np
img1 = cv2.imread('../128128/1.png')
img2 = cv2.imread('../128128/2.png')
batch = torch.tensor([img1, img2]).permute(0,3,1,2)
print(batch.shape) # [2,3,128,128]
hor_block_num = 2
ver_block_num = 4
n,c,h,w = batch.shape
assert not h%ver_block_num
assert not w%hor_block_num
patch_h, patch_w = h//ver_block_num, w//hor_block_num
use_unfold = True
if (not use_unfold):
# Method 1
patches = batch.view(n, c, ver_block_num, patch_h, hor_block_num, patch_w)
print(patches.shape) # [2,3,4,32,2,64]
patches = patches.permute(0,2,4,3,5,1)
print(patches.shape) # [2,4,2,32,64,3]
else:
# Method 2
split_block = torch.nn.Unfold(kernel_size=(patch_h, patch_w), stride=(patch_h, patch_w))
patches = split_block(batch.float())
print(patches.shape) # [2,6144,8]
patches = patches.permute(0,2,1).view(n,ver_block_num,hor_block_num,-1,patch_h,patch_w)
print(patches.shape) # [2,4,2,3,32,64]
patches = patches.permute(0,1,2,4,5,3).byte()
print(patches.shape) # [2,4,2,32,64,3]
# preservation patch
for i in range(patches.shape[0]):
img = patches[i]
for y in range(ver_block_num):
for x in range(hor_block_num):
patch_filename = '../128128/%d_y%d_x%d.png'%(i+1,y,x)
patch = img[y,x].numpy()
cv2.imwrite(patch_filename, patch)
The effect is as follows :

版权声明
本文为[wujpbb7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230611550578.html
边栏推荐
- 基于51单片机的温湿度监测+定时报警系统(c51源码)
- Machine learning III: classification prediction based on logistic regression
- 如何利用qemu搭建SOC protoype:80行代码实现一个Cortex M4 模拟器
- 网络层重要知识(面试、复试、期末)
- C language, a number guessing game
- PyTorch 21. PyTorch中nn.Embedding模块
- Résolution du système
- “Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
- The Cora dataset was trained and tested using the official torch GCN
- 画 ArcFace 中的 margin 曲线
猜你喜欢

画 ArcFace 中的 margin 曲线

SPI NAND FLASH小结
![[point cloud series] sg-gan: advantageous self attention GCN for point cloud topological parts generation](/img/1d/92aa044130d8bd86b9ea6c57dc8305.png)
[point cloud series] sg-gan: advantageous self attention GCN for point cloud topological parts generation

【51单片机交通灯仿真】

GIS实战应用案例100篇(五十一)-ArcGIS中根据指定的范围计算nc文件逐时次空间平均值的方法

【3D形状重建系列】Implicit Functions in Feature Space for 3D Shape Reconstruction and Completion

Chapter 2 pytoch foundation 2

PyTorch 10. 学习率

【无标题】PID控制TT编码器电机

【期刊会议系列】IEEE系列模板下载指南
随机推荐
MySQL的安装与配置——详细教程
Paddleocr image text extraction
【点云系列】Pointfilter: Point Cloud Filtering via Encoder-Decoder Modeling
Some common data type conversion methods in pytorch are similar to list and NP Conversion method of ndarray
Mysql database installation and configuration details
【點雲系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation
pth 转 onnx 时出现的 gather、unsqueeze 等算子
1.1 pytorch and neural network
AUTOSAR从入门到精通100讲(八十六)-UDS服务基础篇之2F
Chapter 1 numpy Foundation
带您遨游太空,美摄科技为航天创意小程序提供全面技术支持
Résolution du système
FATFS FAT32学习小记
基于openmv的无人机Apriltag动态追踪降落完整项目资料(labview+openmv+apriltag+正点原子四轴)
Chapter 4 pytoch data processing toolbox
GIS实用小技巧(三)-CASS怎么添加图例?
[point cloud series] sg-gan: advantageous self attention GCN for point cloud topological parts generation
吴恩达编程作业——Logistic Regression with a Neural Network mindset
AUTOSAR从入门到精通100讲(五十二)-诊断和通信管理功能单元
AUTOSAR从入门到精通100讲(五十)-AUTOSAR 内存管理系列- ECU 抽象层和 MCAL 层