import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Conv2D, pooling, Flatten
(tri, trl), (tei, tel) = mnist.load_data()
print(tri.shape, trl.shape, tei.shape, tel.shape)
tri = tri.reshape(tri.shape[0],28,28,1).astype(np.float32) / 255.0
tei = tei.reshape(tei.shape[0],28,28,1).astype(np.float32) / 255.0
trl = np_utils.to_categorical(trl)
tel = np_utils.to_categorical(tel)
# trl = tf.one_hot(trl, 10)
print(tri.shape, trl.shape, tei.shape, tel.shape)
model = Sequential()
model.add(Conv2D(32, (3,3), strides=(1,1), padding="same", activation="relu", input_shape=(28,28,1)))
model.add(Conv2D(32, (3,3), strides=(1,1), padding="same", activation="relu"))
model.add(pooling.MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, (3,3), strides=(1,1), padding="same", activation="relu"))
model.add(Conv2D(64, (3,3), strides=(1,1), padding="same", activation="relu"))
model.add(pooling.MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(10, activation="softmax"))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])
model.fit(tri, trl, epochs=5, batch_size=32 , verbose=1)
_, a = model.evaluate(tei, tel)
print("acc : " , a)
model.summary()