This project is a Flask web application that enables users to upload images for classification of the MNIST Fashion dataset using a custom neural network. Additionally, users can draw numbers which are processed and displayed. The neural network is built from scratch without using high-level libraries like Keras, relying instead on custom implementations of neural network layers.
- Image Classification: Upload an image to classify it using a custom-built neural network.
- Number Drawing: Draw a number and view the generated image.
- Custom Neural Network: Implemented using custom classes for convolutional layers, dense layers, activation functions, and more.
- Python 3.x
- Flask
- Flask-WTF
- Pandas
- NumPy
- Pillow
- Matplotlib
-
Clone the repository:
git clone https://github.com/koushik2k3/Neural-Networks.git cd Neural-Networks -
Install the required packages:
It is recommended to use a virtual environment. Install the dependencies using
pip:pip install -r requirements.txt
Create a
requirements.txtfile with the following content if it does not already exist:Flask Flask-WTF pandas numpy pillow matplotlib
-
Prepare the dataset and weights:
Ensure you have the following files in the project directory:
weights1.xlsxweights2.xlsxbias1.xlsxbias2.xlsxweights.xlsx
These files are used to initialize the neural network's weights and biases.
-
Run the application:
Start the Flask development server:
python app.py
By default, the server will run on
http://127.0.0.1:4000/.
The drawing feature allows users to create images of numbers and view them. Here’s how it works:
-
Number Drawing:
- When a number is entered on the
/drawroute, the application processes it to generate an image. - The
drawfunction uses a custom neural network to produce an image representing the given number. The network performs a forward pass with the number encoded as a one-hot vector. - The resulting output is a 28x28 image, which is saved as a PNG file. This file is stored in the
static/drawingsdirectory.
- When a number is entered on the
-
Multiple Digits:
- For numbers with multiple digits, the
drawmorethan1digitfunction processes each digit separately and concatenates the images to form a composite image. - Each digit is converted into a 28x28 image, which is then placed side by side to create a single image representing the entire number.
- For numbers with multiple digits, the
-
Displaying the Image:
- The generated image is saved and can be accessed through the
/static/drawings/<filename>route. - The filename is generated dynamically based on the number drawn.
- The generated image is saved and can be accessed through the
This project demonstrates how to build a Convolutional Neural Network (CNN) from scratch. Here's a breakdown of how the custom CNN works:
-
Network Architecture:
The custom CNN is composed of the following layers:
- Convolutional Layer: Applies convolution operations to extract features from input images.
- Activation Layer: Applies activation functions (e.g., ReLU, sigmoid) to introduce non-linearity.
- Reshape Layer: Reshapes the output of the convolutional layer to fit the input requirements of fully connected layers.
- Dense Layer: Implements fully connected layers to process the features extracted by the convolutional layers.
- Softmax Layer: Provides a probability distribution over classes for classification tasks.
-
Layer Details:
-
Convolutional Layer:
- Custom
Convolutionalclass performs 2D convolution operations using kernels and biases. - Implemented with methods for forward and backward propagation, weight updating, and saving/loading weights.
- Custom
-
Dense Layer:
- Custom
Denseclass implements fully connected layers with weight and bias parameters. - Includes methods for forward and backward propagation and weight updates.
- Custom
-
Activation Functions:
- Implementations for various activation functions such as ReLU and sigmoid are included.
- Custom classes for activation functions handle both forward and backward propagation.
-
Softmax Layer:
- Custom
Softmaxclass applies the softmax function to produce class probabilities from the network output.
- Custom
-
-
Training and Prediction:
-
Training:
- The
trainfunction performs training using backpropagation and gradient descent. - Loss functions such as mean squared error and binary cross-entropy are used for calculating the error and gradients.
- The
-
Prediction:
- The
predictfunction applies the trained network to input data and provides output predictions.
- The
-
-
Upload Image for Classification:
Visit the root URL to access the image upload form. Upload an image from the fashion_mnist folder, and it will be classified using the custom neural network. The classification result will be displayed.
-
Draw a Number:
Navigate to the
/drawroute. Enter a number to draw and submit the form. The generated image of the number will be displayed.
app.py- The main Flask application file.libv2.py- Contains custom neural network implementations, including layers and activation functions.libv3.py- Additional custom functions or utilities (if applicable).static/- Directory for static files, including uploaded images and generated drawings.templates/- Directory for HTML templates.weights1.xlsx,weights2.xlsx,bias1.xlsx,bias2.xlsx,weights.xlsx- Files with pre-trained model weights and biases.
Contributions are welcome! Feel free to open issues or submit pull requests for improvements or bug fixes.
- This project utilizes custom implementations of neural network components.
- Inspired by foundational concepts in neural networks and machine learning.

