A from-scratch implementation of a Convolutional Neural Network (CNN) for image classification, written in C++ with Python for preprocessing database.
This project is a complete, from-scratch implementation of a Convolutional Neural Network (CNN) in C++. It is designed to be a learning tool for understanding the inner workings of CNNs without relying on high-level deep learning libraries.
- Custom CNN Implementation: All neural network components are built from the ground up in C++.
- Convolutional Layer: Extracts features from images using filters.
- Activation Functions: Implements ReLU and Softmax.
- Max Pooling: Reduces the spatial dimensions of the feature maps.
- Dense Layer: A fully connected layer for classification.
- Backpropagation: Custom implementation for training the network.
- Python Preprocessing: Uses Python and the Pillow library to preprocess images.
The CNN architecture is composed of the following layers:
- Input Image (64x64 grayscale)
- Convolutional Layer with custom filters.
- ReLU Activation Function to introduce non-linearity.
- Max Pooling Layer to downsample the feature maps.
- Flatten Layer to convert the 2D feature maps into a 1D vector.
- Dense (Fully Connected) Layer to perform classification.
- Softmax Activation Function to output class probabilities.
This project is designed to run on Windows due to its use of the Windows API for file and folder operations.
- A C++ compiler that supports C++17 (e.g., MinGW g++ or Visual C++).
- Python 3.x.
- The Pillow Python library.
You can install Pillow using pip:
pip install Pillow
- Make sure you have a C++ compiler (like MinGW g++) and Python installed and configured in your system's PATH.
- Open a terminal or command prompt in the root directory of the project.
- Compile the C++ code using the following command:
g++ source/main.cpp -o ImageClassification.exe -lcomdlg32 -lshell32 -lole32 -static-libgcc -static-libstdc++ -std=c++17
This will create an ImageClassification.exe
executable in the root directory.
Run the compiled application from the terminal:
./ImageClassification.exe
The application provides a command-line menu with the following options:
- Train On New Dataset: Train the model on a new set of images.
- Predict Image: Use the trained model to predict the class of a single image.
- Clear Screen: Clears the console.
- Exit: Exits the application.
- Select option
1
from the menu. - A folder selection dialog will appear. Choose the root folder of your dataset.
- The dataset must be organized into subdirectories, where each subdirectory's name corresponds to a class label. For example:
Test_Data/ ├── axethrowing/ │ ├── 1.jpg │ └── 2.jpg └── balancebeam/ ├── 1.jpg └── 2.jpg
- The application will then ask for the number of training epochs. Enter a number and press Enter.
- The model will start training. The preprocessing scripts will create a
pixels
directory with.txt
files containing the normalized pixel data.
- After training the model, you can predict a single image by selecting option
2
. - The application will prompt you for the path to an image file.
- Enter the full path to the image and press Enter.
- The model will output the predicted class for the image.
- A
pixel
directory will be created during this process and should be automatically deleted afterward.
.
├── source/ # C++ and Python source code
│ ├── Backprogation_Layer/
│ ├── Convolution_Layer/
│ ├── Dence_Layer/
│ ├── Preprocessing/
│ └── WeigthsBiases/
├── Test_Data/ # Sample data for testing
└── ...
A pure Python version of this project is also available. You can find it here: Image-Classification-PY