1

我正在使用 GAN,我得到了伪像,删除它们的最佳方法是将生成器块 conv2dtranspose 更改为上采样和卷积,正如许多论文所建议的那样。我正在尝试通过我的个人数据集生成 ASL 手形符号 生成的图像 在此处输入图像描述

这是上面屏幕截图中提到的暗示 conv2dtranspose 的块代码

def _block(self, in_channels, out_channels, kernel_size, stride, padding):
        return nn.Sequential(
            nn.ConvTranspose2d(
                in_channels, out_channels, kernel_size, stride, padding, bias=False,
            ),
            nn.BatchNorm2d(out_channels),
            nn.LeakyReLU(0.2),

我想要实现的是更改块,以便它对图像进行采样并对其应用卷积,从而产生与 conv2dTranspose 相同的输出图像大小

这是我试过的

nn.Upsample(scale_factor = 2, mode='bilinear'),
            nn.ReflectionPad2d(1),
            nn.Conv2d(out_channels, int(out_channels / 2),kernel_size=3, stride=1, padding=0),
            nn.BatchNorm2d(out_channels),
            nn.LeakyReLU(0.2),

但是我收到以下错误 [![enter image description here][3]][3]

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-12-4fa61054bc2c> in <module>
     81         for _ in range(CRITIC_ITERATIONS):
     82             noise = torch.randn(cur_batch_size, Z_DIM, 1, 1).to(device)
---> 83             fake = gen(noise,labels)
     84             critic_real = critic(real,labels).reshape(-1)
     85             critic_fake = critic(fake,labels).reshape(-1)

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

<ipython-input-10-6af8005acf1f> in forward(self, x, labels)
     77         embedding=self.embed(labels).unsqueeze(2).unsqueeze(3)
     78         x=torch.cat([x,embedding],dim=1)
---> 79         return self.net(x)
     80 
     81 

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

~\Anaconda3\lib\site-packages\torch\nn\modules\container.py in forward(self, input)
    115     def forward(self, input):
    116         for module in self:
--> 117             input = module(input)
    118         return input
    119 

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

~\Anaconda3\lib\site-packages\torch\nn\modules\container.py in forward(self, input)
    115     def forward(self, input):
    116         for module in self:
--> 117             input = module(input)
    118         return input
    119 

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

~\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)
    421 
    422     def forward(self, input: Tensor) -> Tensor:
--> 423         return self._conv_forward(input, self.weight)
    424 
    425 class Conv3d(_ConvNd):

~\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in _conv_forward(self, input, weight)
    418                             _pair(0), self.dilation, self.groups)
    419         return F.conv2d(input, weight, self.bias, self.stride,
--> 420                         self.padding, self.dilation, self.groups)
    421 
    422     def forward(self, input: Tensor) -> Tensor:

RuntimeError: Given groups=1, weight of size [1024, 2048, 1, 1], expected input[64, 200, 2, 2] to have 2048 channels, but got 200 channels instead

此外,如果我将步幅更改为 1,它会给我嵌入错误

=================================================================
Layer (type:depth-idx)                   Param #
=================================================================
├─Sequential: 1-1                        --
|    └─Sequential: 2-1                   --
|    |    └─Upsample: 3-1                --
|    |    └─Conv2d: 3-2                  411,648
|    |    └─BatchNorm2d: 3-3             4,096
|    |    └─LeakyReLU: 3-4               --
|    └─Sequential: 2-2                   --
|    |    └─Upsample: 3-5                --
|    |    └─Conv2d: 3-6                  2,098,176
|    |    └─BatchNorm2d: 3-7             2,048
|    |    └─LeakyReLU: 3-8               --
|    └─Sequential: 2-3                   --
|    |    └─Upsample: 3-9                --
|    |    └─Conv2d: 3-10                 524,800
|    |    └─BatchNorm2d: 3-11            1,024
|    |    └─LeakyReLU: 3-12              --
|    └─Sequential: 2-4                   --
|    |    └─Upsample: 3-13               --
|    |    └─Conv2d: 3-14                 131,328
|    |    └─BatchNorm2d: 3-15            512
|    |    └─LeakyReLU: 3-16              --
|    └─Sequential: 2-5                   --
|    |    └─Upsample: 3-17               --
|    |    └─Conv2d: 3-18                 32,896
|    |    └─BatchNorm2d: 3-19            256
|    |    └─LeakyReLU: 3-20              --
|    └─ConvTranspose2d: 2-6              6,147
|    └─Tanh: 2-7                         --
├─Embedding: 1-2                         2,700
=================================================================
Total params: 3,215,631
Trainable params: 3,215,631
Non-trainable params: 0
=================================================================
=================================================================
Layer (type:depth-idx)                   Param #
=================================================================
├─Sequential: 1-1                        --
|    └─Conv2d: 2-1                       2,080
|    └─LeakyReLU: 2-2                    --
|    └─Sequential: 2-3                   --
|    |    └─Conv2d: 3-1                  32,768
|    |    └─InstanceNorm2d: 3-2          128
|    |    └─LeakyReLU: 3-3               --
|    └─Sequential: 2-4                   --
|    |    └─Conv2d: 3-4                  131,072
|    |    └─InstanceNorm2d: 3-5          256
|    |    └─LeakyReLU: 3-6               --
|    └─Sequential: 2-5                   --
|    |    └─Conv2d: 3-7                  524,288
|    |    └─InstanceNorm2d: 3-8          512
|    |    └─LeakyReLU: 3-9               --
|    └─Sequential: 2-6                   --
|    |    └─Conv2d: 3-10                 2,097,152
|    |    └─InstanceNorm2d: 3-11         1,024
|    |    └─LeakyReLU: 3-12              --
|    └─Conv2d: 2-7                       8,193
├─Embedding: 1-2                         442,368
=================================================================
Total params: 3,239,841
Trainable params: 3,239,841
Non-trainable params: 0
=================================================================
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-3-4fa61054bc2c> in <module>
     83             fake = gen(noise,labels)
     84             critic_real = critic(real,labels).reshape(-1)
---> 85             critic_fake = critic(fake,labels).reshape(-1)
     86             gp = gradient_penalty(critic,labels, real, fake, device=device)
     87             loss_critic = (

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

<ipython-input-1-4154f5f318f2> in forward(self, x, labels)
     38     def forward(self, x,labels):
     39         embedding=self.embed(labels).view(labels.shape[0],1,self.img_size,self.img_size)
---> 40         x=torch.cat([x,embedding],dim=1)
     41         return self.disc(x)
     42 

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

有人可以解释我将如何为我的架构实现这一目标。

4

0 回答 0