首页 > 解决方案 > RandomAdjustSharpness 给出 IndexError: tuple index out of range

问题描述

在使用 RandomAdjustSharpness 时,我的代码会引发以下错误 - IndexError: tuple index out of range。我按照这里给出的说明进行操作 - https://pytorch.org/vision/stable/transforms.html,因此对这个错误感到困惑。

这是我的代码 -

import math, random
from sklearn.datasets import load_sample_images
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.autograd as autograd
import torch.nn.functional as F
import torchvision.transforms as transforms
import matplotlib.pyplot as plt


def random_crop(imgs):


    imgs = torch.tensor(imgs)
    change = torch.nn.Sequential(
        transforms.RandomCrop(427),
        transforms.RandomAdjustSharpness(1, p=1)
    )
    imgs = change(imgs).numpy()
    return imgs

###Obtaining a random image and preprocessing it!##
dataset = load_sample_images()
first_img_data = dataset.images[0]
first_img_data  = first_img_data.reshape(-1, 427, 640)
first_img_data = first_img_data[1, :, :]
#first_img_data = first_img_data[0:84, 0:84].reshape(-1, 84,84)
# first_img_data = torch.tensor(first_img_data)
plt.figure()
plt.imshow(np.squeeze(first_img_data))

foo = random_crop(first_img_data)
plt.figure()

plt.imshow(np.squeeze(foo))
plt.show()

标签: pytorch

解决方案


你需要这样的张量维度

torch.tensor([imgs])

推荐阅读