当前位置:网站首页>直观理解 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
边栏推荐
猜你喜欢

Binder mechanism principle

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

WinForm scroll bar beautification
![[2021 book recommendation] red hat rhcsa 8 cert Guide: ex200](/img/5a/387baa0f59e5a8a502bb157184d968.png)
[2021 book recommendation] red hat rhcsa 8 cert Guide: ex200

给女朋友写个微信双开小工具
![[Point Cloud Series] SG - Gan: Adversarial Self - attachment GCN for Point Cloud Topological parts Generation](/img/1d/92aa044130d8bd86b9ea6c57dc8305.png)
[Point Cloud Series] SG - Gan: Adversarial Self - attachment GCN for Point Cloud Topological parts Generation

GEE配置本地开发环境

【点云系列】Pointfilter: Point Cloud Filtering via Encoder-Decoder Modeling
![[2021 book recommendation] Red Hat Certified Engineer (RHCE) Study Guide](/img/36/1c484aec5efbac8ae49851844b7946.png)
[2021 book recommendation] Red Hat Certified Engineer (RHCE) Study Guide

【点云系列】Learning Representations and Generative Models for 3D pointclouds
随机推荐
PyTorch最佳实践和代码编写风格指南
1.1 pytorch and neural network
ArcGIS license server administrator cannot start the workaround
Binder mechanism principle
How to standardize multidimensional matrix (based on numpy)
[dynamic programming] triangle minimum path sum
torch.where能否传递梯度
【动态规划】三角形最小路径和
【点云系列】Pointfilter: Point Cloud Filtering via Encoder-Decoder Modeling
Visual studio 2019 installation and use
面试总结之特征工程
【动态规划】不同的二叉搜索树
利用官方torch版GCN训练并测试cora数据集
torch.where能否传递梯度
第4章 Pytorch数据处理工具箱
素数求解的n种境界
扫雷小游戏
【点云系列】 A Rotation-Invariant Framework for Deep Point Cloud Analysis
Some common data type conversion methods in pytorch are similar to list and NP Conversion method of ndarray
PyTorch 21. PyTorch中nn.Embedding模块