【小白学习keras教程】三、Kears中常见模型层Padding、Conv2D、MaxPooling2D、Flatten

@Author:Runsen

@[toc]

基础知识

  • 图像格式数据的输入通常是张量流中的四维数组


(数值、宽度、高度、深度)

  • num_instance:数据实例数。通常指定为,以适应数据大小的波动
  • 宽度:图像的宽度
  • 高度:图像的高度
  • 深度:图像的深度。彩色图像的深度通常为3(RGB为3个通道)。黑白图像的深度通常为1(只有一个通道)

from matplotlib import pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras import optimizers
from tensorflow.keras.layers import Dense, Activation, Flatten, Conv2D, MaxPooling2D, AveragePooling2D, GlobalMaxPooling2D, ZeroPadding2D, Input
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import cifar10, mnist
from tensorflow.keras.preprocessing import image

(x_train, y_train), _ = cifar10.load_data()
print(x_train[0].shape)
# showing figures
fig = plt.figure(figsize = (10, 10))
for i in range(9):
  fig.add_subplot(3, 3, i+1)
  plt.imshow(x_train[i])

plt.show()

(x_train, y_train), _ = mnist.load_data()
print(x_train[0].shape)   # Images in mnist are 2-D since they don't have color channel
# showing figures
fig = plt.figure(figsize = (10, 10))
for i in range(9):
  fig.add_subplot(3, 3, i+1)
  plt.imshow(x_train[i])

plt.show()

1.Padding

  • 两种类型的Padding选项
  • ’valid’:无填充(删除最右边的列和最下面的行)
  • ’same’:填充大小**p=[k/2]**当内核大小=k时
  • 定制填充物可提供零填充nD
# when padding = 'valid'
model = Sequential()
model.add(Conv2D(input_shape = (10, 10, 3), filters = 10, kernel_size = (3,3), strides = (1,1), padding = 'valid'))
print(model.output_shape)

# when padding = 'same'
model = Sequential()
model.add(Conv2D(input_shape = (10, 10, 3), filters = 10, kernel_size = (3,3), strides = (1,1), padding = 'same'))
print(model.output_shape)

# user-customized padding
input_layer = Input(shape = (10, 10, 3))
padding_layer = ZeroPadding2D(padding = (1,1))(input_layer)

model = Model(inputs = input_layer, outputs = padding_layer)
print(model.output_shape)

2. FIlter/kernels

  • 可以指定过滤器的数量
  • 过滤器数量等于下一层的深度
# when filter size = 10
model = Sequential()
model.add(Conv2D(input_shape = (10, 10, 3), filters = 10, kernel_size = (3,3), strides = (1,1), padding = 'same'))
# you could see that the depth of output = 10
print(model.output_shape)

# when filter size = 20
model = Sequential()
model.add(Conv2D(input_shape = (10, 10, 3), filters = 20, kernel_size = (3,3), strides = (1,1), padding = 'same'))
#你可以看到输出的深度=20
print(model.output_shape)

3.Pooling

  • 通常,最大池应用于矩形区域
  • 池大小、填充类型和跨步可以设置为类似于卷积层
model = Sequential()
model.add(Conv2D(input_shape = (10, 10, 3), filters = 10, kernel_size = (3,3), strides = (1,1), padding = 'same'))
print(model.output_shape)

# 如果未定义“步长”参数,步长等于“池大小”
model.add(MaxPooling2D(pool_size = (2,2), padding = 'valid'))
print(model.output_shape)

model = Sequential()
model.add(Conv2D(input_shape = (10, 10, 3), filters = 10, kernel_size = (3,3), strides = (1,1), padding = 'same'))
model.add(MaxPooling2D(pool_size = (2,2), strides = (1,1), padding = 'valid'))
print(model.output_shape)

model = Sequential()
model.add(Conv2D(input_shape = (10, 10, 3), filters = 10, kernel_size = (3,3), strides = (1,1), padding = 'same'))
model.add(AveragePooling2D(pool_size = (2,2), padding = 'valid'))
print(model.output_shape)

在这里插入图片描述

# globalMapPooling在深度为1的整个通道上执行最大池model = Sequential()
model.add(Conv2D(input_shape = (10, 10, 3), filters = 10, kernel_size = (3,3), strides = (1,1), padding = 'same'))
model.add(GlobalMaxPooling2D())
# 当筛选器数=10时,将返回10个值作为globalMapPooling2D的结果
print(model.output_shape)

在这里插入图片描述

4.Flattening

  • 要连接到完全连接的层(密集层),卷积/池层应**“扁平化”**
  • 结果形状=(实例数,宽X高X深)
model = Sequential()
model.add(Conv2D(input_shape = (10, 10, 3), filters = 10, kernel_size = (3,3), strides = (1,1), padding = 'same'))
print(model.output_shape)

model.add(Flatten())
print(model.output_shape)

5.Fully Connected (Dense)

  • 压平层后,可增加全连接层
  • 应指定输出形状(节点数)
model = Sequential()
model.add(Conv2D(input_shape = (10, 10, 3), filters = 10, kernel_size = (3,3), strides = (1,1), padding = 'same'))
model.add(Flatten())
model.add(Dense(50))
print(model.output_shape)

(完)