深度学习原理与框架- tf.nn.conv2d_transpose(反卷积操作)

反卷积操作:

首先对需要进行维度扩张的feature_map 进行补零操作,然后使用3*3的卷积核,进行卷积操作,使得其维度进行扩张,图中可以看出,2*2的feature经过卷积变成了4*4.    3*3的卷积经过扩张以后形成了5*5

                        

      feature_map为偶数                                              feature_map为偶数

 代码:主函数

  with tf.variable_scope('deconv1'):
       # [4, 4, 128, 256]卷积核的大小,4和4表示卷积核的长度,128表示卷积核的个数,256表示卷积核的通道数, [self.batch_size, 64, 64, 128]表示输出层的维度, 2表示1/2的步长
       x = deconv_layer(x, [4, 4, 128, 256], [self.batch_size, 64, 64, 128], 2)
       x = batch_normalize(x, is_training)
       x = tf.nn.relu(x)

调用函数deconv_layer, 个人理解,这里stride的大小,表示的不是补零后卷积核的步长,而是对原始feature的补零的倍数,即扩张的大小

def deconv_layer(x, filter_shape, output_shape, stride):
    # 构造卷积
    filters = tf.get_variable(
        name = 'weight',
        shape=filter_shape,
        dtype=tf.float32,
        initializer=tf.truncated_normal_initializer(stddev=0.1),
        trainable=True
    )
    return tf.nn.conv2d_transpose(x, filters, output_shape, [1, stride, stride, 1])

猜你喜欢

转载自www.cnblogs.com/my-love-is-python/p/10617892.html