keras for MNIST(digit-recognizer) in Kaggle

##CNNによるMNISTデータセット学習
kaggleのコンペ参照:https://www.kaggle.com/c/digit-recognizer

###mnistデータ編集と取得
```python:get_mnist.py
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split

train=pd.read_csv("./train.csv")
X=train.iloc[:,1:]
Y=train["label"]

X=np.asarray(X).astype("float")/255.0
Y=np.asarray(Y).astype("int")

X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.2,random_state=123)

t=(X_train,X_test,Y_train,Y_test)
np.save("./get_data.npy",t)
```

###CNN実装(トレーニングメソッド)
```python:training.py
import keras
from keras.models import Sequential
from keras.layers import Conv2D,MaxPooling2D,Flatten,Dropout,Dense,Activation
from keras.utils import np_utils
from keras.optimizers import Adam
import matplotlib.pyplot as plt
import numpy as np

size=120
epoch=30

X_train,X_test,Y_train,Y_test=np.load("get_data.npy")
Y_train=np_utils.to_categorical(Y_train)
Y_test=np_utils.to_categorical(Y_test)
X_train=X_train.reshape(-1,28,28,1)
X_test=X_test.reshape(-1,28,28,1)

def train():
    model=Sequential()
    model.add(Conv2D(32,(3,3),padding="same",input_shape=X_train.shape[1:]))
    model.add(Activation("relu"))
    model.add(Conv2D(64,(3,3),padding="same"))
    model.add(Activation("relu"))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.20))

    model.add(Conv2D(64,(3,3),padding="same"))
    model.add(Activation("relu"))
    model.add(Conv2D(128,(3,3),padding="same"))
    model.add(Activation("relu"))
    model.add(MaxPooling2D(pool_size=(2,2)))
    model.add(Dropout(0.20))

    model.add(Conv2D(128,(3,3),padding="same"))
    model.add(Activation("relu"))
    model.add(Conv2D(512,(3,3),padding="same"))
    model.add(Activation("relu"))
    model.add(MaxPooling2D(pool_size=(2,2)))
    model.add(Dropout(0.20))

    model.add(Flatten())
    model.add(Dense(1024))
    model.add(Activation("relu"))
    model.add(Dropout(0.50))
    model.add(Dense(Y_train.shape[1]))
    model.add(Activation("softmax"))
    opt=Adam(lr=0.0001,beta_1=0.9,beta_2=0.999,decay=1e-7)
    model.compile(loss="categorical_crossentropy",optimizer=opt,metrics=["accuracy"])
    result=model.fit(X_train,Y_train,batch_size=size,epochs=epoch)
    model.save("./model.h5")
    plt.plot(range(1,epoch+1),result.history["acc"],label="training")
    plt.plot(range(1,epoch+1),result.history["loss"],label="loss")
    plt.xlabel("epoch")
    plt.ylabel("acc/loss")
    plt.legend()
    plt.show()
    return model

if __name__=="__main__":
    train()
```

####学習経過log

Epoch 1/30
2019-04-21 01:01:32.447871: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
33600/33600 [==============================] - 128s 4ms/step - loss: 0.6696 - acc: 0.7753
Epoch 2/30
33600/33600 [==============================] - 128s 4ms/step - loss: 0.1366 - acc: 0.9578
Epoch 3/30
33600/33600 [==============================] - 128s 4ms/step - loss: 0.0888 - acc: 0.9724
Epoch 4/30
33600/33600 [==============================] - 129s 4ms/step - loss: 0.0727 - acc: 0.9774
Epoch 5/30
33600/33600 [==============================] - 128s 4ms/step - loss: 0.0592 - acc: 0.9811
Epoch 6/30
33600/33600 [==============================] - 131s 4ms/step - loss: 0.0480 - acc: 0.9846
Epoch 7/30
33600/33600 [==============================] - 132s 4ms/step - loss: 0.0430 - acc: 0.9864
Epoch 8/30
33600/33600 [==============================] - 132s 4ms/step - loss: 0.0372 - acc: 0.9889
Epoch 9/30
33600/33600 [==============================] - 134s 4ms/step - loss: 0.0318 - acc: 0.9896
Epoch 10/30
33600/33600 [==============================] - 133s 4ms/step - loss: 0.0300 - acc: 0.9901
Epoch 11/30
33600/33600 [==============================] - 133s 4ms/step - loss: 0.0271 - acc: 0.9915
Epoch 12/30
33600/33600 [==============================] - 133s 4ms/step - loss: 0.0244 - acc: 0.9920
Epoch 13/30
33600/33600 [==============================] - 137s 4ms/step - loss: 0.0235 - acc: 0.9925
Epoch 14/30
33600/33600 [==============================] - 132s 4ms/step - loss: 0.0202 - acc: 0.9929
Epoch 15/30
33600/33600 [==============================] - 131s 4ms/step - loss: 0.0173 - acc: 0.9946
Epoch 16/30
33600/33600 [==============================] - 134s 4ms/step - loss: 0.0175 - acc: 0.9941
Epoch 17/30
33600/33600 [==============================] - 133s 4ms/step - loss: 0.0154 - acc: 0.9946
Epoch 18/30
33600/33600 [==============================] - 138s 4ms/step - loss: 0.0141 - acc: 0.9952
Epoch 19/30
33600/33600 [==============================] - 130s 4ms/step - loss: 0.0120 - acc: 0.9957
Epoch 20/30
33600/33600 [==============================] - 137s 4ms/step - loss: 0.0118 - acc: 0.9963
Epoch 21/30
33600/33600 [==============================] - 131s 4ms/step - loss: 0.0113 - acc: 0.9963
Epoch 22/30
33600/33600 [==============================] - 129s 4ms/step - loss: 0.0124 - acc: 0.9961
Epoch 23/30
33600/33600 [==============================] - 128s 4ms/step - loss: 0.0097 - acc: 0.9967
Epoch 24/30
33600/33600 [==============================] - 128s 4ms/step - loss: 0.0085 - acc: 0.9976
Epoch 25/30
33600/33600 [==============================] - 128s 4ms/step - loss: 0.0100 - acc: 0.9964
Epoch 26/30
33600/33600 [==============================] - 128s 4ms/step - loss: 0.0104 - acc: 0.9963
Epoch 27/30
33600/33600 [==============================] - 128s 4ms/step - loss: 0.0070 - acc: 0.9976
Epoch 28/30
33600/33600 [==============================] - 128s 4ms/step - loss: 0.0066 - acc: 0.9979
Epoch 29/30
33600/33600 [==============================] - 128s 4ms/step - loss: 0.0076 - acc: 0.9976
Epoch 30/30
33600/33600 [==============================] - 128s 4ms/step - loss: 0.0075 - acc: 0.9974

accuracy:0.9974
loss:0.0075
![training_log.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/402159/ecee8386-dc8d-4649-4626-df051023db83.png)
###学習モデル評価

```python:model_eval.py
import keras
from keras.models import Sequential,load_model
from keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,Dropout,Activation
from keras.optimizers import Adam
from keras.utils import np_utils
import numpy as np
import pandas as pd

X_train,X_test,Y_train,Y_test=np.load("./get_data.npy")
X_test=X_test.reshape(-1,28,28,1)
Y_test=np_utils.to_categorical(Y_test)

def build_model():
    model=Sequential()
    model.add(Conv2D(32,(3,3),padding="same",input_shape=X_test.shape[1:]))
    model.add(Activation("relu"))
    model.add(Conv2D(64,(3,3),padding="same"))
    model.add(Activation("relu"))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.20))

    model.add(Conv2D(64,(3,3),padding="same"))
    model.add(Activation("relu"))
    model.add(Conv2D(128,(3,3),padding="same"))
    model.add(Activation("relu"))
    model.add(MaxPooling2D(pool_size=(2,2)))
    model.add(Dropout(0.20))

    model.add(Conv2D(128,(3,3),padding="same"))
    model.add(Activation("relu"))
    model.add(Conv2D(512,(3,3),padding="same"))
    model.add(Activation("relu"))
    model.add(MaxPooling2D(pool_size=(2,2)))
    model.add(Dropout(0.20))

    model.add(Flatten())
    model.add(Dense(1024))
    model.add(Activation("relu"))
    model.add(Dropout(0.50))
    model.add(Dense(10))
    model.add(Activation("softmax"))
    opt=Adam(lr=0.0001,beta_1=0.9,beta_2=0.999,decay=1e-7)
    model.compile(loss="categorical_crossentropy",optimizer=opt,metrics=["accuracy"])
    model=load_model("./model.h5")
    return model

def model_eval():
    model=build_model()
    score=model.evaluate(X_test,Y_test,verbose=1)
    print(score)
    print("Test LOSS:",score[0])
    print("Test ACC:",score[1])
if __name__=="__main__":
    model_eval()
```
8400/8400 [==============================] - 8s 977us/step
[0.03062643431244185, 0.9927380952380952]
Test LOSS: 0.03062643431244185
Test ACC: 0.9927380952380952

test accuracy=99.2738%と高精度になった。