from tensorflow.keras.datasets import mnist (training_dataset_x, training_dataset_y), (test_dataset_x, test_dataset_y) = mnist.load_data() import matplotlib.pyplot as plt figure = plt.gcf() figure.set_size_inches(10, 10) for i in range(1, 10): plt.subplot(3, 3, i) axis = plt.gca() axis.set_title(str(training_dataset_y[i])) plt.imshow(training_dataset_x[i], cmap='gray') plt.show() training_dataset_x = training_dataset_x / 255 test_dataset_x = test_dataset_x / 255 from tensorflow.keras.utils import to_categorical training_dataset_y = to_categorical(training_dataset_y) test_dataset_y = to_categorical(test_dataset_y) from tensorflow.keras.models import Model from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense inp = Input(shape=(28, 28, 1), name='Input') x = Conv2D(32, kernel_size=(3, 3), padding='same', activation='relu', name='Convolution-1')(inp) x = MaxPooling2D(name='Pooling-1', padding='same')(x) x = Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu', name='Convolution-2')(x) x = MaxPooling2D(name='Pooling-2', padding='same')(x) x = Flatten(name='Flatten')(x) x = Dense(128, activation='relu', name='Dense-1')(x) x = Dense(128, activation='relu', name='Dense-2')(x) x = Dense(10, activation='softmax', name='Output')(x) model = Model(inputs=[inp], outputs=[x], name='CONV-MNITS') model.summary() model.compile(optimizer='rmsprop', loss=['categorical_crossentropy'], metrics=['categorical_accuracy']) hist = model.fit([training_dataset_x.reshape(-1, 28, 28, 1)], [training_dataset_y], batch_size=32, epochs=5, validation_split=0.2) import matplotlib.pyplot as plt figure = plt.gcf() figure.set_size_inches((15, 5)) plt.title('Loss - Epoch Graphics') plt.xlabel('Epoch') plt.ylabel('Loss') plt.plot(range(1, len(hist.history['loss']) + 1), hist.history['loss']) plt.plot(range(1, len(hist.history['val_loss']) + 1), hist.history['val_loss']) plt.legend(['Loss', 'Validation Loss']) plt.show() figure = plt.gcf() figure.set_size_inches((15, 5)) plt.title('Categorical Accuracy - Epoch Graphics') plt.xlabel('Epoch') plt.ylabel('Categorical Accuracy') plt.plot(range(1, len(hist.history['categorical_accuracy']) + 1), hist.history['categorical_accuracy']) plt.plot(range(1, len(hist.history['val_categorical_accuracy']) + 1), hist.history['val_categorical_accuracy']) plt.legend(['Categorical Accuracy', 'Validation Categorical Accuracy']) plt.show() eval_result = model.evaluate([test_dataset_x.reshape(-1, 28, 28, 1)], [test_dataset_y]) for i in range(len(eval_result)): print(f'{model.metrics_names[i]} ---> {eval_result[i]}') import numpy as np import glob for path in glob.glob('test-images/*.jpg'): img_data = plt.imread(path) gray_img_data = np.average(img_data, weights=(0.3, 0.59, 0.11), axis=2) gray_img_data = gray_img_data / 255 predict_result = model.predict([gray_img_data.reshape(-1, 28, 28, 1)]) number = np.argmax(predict_result[0]) plt.title(f'Predicted: {number}', fontsize=16) plt.imshow(gray_img_data, cmap='gray') plt.show()