cv2.reisze, interpolate采樣比較


cv2.reisze, interpolate采樣比較

resize, unsample, interpolate

本文比較cv2.reisze, PIL.Image.resize, torch.nn.functional.interpolate, mxnet

torch interpolate

注意:

  1. scale_factor與size只能設置一個。
  2. 當設置scale_factor時,會對輸出size下取整,比如輸入[2, 2], scale_factor=2.1, 則輸出size為[4.2, 4.2] = [4, 4]。
  3. 當設置scale_factor時,再設置recompute_scale_factor時,會根據輸出的實際大小重新計算一下scale_factor。
  4. 用scale_factor不用size是因為scale_factor可以不寫死大小,而size會固定輸出大小,在處理多分辨率輸入圖像的時候會有問題。
input:輸入Tensor。

size:插值后輸出Tensor的空間維度的大小,這個spatial size就是去掉Batch,Channel,Depth維度后剩下的值。比如NCHW的spatial size是HW。

scale_factor(float 或者 Tuple[float]):spatial size的乘數,如果是tuple則必須匹配輸入數據的大小。
mode(str):上采樣的模式,包含'nearest' | 'linear' | 'bilinear' | 'bicubic' | 'trilinear' | 'area'。 默認是 'nearest'。

align_corners(bool):在幾何上,我們將輸入和輸出的像素視為正方形而不是點。 如果設置為True,則輸入和輸出張量按其角像素的中心點對齊,保留角像素處的值。 如果設置為False,則輸入和輸出張量按其角像素的角點對齊,插值使用邊緣值填充來處理邊界外值,當scale_factor保持不變時,此操作與輸入大小無關。 這僅在mode為 'linear' | 'bilinear' | 'bicubic' | 'trilinear'時有效。默認值是False。

recompute_scale_factor(bool):重新計算用於插值計算的
scale_factor。 當 scale_factor 作為參數傳遞時,它用於計算 output_size。 如果 recompute_scale_factor 為 False 或未指定,則傳入的 scale_factor 將用於插值計算。 否則,將根據用於插值計算的輸出和輸入大小計算新的 scale_factor(即,等價於顯示傳入output_size)。 請注意,當 scale_factor 是浮點數時,由於舍入和精度問題,重新計算的 scale_factor 可能與傳入的不同。
x = nn.functional.interpolate(x, scale_factor=2, mode='bilinear', align_corners=False) 

ops_version對導出onnx影響:

op9, op10是Unsample,而op11變成了Resize。
onnx_interpolate

不同的ops_version對interpolate的支持程度:

F.interpolate nearest bilinear, align_corners=False bilinear, align_corners=True bicubic
op-9 Y Y N N
op-10 Y Y N N
op-11 Y Y Y Y

align_corner的表現行為:

align_corner
align_corner

opencv, PIL的align_corner為False, mxnet為True,而torch和tensorflow可以設置。

Reference

https://www.freesion.com/article/9926552179/
https://zhuanlan.zhihu.com/p/407963223
https://zhuanlan.zhihu.com/p/87572724?from_voters_page=true


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM