A simple C++ image classification application using LibTorch (PyTorch's C++ API) and OpenCV. This application loads a pre-trained TorchScript model and classifies images through a command-line interface.
- Modular Design: Clean class-based architecture with separation of concerns
- LibTorch Integration: Uses PyTorch's C++ API for model inference
- OpenCV Support: Efficient image loading and preprocessing
- Command-Line Interface: Easy-to-use CLI with multiple options
- Top-K Predictions: Shows multiple prediction results with confidence scores
- Label Support: Optional label file support for human-readable class names
.
├── CMakeLists.txt # CMake build configuration
├── README.md # This file
├── include/
│ └── ImageClassifier.h # ImageClassifier class header
└── src/
├── ImageClassifier.cpp # ImageClassifier implementation
└── main.cpp # Main application with CLI
-
CMake (version 3.14 or higher)
# Ubuntu/Debian sudo apt-get install cmake # macOS brew install cmake
-
LibTorch (PyTorch C++ API)
- Download from: https://pytorch.org/get-started/locally/
- Choose the version appropriate for your system (CPU or CUDA)
- Extract to a location on your system
Example for Linux (CPU):
wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.0.0%2Bcpu.zip unzip libtorch-cxx11-abi-shared-with-deps-2.0.0+cpu.zip
-
OpenCV (version 4.x recommended)
# Ubuntu/Debian sudo apt-get install libopencv-dev # macOS brew install opencv
git clone https://github.com/zhaoxinyi02/AI-test.git
cd AI-testmkdir build
cd build# Set CMAKE_PREFIX_PATH to your LibTorch installation directory
cmake -DCMAKE_PREFIX_PATH=/path/to/libtorch ..
# Example:
# cmake -DCMAKE_PREFIX_PATH=/home/user/libtorch ..cmake --build . --config ReleaseAfter successful build, the executable image_classifier will be in the build directory.
./image_classifier -m <model_path> -i <image_path>Options:
-m, --model <path> Path to TorchScript model file (required)
-i, --image <path> Path to image file to classify (required)
-l, --labels <path> Path to labels file (optional)
-k, --topk <number> Number of top predictions to show (default: 5)
-h, --help Show help message
-
Basic classification with a model:
./image_classifier -m model.pt -i cat.jpg
-
Classification with labels and top-3 predictions:
./image_classifier -m resnet18.pt -i dog.jpg -l imagenet_labels.txt -k 3
-
Show help:
./image_classifier --help
To use a model with this application, you need to convert it to TorchScript format (.pt file).
Example Python script to convert a pre-trained ResNet18 model:
import torch
import torchvision.models as models
# Load a pre-trained model
model = models.resnet18(pretrained=True)
model.eval()
# Create a sample input
example_input = torch.rand(1, 3, 224, 224)
# Trace the model
traced_model = torch.jit.trace(model, example_input)
# Save the traced model
traced_model.save("resnet18.pt")
print("Model saved as resnet18.pt")For ImageNet-based models, create a text file with one label per line (1000 lines for ImageNet):
tench
goldfish
great white shark
tiger shark
hammerhead
...
You can download ImageNet labels from various sources online.
The application preprocesses images with the following specifications:
- Input size: 224x224 pixels (configurable in code)
- Color format: RGB
- Normalization: ImageNet mean
[0.485, 0.456, 0.406]and std[0.229, 0.224, 0.225] - Tensor format: NCHW (batch, channels, height, width)
If your model uses different preprocessing, modify the constants in ImageClassifier.h.
=== AI Image Classifier Application ===
Built with LibTorch and OpenCV
Loading model from: resnet18.pt
Model loaded successfully!
Loaded 1000 labels
Classifying image...
Processing image: cat.jpg
Image size: 800x600
=== Classification Results ===
1. tabby cat (class 281) - Confidence: 45.23%
2. Egyptian cat (class 285) - Confidence: 32.15%
3. tiger cat (class 282) - Confidence: 18.67%
4. Persian cat (class 283) - Confidence: 2.89%
5. lynx (class 287) - Confidence: 0.54%
Classification completed successfully!
- Ensure
CMAKE_PREFIX_PATHpoints to your LibTorch directory - The path should contain
share/cmake/Torch/TorchConfig.cmake
- Install OpenCV development package for your system
- On Ubuntu:
sudo apt-get install libopencv-dev - On macOS:
brew install opencv
- Ensure model file is in TorchScript format (
.pt) - Verify image file exists and is in a supported format (JPG, PNG, etc.)
- Check that the model expects 224x224 input size
- Use the LibTorch version matching your compiler's C++11 ABI
- Download either
cxx11-abiorpre-cxx11-abiversion accordingly
The main class responsible for:
- Loading TorchScript models
- Image preprocessing
- Running inference
- Managing class labels
- Command-line argument parsing
- User interaction
- Result formatting and display
This project is provided as-is for educational and development purposes.
Feel free to submit issues and enhancement requests!