thuml / Transfer-Learning-Library

Transfer Learning Library for Domain Adaptation, Task Adaptation, and Domain Generalization
http://transfer.thuml.ai
MIT License
3.39k stars 553 forks source link

CycleGAN U-Net_128: RuntimeError : Sizes of tensors must match except in dimension 2 #202

Closed murenma05 closed 1 year ago

murenma05 commented 1 year ago

作者您好,感谢您提供的CycleGAN模型,方便我这样的新手可以更好的理解代码,在使用unet作为generator的时候遇到了报错信息,我目前还没有排查明白,在这里向您需求帮助,还请见谅。

在运行CycleGAN的时候,我使用的是 --netG unet_128的参数,也就是generator用的unet_128,在训练完成后,用G_S2T做风格迁移的时候发生了报错,如下

Traceback (most recent call last):
  File "cycle_gan.py", line 507, in <module>
    main(args)
  File "cycle_gan.py", line 305, in main
    dataset.translate(transform, translated_source, image_base=args.image_base)
  File "cycle_gan.py", line 148, in translate
    translated_image = transform(image)
  File "../../../tllib/translation/cyclegan/transform.py", line 44, in forward
    generated_image = self.generator(image.unsqueeze(dim=0)).squeeze(dim=0).cpu()
  File "../../../tllib/translation/cyclegan/generator.py", line 163, in forward
    return self.model(input)
  File "../../../tllib/translation/cyclegan/generator.py", line 231, in forward
    return self.model(x)
  File "../../../tllib/translation/cyclegan/generator.py", line 233, in forward
    return torch.cat([x, self.model(x)], 1)

RuntimeError: Sizes of tensors must match except in dimension 2. Got 16 and 17 (The offending index is 0)

您好,输入数据集的图像大小是1024 x 1024 x 3,请问这种情况是torch.cat([x, self.model(x)], 1)中的x,和self.model(x)的feature map 的大小不一致导致的吗?还是图像的输入尺寸原因呢?后者情况我可以用resize,前者我需要怎么做呢?

谢谢!

murenma05 commented 1 year ago

输入的数据集是通过脚本从1500 x 1125 resize到 1024 x 1024的。 以下是报错前 x.size(2) 的输出:

281
140
70
35
17

谢谢!

thucbx99 commented 1 year ago

这里训练CycleGAN和进行S->T的translation时input size是一致的吗,比如都是1024 x 1024 x 3

murenma05 commented 1 year ago

您好,训练CycleGAN的Source是我自己的数据集图像,从1500 x 1125 x 3 resize为 1024 x 1024 x 3,Target是Cityscapes数据集图像,也就是2048 x 1024 x 3。请问我需要把这两者都resize为同一大小吗?

训练完模型后对Source执行translation时,Source为1500 x 1125 x 3,Target是 Cityscapes的图像,大小2048 x 1024 x 3。

上述报错出现在训练阶段,还没有到执行translation的那一步。

thucbx99 commented 1 year ago

我们这里U-Net的实现要求输入的H, W都是128的倍数,因此这里训练完模型之后做translation时也应该resize到1024 x 1024 x 3(以分割为例,参数是args.test_input_size)

上面这个问题,我们默认的实现都是source和target都resize到同样的大小的,我没有尝试过不同时的效果

murenma05 commented 1 year ago

感谢您的回复,我把source和target都调整到target的大小,然后就可以执行translation了