digit-recognition 0.0.11
Neural network for digit recognition

# 1. Manual

## 1.1 Installation
To install the package, run the command `pip install digit_recognition` in the command prompt (windows) or terminal (mac) (alternative: download the package and run the setup.py script with `python setup.py install`).


The package uses the following dependencies: numpy, urllib3, gzip, tkinter, Pillow. These should be installed automatically during the installation if necessary. If the package does not run as intended, please ensure that the dependencies listed here have been installed correctly.

## 1.2 Setting up the NN
The package does not include a trained NN, but only the necessary functions to build it. Therefore, prior to using the interface, the NN has to be set up (this has only to be done once). To do so, run the command `install_network` in the command prompt/terminal (alternative: import the ‘digit_recognition’ module in python and call the `install_network()` function).


This creates a folder in the current directory (“DR_Data”) and downloads training and test sets from the MNIST database to this folder. It uses this data to train a NN with 784, 200, 100, and 10 nodes in each of four layers, respectively. The training algorithm goes through three epochs each with 60.000 training digits (this may take a few minutes). The NN is evaluated and the accuracy as well as the recall and precision for each digit are printed to the console (__important__: accuracy should be above 95%). Finally, the NN is saved to the “DR_Data” folder.

## 1.3 Starting up the interface
To start the interface, run the command `digit_recognition` in the command prompt/terminal (alternative: import the ‘digit_recognition’ module in python and call the `run_gui()` function). 

__Important__: Ensure that the “DR_Data” folder is located in the current working directory.

## 1.4 Using the interface
<img src="img/exp3.png" width="400">

The interface consists of two fields: a drawing field framed in black (left) and a feedback field showing several outputs (right). The user can draw in the drawing field by pressing the left mouse button and moving the mouse. Located below the drawing field are two buttons: the “Recognize!” button passes the drawing to the NN and displays its output in the feedback field; The “Reset” button deletes the current drawing and output. In the feedback field, three outputs are displayed: first, the digit recognized by the NN in the user’s drawing; second, the confidence of this recognition (i.e. the probability that the recognition is correct); and third, a possible alternative (i.e. the second most likely recognition). If the confidence is above 80%, no alternative is displayed.

__Important__: The performance of the NN is highly sensitive to size and location of the user’s digit in the drawing field. The grey rectangle in the drawing field indicates location and size for optimal performance.

# 2. Documentation
### 2.1 NeuralNetwork (class)
* __\_\_init\_\_(self, design, weights=None, step_size=0.01, activation_function=sigmoid, bias=False)__
Set up basic attributes of neural network.

        Attributes
        ----------
        design: list
            Contains the number of nodes in each layer (length is number of layers)
        weights: list
            Contains weight matrices
        step_size: float
            Step size for training algorithm
        activation_function: function
            Activation function used in neural network
        bias: boolean
            Bias nodes on or off
        activation: list
            Contains activation of nodes at each layer
        confusion_matrix: np.array
            Confusion matrix produced in 'evaluate' method (true labels in rows, predicitons in columns)
        accuracy, recall, precision: float
            Accuracy, recall, and precision of neural network produced in 'evaluate' method

        Methods
        -------
        train(input_data, target_data, epochs=1)
            Loops of 'one_training' function
        one_training(input_data, target_data)
            Computes cost and updates weights accordingly using backpropagation
        run(input_data)
            Forward propagation for single input
        evaluate(input_data, target_data)
            Assesses performance of neural network and computes performance measures
        save(file_name)
            Saves weights of neural network as np.array

* __train(self, input_data, target_data, epochs=1)__
Loop for 'one_train' (backpropagation) function (see below).

        Arguments
        --------- 
        input_data: array
            3-dimensional numpy array containing input data (see 
            pre_processing function for exact format).
        target_data: array
            2-dimensional numpy array containing target data in one-hot 
            representation.
        epochs: int
            Number of times the training algorithms iterates through 
            the *entire* dataset. Defaults to 1.

* __one_training(self, input_data, target_data)__
Backpropagation algorithm to train neural network.

        Arguments
        --------- 
        input_data: array
            2-dimensional numpy array containing a single input. Passed 
            by train function (see above).
        target_data: array
            2-dimensional numpy array containing a single target. Passed 
            by train function (see above).

* __run(self, input_data)__
Forward propagation algorithm.

        Computes output of neural network for a single input.

        Arguments
        --------- 
        input_data: array
            2-dimensional numpy array containing a single input.

* __evaluate(self, input_data, target_data)__
Evaluates performance of neural network.
Computes accuracy of neural network, as well as recall and precision for each class using a confusion matrix. Results are printed to the console, but also defined as attributes of the neural network.
Note: Use independent test data!

        Arguments
        --------- 
        input_data: array
            3-dimensional numpy array containing test input data.
        target_data: array
            2-dimensional numpy array containing test target data in 
            one-hot representation.

* __save(self, file_name)__
Saves weights of neural network as np.array 

        Arguments
        ---------
        file_name: string
            Name of the file that is saved (without file extension)

### 2.2 Pre-processing (function)
* __pre_processing( )__ 
Downloads, imports and preprocess data for digit recognition.
Data is downloaded from the MNIST database. Consists of 70.000 handwritten digits of 28x28 pixels. Each with a corresponding, manually added label. Data is split into 60.000 instances for training and 10.000 instances for testing.

        Returns:
            Matrix representations of digits and correspondings labels in a format optimized for the neural network.

# 3. Example
````python
# Import data
train_images, train_labels, test_images, test_labels = pre_processing()

# Build neural network (with two hidden layers of 200/100 nodes respectively)
neural_network = NeuralNetwork([784,200,100,10], bias=True)
neural_network.train(train_images, train_labels, epochs=3)
neural_network.evaluate(test_images, test_labels)

# Export neural network
os.chdir('DR_Data')
neural_network.save('my_network')
os.chdir('..')
````