ImageClassifier is a C++ project for classifying MNIST dataset using:
- K-Nearest Neighbors (KNN)
- Simple Neural Network implemented with Tiny-dnn
-
Load and preprocess MNIST dataset
-
Train and evaluate:
- KNN classifier
- Neural Network classifier
-
Display images in ASCII format
-
Evaluate accuracy on test dataset
-
Save and load trained neural network models
-
Predict single images from text files
-
Display true and predicted labels
ImageClassifier/
├─ src/ # Source code
│ ├─ main.cpp # Entry point (train models)
│ ├─ Predict.cpp # Predict single image from text file
│ ├─ PredictProb.cpp # Predict and display true label with ASCII image
│ ├─ dataset.cpp / dataset.hpp # MNIST dataset loader
│ ├─ knn_classifier.cpp / knn_classifier.hpp # KNN implementation
│ ├─ SimpleNN.cpp / SimpleNN.hpp # Neural network (Tiny-dnn)
├─ include/ # Header files
├─ data/ # MNIST dataset files / image.txt
├─ build/ # Compiled executable output / saved model
└─ README.md # Project documentation
- C++17 compatible compiler (e.g.,
g++) - Tiny-dnn library (for neural network)
- OpenMP (optional, for multi-threading)
git clone "https://github.com/udaykiriti/ImageClassifier.git"
cd ImageClassifierKNN version:
g++ src/main.cpp src/dataset.cpp src/knn_classifier.cpp -Iinclude -O2 -std=c++17 -fopenmp -o build/ImageClassifier
./build/ImageClassifierNeural Network version (Tiny-dnn installed in D:\ImageClassifier\tiny-dnn):
g++ src/main.cpp src/dataset.cpp src/SimpleNN.cpp -ID:\ImageClassifier\tiny-dnn -Iinclude -O2 -std=c++17 -fopenmp -o build/ImageClassifier
./build/ImageClassifierPredict single image (Predict.cpp):
g++ src/Predict.cpp src/SimpleNN.cpp src/dataset.cpp -ID:\ImageClassifier\tiny-dnn -Iinclude -O2 -std=c++17 -fopenmp -o build/ImagePredict.exe
./build/ImagePredict.exePredict with true label and ASCII image (PredictProb.cpp):
g++ src/PredictProb.cpp src/SimpleNN.cpp src/dataset.cpp -ID:\ImageClassifier\tiny-dnn -Iinclude -O2 -std=c++17 -fopenmp -o build/ImagePredictProb.exe
./build/ImagePredictProb.exeThe project uses the MNIST dataset of handwritten digits. Place the dataset files in the data/ folder:
| File | Description |
|---|---|
train-images-idx3-ubyte |
Training images |
train-labels-idx1-ubyte |
Training labels |
t10k-images-idx3-ubyte |
Test images |
t10k-labels-idx1-ubyte |
Test labels |
For Predict.cpp and PredictProb.cpp, images must be converted to text files of 784 normalized pixel values (0-255).
- Compile as described above.
- Train the model using
main.cpp. - Predict a single image:
./build/ImagePredict.exe- Predict with true label and ASCII image:
./build/ImagePredictProb.exeExample Output (PredictProb.exe):
Predicted label: 5
True label: 5
Prediction is correct!
ASCII Image:
............................
...........@@@...#@@#.......
...........#@@...#@@#.......
...........#@@*..#@@#.......
............##...*##........
.............****##.........
............................
Loading MNIST dataset...
Training KNN classifier...
Accuracy on test set: 95.2%
Sample predictions:
Image 0: 7, Predicted: 7
Image 1: 2, Predicted: 2
...
This project is licensed under the MIT License.