torch.nn.functional.grid_sample实验

# -*- coding: utf-8 -*-
# @Author: H.Yang
# @Date:   2021-08-14 11:58:04
# @Last Modified by:   H.Yang
# @Last Modified time: 2021-08-14 11:59:27
import torch
import torch.nn as nn

test = torch.rand(1,1,3,3)
test[0][0][0][0]=1
test[0][0][0][1]=2
test[0][0][0][2]=3
test[0][0][1][0]=4
test[0][0][1][1]=5
test[0][0][1][2]=6
test[0][0][2][0]=7
test[0][0][2][1]=8
test[0][0][2][2]=9
print(test)
#tensor([[[[1., 2., 3.],
#          [4., 5., 6.],
#          [7., 8., 9.]]]])

sample_one = torch.zeros(1,1,1,2)
sample_one [0][0][0][0] = -1  # x
sample_one [0][0][0][1] = -1  # y

sample_two = torch.zeros(1,1,1,2)
sample_two [0][0][0][0] = -2/3  # x
sample_two [0][0][0][1] = -2/3  # y

sample_thr = torch.zeros(1,1,1,2)
sample_thr [0][0][0][0] = -0.8  # x
sample_thr [0][0][0][1] = -0.3  # y

result_one = torch.nn.functional.grid_sample(test,sample_one,mode='bilinear',padding_mode="zeros",align_corners=True)
print(result_one )
#tensor([[[[1.]]]])

result_two= torch.nn.functional.grid_sample(test,sample_two ,mode='bilinear',padding_mode="zeros",align_corners=False)
print(result_two)
#tensor([[[[1.]]]])

result_thr= torch.nn.functional.grid_sample(test,sample_thr ,mode='bilinear',padding_mode="zeros",align_corners=True)
print(result_thr)
#tensor([[[[3.3000]]]]) 通过双线性插值,坐标距离越近权重越大,直接算就可以得到(1*0.8+2*0.2)*0.3+(4*0.8+5*0.2)*0.7=3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生成滞涨网络~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值