卷积、池化后的图像大小计算(附例子)

用CNN网络进行图片处理,就会遇到卷积、池化后的图像大小问题,一般搜到的答案是这样的:


对于初学者,看到这个公式的唯一疑问是:P值到底是多少?

在Tensoflow中,Padding有2个选型,'SAME'和'VALID' ,下面举例说明差别:

如果 Padding='SAME',输出尺寸为: W / S

import tensorflow as tf
input_image = tf.layers.Input(shape=[32, 32, 3], dtype=tf.float32)
conv0 = tf.layers.conv2d(input_image, 64, kernel_size=[3, 3], strides=[2, 2], padding='same')  # 32/2=16
conv1 = tf.layers.conv2d(input_image, 64, kernel_size=[5, 5], strides=[2, 2], padding='same')  
# kernel_szie不影响输出尺寸
print(conv0)  # shape=(?, 16, 16, 64)
print(conv1)  # shape=(?, 16, 16, 64)

如果 Padding='VALID',输出尺寸为:(W - F + 1) / S

import tensorflow as tf
input_image = tf.layers.Input(shape=[32, 32, 3], dtype=tf.float32)
conv0 = tf.layers.conv2d(input_image, 64, kernel_size=[3, 3], strides=[2, 2], padding='valid')  # (32-3+1)/2=15
conv1 = tf.layers.conv2d(input_image, 64, kernel_size=[5, 5], strides=[2, 2], padding='valid')  # (32-5+1)/2=14
print(conv0)  # shape=shape=(?, 15, 15, 64)
print(conv1)  # shape=(?, 14, 14, 64)

你可能感兴趣的:(基础知识,Convolution,Max_pooling,Python)