当前位置:网站首页>PyTorch中grid_sample的使用方法
PyTorch中grid_sample的使用方法
2022-07-28 16:34:00 【张飞飞~】
https://blog.csdn.net/qq_34914551/article/details/107559031
import torch
from torch.nn import functional as F
inp = torch.ones(1, 1, 4, 4)
# 目的是得到一个 长宽为20的tensor
out_h = 20
out_w = 20
# grid的生成方式等价于用mesh_grid
# 这里是直接生成的[-1,1]的grid坐标,在实际应用中需要把图像的对应像素坐标统一到[-1,1]
new_h = torch.linspace(-1, 1, out_h).view(-1, 1).repeat(1, out_w)
print("new_h shape:",new_h.shape)
print(new_h) # new_h shape: torch.Size([20, 20])
new_w = torch.linspace(-1, 1, out_w).repeat(out_h, 1)
print("new_w shape:", new_w.shape)
print(new_w) #new_w shape: torch.Size([20, 20])
grid = torch.cat((new_h.unsqueeze(2), new_w.unsqueeze(2)), dim=2)
print(grid.shape) #torch.Size([20, 20, 2])
grid = grid.unsqueeze(0)
print(grid.shape) # torch.Size([1, 20, 20, 2])
outp = F.grid_sample(inp, grid=grid, mode='bilinear')
print(outp.shape) #torch.Size([1, 1, 20, 20])
边栏推荐
猜你喜欢

【C语言笔记分享】字符函数和字符串函数(建议收藏)
![【C语言进阶】——指针进阶[Ⅰ]](/img/62/d3410a61b931177fc02c1801489b5a.png)
【C语言进阶】——指针进阶[Ⅰ]

Hgu95av2. Online installation failed

Jerry ac692x --- matrix keyboard addition

Mmcv installation method

Visual object class introduces Pascal VOC dataset
![[阅读笔记] For Paper:R-CNN系列的三篇论文总结](/img/1d/8b862ac66ea04d9371f64e35fc4396.png)
[阅读笔记] For Paper:R-CNN系列的三篇论文总结

Solve package is not available (for R ve [package 'xxx' is not available (for R version x.y.z) "warning?]

7-8 浪漫侧影(25分)建树+新解题思路

Awk of shell script
随机推荐
DOS command Daquan basic command + network common command
In depth sharing of Ali (ant financial) technical interview process, with preliminary preparation and learning direction
Map R language
FreeRTOS learning notes
中南大学研究生复试机试题
转行学习软件测试有前途吗?
Random talk on test platform - platform construction ideas (Part 1)
Talk about the measurement of "post release problems"
strsplit()函数
学软件测试有用吗?
ROS系统安装
软件测试和软件开发应该怎么选择?
Awk of shell script
Can‘t use an undefined value as an ARRAY reference at probe2symbol
ng-repeat在迭代最后一个元素时执行一个方法
【无标题】
Please make sure you have the correct access rights and the repository exists.
Talk about what you know about publishing online (I)
Mmcv installation method
软件测试行业真的饱和了吗?