当前位置:网站首页>直观理解 torch.nn.Unfold
直观理解 torch.nn.Unfold
2022-04-23 06:12:00 【wujpbb7】
torch.nn.Unfold 是把batch中的数据按 C、Kernel_W、Kernel_H 打包,详细解释参考:
PyTorch中torch.nn.functional.unfold函数使用详解
本文主要是把 Unfold 返回的tensor的中间部分还原成 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):
# 方法一
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:
# 方法二
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]
# 保存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)
效果如下:

版权声明
本文为[wujpbb7]所创,转载请带上原文链接,感谢
https://blog.csdn.net/blueblood7/article/details/120786045
边栏推荐
- Pymysql connection database
- Chapter 8 generative deep learning
- 常见的正则表达式
- Record WebView shows another empty pit
- [2021 book recommendation] artistic intelligence for IOT Cookbook
- Android exposed components - ignored component security
- torch.mm() torch.sparse.mm() torch.bmm() torch.mul() torch.matmul()的区别
- 微信小程序 使用wxml2canvas插件生成图片部分问题记录
- ArcGIS license server administrator cannot start the workaround
- 机器学习——PCA与LDA
猜你喜欢

【点云系列】PnP-3D: A Plug-and-Play for 3D Point Clouds

MySQL的安装与配置——详细教程
![[recommendation for new books in 2021] professional azure SQL managed database administration](/img/f1/b38cce1dc328a5b534011169909127.png)
[recommendation for new books in 2021] professional azure SQL managed database administration

【点云系列】Learning Representations and Generative Models for 3D pointclouds

WinForm scroll bar beautification

第3章 Pytorch神经网络工具箱

Binder mechanism principle
![Android interview Online Economic encyclopedia [constantly updating...]](/img/48/dd1abec83ec0db7d68812f5fa9dcfc.png)
Android interview Online Economic encyclopedia [constantly updating...]

【点云系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation

F.pad 的妙用
随机推荐
【点云系列】Multi-view Neural Human Rendering (NHR)
红外传感器控制开关
Visual Studio 2019安装与使用
enforce fail at inline_container.cc:222
N states of prime number solution
Reading notes - activity
图像分类白盒对抗攻击技术总结
[2021 book recommendation] Red Hat Certified Engineer (RHCE) Study Guide
Computer shutdown program
【点云系列】Relationship-based Point Cloud Completion
Pytorch best practices and coding style guide
cmder中文乱码问题
Summary of image classification white box anti attack technology
使用 trt 的int8 量化和推断 onnx 模型
【指标】Precision、Recall
[dynamic programming] longest increasing subsequence
PyTorch 21. PyTorch中nn.Embedding模块
.net加载字体时遇到 Failed to decode downloaded font:
PyTorch 18. torch.backends.cudnn
【点云系列】Unsupervised Multi-Task Feature Learning on Point Clouds