tensorflow学习笔记-卷积,反卷积,空洞卷积

这篇博客详细介绍了卷积神经网络中的卷积操作,包括基本的卷积函数,解释了输入和输出维度的关系。接着探讨了反卷积的概念,指出在反卷积中滤波器的输入输出维度位置调换,并强调了输出形状的正确计算方式。此外,还讨论了空洞卷积(Dilated Convolution),说明其在保持输出尺寸不变的同时增大感受野的功能,以及如何通过调整采样步长来应用空洞卷积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

卷积

卷积函数为:

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None,
           data_format=None, name=None)

input为一个4-D的输入,fileter为滤波器(卷积核),4-D,通常为[height, width, input_dim, output_dim],height, width分别表示卷积核的高,宽.input_dim, output_dim分别表式输入维度,输出维度.

import tensorflow as tf

x1 = tf.constant(1.0, shape=[1, 5, 5, 3])
x2 = tf.constant(1.0, shape=[1, 6, 6, 3])
kernel = tf.constant(1.0, shape=[3, 3, 3, 1])
y1 = tf.nn.conv2d(x1, kernel, strides=[1, 2, 2, 1], padding="SAME")
y2 = tf.nn.conv2d(x2, kernel, strides=[1, 2, 2, 1], padding="SAME")

sess = tf.Session()
tf.global_variables_initializer().run(session=sess)
x1_cov,  x2_cov = sess.run([y1, y2])

print(x1_cov.shape)
print(x2_cov.shape)

这里写图片描述

反卷积

反卷积函数为:

tf.nn.conv2d_transpose(value,
                     filter,
                     output_shape,
                     strides,
                     padding="SAME",
                     data_format="NHWC",
                     name=None)

output_shape为输出shape,由于是卷积的反过程,因此这里filter的输入输出为维度位置调换,[height, width, output_channels, in_channels].

import tensorflow as tf

x1 = tf.constant(1.0, shape=[1, 5, 5, 3])
x2 = tf.constant(1.0, shape=[1, 6, 6, 3])
kernel = tf.constant(1.0, shape=[3, 3, 3, 1])
y1 = tf.nn.conv2d(x1, kernel, strides=[1, 2, 2, 1], padding="SAME")
y2 = tf.nn.conv2d(x2, kernel, strides=[1, 2, 2, 1], padding="SAME")
y3 = tf.nn.conv2d_transpose(y1,kernel,output_shape=[1,5,5,3],
    strides=[1,2,2,1],padding="SAME")
y4 = tf.nn.conv2d_transpose(y2,kernel,output_shape=[1,6,6,3],
    strides=[1,2,2,1],padding="SAME")
sess = tf.Session()
tf.global_variables_initializer().run(session=sess)
x1_cov,  x2_cov,y1_decov,y2_decov = sess.run([y1, y2,y3,y4])

print(x1_cov.shape)
print(x2_cov.shape)
print(y1_decov.shape)
print(y2_decov.shape)

这里写图片描述
注意output_shape需要根据input shape,filter shape,以及output_dim指定,但不可以随意指定,例如

y4 = tf.nn.conv2d_transpose(y2,kernel,output_shape=[1,5,5,3],
    strides=[1,2,2,1],padding="SAME")

得到y4 shape为(1, 5, 5, 3),但是若设置output_shape=[1,7,7,3],

y4 = tf.nn.conv2d_transpose(y2,kernel,output_shape=[1,7,7,3],
    strides=[1,2,2,1],padding="SAME")

则出错:

InvalidArgumentError (see above for traceback): Conv2DSlowBackpropInput: Size of out_backprop doesn’t match computed: actual = 3, computed = 4

 [[Node: conv2d_transpose_1 = Conv2DBackpropInput[T=DT_FLOAT, data_format="NHWC", padding="SAME", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/gpu:0"](conv2d_transpose_1/output_shape, Const_2, Conv2D_1)]]
 [[Node: conv2d_transpose/_5 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_23_conv2d_transpose", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

空洞卷积(dilated convolution):

空洞卷积函数为:

tf.nn.atrous_conv2d(value, filters, rate, padding, name=None)

fileter为滤波器(卷积核),格式与卷积相同,为[height, width, input_dim, output_dim].rate为对输入的采样步长(sample stride).

x1 = tf.constant(1.0, shape=[1, 5, 5, 3])
kernel = tf.constant(1.0, shape=[3, 3, 3, 1])
y5=tf.nn.atrous_conv2d(x1,kernel,10,'SAME')

y5.shape为(1, 5, 5, 1).

完整调用代码为:

import tensorflow as tf

x1 = tf.constant(1.0, shape=[1, 5, 5, 3])
x2 = tf.constant(1.0, shape=[1, 6, 6, 3])
kernel = tf.constant(1.0, shape=[3, 3, 3, 1])
y1 = tf.nn.conv2d(x1, kernel, strides=[1, 2, 2, 1], padding="SAME")
y2 = tf.nn.conv2d(x2, kernel, strides=[1, 2, 2, 1], padding="SAME")
y3 = tf.nn.conv2d_transpose(y1,kernel,output_shape=[1,5,5,3],
    strides=[1,2,2,1],padding="SAME")
y4 = tf.nn.conv2d_transpose(y2,kernel,output_shape=[1,6,6,3],
    strides=[1,2,2,1],padding="SAME")

y5=tf.nn.atrous_conv2d(x1,kernel,10,'SAME')
sess = tf.Session()
tf.global_variables_initializer().run(session=sess)
x1_cov,  x2_cov,y1_decov,y2_decov,y5_dicov = sess.run([y1, y2,y3,y4,y5])

print(x1_cov.shape)
print(x2_cov.shape)
print(y1_decov.shape)
print(y2_decov.shape)
print(y5_dicov.shape)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值