(x,
           kernel_size,
           stride,
           channels,
           is_training,
           scope="conv2d",
           batch_norm=False,
           residual=False,
           gated=False,
           activation_fn=tf.nn.relu,
           resize=False,
           transpose=False,
           stacked_layers=1)
2D-Conv with optional batch_norm, gating, residual.

  Args:
    x: Tensor input [MB, H, W, CH].
    kernel_size: List [H, W].
    stride: List [H, W].
    channels: Int, output channels.
    is_training: Whether to collect stats for BatchNorm.
    scope: Enclosing scope name.
    batch_norm: Apply batch normalization
    residual: Residual connections, have stacked_layers >= 2.
    gated: Gating ala Wavenet.
    activation_fn: Nonlinearity function.
    resize: On transposed convolution, do ImageResize instead of conv_transpose.
    transpose: Use conv_transpose instead of conv.
    stacked_layers: Number of layers before a residual connection.

  Returns:
    x: Tensor output.
2D-Conv with optional batch_norm, gating, residual.
2D - Conv with optional batch_norm gating residual .
def conv2d(x,
           kernel_size,
           stride,
           channels,
           is_training,
           scope="conv2d",
           batch_norm=False,
           residual=False,
           gated=False,
           activation_fn=tf.nn.relu,
           resize=False,
           transpose=False,
           stacked_layers=1):
  """2D-Conv with optional batch_norm, gating, residual.

  Args:
    x: Tensor input [MB, H, W, CH].
    kernel_size: List [H, W].
    stride: List [H, W].
    channels: Int, output channels.
    is_training: Whether to collect stats for BatchNorm.
    scope: Enclosing scope name.
    batch_norm: Apply batch normalization
    residual: Residual connections, have stacked_layers >= 2.
    gated: Gating ala Wavenet.
    activation_fn: Nonlinearity function.
    resize: On transposed convolution, do ImageResize instead of conv_transpose.
    transpose: Use conv_transpose instead of conv.
    stacked_layers: Number of layers before a residual connection.

  Returns:
    x: Tensor output.
  """
  # For residual
  x0 = x
  # Choose convolution function
  conv_fn = slim.conv2d_transpose if transpose else slim.conv2d
  # Double output channels for gates
  num_outputs = channels * 2 if gated else channels
  normalizer_fn = slim.batch_norm if batch_norm else None

  with tf.variable_scope(scope + "_Layer"):
    # Apply a stack of convolutions Before adding residual
    for layer_idx in range(stacked_layers):
      with slim.arg_scope(
          slim_batchnorm_arg_scope(is_training, activation_fn=None)):
        # Use interpolation to upsample instead of conv_transpose
        if transpose and resize:
          unused_mb, h, w, unused_ch = x.get_shape().as_list()
          x = tf.image.resize_images(
              x, size=[h * stride[0], w * stride[1]], method=0)
          stride_conv = [1, 1]
        else:
          stride_conv = stride

        x = conv_fn(
            inputs=x,
            stride=stride_conv,
            kernel_size=kernel_size,
            num_outputs=num_outputs,
            normalizer_fn=normalizer_fn,
            biases_initializer=tf.zeros_initializer(),
            scope=scope)

        if gated:
          with tf.variable_scope("Gated"):
            x1, x2 = x[:, :, :, :channels], x[:, :, :, channels:]
            if activation_fn:
              x1, x2 = activation_fn(x1), tf.sigmoid(x2)
            else:
              x2 = tf.sigmoid(x2)
            x = x1 * x2

        # Apply residual to last layer  before the last nonlinearity
        if residual and (layer_idx == stacked_layers - 1):
          with tf.variable_scope("Residual"):
            # Don't upsample residual in time
            if stride[0] == 1 and stride[1] == 1:
              channels_in = x0.get_shape().as_list()[-1]
              # Make n_channels match for residual
              if channels != channels_in:
                x0 = slim.conv2d(
                    inputs=x0,
                    stride=[1, 1],
                    kernel_size=[1, 1],
                    num_outputs=channels,
                    normalizer_fn=None,
                    activation_fn=None,
                    biases_initializer=tf.zeros_initializer,
                    scope=scope + "_residual")
                x += x0
              else:
                x += x0
        if activation_fn and not gated:
          x = activation_fn(x)
    return x
def conv2d ( x , kernel_size , stride , channels , is_training , scope = "conv2d" , batch_norm = False , residual = False , gated = False , activation_fn = tf . nn . relu , resize = False , transpose = False , stacked_layers = 1 ) : # For residual x0 = x # Choose convolution function conv_fn = slim . conv2d_transpose if transpose else slim . conv2d # Double output channels for gates num_outputs = channels * 2 if gated else channels normalizer_fn = slim . batch_norm if batch_norm else None with tf . variable_scope ( scope + "_Layer" ) : # Apply a stack of convolutions Before adding residual for layer_idx in range ( stacked_layers ) : with slim . arg_scope ( slim_batchnorm_arg_scope ( is_training , activation_fn = None ) ) : # Use interpolation to upsample instead of conv_transpose if transpose and resize : unused_mb , h , w , unused_ch = x . get_shape ( ) . as_list ( ) x = tf . image . resize_images ( x , size = [ h * stride [ 0 ] , w * stride [ 1 ] ] , method = 0 ) stride_conv = [ 1 , 1 ] else : stride_conv = stride x = conv_fn ( inputs = x , stride = stride_conv , kernel_size = kernel_size , num_outputs = num_outputs , normalizer_fn = normalizer_fn , biases_initializer = tf . zeros_initializer ( ) , scope = scope ) if gated : with tf . variable_scope ( "Gated" ) : x1 , x2 = x [ : , : , : , : channels ] , x [ : , : , : , channels : ] if activation_fn : x1 , x2 = activation_fn ( x1 ) , tf . sigmoid ( x2 ) else : x2 = tf . sigmoid ( x2 ) x = x1 * x2 # Apply residual to last layer  before the last nonlinearity if residual and ( layer_idx == stacked_layers - 1 ) : with tf . variable_scope ( "Residual" ) : # Don't upsample residual in time if stride [ 0 ] == 1 and stride [ 1 ] == 1 : channels_in = x0 . get_shape ( ) . as_list ( ) [ - 1 ] # Make n_channels match for residual if channels != channels_in : x0 = slim . conv2d ( inputs = x0 , stride = [ 1 , 1 ] , kernel_size = [ 1 , 1 ] , num_outputs = channels , normalizer_fn = None , activation_fn = None , biases_initializer = tf . zeros_initializer , scope = scope + "_residual" ) x += x0 else : x += x0 if activation_fn and not gated : x = activation_fn ( x ) return x