A practical pipeline demonstrating:
- Caltech101 usage for single-label image classification
- ResNet-50 (CNN-based) for end-to-end training
- GPU acceleration (PyTorch, Torchvision, etc.) on PC/server
- Edge device inference on resource-constrained hardware (Kria KV260, Jetson Orin NX, Raspberry Pi 5)
No separate dataset download required – the code automatically fetches Caltech101 if it’s not already present.
- Overview
- Features
- Project Structure
- Installation
- Usage
- Results
- Troubleshooting
- Contributing
- License
- References
This repository, EdgeAI-Classification-pipeline, demonstrates ResNet-50 classification on Caltech101. The code automatically downloads Caltech101 through the PyTorch torchvision.datasets.Caltech101 class if it’s not already in your data/ folder. After training, the project saves a checkpoint (resnet.pth) that you can copy to various edge devices (e.g., Jetson, Kria, Raspberry Pi) to perform real-time camera inference.
- Automatic Dataset Download: No manual steps; if Caltech101 is absent, PyTorch downloads it.
- ResNet-50: A CNN architecture with 102 output classes (101 categories + background).
- Single-Label Classification: One predicted label per image.
- GPU or CPU: By default, code can run on CPU or CUDA if available.
- Edge Inference: Provided script for real-time camera classification on resource-constrained devices.
You have seven main Python files:
.
├── dataPreprocessLoader.py # Loads and splits Caltech101; auto-downloads if needed
├── modelTraining.py # Main training loop for ResNet-50
├── inference_edge.py # Real-time camera inference on edge devices
├── validation.py # Evaluation function for validation data
├── resnet50.py # Defines custom ResNet-50 model
├── residual_block.py # Defines the building blocks for ResNet
├── plots.py # Hardcoded plotting utility (loss/accuracy)
└── dependencies.txt # Dependencies needed for this project
- Install Anaconda/Miniconda if you haven’t already.
- Create a new environment (example name:
edgeai):conda create -n edgeai python=3.9
- Activate it:
conda activate edgeai
Within your activated conda environment:
conda install --file dependencies.txtOr, if you prefer pip:
pip install -r dependencies.txt(Make sure you are in the conda environment so packages are installed there.)
GPU/CUDA Note
If you’re running on GPU and want CUDA acceleration, ensure your PyTorch install is compatible with your installed CUDA driver. You can use the command:conda install pytorch torchvision cudatoolkit=<version> -c pytorchor install a compatible wheel (e.g., from PyPI) that matches your CUDA version. Installing via
pip install -r dependencies.txtalone may not set up CUDA support automatically. You also need to have the appropriate NVIDIA drivers and CUDA toolkit for your system.
Typical Requirements (already in dependencies.txt):
- Python 3.9 (or similar)
- PyTorch (with
torchvision) - OpenCV (e.g.,
opencv-python) - Matplotlib (for plotting)
- scikit-learn (for classification reports)
No manual download required. When you run dataPreprocessLoader.py, the Caltech101 dataset is automatically fetched and stored in a data/ folder inside your working directory:
dataset = Caltech101(root='data', download=True, transform=transform)This script splits the dataset into train and validation sets (80/20) and creates PyTorch DataLoader objects (trainDataloader, valDataloader).
Run:
python modelTraining.pyWhat happens:
- Imports
trainDataloaderandvalDataloaderfromdataPreprocessLoader.py. - Builds a ResNet-50 with 102 output classes (
resnet50.py). - Trains on CPU or CUDA (change
device = torch.device("cpu")totorch.device("cuda")if you want GPU). - Prints training/validation loss and accuracy each epoch.
- Saves the final model to
resnet.pth.
The training code calls evaluate(...) from validation.py after each epoch to compute validation loss and accuracy. You can also modify validation.py to produce confusion matrices or other metrics.
- Copy
resnet.pthto your device (e.g.,scpto Jetson). - Install PyTorch (CPU or GPU version) and OpenCV on that device.
- Run:
python inference_edge.py resnet.pth
- This opens your default camera (ID=0), preprocesses each frame to 224×224, and feeds it into the model. The script prints and overlays the numeric class ID. (You can add a label mapping if desired.)
- The project logs training and validation accuracy for each epoch.
- Caltech101 is relatively small, so you can train quickly on CPU or GPU.
- If you want to visualize metrics, see the plots.py script.
- No GPU Detected
- By default,
modelTraining.pysetsdevice = torch.device("cpu"). Change totorch.device("cuda")if you have a supported GPU.
- By default,
- Dataset Download Failures
- If your internet connection is restricted or the download fails, ensure you can reach the PyTorch data mirrors or manually place the dataset in the
data/folder.
- If your internet connection is restricted or the download fails, ensure you can reach the PyTorch data mirrors or manually place the dataset in the
- Edge Device Issues
- For Jetson/ARM boards, install PyTorch specifically built for that architecture.
- Fork this repository.
- Create a new branch for your feature/fix:
git checkout -b feature-my-improvement
- Commit your changes and push to your fork:
git commit -m "Add my new feature" git push origin feature-my-improvement - Open a Pull Request into the main branch.
We welcome suggestions, bug reports, and community contributions!
This project is licensed under the MIT License. You’re free to use, modify, and distribute the code as allowed by that license.
- Caltech101 Dataset – PyTorch Docs (Caltech101)
- ResNet Paper – Deep Residual Learning for Image Recognition (He et al.)
- PyTorch – Docs
- NVIDIA Jetson – Developer Site
- Xilinx Kria KV260 – Documentation
- Raspberry Pi – Official Site
Thank you for visiting EdgeAI-Classification-pipeline! If you have any questions or issues, feel free to open an issue or reach out.