def apply(self, x, y, is_training):
    """Apply the discriminator on a input.

    Args:
      x: `Tensor` of shape [batch_size, ?, ?, ?] with real or fake images.
      y: `Tensor` of shape [batch_size, num_classes] with one hot encoded
        labels.
      is_training: Boolean, whether the architecture should be constructed for
        training or inference.

    Returns:
      Tuple of 3 Tensors, the final prediction of the discriminator, the logits
      before the final output activation function and logits form the second
      last layer.
    """
    use_sn = self._spectral_norm
    batch_size = x.shape.as_list()[0]
    # Resulting shape: [bs, h/2, w/2, 64].
    net = lrelu(conv2d(x, 64, 4, 4, 2, 2, name="d_conv1", use_sn=use_sn))
    # Resulting shape: [bs, h/4, w/4, 128].
    net = conv2d(net, 128, 4, 4, 2, 2, name="d_conv2", use_sn=use_sn)
    net = self.batch_norm(net, y=y, is_training=is_training, name="d_bn2")
    net = lrelu(net)
    # Resulting shape: [bs, h * w * 8].
    net = tf.reshape(net, [batch_size, -1])
    # Resulting shape: [bs, 1024].
    net = linear(net, 1024, scope="d_fc3", use_sn=use_sn)
    net = self.batch_norm(net, y=y, is_training=is_training, name="d_bn3")
    net = lrelu(net)
    # Resulting shape: [bs, 1].
    out_logit = linear(net, 1, scope="d_fc4", use_sn=use_sn)
    out = tf.nn.sigmoid(out_logit)
    return out, out_logit, net
  def apply(self, z, y, is_training):
    """Build the generator network for the given inputs.

    Args:
      z: `Tensor` of shape [batch_size, z_dim] with latent code.
      y: `Tensor` of shape [batch_size, num_classes] with one hot encoded
        labels.
      is_training: boolean, are we in train or eval model.

    Returns:
      A tensor of size [batch_size] + self._image_shape with values in [0, 1].
    """
    del y
    h, w, c = self._image_shape
    bs = z.shape.as_list()[0]
    net = linear(z, 1024, scope="g_fc1")
    net = lrelu(batch_norm(net, is_training=is_training, name="g_bn1"))
    net = linear(net, 128 * (h // 4) * (w // 4), scope="g_fc2")
    net = lrelu(batch_norm(net, is_training=is_training, name="g_bn2"))
    net = tf.reshape(net, [bs, h // 4, w // 4, 128])
    net = deconv2d(net, [bs, h // 2, w // 2, 64], 4, 4, 2, 2, name="g_dc3")
    net = lrelu(batch_norm(net, is_training=is_training, name="g_bn3"))
    net = deconv2d(net, [bs, h, w, c], 4, 4, 2, 2, name="g_dc4")
    out = tf.nn.sigmoid(net)
    return out
예제 #3
0
 def f(name, x, width, n_out=None):
     with tf.variable_scope(name):
         with tf.variable_scope('dense1'):
             x = ops.linear(x, width, use_sn=False, use_bias=True)
             x = ops.lrelu(x)
         with tf.variable_scope('dense2'):
             x = ops.linear(x, n_out, use_sn=False, use_bias=True)
             x = ops.lrelu(x)
     return x
예제 #4
0
  def apply(self, x):
    """Overwriting compare_gan's apply as we only need `x`."""
    if not isinstance(x, tuple) or len(x) != 2:
      raise ValueError("Expected 2-tuple, got {}".format(x))
    x, latent = x
    x_shape = tf.shape(x)

    # Upscale and fuse latent.
    latent = arch_ops.conv2d(latent, 12, 3, 3, 1, 1,
                             name="latent", use_sn=self._spectral_norm)
    latent = arch_ops.lrelu(latent, leak=0.2)
    latent = tf.image.resize(latent, [x_shape[1], x_shape[2]],
                             tf.image.ResizeMethod.NEAREST_NEIGHBOR)
    x = tf.concat([x, latent], axis=-1)

    # The discriminator:
    k = 4
    net = arch_ops.conv2d(x, self._num_filters_base, k, k, 2, 2,
                          name="d_conv_head", use_sn=self._spectral_norm)
    net = arch_ops.lrelu(net, leak=0.2)

    num_filters = self._num_filters_base
    for i in range(self._num_layers - 1):
      num_filters = min(num_filters * 2, 512)
      net = arch_ops.conv2d(net, num_filters, k, k, 2, 2,
                            name=f"d_conv_{i}", use_sn=self._spectral_norm)
      net = arch_ops.lrelu(net, leak=0.2)

    num_filters = min(num_filters * 2, 512)
    net = arch_ops.conv2d(net, num_filters, k, k, 1, 1,
                          name="d_conv_a", use_sn=self._spectral_norm)
    net = arch_ops.lrelu(net, leak=0.2)

    # Final 1x1 conv that maps to 1 Channel
    net = arch_ops.conv2d(net, 1, k, k, 1, 1,
                          name="d_conv_b", use_sn=self._spectral_norm)

    out_logits = tf.reshape(net, [-1, 1])  # Reshape all into batch dimension.
    out = tf.nn.sigmoid(out_logits)

    return DiscOutAll(out, out_logits)
    def apply(self, x, y, is_training):
        """Apply the discriminator on a input.

    Args:
      x: `Tensor` of shape [batch_size, ?, ?, ?] with real or fake images.
      y: `Tensor` of shape [batch_size, num_classes] with one hot encoded
        labels.
      is_training: Boolean, whether the architecture should be constructed for
        training or inference.

    Returns:
      Tuple of 3 Tensors, the final prediction of the discriminator, the logits
      before the final output activation function and logits form the second
      last layer.
    """
        del is_training, y
        use_sn = self._spectral_norm
        # In compare gan framework, the image preprocess normalize image pixel to
        # range [0, 1], while author used [-1, 1]. Apply this trick to input image
        # instead of changing our preprocessing function.
        x = x * 2.0 - 1.0
        net = conv2d(x, 64, 3, 3, 1, 1, name="d_conv1", use_sn=use_sn)
        net = lrelu(net, leak=0.1)
        net = conv2d(net, 128, 4, 4, 2, 2, name="d_conv2", use_sn=use_sn)
        net = lrelu(net, leak=0.1)
        net = conv2d(net, 128, 3, 3, 1, 1, name="d_conv3", use_sn=use_sn)
        net = lrelu(net, leak=0.1)
        net = conv2d(net, 256, 4, 4, 2, 2, name="d_conv4", use_sn=use_sn)
        net = lrelu(net, leak=0.1)
        net = conv2d(net, 256, 3, 3, 1, 1, name="d_conv5", use_sn=use_sn)
        net = lrelu(net, leak=0.1)
        net = conv2d(net, 512, 4, 4, 2, 2, name="d_conv6", use_sn=use_sn)
        net = lrelu(net, leak=0.1)
        net = conv2d(net, 512, 3, 3, 1, 1, name="d_conv7", use_sn=use_sn)
        net = lrelu(net, leak=0.1)
        batch_size = x.shape.as_list()[0]
        net = tf.reshape(net, [batch_size, -1])
        out_logit = linear(net, 1, scope="d_fc1", use_sn=use_sn)
        out = tf.nn.sigmoid(out_logit)
        return out, out_logit, net
    def apply(self, x, y, is_training):
        """Apply the discriminator on a input.

    Args:
      x: `Tensor` of shape [batch_size, ?, ?, ?] with real or fake images.
      y: `Tensor` of shape [batch_size, num_classes] with one hot encoded
        labels.
      is_training: Boolean, whether the architecture should be constructed for
        training or inference.

    Returns:
      Tuple of 3 Tensors, the final prediction of the discriminator, the logits
      before the final output activation function and logits form the second
      last layer.
    """
        bs = x.shape[0].value
        df_dim = 64  # Dimension of filters in the first convolutional layer.
        net = lrelu(
            conv2d(x,
                   df_dim,
                   5,
                   5,
                   2,
                   2,
                   name="d_conv1",
                   use_sn=self._spectral_norm))
        net = conv2d(net,
                     df_dim * 2,
                     5,
                     5,
                     2,
                     2,
                     name="d_conv2",
                     use_sn=self._spectral_norm)

        net = self.batch_norm(net, y=y, is_training=is_training, name="d_bn1")
        net = lrelu(net)
        net = conv2d(net,
                     df_dim * 4,
                     5,
                     5,
                     2,
                     2,
                     name="d_conv3",
                     use_sn=self._spectral_norm)

        net = self.batch_norm(net, y=y, is_training=is_training, name="d_bn2")
        net = lrelu(net)
        net = conv2d(net,
                     df_dim * 8,
                     5,
                     5,
                     2,
                     2,
                     name="d_conv4",
                     use_sn=self._spectral_norm)

        net = self.batch_norm(net, y=y, is_training=is_training, name="d_bn3")
        net = lrelu(net)
        out_logit = linear(tf.reshape(net, [bs, -1]),
                           1,
                           scope="d_fc4",
                           use_sn=self._spectral_norm)
        out = tf.nn.sigmoid(out_logit)
        return out, out_logit, net