網站首頁 編程語言 正文
關于grid_sample的使用
grid_sample底層是應用雙線性插值,把輸入的tensor轉換為指定大小。
那它和interpolate有啥區別呢?
interpolate是規則采樣(uniform),但是grid_sample的轉換方式,內部采點的方式并不是規則的,是一種更為靈活的方式。
torch.nn.functional.grid_sample(input, grid, mode=‘bilinear', padding_mode=‘zeros')
-
input
:輸入tensor, shape為 [N, C, H_in, W_in] -
grid
:一個field flow, shape為[N, H_out, W_out, 2],最后一個維度是每個grid(H_out_i, W_out_i)在input的哪個位置的鄰域去采點。數值范圍被歸一化到[-1,1]。
下面將介紹具體的例子
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 new_h = torch.linspace(-1, 1, out_h).view(-1, 1).repeat(1, out_w) new_w = torch.linspace(-1, 1, out_w).repeat(out_h, 1) grid = torch.cat((new_h.unsqueeze(2), new_w.unsqueeze(2)), dim=2) grid = grid.unsqueeze(0) outp = F.grid_sample(inp, grid=grid, mode='bilinear') print(outp.shape) #torch.Size([1, 1, 20, 20])
在上面的例子中,我們將一個大小為4x4的tensor 轉換為了一個20x20的。
grid的大小指定了輸出大小,每個grid的位置是一個(x,y)坐標,其值來自于:輸入input的(x,y)中 的四鄰域插值得到的。
圖片來自于SFnet(eccv2020)。flow field是grid, low_resolution是input, high resolution是output。
至于grid的值是控制在-1,1的。那如何對應在input上呢。
這個來看一下pytorch的底層源碼。
第66行到71行,獲取到了grid的x和y,之后對其做了新的變換,變到input的坐標系下了。
IW和IH是input的寬和高。
real ix = THTensor_fastGet4d(grid, n, h, w, 0); real iy = THTensor_fastGet4d(grid, n, h, w, 1); // normalize ix, iy from [-1, 1] to [0, IH-1] & [0, IW-1] ix = ((ix + 1) / 2) * (IW-1); iy = ((iy + 1) / 2) * (IH-1);
torch.nn.functional.grid_sample() 注意點
用法: 主要用于采樣,一般是使用bilinear根據grid的坐標采樣
F.grid_sample(img, grid, align_corners=True)
-
img
是采樣的空間,grid是生成的網格坐標。 -
grid
通常由torch.meshgrid()生成,且要映射到(-1,1)之間,如:
dx = torch.linspace(-1,1, 9) dy = torch.linspace(-1, 1,7) coords = torch.stack(torch.meshgrid(dy, dx), axis=-1) ? #[dy*dx*2]
輸入輸出情況:
假如是4D 的input:
img.shape : [B,C,H_in,W_in] grid.shape: [B,H_out,W_out,2] out: [B,C,H_out,W_out]
細節
1.為什么meshgrid生成坐標的時候,stack成coords時需要逆序(第一層是y,第二層是x)?
Ans:采樣的時候,在img上取點,坐標是根據grid來的,grid[:,:,0]是W維度的坐標,grid[:,:,1]是H維度的坐標,所以這個地方需要注意,是反過來的
2.grid的形狀僅僅影響output的形狀,直接決定取點的還是坐標,所以尤其要注意grid坐標疊。
總結
原文鏈接:https://blog.csdn.net/qq_34914551/article/details/107559031
相關推薦
- 2022-06-12 Python數據傳輸黏包問題_python
- 2022-07-23 .Net創建型設計模式之工廠方法模式(Factory?Method)_基礎應用
- 2024-03-17 為解決Win11子系統的Ubuntu被刪除后,重新安裝出現找不到系統路徑問題,無法正常安裝
- 2022-12-13 C++?POSIX?API超詳細分析_C 語言
- 2022-09-22 linux進程概念
- 2022-07-06 python中csv文件創建、讀取及修改等操作實例_python
- 2022-09-22 ansible-playbook 可用參數
- 2022-11-09 Android性能優化之plt?hook與native線程監控詳解_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支