Keras框架是在TensorFlow之上开发一种框架,其使用相比于TensorFlow更简便,其中文使用文档如下:
https://keras-cn.readthedocs.io/en/latest/layers/convolutional_layer/#conv2d
import numpy as np
from keras import layers
from keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D
from keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D
from keras.models import Model, load_model
from keras.preprocessing import image
from keras.utils import layer_utils
from keras.utils.data_utils import get_file
from keras.applications.imagenet_utils import preprocess_input
import pydot
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
from keras.utils import plot_model
import kt_utils
import keras.backend as K
K.set_image_data_format('channels_last')
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
import pylab
可以看出Keras提供了许多高度集成的函数模块,例如卷积层Conv2D等。
Keras建立模型十分简洁,下面以建立一个笑脸识别的模型为例
def HappyModel(input_shape):
X_input = Input(input_shape)
X = ZeroPadding2D((3, 3))(X_input)
X = Conv2D(32, (7, 7), strides=(1, 1), name='conv0')(X)
X = BatchNormalization(axis=3, name='bn0')(X) # 归一化
X = Activation('relu')(X)
X = MaxPooling2D((2, 2), name='max_pool')(X)
X = Flatten()(X)
X = Dense(1, activation='sigmoid', name='fc')(X)
model = Model(inputs=X_input, outputs=X, name='HappyModel')
return model
可以看出,Keras放弃了TensorFlow中Z1,A1,的中间表达式,一律采用X的方式进行记录中间值。
其中Input函数用于生成输入的图片的维度的Tensor变量,十分方便。
def main():
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = kt_utils.load_dataset()
# Normalize image vectors
X_train = X_train_orig / 255.
X_test = X_test_orig / 255.
# Reshape
Y_train = Y_train_orig.T
Y_test = Y_test_orig.T
'''
img = image.load_img('./111.jpg', target_size=(64, 64))
imshow(img)
pylab.show()
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
model = load_model('./test.h5')
print(model.predict(x))
'''
model = HappyModel(X_train.shape[1:])
model.compile('Adam', 'binary_crossentropy', metrics=['accuracy'])
model.fit(X_train, Y_train, batch_size=50, epochs=40)
preds = model.evaluate(X_test, Y_test, batch_size=32, verbose=1, sample_weight=None)
model.save('./test.h5', overwrite=True, include_optimizer=True)
print("误差值 = " + str(preds[0]))
print("准确度 = " + str(preds[1]))
其中,model.compile()用于编译模型
model.fit()用于训练模型
model.evaluate()用于评价模型,即用测试集测试训练好的模型
model.save()用于保存训练好的模型以及权重,以便下次使用
上面代码中注释掉的部分为读取训练好的权重,并使用自己的照片进行测试的测试demo