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
主要相同点
- input:输入,一个4维张量,默认输入格式为[batch, in_height, in_width, in_channels]
- strides:卷积步长,一个长度为4的一维数组
- padding:卷积步长填充方式,“SAME"或者"VALID”
- data_format:数据的输入格式,默认为NHWC——[batch_size, in_height, in_width, in_channels],
也支持NCHW——[batch_size, in_channels, in_height, in_width]
主要不同点
- 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] - tf.nn.conv2d_transpose中多了一个参数output_shape,通过指定该参数可以固定反卷积输出的维度
2.3.2 slim.conv2d和slim.conv2d_transpose
两者使用基本相同
- input:输入,一个4维张量,默认输入格式为[batch, in_height, in_width, in_channels]
- num_outputs:输出特征维度的数量
- kernel_size:卷积核尺寸,即卷积核的高和宽
- stride:可以是一个正整数,让所有的步长都等于该正整数
- padding:卷积步长填充方式,“SAME"或者"VALID”
- 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得到输出的高和宽