PyTorch中grid_sample的使用方法

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])

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值