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()

'도서관 I > AI' 카테고리의 다른 글

Fashion-MNIST 예제  (0) 2020.06.01
top_K  (0) 2020.06.01
softmax keras  (0) 2020.05.29
MNIST 데이터 불러오기  (0) 2020.05.29
python 정리  (0) 2020.05.28

+ Recent posts