卷积层的输入是一张或者多张图片,图片有可能是单通道或者多通道,但不管是单通道还是多通道,经过卷积层后,得到的输出map都是单通道的特征图。
1、假如输入1张图片,通道数为5,那么在设计卷积核的时候,对应的卷积核的通道数也因该是5
import tensorflow as tf
input = tf.Variable(tf.random_normal([1, 3, 3, 5]))
filter = tf.Variable(tf.random_normal([1, 1, 5, 1]))
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(input)
print("*********************")
print(filter)
print("*********************")
print(op)
print("*********************")
输出:
<tf.Variable 'Variable:0' shape=(1, 3, 3, 5) dtype=float32_ref>
*********************
<tf.Variable 'Variable_1:0' shape=(1, 1, 5, 1) dtype=float32_ref>
*********************
Tensor("Conv2D:0", shape=(1, 3, 3, 1), dtype=float32)
*********************
2、如果要输出多张特征图,则需要更改卷积核的个数,假设卷积核的数量为10
import tensorflow as tf
input = tf.Variable(tf.random_normal([1, 3, 3, 5]))
filter = tf.Variable(tf.random_normal([1, 1, 5, 10]))
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(input)
print("*********************")
print(filter)
print("*********************")
print(op)
print("*********************")
输出:
<tf.Variable 'Variable:0' shape=(1, 3, 3, 5) dtype=float32_ref>
*********************
<tf.Variable 'Variable_1:0' shape=(1, 1, 5, 10) dtype=float32_ref>
*********************
Tensor("Conv2D:0", shape=(1, 3, 3, 10), dtype=float32)
*********************