TensorFlow卷积与反卷积代码表示

一、代码表示

	print("way1--tf.nn.conv2d and tf.nn.conv2d_transpose: ")
    print("\nwhen input shape's height and width are both even numbers")
    """
    format: NHWC--batch_size, image_height, image_width, image_channels, 
    shape=[1, 5, 5, 3], this means a rgb image with size 5x5
    shape=[16, 224, 224, 3], this means 16 rgb images with size 224x224
    """
    x1 = tf.constant(1.0, shape=[1, 6, 6, 3])
    """
    format: kernel_height, kernel_width, input_channel, output_channel
    shape=[3, 3, 3, 1], this means a filter with size 3x3, and its input_channel is 3, and output_channel is 1
    shape=[7, 7, 3, 64], this means a filter with size 7x7, and its input_channel is 3, and output_channel is 64
    """
    kernel1 = tf.constant(1.0, shape=[3, 3, 3, 1])
    print("input: {}".format(x1))
    print("kernel: {}".format(kernel1))
    y1 = tf.nn.conv2d(x1, kernel1, strides=[1, 2, 2, 1], padding="SAME")
    print("convolution result: {}".format(y1))
    y1_ = tf.nn.conv2d_transpose(y1, kernel1, output_shape=[1, 6, 6, 3], strides=[1, 2, 2, 1], padding="SAME")
    print("convolution transpose result: {}".format(y1_))

    print("\nwhen input shape's height and width are both odd numbers")
    x2 = tf.constant(1.0, shape=[1, 5, 5, 3])
    kernel2 = tf.constant(1.0, shape=[3, 3, 3, 1])
    print("input: {}".format(x2))
    print("kernel: {}".format(kernel2))
    y2 = tf.nn.conv2d(x2, kernel2, strides=[1, 2, 2, 1], padding="SAME")
    print("convolution result: {}".format(y2))
    y2_ = tf.nn.conv2d_transpose(y2, kernel1, output_shape=[1, 5, 5, 3], strides=[1, 2, 2, 1], padding="SAME")
    print("convolution transpose result: {}".format(y2_))

    print("\n\nway2--tf.nn.conv2d and tf.nn.conv2d_transpose: ")
    print("\nwhen input shape's height and width are both even numbers")
    x3 = tf.constant(1.0, shape=[1, 6, 6, 3])
    kernel3 = [3, 3]
    print("input: {}".format(x3))
    y3 = slim.conv2d(x3, num_outputs=1, kernel_size=kernel3, stride=2, padding="SAME")
    print("convolution result: {}".format(y3))
    y3_ = slim.conv2d_transpose(y3, num_outputs=3, kernel_size=kernel3, stride=2, padding="SAME")
    print("convolution transpose result: {}".format(y3_))

    print("\nwhen input shape's height and width are both odd numbers")
    x4 = tf.constant(1.0, shape=[1, 5, 5, 3])
    kernel4 = [3, 3]
    print("input: {}".format(x4))
    y4 = slim.conv2d(x4, num_outputs=1, kernel_size=kernel4, stride=2, padding="SAME")
    print("convolution result: {}".format(y4))
    y4_ = slim.conv2d_transpose(y4, num_outputs=3, kernel_size=kernel4, stride=2, padding="SAME")
    print("convolution transpose result: {}".format(y4_))

二、相关解释

2.1 关于输入

在TensorFlow中,数据输入的默认格式是NHWC——[batch_size, height, width, channels]
在这里插入图片描述
这个表示定义一个尺寸为6x6的特征图,它的特征维度是3,同时该特征图值是1.0
扩展:当shape=[16, 224, 224, 3]时,表示16个尺寸为224x224的特征图,它的特征维度是3

2.2 关于卷积核与反卷积核

2.2.1 卷积核

在TensorFlow中,卷积核的默认格式是[kernel_height, kernel_width, in_channels, output_channels]
在这里插入图片描述
这个表示定义一个尺寸为3x3的卷积核,它的输入特征维度是3,输出特征维度是1
扩展:当shape=[7, 7, 3, 64]时,表示一个尺寸为7x7的卷积核,它的输入特征维度是3,输出特征维度是64

2.2.2 反卷积核

在TensorFlow中,反卷积核的默认格式是[kernel_height, kernel_width, output_channels, in_channels]
在这里插入图片描述
这个表示定义一个尺寸为3x3的反卷积核,它的输出特征维度是3,输入特征维度是1
扩展:当shape=[7, 7, 3, 64]时,表示一个尺寸为7x7的反卷积核,它的输出特征维度是3,输入特征维度是64

2.3 tf.nn.conv2d(transpose)和slim.conv2d(transpose)

2.3.1 tf.nn.conv2d和tf.nn.conv2d_transpose

主要相同点

  1. input:输入,一个4维张量,默认输入格式为[batch, in_height, in_width, in_channels]
  2. strides:卷积步长,一个长度为4的一维数组
  3. padding:卷积步长填充方式,“SAME"或者"VALID”
  4. data_format:数据的输入格式,默认为NHWC——[batch_size, in_height, in_width, in_channels],
    也支持NCHW——[batch_size, in_channels, in_height, in_width]

主要不同点

  1. filter:虽然都是4维张量,但是卷积核中各维度表示意义不同。
    在tf.nn.conv2d中,filter默认格式为[kernel_height, kernel_width, in_channels, output_channels]
    而在tf.nn.conv2d_transpose中,filter默认格式为[kernel_height, kernel_width, output_channels, in_channels]
  2. tf.nn.conv2d_transpose中多了一个参数output_shape,通过指定该参数可以固定反卷积输出的维度

2.3.2 slim.conv2d和slim.conv2d_transpose

两者使用基本相同

  1. input:输入,一个4维张量,默认输入格式为[batch, in_height, in_width, in_channels]
  2. num_outputs:输出特征维度的数量
  3. kernel_size:卷积核尺寸,即卷积核的高和宽
  4. stride:可以是一个正整数,让所有的步长都等于该正整数
  5. padding:卷积步长填充方式,“SAME"或者"VALID”
  6. data_format:数据的输入格式,默认为NHWC——[batch_size, in_height, in_width, in_channels],
    也支持NCHW——[batch_size, in_channels, in_height, in_width]

2.3.3 slim和tf.nn中的反卷积

tf.nn.conv2d_transpose当输入是奇数维度时,和slim.conv2d_transpose输出不一样,当是偶数时,两者输出的结果一样。
tf.nn.conv2d_transpose输出的结果由output_shape确定,slim.conv2d_transpose根据input_size*s得到输出的高和宽

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
深度可分离卷积(Depth-wise Separable Convolutions)常用于降低参数量、增加非线性和实现跨通道信息融合。在反卷积过程中,可以使用深度可分离卷积的相应代码进行操作。具体的代码实现如下: ```python import tensorflow as tf def depthwise_separable_conv2d(inputs, filters, kernel_size, strides, padding): # Depthwise Convolution depthwise_conv = tf.keras.layers.DepthwiseConv2D(kernel_size, strides=strides, padding=padding)(inputs) # Pointwise Convolution pointwise_conv = tf.keras.layers.Conv2D(filters, kernel_size=1, strides=1, padding='same')(depthwise_conv) return pointwise_conv def upsample(inputs, scale): # Upsample by using nearest neighbor interpolation upsampled = tf.keras.layers.UpSampling2D(size=scale, interpolation='nearest')(inputs) return upsampled # Example usage input_tensor = tf.keras.Input(shape=(32, 32, 3)) depthwise_separable = depthwise_separable_conv2d(input_tensor, filters=64, kernel_size=3, strides=1, padding='same') upsampled = upsample(depthwise_separable, scale=2) # Continue with other operations on the upsampled feature map ``` 上述代码中,`depthwise_separable_conv2d`函数实现了深度可分离卷积操作,根据输入的参数,先进行深度卷积(Depthwise Convolution),再进行逐点卷积(Pointwise Convolution)。`upsample`函数则使用最近邻插值法进行上采样操作。可以根据自己的需求调整参数和网络结构。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值