def deconv2d(self, filter_size, output_channels, stride=1, padding='SAME', activation_fn=tf.nn.relu, b_value=0.0,
                 s_value=1.0, bn=True, trainable=True):
        """
        2D Deconvolutional Layer
        :param filter_size: int. assumes square filter
        :param output_channels: int
        :param stride: int
        :param padding: 'VALID' or 'SAME'
        :param activation_fn: tf.nn function
        :param b_value: float
        :param s_value: float
        """
        self.count['deconv'] += 1
        scope = 'deconv_' + str(self.count['deconv'])
        with tf.variable_scope(scope):

            # Calculate the dimensions for deconv function
            batch_size = tf.shape(self.input)[0]
            input_height = tf.shape(self.input)[1]
            input_width = tf.shape(self.input)[2]

            if padding == "VALID":
                out_rows = (input_height - 1) * stride + filter_size
                out_cols = (input_width - 1) * stride + filter_size
            else:  # padding == "SAME":
                out_rows = input_height * stride
                out_cols = input_width * stride

            # Deconv function
            input_channels = self.input.get_shape()[3]
            output_shape = [filter_size, filter_size, output_channels, input_channels]
            w = self.weight_variable(name='weights', shape=output_shape, trainable=trainable)
            deconv_out_shape = tf.stack([batch_size, out_rows, out_cols, output_channels])
            self.input = tf.nn.conv2d_transpose(self.input, w, deconv_out_shape, [1, stride, stride, 1], padding)

            if bn is True:  # batch normalization
                self.input = self.batch_norm(self.input)
            if b_value is not None:  # bias value
                b = self.const_variable(name='bias', shape=[output_channels], value=b_value, trainable=trainable)
                self.input = tf.add(self.input, b)
            if s_value is not None:  # scale value
                s = self.const_variable(name='scale', shape=[output_channels], value=s_value, trainable=trainable)
                self.input = tf.multiply(self.input, s)
            if activation_fn is not None:  # non-linear activation function
                self.input = activation_fn(self.input)
        print(scope + ' output: ' + str(self.input.get_shape()))
def deconv2d ( self , filter_size , output_channels , stride = 1 , padding = 'SAME' , activation_fn = tf . nn . relu , b_value = 0.0 , s_value = 1.0 , bn = True , trainable = True ) : self . count [ 'deconv' ] += 1 scope = 'deconv_' + str ( self . count [ 'deconv' ] ) with tf . variable_scope ( scope ) : batch_size = tf . shape ( self . input ) [ 0 ] input_height = tf . shape ( self . input ) [ 1 ] input_width = tf . shape ( self . input ) [ 2 ] if padding == "VALID" : out_rows = ( input_height - 1 ) * stride + filter_size out_cols = ( input_width - 1 ) * stride + filter_size else : out_rows = input_height * stride out_cols = input_width * stride input_channels = self . input . get_shape ( ) [ 3 ] output_shape = [ filter_size , filter_size , output_channels , input_channels ] w = self . weight_variable ( name = 'weights' , shape = output_shape , trainable = trainable ) deconv_out_shape = tf . stack ( [ batch_size , out_rows , out_cols , output_channels ] ) self . input = tf . nn . conv2d_transpose ( self . input , w , deconv_out_shape , [ 1 , stride , stride , 1 ] , padding ) if bn is True : self . input = self . batch_norm ( self . input ) if b_value is not None : b = self . const_variable ( name = 'bias' , shape = [ output_channels ] , value = b_value , trainable = trainable ) self . input = tf . add ( self . input , b ) if s_value is not None : s = self . const_variable ( name = 'scale' , shape = [ output_channels ] , value = s_value , trainable = trainable ) self . input = tf . multiply ( self . input , s ) if activation_fn is not None : self . input = activation_fn ( self . input ) print ( scope + ' output: ' + str ( self . input . get_shape ( ) ) )
2D Deconvolutional Layer
        :param filter_size: int. assumes square filter
        :param output_channels: int
        :param stride: int
        :param padding: 'VALID' or 'SAME'
        :param activation_fn: tf.nn function
        :param b_value: float
        :param s_value: float