<jupyter_start><jupyter_text>Digit Recognition ------ Imports needed for the project<jupyter_code># Tkinter is Python's de-facto standard GUI (Graphical User Interface) package.
import tkinter as tk
import keras as kr
import numpy as np
import matplotlib.pyplot as plt
import math
import sklearn.preprocessing as pre
import gzip
import PIL
from PIL import Image, ImageDraw
import os.path<jupyter_output>Using TensorFlow backend.<jupyter_text>Read in the images for training ---- We can start by Importing the Images that we will use for training the model, then the images we will use to test the model and images used to identify the classes needed to identify them.<jupyter_code>with gzip.open('data/train-images-idx3-ubyte.gz', 'rb') as f:
    train_img = f.read()

with gzip.open('data/train-labels-idx1-ubyte.gz', 'rb') as f:
     train_lbl = f.read()

with gzip.open('data/t10k-images-idx3-ubyte.gz', 'rb') as f:
     test_img = f.read()

with gzip.open('data/t10k-labels-idx1-ubyte.gz', 'rb') as f:
     test_lbl = f.read()<jupyter_output><empty_output><jupyter_text>Create the model for training ---- Here we can create a sequential model, we can build it using layers to identify the dimentions to use. the tools to normalise the data, and which algorithm to use to optimise and train the network.<jupyter_code># Start a neural network, building it by layers.
# using sequential model
model = kr.models.Sequential()

# Add a hidden layer with 1000 neurons and an input layer with 784.
model.add(kr.layers.Dense(512, input_dim=784, activation="relu", kernel_initializer="normal"))
model.add(kr.layers.Dense(10, activation="softmax", kernel_initializer="normal"))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Reshape the mnist data set to match the conditions that we need ---- Now we can reshape the inputs in or to train the network, using a list of images to train and a group of labels to identify the differnt classes.<jupyter_code># reshape the images and labels.
train_img = ~np.array(list(train_img[16:])).reshape(60000, 1, 784).astype(np.uint8)
train_lbl =  np.array(list(train_lbl[ 8:])).astype(np.uint8)

train_img = train_img/ 255
train_lbl = kr.utils.to_categorical(train_lbl)

# reshape the image array
inputs = train_img.reshape(60000, 784)<jupyter_output><empty_output><jupyter_text>Create the encoder ---- Now we can train the Encoder so that we can use it to interpret the expected result.<jupyter_code># Binarize labels in a one-vs-all fashion
encoder = pre.LabelBinarizer()
# Trains the model for a fixed number of epochs (iterations on a dataset).
encoder.fit(train_lbl)
outputs = encoder.transform(train_lbl)<jupyter_output><empty_output><jupyter_text>Train the Neural Network ---- Train the nueral network by giving it a group of inputs and expected outputs.<jupyter_code># Train the model
model.fit(inputs, outputs, epochs=15, batch_size=100)<jupyter_output>Epoch 1/15
60000/60000 [==============================] - 9s 152us/step - loss: 0.5218 - acc: 0.8463
Epoch 2/15
60000/60000 [==============================] - 9s 149us/step - loss: 0.3155 - acc: 0.9063
Epoch 3/15
60000/60000 [==============================] - 9s 148us/step - loss: 0.2498 - acc: 0.9258 1s - loss: 
Epoch 4/15
60000/60000 [==============================] - 9s 150us/step - loss: 0.2001 - acc: 0.9397 
Epoch 5/15
60000/60000 [==============================] - 9s 149us/step - loss: 0.1677 - acc: 0.9501
Epoch 6/15
60000/60000 [==============================] - 9s 148us/step - loss: 0.1475 - acc: 0.9560
Epoch 7/15
60000/60000 [==============================] - 9s 149us/step - loss: 0.1359 - acc: 0.9588
Epoch 8/15
60000/60000 [==============================] - 9s 149us/step - loss: 0.1247 - acc: 0.9625
Epoch 9/15
60000/60000 [==============================] - 9s 148us/step - loss: 0.1139 - acc: 0.9647 0s - loss: 0.1141 - acc: 0
Epoch 10/15
60000/60000 [==========================[...]<jupyter_text>Saving the model to file ---- Here we can save the model so that it can be read in again.<jupyter_code># save the current model
kr.models.save_model(
    model,
    "model2.h5py",
    overwrite=True,
    include_optimizer=True
)<jupyter_output><empty_output><jupyter_text>Loading the model from file ---- If the model exists, we can load it so that the network wont have to be trained each time.<jupyter_code># if the model file exists load it
if os.path.isfile('data/model2.h5py'): 
    model = kr.models.load_model('data/model2.h5py')<jupyter_output><empty_output><jupyter_text>Read in an image to test ---- Lets open the image we want to test, feel free to test without images by changing the relative paths.<jupyter_code># read in an image and greyscale
im = Image.open('data/image.png').convert('L')<jupyter_output><empty_output><jupyter_text>Here we can show the image that we loaded and see the image using the default image viewer in the OS. It will show a black background with a white digit. This is loading the image saved by the project program "digitrecognition.py", so it can change.<jupyter_code>im.show()<jupyter_output><empty_output><jupyter_text>Normalise the data and reshape ---- Now we must normalise the data so that the values are easier to read for the model.<jupyter_code># get the data from the image
tv = list(im.getdata())

# normalize pixels to 0 and 1. 0 is pure white, 1 is pure black.
tv = [(255 - x) * 1.0 / 255.0 for x in tv]<jupyter_output><empty_output><jupyter_text>We resize the image to an array of 1*784 so that it can be compared to the training data we have trained the network using.<jupyter_code>img =  np.array(list(tv)).reshape(1,784)<jupyter_output><empty_output><jupyter_text>Finally we can predict the class from the model we trained and get the response expected for the image we have input.  Make the prediction ----<jupyter_code>model.predict_classes(img)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup