""" Trains a simple convnet on the MNIST dataset. https://keras.io/examples/vision/mnist_convnet/ """ # Silence tensorflow import verbose messages import os #os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # Setup # ----- import numpy as np from tensorflow import keras from tensorflow.keras import layers import matplotlib.pyplot as plt # Plotting # -------- def plot_images(title,images,labels,findings=None): """ title: title of the whole 5x5 plot images: set of images to plot, first 25 picked labels: set of digits the images are showing findings (optional): set of digits the NN thinks he images are showing """ fig,axs = plt.subplots(5,5,figsize=(8,8)) plt.suptitle(title) k = 0 for i in range(5): for j in range(5): axs[i,j].axis('off') try: axs[i,j].imshow(images[k], cmap='Greys') try: axs[i,j].set_title(f'{labels[k]} not {findings[k]}') except: axs[i,j].set_title(f'{labels[k]}') k+=1 except: pass plt.ion() plt.draw() plt.pause(1e-3) plt.ioff() # Model / data parameters # ----------------------- num_classes = 10 input_shape = (28, 28, 1) # Read in the data # ---------------- # split between train and test sets # (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() plot_images('Sample of digits',x_train,y_train) # Scale images to the [0, 1] range x_train = x_train/255 x_test = x_test/255 # x shape is (:,28,28), need (:,28,28,1); add an extra dimension to the end # images should have shape (28, 28, 1) x_train = x_train[...,np.newaxis] # notice the use of the ellipsis ... x_test = x_test[...,np.newaxis] print("x_train shape:", x_train.shape) print(x_train.shape[0], "train samples") print(x_test.shape[0], "test samples") # convert class vectors to binary class matrices y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) # Build the model # --------------- model = keras.Sequential( [ keras.Input(shape=input_shape), layers.Conv2D(32, kernel_size=(3, 3), activation="relu"), layers.MaxPooling2D(pool_size=(2, 2)), layers.Conv2D(64, kernel_size=(3, 3), activation="relu"), layers.MaxPooling2D(pool_size=(2, 2)), layers.Flatten(), layers.Dropout(0.5), layers.Dense(num_classes, activation="softmax"), ] ) model.summary() # Train the model # --------------- batch_size = 128 epochs = 15 model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"]) model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1) # Evaluate the trained model # -------------------------- score = model.evaluate(x_test, y_test, verbose=0) print("Test loss:", score[0]) print("Test accuracy:", score[1])