亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何以支持 autograd 的方式圍繞其中心旋轉 PyTorch 圖像張量?

如何以支持 autograd 的方式圍繞其中心旋轉 PyTorch 圖像張量?

米琪卡哇伊 2023-10-06 11:03:41
我想圍繞其中心隨機旋轉圖像張量(B、C、H、W)(我認為是二維旋轉?)。我想避免使用 NumPy 和 Kornia,這樣我基本上只需要從 torch 模塊導入。我也沒有使用torchvision.transforms,因為我需要它與 autograd 兼容。本質上,我正在嘗試為 DeepDream 等可視化技術創建一個 autograd 兼容版本torchvision.transforms.RandomRotation()(因此我需要盡可能避免偽影)。import torchimport mathimport randomimport torchvision.transforms as transformsfrom PIL import Image# Load imagedef preprocess_simple(image_name, image_size):    Loader = transforms.Compose([transforms.Resize(image_size), transforms.ToTensor()])    image = Image.open(image_name).convert('RGB')    return Loader(image).unsqueeze(0)    # Save image   def deprocess_simple(output_tensor, output_name):    output_tensor.clamp_(0, 1)    Image2PIL = transforms.ToPILImage()    image = Image2PIL(output_tensor.squeeze(0))    image.save(output_name)# Somehow rotate tensor around it's centerdef rotate_tensor(tensor, radians):    ...    return rotated_tensor# Get a random angle within a specified range r_degrees = 5angle_range = list(range(-r_degrees, r_degrees))n = random.randint(angle_range[0], angle_range[len(angle_range)-1])# Convert angle from degrees to radiansang_rad = angle * math.pi / 180# test_tensor = preprocess_simple('path/to/file', (512,512))test_tensor = torch.randn(1,3,512,512)# Rotate input tensor somehowoutput_tensor = rotate_tensor(test_tensor, ang_rad)# Optionally use this to check rotated image# deprocess_simple(output_tensor, 'rotated_image.jpg')我想要完成的一些示例輸出:
查看完整描述

3 回答

?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

因此,網格生成器和采樣器是 Spatial Transformer 的子模塊(JADERBERG、Max 等人)。這些子模塊不可訓練,它們可讓您應用可學習的以及不可學習的空間變換。theta在這里,我使用這兩個子模塊,并使用 PyTorch 的函數torch.nn.functional.affine_grid和(這些函數分別是生成器和采樣器的實現)來旋轉圖像torch.nn.functional.affine_sample:


import torch

import torch.nn.functional as F

import numpy as np

import matplotlib.pyplot as plt


def get_rot_mat(theta):

    theta = torch.tensor(theta)

    return torch.tensor([[torch.cos(theta), -torch.sin(theta), 0],

                         [torch.sin(theta), torch.cos(theta), 0]])



def rot_img(x, theta, dtype):

    rot_mat = get_rot_mat(theta)[None, ...].type(dtype).repeat(x.shape[0],1,1)

    grid = F.affine_grid(rot_mat, x.size()).type(dtype)

    x = F.grid_sample(x, grid)

    return x



#Test:

dtype =  torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor

#im should be a 4D tensor of shape B x C x H x W with type dtype, range [0,255]:

plt.imshow(im.squeeze(0).permute(1,2,0)/255) #To plot it im should be 1 x C x H x W

plt.figure()

#Rotation by np.pi/2 with autograd support:

rotated_im = rot_img(im, np.pi/2, dtype) # Rotate image by 90 degrees.

plt.imshow(rotated_im.squeeze(0).permute(1,2,0)/255)

在上面的示例中,假設我們將圖像im視為一只穿著裙子跳舞的貓:

https://img1.sycdn.imooc.com//651f795f0001b9bb02550250.jpg

rotated_im將是一只穿著裙子逆時針旋轉 90 度的跳舞貓:

https://img1.sycdn.imooc.com//651f796c0001a41a02550249.jpg

如果我們用rot_img等號theta調用,就會得到以下結果np.pi/4: 

https://img1.sycdn.imooc.com//651f797d00011bd402550249.jpg

最好的部分是它可以區分輸入并具有 autograd 支持!萬歲!



查看完整回答
反對 回復 2023-10-06
?
森林海

TA貢獻2011條經驗 獲得超2個贊

使用 torchvision 應該很簡單:


import torchvision.transforms.functional as TF


angle = 30

x = torch.randn(1,3,512,512)


out = TF.rotate(x, angle)

例如如果x是:

https://img1.sycdn.imooc.com//651f799700012aa802200223.jpg

out旋轉 30 度為(注:逆時針):

https://img1.sycdn.imooc.com//651f79a30001250b02230223.jpg


查看完整回答
反對 回復 2023-10-06
?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

pytorch 有一個函數:

x = torch.tensor([[0, 1],
            [2, 3]])

x = torch.rot90(x, 1, [0, 1])
>> tensor([[1, 3],
           [0, 2]])

以下是文檔:https://pytorch.org/docs/stable/ generated/torch.rot90.html


查看完整回答
反對 回復 2023-10-06
  • 3 回答
  • 0 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號