(
    inputs,
    num_output_channels,
    kernel_size,
    scope,
    stride=[1, 1],
    padding="SAME",
    use_xavier=True,
    stddev=1e-3,
    weight_decay=0.0,
    activation_fn=tf.nn.relu,
    bn=False,
    bn_decay=None,
    is_training=None,
)
2D convolution transpose with non-linear operation.

  Args:
    inputs: 4-D tensor variable BxHxWxC
    num_output_channels: int
    kernel_size: a list of 2 ints
    scope: string
    stride: a list of 2 ints
    padding: 'SAME' or 'VALID'
    use_xavier: bool, use xavier_initializer if true
    stddev: float, stddev for truncated_normal init
    weight_decay: float
    activation_fn: function
    bn: bool, whether to use batch norm
    bn_decay: float or float tensor variable in [0,1]
    is_training: bool Tensor variable

  Returns:
    Variable tensor

  Note: conv2d(conv2d_transpose(a, num_out, ksize, stride), a.shape[-1], ksize, stride) == a
2D convolution transpose with non-linear operation.
2D convolution transpose with non - linear operation .
def conv2d_transpose(
    inputs,
    num_output_channels,
    kernel_size,
    scope,
    stride=[1, 1],
    padding="SAME",
    use_xavier=True,
    stddev=1e-3,
    weight_decay=0.0,
    activation_fn=tf.nn.relu,
    bn=False,
    bn_decay=None,
    is_training=None,
):
    """ 2D convolution transpose with non-linear operation.

  Args:
    inputs: 4-D tensor variable BxHxWxC
    num_output_channels: int
    kernel_size: a list of 2 ints
    scope: string
    stride: a list of 2 ints
    padding: 'SAME' or 'VALID'
    use_xavier: bool, use xavier_initializer if true
    stddev: float, stddev for truncated_normal init
    weight_decay: float
    activation_fn: function
    bn: bool, whether to use batch norm
    bn_decay: float or float tensor variable in [0,1]
    is_training: bool Tensor variable

  Returns:
    Variable tensor

  Note: conv2d(conv2d_transpose(a, num_out, ksize, stride), a.shape[-1], ksize, stride) == a
  """
    with tf.variable_scope(scope) as sc:
        kernel_h, kernel_w = kernel_size
        num_in_channels = inputs.get_shape()[-1].value
        kernel_shape = [
            kernel_h,
            kernel_w,
            num_output_channels,
            num_in_channels,
        ]  # reversed to conv2d
        kernel = _variable_with_weight_decay(
            "weights",
            shape=kernel_shape,
            use_xavier=use_xavier,
            stddev=stddev,
            wd=weight_decay,
        )
        stride_h, stride_w = stride

        # from slim.convolution2d_transpose
        def get_deconv_dim(dim_size, stride_size, kernel_size, padding):
            dim_size *= stride_size

            if padding == "VALID" and dim_size is not None:
                dim_size += max(kernel_size - stride_size, 0)
            return dim_size

        # caculate output shape
        batch_size = inputs.get_shape()[0].value
        height = inputs.get_shape()[1].value
        width = inputs.get_shape()[2].value
        out_height = get_deconv_dim(height, stride_h, kernel_h, padding)
        out_width = get_deconv_dim(width, stride_w, kernel_w, padding)
        output_shape = [batch_size, out_height, out_width, num_output_channels]

        outputs = tf.nn.conv2d_transpose(
            inputs, kernel, output_shape, [1, stride_h, stride_w, 1], padding=padding
        )
        biases = _variable_on_cpu(
            "biases", [num_output_channels], tf.constant_initializer(0.0)
        )
        outputs = tf.nn.bias_add(outputs, biases)

        if bn:
            outputs = batch_norm_for_conv2d(
                outputs, is_training, bn_decay=bn_decay, scope="bn"
            )

        if activation_fn is not None:
            outputs = activation_fn(outputs)
        return outputs
def conv2d_transpose ( inputs , num_output_channels , kernel_size , scope , stride = [ 1 , 1 ] , padding = "SAME" , use_xavier = True , stddev = 1e-3 , weight_decay = 0.0 , activation_fn = tf . nn . relu , bn = False , bn_decay = None , is_training = None , ) : with tf . variable_scope ( scope ) as sc : kernel_h , kernel_w = kernel_size num_in_channels = inputs . get_shape ( ) [ - 1 ] . value kernel_shape = [ kernel_h , kernel_w , num_output_channels , num_in_channels , ] # reversed to conv2d kernel = _variable_with_weight_decay ( "weights" , shape = kernel_shape , use_xavier = use_xavier , stddev = stddev , wd = weight_decay , ) stride_h , stride_w = stride # from slim.convolution2d_transpose def get_deconv_dim ( dim_size , stride_size , kernel_size , padding ) : dim_size *= stride_size if padding == "VALID" and dim_size is not None : dim_size += max ( kernel_size - stride_size , 0 ) return dim_size # caculate output shape batch_size = inputs . get_shape ( ) [ 0 ] . value height = inputs . get_shape ( ) [ 1 ] . value width = inputs . get_shape ( ) [ 2 ] . value out_height = get_deconv_dim ( height , stride_h , kernel_h , padding ) out_width = get_deconv_dim ( width , stride_w , kernel_w , padding ) output_shape = [ batch_size , out_height , out_width , num_output_channels ] outputs = tf . nn . conv2d_transpose ( inputs , kernel , output_shape , [ 1 , stride_h , stride_w , 1 ] , padding = padding ) biases = _variable_on_cpu ( "biases" , [ num_output_channels ] , tf . constant_initializer ( 0.0 ) ) outputs = tf . nn . bias_add ( outputs , biases ) if bn : outputs = batch_norm_for_conv2d ( outputs , is_training , bn_decay = bn_decay , scope = "bn" ) if activation_fn is not None : outputs = activation_fn ( outputs ) return outputs