tf.nn.conv2d is implemented inside TensorFlow convolution function, it is a method to build a more central convolutional neural network.
Function format:
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu = Noen, name = None)
Parameter Description:
The first parameter input: refers to the need to do the convolution of the input image, it requires the Tensor is a 4-dimensional, and the type is one float32 float64, shape of [batch, in_height, in_width, in_channels]:
batch: batch a number of training images
in_height: height of input image
in_width: width of the input image
in_channels: number of channels, compared with the gradation of input image 1, a color image is 3
second parameter filter: CNN convolution a convolution kernel in the network, a requirement is that the Tensor, and input types of the same type, as Shape [filter_height, filter_width, in_channels, out_channels ]:filter_height: height of the convolution kernel
filter_width: the width of the convolution kernel
in_channels: number of channels of the image, in_channels the same input
out_channels: the number of convolution
third parameter strides: step on the different dimensions, a length a 4-dimensional vector, [1, strides, strides, 1], a first dimension and a dimension of the final numerical requirements must be 1. Because the convolution step only the layer length and width of the active matrix.The fourth parameter padding: string type, convolution representation, consider whether the boundary value of "SAME" and "VALID", "SAME" boundary is considered, insufficient time is filled with the surrounding, "VALID" boundary is not considered .
The fifth parameter use_cudnn_on_gpu: bool type, whether cudnn acceleration, the default is true.
This function returns is that we often say that the feature map, shape still [batch, height, width, channels] in this form.
Will be described below tf.nn.conv2d () function usage by way of example:
Case1 : the input is a 3 * 3 size image, the image number of channels is 5, a convolution kernel size is 1 * 1, the number is 1, the step size is [1,1,1,1], to give a final 3 * feature 3 of the map. FIG 1 is a shape for the final output [1,3,3,1] of the tensor.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import tensorflow as tf
Input = tf.Variable(tf.random_normal([1, 3, 3, 5]))
Filter = tf.Variable(tf.random_normal([1, 1, 5, 1]))
conv1 = tf.nn.conv2d(Input, Filter, strides=[1, 1, 1, 1], padding='VALID')
with tf.Session() as sess:
# 初始化变量
op_init = tf.global_variables_initializer()
sess.run(op_init)
print(sess.run(conv1))
operation result:
Case2 : the input is a 3 * 3 size image, the image number of channels is 5, a convolution kernel size is 2 * 2, the number is 1, the step size is [1,1,1,1], padding is set to "VALID "and finally get a feature map 2 * 2. FIG 1 is a shape for the final output [1,2,2,1] of the tensor.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import tensorflow as tf
Input = tf.Variable(tf.random_normal([1, 3, 3, 5]))
Filter = tf.Variable(tf.random_normal([2, 2, 5, 1]))
conv2 = tf.nn.conv2d(Input, Filter, strides=[1, 1, 1, 1], padding='VALID')
with tf.Session() as sess:
# 初始化变量
op_init = tf.global_variables_initializer()
sess.run(op_init)
print(sess.run(conv2))
operation result:
Case3 : case2 example of the padding value to "SAME", that is, considering the boundary. (Input is a 3 * 3 size image, the image number of channels is 5, a convolution kernel size is 2 * 2, the number is 1, the step size is [1,1,1,1], padding is set to "SAME" Finally, to obtain a 3 * 3 Photo feature map.1 shape of a final output is [1,3,3,1] of the tensor.)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import tensorflow as tf
Input = tf.Variable(tf.random_normal([1, 3, 3, 5]))
Filter = tf.Variable(tf.random_normal([2, 2, 5, 1]))
conv3= tf.nn.conv2d(Input, Filter, strides=[1, 1, 1, 1], padding='SAME')
with tf.Session() as sess:
# 初始化变量
op_init = tf.global_variables_initializer()
sess.run(op_init)
print(sess.run(conv3))
Operating results as follows:
Case 4 : Input 2 3 * 3 is the size of the picture, the number of channels is an image 5, a convolution kernel size is 2 * 2, the number is 1, the step size is [1,1,1,1], padding is set to "SAME "Finally, get two feature map 3 * 3's. FIG 1 is a shape for the final output [2,3,3,1] of the tensor.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import tensorflow as tf
Input = tf.Variable(tf.random_normal([2, 3, 3, 5]))
Filter = tf.Variable(tf.random_normal([2, 2, 5, 1]))
conv4 = tf.nn.conv2d(Input, Filter, strides=[1, 1, 1, 1], padding='SAME')
with tf.Session() as sess:
# 初始化变量
op_init = tf.global_variables_initializer()
sess.run(op_init)
print(sess.run(conv4))
operation result:
case5 : Enter 4 3 * 3 is the size of the picture, the number of channels is an image 5, a convolution kernel size is 2 * 2, the number is 4, the step size is [1,1,1,1], padding is set to "SAME "Finally, each image to get four of the feature map 3 * 3. FIG 1 is a shape for the final output [4,3,3,4] of the tensor.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import tensorflow as tf
Input = tf.Variable(tf.random_normal([4, 3, 3, 5]))
Filter = tf.Variable(tf.random_normal([2, 2, 5, 4]))
conv5 = tf.nn.conv2d(Input, Filter, strides=[1, 1, 1, 1], padding='SAME')
with tf.Session() as sess:
# 初始化变量
op_init = tf.global_variables_initializer()
sess.run(op_init)
print(sess.run(conv5))
Operating results as follows:
[[[[ 4.1313605 -4.5319715 4.3040133 -0.13911057]
[-10.3389225 -2.2463617 -3.033617 2.0877244 ]
[ -2.3154557 5.4515543 -4.647153 -3.0869713 ]]
[[ 3.0420232 -0.87613493 4.6381464 0.90558195]
[ 3.7932742 -2.4520369 -0.7195463 4.9921722 ]
[ 1.5384624 0.11533099 -2.408429 5.3733883 ]]
[[ 0.45401835 -2.7483764 0.07065094 0.443908 ]
[ -6.8117185 2.2884533 -5.3677235 1.5834118 ]
[ -1.2048031 1.848783 -1.6733127 -1.7782905 ]]]
[[[ 0.5970616 0.77205956 -3.116805 -2.0577238 ]
[ -1.9527029 0.34587446 -5.7365794 -7.39325 ]
[ 2.9361765 -3.504585 4.057106 1.7304868 ]]
[[ 7.262891 -2.492215 4.7126684 1.7249267 ]
[ -7.4239445 2.9972248 4.2400084 0.9729483 ]
[ 1.9529393 3.4738922 0.24985534 -2.922786 ]]
[[ -0.3828507 0.49657637 0.47466695 0.8126482 ]
[ -0.3671472 -0.67494106 0.46129555 -0.9638461 ]
[ 1.6664319 0.885748 0.31974202 -1.9972321 ]]]
[[[ -1.7906806 -1.0376648 1.7166338 -1.0403266 ]
[ 2.5069232 4.0962963 -1.6884253 -0.23492575]
[ -0.3881849 -2.4799151 3.8491216 -2.5564618 ]]
[[ -3.5605621 1.6819414 -4.8645535 -1.9251393 ]
[ -1.8077193 -5.6664057 4.750779 1.2238139 ]
[ 2.346087 -6.2254734 5.1787786 4.3055882 ]]
[[ 0.21012396 -2.145918 1.358845 -0.25860584]
[ -2.3559752 4.263964 -0.51586103 -6.9163604 ]
[ 5.504827 4.0707703 0.11547554 -7.818963 ]]]
[[[ 3.716207 0.63655686 -1.2434999 -4.472955 ]
[ 2.067041 2.0510454 4.2357826 -4.159449 ]
[ 0.63638914 1.9863756 0.42491168 0.13413942]]
[[ 2.5624473 5.041215 3.689196 -1.1119944 ]
[ 4.470556 6.4554853 -7.154124 3.396954 ]
[ -4.9033055 -4.636659 3.7072423 -0.6310719 ]]
[[ -0.92771626 5.868825 -3.1153893 -3.9012384 ]
[ 3.4494157 -2.7036839 5.6135087 3.6358144 ]
[ 1.0283424 -4.246024 0.7325883 0.4899721 ]]]]