pix2pix中的生成器和判别器详解

目录

一.生成器:

结构:

生成器损失:

二.判别器:

结构:

判别器损失:


一.生成器:

结构:

pix2pix中的生成器和判别器详解

即一个修改过的U-Net网络,跳连在编码器和解码器之间。

编码器(下采样)的每个块由卷积层,batchnorm层和leakyrelu层构成;

解码器(上采样)的每个块由反卷积层,batchnorm层和Dropout层(仅应用于前三个块)构成。

 代码:

OUTPUT_CHANNELS = 3

#编码器(下采样)
def downsample(filters, size, apply_batchnorm=True):
  initializer = tf.random_normal_initializer(0., 0.02)

  result = tf.keras.Sequential()
  result.add(
      tf.keras.layers.Conv2D(filters, size, strides=2, padding='same',
                             kernel_initializer=initializer, use_bias=False))

  if apply_batchnorm:
    result.add(tf.keras.layers.BatchNormalization())

  result.add(tf.keras.layers.LeakyReLU())

  return result


#down_model = downsample(3, 4)
#down_result = down_model(tf.expand_dims(inp, 0))
#print (down_result.shape)
##(1, 128, 128, 3)

########################################################

#上采样(解码器)
def upsample(filters, size, apply_dropout=False):
  initializer = tf.random_normal_initializer(0., 0.02)

  result = tf.keras.Sequential()
  result.add(
    tf.keras.layers.Conv2DTranspose(filters, size, strides=2,
                                    padding='same',
                                    kernel_initializer=initializer,
                                    use_bias=False))

  result.add(tf.keras.layers.BatchNormalization())

  if apply_dropout:
      result.add(tf.keras.layers.Dropout(0.5))

  result.add(tf.keras.layers.ReLU())

  return result

#up_model = upsample(3, 4)
#up_result = up_model(down_result)
#print (up_result.shape)
##(1, 256, 256, 3)

####################################################
#生成器
def Generator():
  inputs = tf.keras.layers.Input(shape=[256, 256, 3])

  down_stack = [
    downsample(64, 4, apply_batchnorm=False),  # (bs, 128, 128, 64)
    downsample(128, 4),  # (bs, 64, 64, 128)
    downsample(256, 4),  # (bs, 32, 32, 256)
    downsample(512, 4),  # (bs, 16, 16, 512)
    downsample(512, 4),  # (bs, 8, 8, 512)
    downsample(512, 4),  # (bs, 4, 4, 512)
    downsample(512, 4),  # (bs, 2, 2, 512)
    downsample(512, 4),  # (bs, 1, 1, 512)
  ]

  up_stack = [
    upsample(512, 4, apply_dropout=True),  # (bs, 2, 2, 1024)
    upsample(512, 4, apply_dropout=True),  # (bs, 4, 4, 1024)
    upsample(512, 4, apply_dropout=True),  # (bs, 8, 8, 1024)
    upsample(512, 4),  # (bs, 16, 16, 1024)
    upsample(256, 4),  # (bs, 32, 32, 512)
    upsample(128, 4),  # (bs, 64, 64, 256)
    upsample(64, 4),  # (bs, 128, 128, 128)
  ]

  initializer = tf.random_normal_initializer(0., 0.02)
  last = tf.keras.layers.Conv2DTranspose(OUTPUT_CHANNELS, 4,
                                         strides=2,
                                         padding='same',
                                         kernel_initializer=initializer,
                                         activation='tanh')  # (bs, 256, 256, 3)

  x = inputs

  # Downsampling through the model
  skips = []
  for down in down_stack:
    x = down(x)
    skips.append(x)

  skips = reversed(skips[:-1])

  # Upsampling and establishing the skip connections
  for up, skip in zip(up_stack, skips):
    x = up(x)
    x = tf.keras.layers.Concatenate()([x, skip])

  x = last(x)

  return tf.keras.Model(inputs=inputs, outputs=x)

 

如图:

pix2pix中的生成器和判别器详解生成器损失:

(1)生成的图像和元素为1的张量间的叉熵损失

(2)生成的图像和真实图像L1,

这使得生成图像结构上和真实图像相似。

总LAMBDA=100,total generator loss = gan_loss + LAMBDA * l1_loss

代码:

LAMBDA = 100
loss_object = tf.keras.losses.BinaryCrossentropy(from_logits=True)
#计算真实标签和预测标签之间的交叉熵损失
#当只有两个标签类(假设为0和1)时,使用这个交叉熵损失。对于每个示例,每个预测都应该有一个浮点值
def generator_loss(disc_generated_output, gen_output, target):
  gan_loss = loss_object(tf.ones_like(disc_generated_output), disc_generated_output)

  # mean absolute error
  l1_loss = tf.reduce_mean(tf.abs(target - gen_output))

  total_gen_loss = gan_loss + (LAMBDA * l1_loss)

  return total_gen_loss, gan_loss, l1_loss

如图:

pix2pix中的生成器和判别器详解

二.判别器:

结构:

判别器使用的所谓的patchGAN结构,由patchGAN得到的30*30*1的输出对应

它首先将输入图(256,256,3)和目标(或者生成)图(256,256,256,3)在channel维度上合在一起(256,256,6),然后进行三次下采样(32,32,32,256),一次padding后(34,34,34,256),再依次使用卷积(31*31*512)、BN(31*31*512),LeakyRelu(31*31*512)、padding(33*33*512)、卷积(30*30*1),得到一个30*30*1的输出。

 

注:如果输入图像和目标图像送入判别器,应被判为真;如果输入图像和生成图像送入判别器,最后应判别为假。

代码:

def Discriminator():
  initializer = tf.random_normal_initializer(0., 0.02)

  inp = tf.keras.layers.Input(shape=[256, 256, 3], name='input_image')
  tar = tf.keras.layers.Input(shape=[256, 256, 3], name='target_image')

  x = tf.keras.layers.concatenate([inp, tar])  # (bs, 256, 256, channels*2)

  down1 = downsample(64, 4, False)(x)  # (bs, 128, 128, 64)
  down2 = downsample(128, 4)(down1)  # (bs, 64, 64, 128)
  down3 = downsample(256, 4)(down2)  # (bs, 32, 32, 256)

  zero_pad1 = tf.keras.layers.ZeroPadding2D()(down3)  # (bs, 34, 34, 256)
  conv = tf.keras.layers.Conv2D(512, 4, strides=1,
                                kernel_initializer=initializer,
                                use_bias=False)(zero_pad1)  # (bs, 31, 31, 512)

  batchnorm1 = tf.keras.layers.BatchNormalization()(conv)

  leaky_relu = tf.keras.layers.LeakyReLU()(batchnorm1)

  zero_pad2 = tf.keras.layers.ZeroPadding2D()(leaky_relu)  # (bs, 33, 33, 512)

  last = tf.keras.layers.Conv2D(1, 4, strides=1,
                                kernel_initializer=initializer)(zero_pad2)  # (bs, 30, 30, 1)

  return tf.keras.Model(inputs=[inp, tar], outputs=last)

如图:pix2pix中的生成器和判别器详解

判别器损失:

判别损失函数有两个输入,真的图像和假的图像。

总损失由两部分和构成:real_loss + generated_loss。其中real_loss为真实图像和元素为1的张量间的交叉熵损失; generated_loss为生成图像和元素为0的张量间的交叉熵损失

代码:

def discriminator_loss(disc_real_output, disc_generated_output):
  real_loss = loss_object(tf.ones_like(disc_real_output), disc_real_output)

  generated_loss = loss_object(tf.zeros_like(disc_generated_output), disc_generated_output)

  total_disc_loss = real_loss + generated_loss

  return total_disc_loss

如图:

pix2pix中的生成器和判别器详解

 

    •  

上一篇:动手学CV-目标检测入门教程4:模型结构


下一篇:阿里云上创建Oracle RAC-静默模式