转载自:
https://blog.csdn.net/zSean/article/details/77837589
卷积函数是在一批图像上扫描的二维过滤器。卷积函数定义在tensorflow-1.1.0/tensorflow/python/ops下的nn_impl.py和nn_ops.py文件中。
(1)计算N维卷积的和的函数tf.nn.convolution()
- tf.nn.convolution(input,filter,padding,strides=None,dilation_rate=None,name=None,data_format=None)
- tf.nn.conv2d(input,filter, strides, padding, use_cudnn_on_gpu=None, name=None)
filter:为一个tensor,数据类型必须是与input输入的数据类型相同;
strides:一个长度是4的一维整数类型数组,每一维度对应的是input中每一维的对应移动步数;如strides[1]对应input[1]的移动步数;
padding:一个字符串,取值为SAME或者VALID;‘SAME'适用于全尺寸操作,即输入数据维度和输出数据维度相同;'VALID'适用于部分窗口;
use_cudnn_on_gpu:一个可选布尔值,默认情况下是True;
name:为这个操作取一个名字;
- import tensorflow as tf
- import os
- import numpy as np
- input_data= tf.Variable(np.random.rand(10,9,9,3),dtype=np.float32)
- filter_data=tf.Variable(np.random.rand(2,2,3,4),dtype=np.float32)
- y = tf.nn.conv2d(input_data,filter_data,strides=[1,3,3,1],padding='SAME')
- print('输入的结果为:', y)
filter为卷积核,filter[filter_height, filter_width, in_channels, out_channels]的shape,这个是以conv2d为例;
其中对应的含义分别为:[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数]
在使用过程中,通常不对input的第一维和第四维进行卷积操作,所以通常strides一般为[1,X,X,1]
- 输入的结果为: Tensor("Conv2D:0", shape=(10, 3, 3, 4), dtype=float32)
- import tensorflow as tf
- import os
- import numpy as np
- input_data= tf.Variable(np.random.rand(10,9,9,3),dtype=np.float32)
- filter_data=tf.Variable(np.random.rand(2,2,3,4),dtype=np.float32)
- y = tf.nn.depthwise_conv2d(input_data,filter_data,strides=[1,3,3,1],padding='SAME')
- print('输入的结果为:', y)
filter的维度[filter_height, filter_width,in_channel, channel_multiplierl]
在通道in_channels上面的卷积深度是3,
将不同的卷积核独立地应用在in_channels的每条通道上,
然后将所有的结果进行汇总,输出通道的总数,in_channel*channel_multiplier
- 输入的结果为: Tensor("depthwise:0", shape=(10, 3, 3, 12), dtype=float32)
depthwise_filter:为一个张量,数据维度是四维[filter_height, filter_width, in_channels, channel_multiplier]
pointwise_filter:一个四维的张量,数据维度是四维[1,1,channel_multipliter*in_channels,out_channels]。
pointwise_filter是在depthwise_filter卷积之后的混合卷积;
strides:一个长度是4的一维整数类型数组,每一个维度对应的是input中每一维的对应移动步数
- import tensorflow as tf
- import os
- import numpy as np
- input_data= tf.Variable(np.random.rand(1,9,9,3),dtype=np.float32)
- depthwise_filter=tf.Variable(np.random.rand(2,2,3,4),dtype=np.float32)
- pointwise_filter = tf.Variable(np.random.rand(1,1,12,20),dtype=np.float32)
- y = tf.nn.separable_conv2d(input_data,depthwise_filter,pointwise_filter,strides=[1,3,3,1],padding='SAME')
- print('输入的结果为:', y)
- 输入的结果为: Tensor("separable_conv2d:0", shape=(1, 3, 3, 20), dtype=float32)
(5)函数tf.nn.atrous_conv2d(value, filters, rate, padding, name=None)计算Atrous卷积,称为扩张卷积
(6)函数tf.nn.conv2d_transpose(value, filter, output_shape,strides, padding='SAME',data_format='NHWC',name=None)为conv2d的转置。
(7)函数tf.nn.conv1d(value, filters, stride, padding , use_cudnn_on_gpu=None,data_format=None,name=None)与二维卷积类似
该函数用来计算给定三维的输入和过滤器的情况下的一维卷积。
输入为三维,[batch, in_width, in_channels]
卷积核的维度为三维,少了一维filter_heigth,如[filter_width,in_channels, out_channels].
stride是一个正整数,代表卷积核向右移动每一步的长度。
(8)函数tf.nn.conv3d(input, filter, strides, padding, name=None)与二维卷积类似。
用来计算给定五维的输入和过滤器的情况下的三维卷积
与二维卷积相对比:
input的shape中多了一维in_depth,形状为[batch, in_depth, in_height, in_width, in_channels]
filter的shape中多了一维filter_depth,[filter_depth, filter_height, in_channel,channel_multiplierl]构成卷积核大小
strides的shape中多了一维strides_depth, [strides_batch, strides_depth, strides_height, strides_width, strides_channel]
(9)函数tf.nn.conv3d_tranpose(value, filter, output_shape, strides, padding='SAME',name=None)与二维反卷积类似。