Convolutional Neural Networks: Application
Packages
首先是先引入了包文件
import math
import numpy as np
import h5py
import matplotlib.pyplot as plt
from matplotlib.pyplot import imread
import scipy
from PIL import Image
import pandas as pd
import tensorflow as tf
import tensorflow.keras.layers as tfl
from tensorflow.python.framework import ops
from cnn_utils import *
from test_utils import summary, comparator
主要的包是tensorflow
3.1 - Create the Sequential Model
TensorFlow 中的Keras包里面Sequential API可用于构建具有按顺序进行的层操作的简单模型。并且可以使用.add()方法递增地向Sequential模型添加层,或者使用.pop()方法删除层。我们可以将Sequential模型看作是一个层列表。与Python列表一样,顺序层是有序的,它们的指定顺序很重要。如果您的模型是非线性的,或者包含有多个输入或输出的层,那么序列模型就不是正确的选择!
对于Keras中的任何层构建,您都需要提前指定输入shape。这是因为在Keras中,W的shape是基于输入的shape。权重仅在模型第一次看到一些输入数据时创建。可以通过向Sequential构造函数传递层列表来创建顺序模型。
Exercise 1 - happyModel
代码段1
happy_model = happyModel()
# Print a summary for each layer
for layer in summary(happy_model):
print(layer)
output = [['ZeroPadding2D', (None, 70, 70, 3), 0, ((3, 3), (3, 3))],
['Conv2D', (None, 64, 64, 32), 4736, 'valid', 'linear', 'GlorotUniform'],
['BatchNormalization', (None, 64, 64, 32), 128],
['ReLU', (None, 64, 64, 32), 0],
['MaxPooling2D', (None, 32, 32, 32), 0, (2, 2), (2, 2), 'valid'],
['Flatten', (None, 32768), 0],
['Dense', (None, 1), 32769, 'sigmoid']]
comparator(summary(happy_model), output)
代码段2
def happyModel():
"""
Implements the forward propagation for the binary classification model:
ZEROPAD2D -> CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> FLATTEN -> DENSE
Note that for simplicity and grading purposes, you'll hard-code all the values
such as the stride and kernel (filter) sizes.
Normally, functions should take these values as function parameters.
Arguments:
None
Returns:
model -- TF Keras model (object containing the information for the entire training process)
"""
model = tf.keras.Sequential([
## ZeroPadding2D with padding 3, input shape of 64 x 64 x 3
tfl.ZeroPadding2D(padding=3,input_shape=(64,64,3)),
## Conv2D with 32 7x7 filters and stride of 1
tfl.Conv2D(32,7, strides=(1,1)),
## BatchNormalization for axis 3
tfl.BatchNormalization(axis=3),
## ReLU
tfl.ReLU(),
## Max Pooling 2D with default parameters
tfl.MaxPooling2D(),
## Flatten layer
tfl.Flatten(),
## Dense layer with 1 unit for output & 'sigmoid' activation
tfl.Dense(1,activation='sigmoid')
# YOUR CODE STARTS HERE
# YOUR CODE ENDS HERE
])
return model
分析:
- 首先是通过happyModel() 返回一个TensorFlow.Keras的模型,在happyModel()函数中先是通过Sequential 的方法里面存的训练过程的信息。
- 首先给出的提示ZeroPadding2D 我们先要通过上面引入的包tf1去使用ZeroPadding2D函数,对于函数中的参数可以根据网上找的对应函数进行填写,上面给出了要填的具体参数。很重要的一点就是下面的所有函数都是在Sequential()方法中进行的并且是该方法的参数,所以对于每个函数之间都要用
,
隔开。
happy_model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
- 最后得到happyModel() 后可以对其进行编译使用到了compile()函数 其参数为
optimizer
loss
和metrics
,有一个函数summary() 可以对模型中的进行打印
happy_model.summary()
- 在创建模型之后,使用您选择的优化器和损失函数编译它,并对其内容进行完整性检查,现在就可以开始构建了,只需调用.fit()进行训练。其中参数
epochs
是指训练的次数,batch_size
是指定进行梯度下降时每个batch包含的样本数
happy_model.fit(X_train, Y_train, epochs=10, batch_size=16)
- 完成训练之后可以利用evaluate()函数 对其进行评估
happy_model.evaluate(X_test, Y_test)
Exercise 2 - convolutional_model
conv_model = convolutional_model((64, 64, 3))
conv_model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
conv_model.summary()
output = [['InputLayer', [(None, 64, 64, 3)], 0],
['Conv2D', (None, 64, 64, 8), 392, 'same', 'linear', 'GlorotUniform'],
['ReLU', (None, 64, 64, 8), 0],
['MaxPooling2D', (None, 8, 8, 8), 0, (8, 8), (8, 8), 'same'],
['Conv2D', (None, 8, 8, 16), 528, 'same', 'linear', 'GlorotUniform'],
['ReLU', (None, 8, 8, 16), 0],
['MaxPooling2D', (None, 2, 2, 16), 0, (4, 4), (4, 4), 'same'],
['Flatten', (None, 64), 0],
['Dense', (None, 6), 390, 'softmax']]
comparator(summary(conv_model), output)
def convolutional_model(input_shape):
"""
Implements the forward propagation for the model:
CONV2D -> RELU -> MAXPOOL -> CONV2D -> RELU -> MAXPOOL -> FLATTEN -> DENSE
Note that for simplicity and grading purposes, you'll hard-code some values
such as the stride and kernel (filter) sizes.
Normally, functions should take these values as function parameters.
Arguments:
input_img -- input dataset, of shape (input_shape)
Returns:
model -- TF Keras model (object containing the information for the entire training process)
"""
input_img = tf.keras.Input(shape=input_shape)
## CONV2D: 8 filters 4x4, stride of 1, padding 'SAME'
Z1 = tfl.Conv2D(8,(4,4),(1,1),padding = 'same')(input_img)
## RELU
A1 = tfl.ReLU()(Z1)
## MAXPOOL: window 8x8, stride 8, padding 'SAME'
P1 = tfl.MaxPool2D(pool_size = (8,8),strides = (8, 8),padding='same')(A1)
## CONV2D: 16 filters 2x2, stride 1, padding 'SAME'
Z2 = tfl.Conv2D(filters = 16,kernel_size = 2,strides = 1,padding = 'same')(P1)
## RELU
A2 = tfl.ReLU()(Z2)
## MAXPOOL: window 4x4, stride 4, padding 'SAME'
P2 = tfl.MaxPool2D(pool_size = (4,4),strides = (4, 4),padding='same')(A2)
## FLATTEN
F = tfl.Flatten()(P2)
## Dense layer
## 6 neurons in output layer. Hint: one of the arguments should be "activation='softmax'"
outputs = tfl.Dense(6,activation='softmax')(F)
# YOUR CODE STARTS HERE
# YOUR CODE ENDS HERE
model = tf.keras.Model(inputs=input_img, outputs=outputs)
return model
分析:
- 通过自定义的convolutional_model(input_shape) 函数将输入一个64x64x3大小的图像,具体在该函数中先是通过keras.Input() 将输入的(nxnxnc)的数实例化一个keras张量。
- 通过keras库中的Conv2D()函数与输入张量结合,其中Conv2D()函数参数有过滤器数量、步长、大小等等。根据提示输入需要的参数。最后通过keras.Model() 函数对其参数为输入图像和最后经过卷积等操作输出的值实现前向传播的过程,模型构建成功之后,下面就是进行训练模型了
train_dataset = tf.data.Dataset.from_tensor_slices((X_train, Y_train)).batch(64)
test_dataset = tf.data.Dataset.from_tensor_slices((X_test, Y_test)).batch(64)
history = conv_model.fit(train_dataset, epochs=100, validation_data=test_dataset)
- 对于tf.data.Dataset.from_tensor_slices() 函数的作用是把给定的元组、列表和张量等数据进行特征切片。对于
batch()
其实就是对每次训练选取的样本数。最后history就是对上面构建的模型训练100次
history.history
- 利用
.history
可以将模型训练过程的损失值和度量值打印出来,后面是用图片的形式演示出了损失值变化