This project combines classification (ResNet50) and detection (YOLOv8) for comprehensive car part recognition.
For single-class image classification with ontology enrichment.
Install dependencies:
pip install -r requirements.txtPrepare a model file:
python prepare_model.py --train-dir train --out models/resnet50_pretrained_head.ptTrain classifier:
python train.py --train-dir train --val-dir valid --epochs 10 --batch-size 32 --out models/best_model.ptInfer (top-5 predictions with category):
python infer.py path/to/image.jpg --model models/best_model.pt --topk 5For detecting multiple car parts in a single image with bounding boxes.
If you don't have manual bounding-box annotations, use Class Activation Maps (CAM) to auto-generate them:
python weak_supervision.py --train-dir train --output datasets/detectionThis creates a YOLO-format dataset in datasets/detection/ with:
images/- all images from train/labels/- YOLO format bounding boxes (class_id cx cy w h)
Optional: If you have manual YOLO annotations already, organize them as:
datasets/detection/
images/
image1.jpg
image2.jpg
...
labels/
image1.txt
image2.txt
...
python yolo_train.py --dataset datasets/detection --epochs 50 --batch-size 16 --model mParameters:
--model: n (nano), s (small), m (medium), l (large), x (xlarge) - trade-off between speed and accuracy--epochs: training epochs (50-100 recommended)--batch-size: batch size (adjust based on GPU memory)--device: GPU device id or 'cpu'
Training outputs are saved to runs/detect/car_parts/
Run detection on a single image:
python yolo_infer.py path/to/image.jpg --model runs/detect/car_parts/weights/best.pt --topk 10 --visualizeParameters:
--conf: confidence threshold (0.0-1.0, default 0.5)--iou: NMS IOU threshold (default 0.45)--topk: return top-K detections--visualize: save annotated image todetection_output.jpg
Output: For each detected part:
- Bounding box (pixel and normalized coordinates)
- Class name and confidence
- Ontology category and synonyms
train/
AIR COMPRESSOR/
img1.jpg
img2.jpg
ALTERNATOR/
img1.jpg
...
valid/
AIR COMPRESSOR/
img1.jpg
...
- Ontology Integration: All 50 car parts organized into 18 semantic categories (Engine, Brakes, Electrical, etc.)
- Multi-Part Detection: YOLOv8 detects multiple parts per image with bounding boxes
- Weak Supervision: Auto-generate pseudo-labels from single-class images using CAM
- Mixed Precision Training: Faster training with FP16
- Scheduled Learning Rate: Adaptive LR reduction on validation plateau
- Class Weighting: Handles imbalanced datasets
- Annotate manually (optional): Use tools like Roboflow or Label Studio for high-quality bounding boxes.
- Ensemble: Train multiple YOLOv8 sizes and average predictions.
- Fine-tune on edge: Convert to ONNX/TensorRT for deployment on mobile/edge devices.
- Monitor: Log metrics to WandB or TensorBoard.
- Classification (
infer.py) treats each image as containing one part type. - Detection (
yolo_infer.py) finds all parts in an image—use this for real-world scenarios. - Ontology (
ontology.json) maps part names to categories and synonyms for intelligent recognition.