This project implements a simple neural network for recognizing handwritten digits from the MNIST dataset. The network is trained using backpropagation and supports various activation functions like sigmoid, ReLU, leaky ReLU, ELU, and tanh. The network can be trained, tested on the MNIST dataset, and used to recognize custom images of handwritten digits.
- Modular Design: The code is organized into packages, making it easy to understand and extend.
- Multiple Activation Functions: The network supports sigmoid, ReLU, leaky ReLU, ELU, and tanh activation functions.
- Image Processing: Custom images can be processed and tested against the trained model.
- Persistence: The trained network can be saved and loaded for later use.
The project is structured into the following packages:
- MNIST.domain: Contains classes related to the MNIST dataset and image processing.
- MNIST.reader: Handles reading the MNIST dataset from files.
- neural_network.domain: Contains the core neural network components, including layers and neurons.
- neural_network.service: Provides services for training, testing, and managing the neural network.
- neural_network.reader: Handles saving and loading the trained neural network model.
- neural_network.exception: Contains custom exceptions related to the neural network operations.
- neural_network.interfaces: Defines interfaces for serializable functions used in the network.
- Java: Make sure you have Java Development Kit (JDK) installed on your system.
- MNIST Dataset: Download the MNIST dataset files (
train-images.idx3-ubyte,train-labels.idx1-ubyte,t10k-images.idx3-ubyte, andt10k-labels.idx1-ubyte) from DATA and place them in thedatadirectory. - Images: Place image into
Imagesfolder for which you wants to train the network
-
Clone the Repository:
git clone https://github.com/DilipKhunti/MNIST-Neural-Network-Java.git cd MNIST-Neural-Network-Java -
Compile the Code:
javac -d bin src/**/*.java
-
Run the Application:
java -cp bin NeuralNetworkApp
When you run the application, you will be presented with the following menu:
- Train Neural Network for MNIST: Train the neural network on the MNIST training dataset.
- Test Neural Network for MNIST: Test the trained neural network on the MNIST test dataset.
- Test Neural Network for Image: Test the network on a custom image of a handwritten digit.
- Exit Program: Exit the application.
When you choose to train the network, the program will read the MNIST training data and train the network using backpropagation. The learning rate is halved after each epoch to help the network converge.
You can test the network on both the MNIST test dataset and custom images. The network's accuracy on the test dataset will be displayed, and the result of the custom image test will be shown along with the expected and actual outputs.
The trained network is automatically saved after training, allowing you to load it later for testing without retraining.
To test the network on a custom image:
- Place the image in the
Imagesdirectory. - The image should be a grayscale image of size 28x28 pixels. If it is not, the program will resize it automatically.
- Choose option
[3] Test Neural Network for Imagein the menu, enter the expected digit value, and the image name.
Enter Choice: 1
# Trains the neural network
Enter Choice: 2
# Tests the network on the MNIST test dataset and displays accuracy
Enter Choice: 3
Enter Digit Value: 7
Enter Image Name: digit7.png
# Tests the network on a custom image of the digit 7
Contributions are welcome! If you find any bugs or want to add features, feel free to open an issue or submit a pull request.
The MNIST dataset is provided by Yann LeCun. The neural network concepts are based on standard machine learning and deep learning principles.