pix2pix网络,Pytorch代码中, valid 和 fake 两个参数表示啥
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pix2pix网络,Pytorch代码中, valid 和 fake 两个参数表示啥相关的知识,希望对你有一定的参考价值。
valid = Variable(Tensor(np.ones((real_A.size(0), *patch))), requires_grad=False)
fake = Variable(Tensor(np.zeros((real_A.size(0), *patch))), requires_grad=False)
这两句是什么意思
def __init__(self, in_size, out_size, normalize=True, dropout=0.0):
super(UNetDown, self).__init__()
layers = [nn.Conv2d(in_size, out_size, 4, 2, 1, bias=False)]
if normalize:
layers.append(nn.InstanceNorm2d(out_size))
layers.append(nn.LeakyReLU(0.2))
if dropout:
layers.append(nn.Dropout(dropout))
self.model = nn.Sequential(*layers)
def forward(self, x):
return self.model(x)
class UNetUp(nn.Module):
def __init__(self, in_size, out_size, dropout=0.0):
super(UNetUp, self).__init__()
layers = [ nn.ConvTranspose2d(in_size, out_size, 4, 2, 1, bias=False),
nn.InstanceNorm2d(out_size),
nn.ReLU(inplace=True)]
if dropout:
layers.append(nn.Dropout(dropout))
self.model = nn.Sequential(*layers)
def forward(self, x, skip_input):
x = self.model(x)
x = torch.cat((x, skip_input), 1)
return x
class GeneratorUNet(nn.Module):
def __init__(self, in_channels=3, out_channels=3):
super(GeneratorUNet, self).__init__()
self.down1 = UNetDown(in_channels, 64, normalize=False)
self.down2 = UNetDown(64, 128)
self.down3 = UNetDown(128, 256)
self.down4 = UNetDown(256, 512, dropout=0.5)
self.down5 = UNetDown(512, 512, dropout=0.5)
self.down6 = UNetDown(512, 512, dropout=0.5)
self.down7 = UNetDown(512, 512, dropout=0.5)
self.down8 = UNetDown(512, 512, normalize=False, dropout=0.5)
self.up1 = UNetUp(512, 512, dropout=0.5)
self.up2 = UNetUp(1024, 512, dropout=0.5)
self.up3 = UNetUp(1024, 512, dropout=0.5)
self.up4 = UNetUp(1024, 512, dropout=0.5)
self.up5 = UNetUp(1024, 256)
self.up6 = UNetUp(512, 128)
self.up7 = UNetUp(256, 64)
self.final = nn.Sequential(
nn.Upsample(scale_factor=2),
nn.ZeroPad2d((1, 0, 1, 0)),
nn.Conv2d(128, out_channels, 4, padding=1),
nn.Tanh()
)
def forward(self, x):
# U-Net generator with skip connections from encoder to decoder
d1 = self.down1(x)
d2 = self.down2(d1)
d3 = self.down3(d2)
d4 = self.down4(d3)
d5 = self.down5(d4)
d6 = self.down6(d5)
d7 = self.down7(d6)
d8 = self.down8(d7)
u1 = self.up1(d8, d7)
u2 = self.up2(u1, d6)
u3 = self.up3(u2, d5)
u4 = self.up4(u3, d4)
u5 = self.up5(u4, d3)
u6 = self.up6(u5, d2)
u7 = self.up7(u6, d1)
return self.final(u7)
---------------------追问
您好,pix2pix网络结构我看懂了,多谢。
我想问的是,在计算目标函数时,valid 和 fake 这两个参数是什么意思
如何在 Pytorch 中编写以下 Keras 神经网络的等效代码?
【中文标题】如何在 Pytorch 中编写以下 Keras 神经网络的等效代码?【英文标题】:How can I write the below equivalent code of Keras Neural Net in Pytorch? 【发布时间】:2019-06-05 03:31:08 【问题描述】:actor = Sequential()
actor.add(Dense(20, input_dim=9, activation='relu', kernel_initializer='he_uniform'))
actor.add(Dense(20, activation='relu'))
actor.add(Dense(27, activation='softmax', kernel_initializer='he_uniform'))
actor.summary()
# See note regarding crossentropy in cartpole_reinforce.py
actor.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=self.actor_lr))[Please find the image eq here.][1]
[1]: https://i.stack.imgur.com/gJviP.png
【问题讨论】:
到目前为止你有什么尝试? 【参考方案1】:类似的问题已经被问过,但这里是这样的:
import torch
actor = torch.nn.Sequential(
torch.nn.Linear(9, 20), # output shape has to be specified
torch.nn.ReLU(),
torch.nn.Linear(20, 20), # same goes over here
torch.nn.ReLU(),
torch.nn.Linear(20, 27), # and here
torch.nn.Softmax(),
)
print(actor)
初始化:默认情况下,从 1.0 版本开始,线性层将使用 Kaiming Uniform 进行初始化(参见this post)。如果您想以不同的方式初始化权重,请参阅this question 的最受好评的回答。
您也可以使用 Python 的 OrderedDict
来更轻松地匹配某些层,请参阅 Pytorch's documentation,您应该可以从那里继续。
【讨论】:
以上是关于pix2pix网络,Pytorch代码中, valid 和 fake 两个参数表示啥的主要内容,如果未能解决你的问题,请参考以下文章
Pytorch深度学习50篇·······第七篇:GAN生成对抗网络---PIX2PIX
[Pytorch系列-66]:生成对抗网络GAN - 图像生成开源项目pytorch-CycleGAN-and-pix2pix - 使用预训练模型测试pix2pix模型
[Pytorch系列-67]:生成对抗网络GAN - 图像生成开源项目pytorch-CycleGAN-and-pix2pix - 使用预训练模型进行测试pix2pix模型
[Pytorch系列-71]:生成对抗网络GAN - 图像生成开源项目pytorch-CycleGAN-and-pix2pix - 使用预训练模型训练pix2pix模型
[Pytorch系列-64]:生成对抗网络GAN - 图像生成开源项目pytorch-CycleGAN-and-pix2pix : 有监督图像生成pix2pix的基本原理