pytorch - 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()
解决方案
你需要这样的张量维度
torch.tensor([imgs])
推荐阅读
- azure-ad-b2c - 如何对 Azure AD B2C 自定义策略中的 RESTful 终结点响应进行故障排除
- javascript - 无法匹配除 ' 和 " 之外的任何字符的正则表达式
- javascript - Javascript:如何遍历值数组并创建表行
- flutter - 颤振变量未传入类小部件
- gekko - 如何设置 GEKKO 以从多个独立的数据集进行参数估计?
- r - 在跨列的 for 循环中运行 if 和 else
- shader - 具有非均匀倾斜的Godot着色器?
- python - 自动点击器代码不起作用(第一个项目)
- go - 如何在我的 CLI 中获取用户选择的标志
- python - 构建具有不同扩展名选择的文件路径