from tensorflow import keras
import numpy as np
np.random.seed(0)
from tensorflow.keras.datasets import mnist
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.models import Sequential
import matplotlib.pyplot as plt
# To set the color used to display monochrome images.
plt.rcParams['image.cmap'] = 'Blues'
(x_train, y_train), (x_test, y_test) = mnist.load_data()
img_width, img_height = x_train[0].shape
img_width, img_height
x_train = x_train.reshape(x_train.shape[0], img_width, img_height, 1)
x_test = x_test.reshape(x_test.shape[0], img_width, img_height, 1)
# rescale
x_train = x_train / 255
x_test = x_test / 255
y_train = keras.utils.to_categorical(y_train)
y_test = keras.utils.to_categorical(y_test)
# For instance, here is how the first instance in the training set is encodes.
y_train[0]
num_classes = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5), strides=(1, 1),
activation='relu',
input_shape=(img_width, img_height, 1)))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 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'])
model.fit(x_train, y_train,
batch_size=256,
epochs=1,
verbose=1,
validation_data=(x_test, y_test));
data = np.load('xTest2.npy')
temp = np.zeros((10000, 28, 28, 1))
for i in range(10000):
temp[i,:,:,:] = data[:,:,:,i]
data = temp
data = data / 255
predictions = model.predict(data)
predictions = np.argmax(predictions, axis = 1)
plt.imshow(data[13,:,:,0])
np.savetxt('classifications.csv', predictions, delimiter=',', fmt = '%d')