欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pytorch之上/下采樣函數(shù)torch.nn.functional.interpolate插值詳解

 更新時間:2025年04月16日 09:31:12   作者:Yuezero_  
這篇文章主要介紹了Pytorch之上/下采樣函數(shù)torch.nn.functional.interpolate插值,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

Pytorch上/下采樣函數(shù)torch.nn.functional.interpolate插值

torch.nn.functional.interpolate(input_tensor, size=None, scale_factor=8, mode='bilinear', align_corners=False)
'''
Down/up samples the input to either the given size or the given scale_factor
The algorithm used for interpolation is determined by mode.
Currently temporal, spatial and volumetric sampling are supported, i.e. expected inputs are 3-D, 4-D or 5-D in shape.
The input dimensions are interpreted in the form: mini-batch x channels x [optional depth] x [optional height] x width.
The modes available for resizing are: nearest, linear (3D-only), bilinear, bicubic (4D-only), trilinear (5D-only), area
'''

這個函數(shù)是用來上采樣下采樣tensor的空間維度(h,w)

input_tensor支持輸入3D (b, c, w)或(batch,seq_len,dim)、4D (b, c, h, w)、5D (b, c, f, h, w)的 tensor shape。其中b表示batch_size,c表示channel,f表示frames,h表示height,w表示weight。

size是目標tensor的(w)/(h,w)/(f,h,w)的形狀;scale_factor是采樣tensor的saptial shape(w)/(h,w)/(f,h,w)的縮放系數(shù),sizescale_factor兩個參數(shù)只能定義一個,具體是上采樣,還是下采樣根據這兩個參數(shù)判斷。如果size或者scale_factorlist序列,則必須匹配輸入的大小。

  • 如果輸入3D,則它們的序列長度必須是1(只縮放最后1個維度w)。
  • 如果輸入4D,則它們的序列長度必須是2(縮放最后2個維度h,w)。
  • 如果輸入是5D,則它們的序列長度必須是3(縮放最后3個維度f,h,w)。

插值算法mode可選:最近鄰(nearest, 默認)線性(linear, 3D-only)、雙線性(bilinear, 4D-only)三線性(trilinear, 5D-only)等等。

是否align_corners對齊角點:可選的bool值, 如果 align_corners=True,則對齊 input 和 output 的角點像素(corner pixels),保持在角點像素的值. 只會對 mode=linear, bilinear, trilinear 有作用. 默認是 False。一圖看懂align_corners=TrueFalse的區(qū)別,從4×4上采樣成8×8。

一個是按四角的像素點中心對齊,另一個是按四角的像素角點對齊:

import torch
import torch.nn.functional as F
b, c, f, h, w = 1, 3, 8, 64, 64

1. upsample/downsample 3D tensor

# interpolate 3D tensor
x = torch.randn([b, c, w])
## downsample to (b, c, w/2)
y0 = F.interpolate(x, scale_factor=0.5, mode='nearest')
y1 = F.interpolate(x, size=[w//2], mode='nearest')
y2 = F.interpolate(x, scale_factor=0.5, mode='linear')  # only 3D
y3 = F.interpolate(x, size=[w//2], mode='linear')  # only 3D
print(y0.shape, y1.shape, y2.shape, y3.shape)
# torch.Size([1, 3, 32]) torch.Size([1, 3, 32]) torch.Size([1, 3, 32]) torch.Size([1, 3, 32])

## upsample to (b, c, w*2)
y0 = F.interpolate(x, scale_factor=2, mode='nearest')
y1 = F.interpolate(x, size=[w*2], mode='nearest')
y2 = F.interpolate(x, scale_factor=2, mode='linear')  # only 3D
y3 = F.interpolate(x, size=[w*2], mode='linear')  # only 3D
print(y0.shape, y1.shape, y2.shape, y3.shape)
# torch.Size([1, 3, 128]) torch.Size([1, 3, 128]) torch.Size([1, 3, 128]) torch.Size([1, 3, 128])

2. upsample/downsample 4D tensor

# interpolate 4D tensor
x = torch.randn(b, c, h, w)
## downsample to (b, c, h/2, w/2)
y0 = F.interpolate(x, scale_factor=0.5, mode='nearest')
y1 = F.interpolate(x, size=[h//2, w//2], mode='nearest')
y2 = F.interpolate(x, scale_factor=0.5, mode='bilinear')  # only 4D
y3 = F.interpolate(x, size=[h//2, w//2], mode='bilinear')  # only 4D
print(y0.shape, y1.shape, y2.shape, y3.shape)
# torch.Size([1, 3, 32, 32]) torch.Size([1, 3, 32, 32]) torch.Size([1, 3, 32, 32]) torch.Size([1, 3, 32, 32])

## upsample to (b, c, h*2, w*2)
y0 = F.interpolate(x, scale_factor=2, mode='nearest')
y1 = F.interpolate(x, size=[h*2, w*2], mode='nearest')
y2 = F.interpolate(x, scale_factor=2, mode='bilinear')  # only 4D
y3 = F.interpolate(x, size=[h*2, w*2], mode='bilinear')  # only 4D
print(y0.shape, y1.shape, y2.shape, y3.shape)
# torch.Size([1, 3, 128, 128]) torch.Size([1, 3, 128, 128]) torch.Size([1, 3, 128, 128]) torch.Size([1, 3, 128, 128])

3. upsample/downsample 5D tensor

# interpolate 5D tensor
x = torch.randn(b, c, f, h, w)
## downsample to (b, c, f/2, h/2, w/2)
y0 = F.interpolate(x, scale_factor=0.5, mode='nearest')
y1 = F.interpolate(x, size=[f//2, h//2, w//2], mode='nearest')
y2 = F.interpolate(x, scale_factor=2, mode='trilinear')  # only 5D
y3 = F.interpolate(x, size=[f//2, h//2, w//2], mode='trilinear')  # only 5D
print(y0.shape, y1.shape, y2.shape, y3.shape)
# torch.Size([1, 3, 4, 32, 32]) torch.Size([1, 3, 4, 32, 32]) torch.Size([1, 3, 16, 128, 128]) torch.Size([1, 3, 4, 32, 32])

## upsample to (b, c, f*2, h*2, w*2)
y0 = F.interpolate(x, scale_factor=2, mode='nearest')
y1 = F.interpolate(x, size=[f*2, h*2, w*2], mode='nearest')
y2 = F.interpolate(x, scale_factor=2, mode='trilinear')  # only 5D
y3 = F.interpolate(x, size=[f*2, h*2, w*2], mode='trilinear')  # only 5D
print(y0.shape, y1.shape, y2.shape, y3.shape)
# torch.Size([1, 3, 16, 128, 128]) torch.Size([1, 3, 16, 128, 128]) torch.Size([1, 3, 16, 128, 128]) torch.Size([1, 3, 16, 128, 128])

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Python 通過監(jiān)聽端口實現(xiàn)唯一腳本運行方式

    Python 通過監(jiān)聽端口實現(xiàn)唯一腳本運行方式

    這篇文章主要介紹了Python 通過監(jiān)聽端口實現(xiàn)唯一腳本運行方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • pytorch 固定部分參數(shù)訓練的方法

    pytorch 固定部分參數(shù)訓練的方法

    今天小編就為大家分享一篇pytorch 固定部分參數(shù)訓練的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • 一步步教你用Python畫五彩氣球

    一步步教你用Python畫五彩氣球

    這篇文章主要給大家介紹了關于如何用Python畫五彩氣球的相關資料,主要是用turtle庫自帶的畫筆turtle.Turtle()來繪制氣球,文中給出了詳細的實例代碼,需要的朋友可以參考下
    2023-06-06
  • Python通過Pillow實現(xiàn)圖片對比

    Python通過Pillow實現(xiàn)圖片對比

    這篇文章主要介紹了Python Pillow實現(xiàn)圖片對比,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Python實現(xiàn)八大排序算法

    Python實現(xiàn)八大排序算法

    這篇文章主要介紹了Python實現(xiàn)八大排序算法,如何用Python實現(xiàn)八大排序算法,感興趣的小伙伴們可以參考一下
    2016-08-08
  • pandas 如何保存數(shù)據到excel,csv

    pandas 如何保存數(shù)據到excel,csv

    這篇文章主要介紹了pandas 如何保存數(shù)據到excel,csv的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Gauss-Seidel迭代算法的Python實現(xiàn)詳解

    Gauss-Seidel迭代算法的Python實現(xiàn)詳解

    這篇文章主要介紹了Gauss-Seidel迭代算法的Python實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-06-06
  • 一文帶你了解Python中Scikit-learn庫的使用

    一文帶你了解Python中Scikit-learn庫的使用

    Scikit-learn是Python的一個開源機器學習庫,它支持監(jiān)督和無監(jiān)督學習,本文主要來深入探討一下Scikit-learn的更高級的特性,感興趣的小伙伴可以了解下
    2023-07-07
  • python初學之用戶登錄的實現(xiàn)過程(實例講解)

    python初學之用戶登錄的實現(xiàn)過程(實例講解)

    下面小編就為大家分享一篇python初學之用戶登錄的實現(xiàn)過程(實例講解),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • Python實現(xiàn)的簡單讀寫csv文件操作示例

    Python實現(xiàn)的簡單讀寫csv文件操作示例

    這篇文章主要介紹了Python實現(xiàn)的簡單讀寫csv文件操作,結合實例形式分析了Python使用csv模塊針對csv文件進行讀寫操作的相關實現(xiàn)技巧與注意事項,需要的朋友可以參考下
    2018-07-07

最新評論