【问题标题】:Pytorch - TypeError: ToTensor() takes no arguments using torchvision.transformPytorch - TypeError: ToTensor() 使用 torchvision.transform 不接受任何参数
【发布时间】:2020-07-29 16:39:35
【问题描述】:

我正在尝试加载数据集以实现超分辨率,并且我设置了两个函数,它们使用 Compose 来裁剪和调整图像大小。

我为输入图像创建的功能正常工作,并且按预期输出。目标图片的transform函数基本相同,只是省略了resize部分。

def input_trans(c_size, sF):
    return Compose([
        CenterCrop(c_size),
        Resize(c_size // sF),
        ToTensor(),
    ])


def goal_trans(c_size):
    return Compose([
        CenterCrop(c_size),
        ToTensor(),
    ])

这些函数在加载图像时在我的数据集类中使用。我最初有目标 = input.Copy() 但我已经更改了它,因此输入和目标都分别加载图像。 (正在测试 .copy() 是否是问题

def __getitem__(self, idx):
    input = Image.open(self.image_filenames[idx]).convert('RGB')
    goal = Image.open(self.image_filenames[idx]).convert('RGB')
    if self.input_transform:
        input = self.input_transform(input)
    if self.goal_transform:
        print(goal)
        print(goal.size)
        goal = self.goal_transform(goal)

    return input, goal

我收到的错误如下:

Traceback (most recent call last):
  File "main.py", line 128, in <module>
    main()  # execute this only when run directly, not when imported!
  File "main.py", line 55, in main
    train_model(epoch)
  File "main.py", line 40, in train_model
    for data_item, batch in enumerate(training_data_loader):
  File "C:\Users\[NAME]\anaconda3\envs\pytorch\lib\site-packages\torch\utils\data\dataloader.py", line 345, in __next__
    data = self._next_data()
  File "C:\Users\[NAME]\anaconda3\envs\pytorch\lib\site-packages\torch\utils\data\dataloader.py", line 385, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "C:\Users\[NAME]\anaconda3\envs\pytorch\lib\site-packages\torch\utils\data\_utils\fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "C:\Users\[NAME]\anaconda3\envs\pytorch\lib\site-packages\torch\utils\data\_utils\fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "main.py", line 118, in __getitem__
    goal = self.goal_transform(goal)
  File "C:\Users\[NAME]\anaconda3\envs\pytorch\lib\site-packages\torchvision\transforms\transforms.py", line 70, in __call__
    img = t(img)
TypeError: ToTensor() takes no arguments

让我感到困惑,因为第一次转换似乎没有问题(我检查过,它在崩溃前确实输出了)。

如果你们能提供任何帮助,我将不胜感激,

谢谢:)

【问题讨论】:

    标签: python pytorch torch torchvision


    【解决方案1】:

    问题解决了!与torchvision.transforms 无关。

    我实际上并没有使用上面的函数,而是我之前尝试使用的 compose 的内联声明。

    我的错

    【讨论】:

      猜你喜欢
      • 2020-10-28
      • 2020-02-23
      • 1970-01-01
      • 2013-04-19
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多