Skip to content

udaykiriti/ImageClassifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ImageClassifier ImageClassifier

ImageClassifier is a C++ project for classifying MNIST dataset using:

  • K-Nearest Neighbors (KNN)
  • Simple Neural Network implemented with Tiny-dnn

Flow...

  • 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


Project Structure ---------------

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

Libraries

  • C++17 compatible compiler (e.g., g++)
  • Tiny-dnn library (for neural network)
  • OpenMP (optional, for multi-threading)

Installation & Compilation

1. Clone the repository

git clone "https://github.com/udaykiriti/ImageClassifier.git"
cd ImageClassifier

2. Compile

KNN version:

g++ src/main.cpp src/dataset.cpp src/knn_classifier.cpp -Iinclude -O2 -std=c++17 -fopenmp -o build/ImageClassifier
./build/ImageClassifier

Neural 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/ImageClassifier

Predict 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.exe

Predict 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.exe

Dataset

The 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).


Usage

  1. Compile as described above.
  2. Train the model using main.cpp.
  3. Predict a single image:
./build/ImagePredict.exe
  1. Predict with true label and ASCII image:
./build/ImagePredictProb.exe

Example Output (PredictProb.exe):

Predicted label: 5
True label: 5
Prediction is correct!

ASCII Image:
............................
...........@@@...#@@#.......
...........#@@...#@@#.......
...........#@@*..#@@#.......
............##...*##........
.............****##.........
............................

Example Output

Loading MNIST dataset...
Training KNN classifier...
Accuracy on test set: 95.2%
Sample predictions:
Image 0: 7, Predicted: 7
Image 1: 2, Predicted: 2
...

License

This project is licensed under the MIT License.


References

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published