Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 鉴别器和生成器的图像大小不同的DCGAN_Python_Deep Learning_Pytorch_Dcgan_Generative Adversarial Network - Fatal编程技术网

Python 鉴别器和生成器的图像大小不同的DCGAN

Python 鉴别器和生成器的图像大小不同的DCGAN,python,deep-learning,pytorch,dcgan,generative-adversarial-network,Python,Deep Learning,Pytorch,Dcgan,Generative Adversarial Network,我正在使用DCGAN,在这里,我的鉴别器将有大小为128 x 128的真实图像输入,而发生器网络的输入是64x64图像(较低分辨率),而不是多项式高斯分布。 当我试图运行训练循环时,我得到了维度错误,我相信我的鉴别器和生成器维度架构可能是错误的。然而,我不知道错误在哪里。 ngf=64,ndf=128,nz=745 以下是生成器和鉴别器的代码: class G(nn.Module): # We introduce a class to define the generator. def

我正在使用DCGAN,在这里,我的鉴别器将有大小为128 x 128的真实图像输入,而发生器网络的输入是64x64图像(较低分辨率),而不是多项式高斯分布。 当我试图运行训练循环时,我得到了维度错误,我相信我的鉴别器和生成器维度架构可能是错误的。然而,我不知道错误在哪里。 ngf=64,ndf=128,nz=745 以下是生成器和鉴别器的代码:

class G(nn.Module): # We introduce a class to define the generator.

    def __init__(self): # We introduce the __init__() function that will define the architecture of the generator.
        super(G, self).__init__() # We inherit from the nn.Module tools.
        self.main = nn.Sequential( # We create a meta module of a neural network that will contain a sequence of modules (convolutions, full connections, etc.).
            # input is Z, going into a convolution
            nn.ConvTranspose2d(in_channels=nz, out_channels=ngf*8, kernel_size=4, stride=1, padding=0, bias = False), # first index is HR_data.shape[0] aka nz, second index is ngf * 8, where ngf relates to the size of the feature maps, i.e. 64
            nn.BatchNorm2d(ngf*8), # We normalize all the features along the dimension of the batch.
            nn.ReLU(True), # We apply a ReLU rectification to break the linearity.
            # state size. (ngf*8) x 4 x 4
            nn.ConvTranspose2d(ngf*8, ngf*4, 4, 2, 1, bias = False), # We add another inversed convolution.
            nn.BatchNorm2d(ngf*4), # We normalize again.
            nn.ReLU(True), # We apply another ReLU.
            # state size. (ngf*4) x 8 x 8
            nn.ConvTranspose2d(ngf*4, ngf*2, 4, 2, 1, bias = False), # We add another inversed convolution.
            nn.BatchNorm2d(ngf*2), # We normalize again.
            nn.ReLU(True), # We apply another ReLU.
            # state size. (ngf*2) x 16 x 16
            nn.ConvTranspose2d(ngf*2, ngf, 4, 2, 1, bias = False), # We add another inversed convolution.
            nn.BatchNorm2d(ngf), # We normalize again.
            nn.ReLU(True), # We apply another ReLU.
            # state size. (ngf) x 32 x 32
            nn.ConvTranspose2d(ngf, 3, 4, 2, 1, bias = False), # We add another inversed convolution.
            nn.Tanh() # We apply a Tanh rectification to break the linearity and stay between -1 and +1.
            # state size. 3 x 64 x 64
        )

    def forward(self, input): # We define the forward function that takes as argument an input that will be fed to the neural network, and that will return the output containing the generated images.
        output = self.main(input) # We forward propagate the signal through the whole neural network of the generator defined by self.main.
        return output # We return the output containing the generated images.

# Creating the generator
netG = G() # We create the generator object.
netG.apply(weights_init) # We initialize all the weights of its neural network.

# Defining the discriminator

class D(nn.Module): # We introduce a class to define the discriminator.

    def __init__(self): # We introduce the __init__() function that will define the architecture of the discriminator.
        super(D, self).__init__() # We inherit from the nn.Module tools.
        self.main = nn.Sequential( # We create a meta module of a neural network that will contain a sequence of modules (convolutions, full connections, etc.).
             # input is (nc) x 128 x 128
            nn.Conv2d(3, ndf, 4, 2, 1, bias = False),
            nn.LeakyReLU(0.2, inplace = True), # We apply a LeakyReLU.
            # state size. (ndf) x 64 x 64
            nn.Conv2d(ndf, ndf*2, 4, 2, 1, bias = False), # We add another convolution.
            nn.BatchNorm2d(ndf*2), # ndf * 2
            nn.LeakyReLU(0.2, inplace = True), # We apply another LeakyReLU.
            # state size. (ndf*2) x 32 x 32
            nn.Conv2d(ndf*2, ndf*4, 4, 2, 1, bias = False), # We add another convolution.
            nn.BatchNorm2d(ndf*4), # We normalize again.
            nn.LeakyReLU(0.2, inplace = True), # We apply another LeakyReLU.
            # state size. (ndf*4) x 16 x 16
            nn.Conv2d(ndf*4, ndf*8, 4, 2, 1, bias = False), # We add another convolution.
            nn.BatchNorm2d(ndf*8), # We normalize again.
            nn.LeakyReLU(0.2, inplace = True), # We apply another LeakyReLU.
            # state size. (ndf*8) x 8 x 8
            nn.Conv2d(ndf*8, 1, 4, 1, 0, bias = False), # We add another convolution.
            nn.Sigmoid() # We apply a Sigmoid rectification to break the linearity and stay between 0 and 1.
        )

    def forward(self, input): # We define the forward function that takes as argument an input that will be fed to the neural network, and that will return the output which will be a value between 0 and 1.
        output = self.main(input) # We forward propagate the signal through the whole neural network of the discriminator defined by self.main.
        return output.view(-1) # We return the output which will be a value between 0 and 1.

# Creating the discriminator
netD = D() # We create the discriminator object.
netD.apply(weights_init) # We initialize all the weights of its neural network.

错误来自您的生成器。如果生成器的输入大小为64*64,则其输出大于1024。通过打印输出的大小应该很容易检查。我建议您修改生成器的体系结构(例如,只保留一个转置conv2d,并用传统的转置conv2d替换所有其他转置)。由于我的输入是64x64图像,我是否需要转置?目前我有:
类G(nn.Module):def uu init uu u_;(self):super(G,self)。uuu init u__; self()self.main=nn.Sequential(nn.ConvTranspose2d(3,ngf*2,4,1,0,bias=False),nn.BatchNorm2d(ngf*2),nn.ReLU(True),#状态大小(ngf)x64 x 64 nn.ConvTranspose2d(ngf*2,3,4,2,1,bias=False),nn.Tanh())
很抱歉格式不好,stackoverflow注释是新的。请参阅。您可以计算最终输出大小并调整参数以符合您的期望。