CNN 手写字体的识别

import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.datasets import mnist
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report

def classificationReport(y_test,y_pred,category_names):
    print('start doing report')
    report_string = classification_report(y_test,y_pred,target_names=category_names)
    print("report_string=",report_string)

def history_plot(model_id,history):
    plt.figure(figsize=(8,8))
    plt.subplot(2,1,1)
    plt.title('cross entropy loss -'+model_id,fontsize=12)
    plt.plot(history.history['loss'],color='blue',label='train')
    plt.plot(history.history['val_loss'],color='orange',label='val')
    plt.xlabel("Epochs",fontsize=12)
    plt.ylabel("loss",fontsize=12)
    plt.legend(loc='upper right')


    plt.subplot(2,1,2)
    plt.title('classification accuracy'+model_id,fontsize=10)
    plt.plot(history.history['accuracy'],color='blue',label='train')
    # plt.plot()

    plt.plot(history.history['val_accuracy'],color='orange',label='val')
    plt.xlabel("Epochs",fontsize=12)
    plt.ylabel("Accuracy",fontsize=12)
    plt.legend(loc='lower right')

    plt.show()

def cnn_model():
    model=Sequential()
    model.add(Conv2D(32,5,5, padding='same',input_shape=(1,28,28), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2,2), padding='same'))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(num_classes, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model
seed=10
np.random.seed(seed)
(X_train,y_train), (X_test, y_test)= mnist.load_data()


# Convert the data into float values
X_train=X_train.reshape(X_train.shape[0], 1,28,28).astype('float32')
X_test=X_test.reshape(X_test.shape[0], 1,28,28).astype('float32')

# Normalize the data
X_train=X_train/255
X_test=X_test/255
y_train= np_utils.to_categorical(y_train)
y_test= np_utils.to_categorical(y_test)
num_classes=y_train.shape[1]
print(num_classes)


model=cnn_model()
history=model.fit(X_train, y_train, validation_data=(X_test,y_test),epochs=100, batch_size=200, verbose=2)
score= model.evaluate(X_test, y_test, verbose=0)
print('The error is: %.2f%%'%(100-score[1]*100))

history_plot('CNN',history)

50个epoch后:

 

100个epoches后:

 

 

TODO :

1.没有画那个roc曲线  计算auc的值 .

2.没有进行data arguation.

3.这个没有区分validation test.

4.如果想要提升一下性能 能不能增加一下这个的数据量呢。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qqqweiweiqq

你的鼓励将是我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值