文章目录
一.数据输入。
在图像领域中,CNN接收数据格式是四维向量。
其中根据K.image_data_format的不同,接收的数据输入格式也不同。
- 当K.image_data_format()='channels_first’时,接收的数据输入格式为(batch_size, channels, width, height)
- 当K.image_data_format()='channels_last’时,接收的数据输入格式为(batch_size, width, height, channels)
其中,channels指 通道数.
灰度图是单通道,channels=1
彩色图是三通道,channels=3
假如为mnist数据,则有代码示例如下
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(-1,1,28,28)
x_test = X_test.reshape(-1,1,28,28)
input_shape = (1,28,28)
else:
x_train = x_train.reshape(-1,28,28,1)
x_test = x_test.reshape(-1,28,28,1)
input_shape = (28,28,1)
二.构建模型结构。
2.1 方案一:使用Sequential模型搭建CNN.
model = Sequential().
model.add(Conv2D(fileters = 32,
kernel_size=(3,3),
activation = 'relu',
input_shape = input_shape))
model.add(Conv2D(64,(3,3),activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.25)) # 表示25%的参数会被舍弃
model.add(Flatten())
model.add(Dense(128, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
2.2 方案二:进阶版,在Sequential初始化的时候 就全部构建好模型,而不是使用add方法一层一层叠加。
model = Sequential([
Conv2D(32, (3,3), padding='same',input_shape=(32,32,3),activation='relu'),
Conv2D(32, (3,3), activation='relu'),
MaxPooling2D(pool_size=(2,2)),
Dropout(0.25),
Conv2D(64, (3,3), padding='same',activation='relu'),
Conv2D(64, (3,3), activation='relu'),
MaxPooling2D(pool_size = (2,2)),
Dropout(0.25),
Flatten(),
Dense(512, activation = 'relu'),
Dropout(0.5),
Dense(10,activation='softmax')
])
三. 训练技巧
如何让模型更快地收敛 以及具有更好的泛化性能?
方法:使用keras自带的图像增强来对图像做一些变换。
举例,定义1个图像增强器,包含了范围20度内的随机旋转,正负15%的缩放 以及 随机的水平翻转。
datagen = ImageDataGenerator(
rotation_range = 20,
zoom_range = 0.15,
horizontal_flip = True
)
注意,通过ImageDataGenerator生成的数据需要使用model.fit_generator方法进行训练,其中workers表示多线程运算。
datagen.flow可以实现 按批次地生成训练所需数据。
model.fit_generator(datagen.flow(x_train, y_train,batch_size=64),
steps_per_epoch = 1000,
epochs = 2,
validation_data = (x_test, y_test),
workers = 4,
verbose = 1)