Keras入门课2 -- 使用CNN识别mnist手写数字
2017-12-18 17:41
1151 查看
Keras入门课2:使用CNN识别mnist手写数字
本文用一个最简单的两层CNN神经网络来对mnist数据库进行分类识别。import keras from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten from keras.layers import Conv2D, MaxPooling2D from keras import backend as K
Using TensorFlow backend. /usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/_bootstrap.py:205: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6 return f(*args, **kwds)
(x_train,y_train),(x_test,y_test) = mnist.load_data() # out: np.ndarray print(x_train.shape,y_train.shape) print(x_test.shape,y_test.shape)
(60000, 28, 28) (60000,) (10000, 28, 28) (10000,)
↓可视化一些图片
import matplotlib.pyplot as plt im = plt.imshow(x_train[0],cmap='gray') plt.show() im2 = plt.imshow(x_train[1],cmap='gray') plt.show()
print(K.image_data_format())
channels_last
这里用卷积神经网络来对图像做特征处理,一般来说,输入到网络的图像格式有以下两种:
1. channels_first (batch_size,channels,width,height)
1. channels_last (batch_size,width,height,channels)
这里channels指的是通道数,灰度图是单通道channels=1,彩色图是三通道channels=3,需要注意的是,即使图像是单通道的,输入数据的维度依然是4维。反观我们的mnist图像数据,只有三维,所以我们要手动把channels这个维度加上。由于Keras使用不同后端的时候,数据格式不一样,所以要分情况进行维度增加
值得注意的是,reshape函数第一个参数为-1,意思为保持当前维度不变
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)
print(x_train.shape,x_test.shape)
(60000, 28, 28, 1) (10000, 28, 28, 1)
↓数据归一化
x_train = x_train/255 x_test = x_test/255
y_train = keras.utils.to_categorical(y_train,10) y_test = keras.utils.to_categorical(y_test,10)
↓构建网络模型
model = Sequential() model.add(Conv2D(filters = 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'))
model.summary()
_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_1 (Conv2D) (None, 26, 26, 32) 320 _________________________________________________________________ conv2d_2 (Conv2D) (None, 24, 24, 64) 18496 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 12, 12, 64) 0 _________________________________________________________________ dropout_1 (Dropout) (None, 12, 12, 64) 0 _________________________________________________________________ flatten_1 (Flatten) (None, 9216) 0 _________________________________________________________________ dense_1 (Dense) (None, 128) 1179776 _________________________________________________________________ dropout_2 (Dropout) (None, 128) 0 _________________________________________________________________ dense_2 (Dense) (None, 10) 1290 ================================================================= Total params: 1,199,882 Trainable params: 1,199,882 Non-trainable params: 0 _________________________________________________________________
model.compile(loss = keras.losses.categorical_crossentropy, optimizer = keras.optimizers.Adadelta(), metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=64,epochs=2 ,verbose=1,validation_data=(x_test,y_test))
Train on 60000 samples, validate on 10000 samples Epoch 1/2 60000/60000 [==============================] - 268s - loss: 0.2577 - acc: 0.9219 - val_loss: 0.0741 - val_acc: 0.9779 Epoch 2/2 60000/60000 [==============================] - 255s - loss: 0.1051 - acc: 0.9687 - val_loss: 0.0492 - val_acc: 0.9836
score = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1])
Test loss: 0.0492013186394 Test accuracy: 0.9836
总结
学习了如何根据不同的模型数据要求,给原始数据图像增加维度学习了Conv2D卷积层和MaxPooling2D池化层的使用
本文代码地址:https://github.com/tsycnh/Keras-Tutorials/blob/master/class_2.ipynb
参考:
https://github.com/keras-team/keras/tree/master/examples
相关文章推荐
- 深度学习-CNN卷积神经网络使用TensorFlow框架实现MNIST手写数字识别
- 用MXnet入门实战深度学习之一:安装GPU版mxnet并跑一个MNIST手写数字识别
- 基于tensorflow的MNIST手写数字识别--入门篇
- Caffe学习总结(四)——使用mnist训练模型识别一张手写数字图像
- 使用Tensorflow和MNIST识别自己手写的数字
- 基于tensorflow的MNIST手写数字识别(二)--入门篇
- 用MXnet入门实战深度学习之一:安装GPU版mxnet并跑一个MNIST手写数字识别
- torch入门笔记4:用torch实现MNIST手写数字识别
- Keras 浅尝之MNIST手写数字识别
- 用MXnet入门实战深度学习之一:安装GPU版mxnet并跑一个MNIST手写数字识别
- 用MXnet入门实战深度学习之一:安装GPU版mxnet并跑一个MNIST手写数字识别
- 用MXnet入门实战深度学习之一:安装GPU版mxnet并跑一个MNIST手写数字识别
- 用MXnet入门实战深度学习之一:安装GPU版mxnet并跑一个MNIST手写数字识别
- 机器学习-实战-入门-MNIST手写数字识别
- TensorFlow学习_02_CNN卷积神经网络_Mnist手写数字识别
- 用MXnet入门实战深度学习之一:安装GPU版mxnet并跑一个MNIST手写数字识别
- 用MXnet入门实战深度学习之一:安装GPU版mxnet并跑一个MNIST手写数字识别
- Deep Learning-TensorFlow (1) CNN卷积神经网络_MNIST手写数字识别代码实现详解
- 【深度学习】笔记3_caffe自带的第一个例子,Mnist手写数字识别所使用的LeNet网络模型的详细解释
- 用MXnet入门实战深度学习之一:安装GPU版mxnet并跑一个MNIST手写数字识别