对MLP、简单CNN和多层CNN区别做简单性能对比
MLP(需将图片宽高数据转换成一维数据形式)
from keras.datasets import mnist
from matplotlib import pyplot as plt
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils
# 从Keras导入Mnist数据集
(X_train, y_train), (X_validation, y_validation) = mnist.load_data()
# 显示4张手写数字的图片
plt.subplot(221)
plt.imshow(X_train[0], cmap=plt.get_cmap('gray'))
plt.subplot(222)
plt.imshow(X_train[1], cmap=plt.get_cmap('gray'))
plt.subplot(223)
plt.imshow(X_train[2], cmap=plt.get_cmap('gray'))
plt.subplot(224)
plt.imshow(X_train[3], cmap=plt.get_cmap('gray'))
plt.show()
# 设定随机种子
seed = 7
np.random.seed(seed)
num_pixels = X_train.shape[1] * X_train.shape[2]
print(num_pixels)
X_train = X_train.reshape(X_train.shape[0], num_pixels).astype('float32')
X_validation = X_validation.reshape(X_validation.shape[0], num_pixels).astype('float32')
# 格式化数据到0-1之前。因为输入数据是0-255的整数,需要将其进行归一化
X_train = X_train / 255
X_validation = X_validation / 255
# one-hot编码
y_train = np_utils.to_categorical(y_train)
y_validation = np_utils.to_categorical(y_validation)
num_classes = y_validation.shape[1]
print(num_classes)
# 定义基准MLP模型
def create_model():
# 创建模型
model = Sequential()
model.add(Dense(units=num_pixels, input_dim=num_pixels, kernel_initializer='normal', activation='relu'))
model.add(Dense(units=num_classes, kernel_initializer='normal', activation='softmax'))
# 编译模型
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
model = create_model()
model.fit(X_train, y_train, epochs=10, batch_size=200)
score = model.evaluate(X_validation, y_validation)
print('MLP: %.2f%%' % (score[1] * 100))
简单CNN
from keras.datasets import mnist
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras import backend
#Keras技术文档中对backend称为后端,处理如张量乘积和卷积等低级操作。Keras主要有三个后端可用:TensorFlow、Theano、CNTK。Image_data_format(),返回默认图像的维度顺序(“channels_first"或"channels_last”)。
# CPU环境下
backend.set_image_data_format('channels_last')
# GPU环境下
backend.set_image_data_format('channels_first')
# 设定随机种子
seed = 7
np.random.seed(seed)
# 从Keras导入Mnist数据集
(X_train, y_train), (X_validation, y_validation) = mnist.load_data()
# 原始X_train.shape为(6000,28,28)
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32')
X_validation = X_validation.reshape(X_validation.shape[0], 28, 28,1).astype('float32')
# 格式化数据到0-1之前
X_train = X_train / 255
X_validation = X_validation / 255
# one-hot编码
y_train = np_utils.to_categorical(y_train)
y_validation = np_utils.to_categorical(y_validation)
# 创建模型
def create_model():
model = Sequential()
model.add(Conv2D(32, (5, 5),input_shape=(28, 28,1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2),data_format='channels_last'))
#GPU环境下使用model.add(MaxPooling2D(pool_size=(2, 2),data_format='channels_first'))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(units=128, activation='relu'))
model.add(Dense(units=10, activation='softmax'))
# 编译模型
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
model = create_model()
model.fit(X_train, y_train, epochs=10, batch_size=200, verbose=2)
score = model.evaluate(X_validation, y_validation, verbose=0)
print('CNN_Small: %.2f%%' % (score[1] * 100))
复杂CNN(仅层数有所增加)
from keras.datasets import mnist
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras import backend
backend.set_image_data_format('channels_last')
# 设定随机种子
seed = 7
np.random.seed(seed)
# 从Keras导入Mnist数据集
(X_train, y_train), (X_validation, y_validation) = mnist.load_data()
# 原始X_train.shape为(6000,28,28)
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32')
X_validation = X_validation.reshape(X_validation.shape[0], 28, 28,1).astype('float32')
# 格式化数据到0-1之前
X_train = X_train / 255
X_validation = X_validation / 255
# one-hot编码
y_train = np_utils.to_categorical(y_train)
y_validation = np_utils.to_categorical(y_validation)
# 创建模型
def create_model():
model = Sequential()
model.add(Conv2D(32, (5, 5),input_shape=(28, 28,1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2),data_format='channels_last'))
model.add(Conv2D(15, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2),data_format='channels_last'))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(units=128, activation='relu'))
model.add(Dense(units=50, activation='relu'))
model.add(Dense(units=10, activation='softmax'))
# 编译模型
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
model = create_model()
model.fit(X_train, y_train, epochs=10, batch_size=200, verbose=2)
score = model.evaluate(X_validation, y_validation, verbose=0)
print('CNN_Small: %.2f%%' % (score[1] * 100))