5.1 KiB
Shiny Icon Detection Issue - Debug Report
Problem Summary
The ONNX model with NMS=True is not detecting shiny icons, while the original .pt model detects them at 0.97 confidence. This investigation aimed to identify why the ONNX model fails to detect shiny icons (class 50).
Root Cause Identified ✅
The built-in NMS in the ONNX model is filtering out shiny icon detections because they don't make it into the top 300 highest-confidence detections.
Investigation Process
1. Initial Debugging Setup
- Added
DEBUG_SHINY_DETECTION = trueflag to enable detailed logging - Lowered confidence threshold from 0.45f to 0.25f temporarily
- Added special debug logging for shiny_icon (class 50) candidates
2. Raw Model Output Analysis
Key Discovery: The ONNX model output format is 1 x 300 x 6 instead of the expected 8400 x 99:
- Expected (raw model): 8400 detections × (4 coords + 95 classes) = 831,600 values
- Actual (NMS model): 300 final detections × (4 coords + 1 confidence + 1 class_id) = 1,800 values
This confirmed the model has built-in NMS that only returns the top 300 detections.
3. Class Detection Analysis
Test Results from logs:
🔬 [NMS CLASSES] Detected classes: [29, 32, 33, 47, 48, 62, 63, 64, 67, 68, 69, 70, 71, 72]
❌ [NO SHINY] Shiny icon (class 50) not found in NMS output
The model consistently detects other classes but class 50 (shiny_icon) never appears in the NMS output across all preprocessing methods (ultralytics, enhanced, sharpened, original).
4. Model Performance Comparison
- .pt model: Detects shiny icon at 0.97 confidence
- ONNX model: Shiny icon completely absent from top 300 NMS results
- Other detection classes: Working fine in ONNX (20-22 detections per method)
Technical Details
Debug Infrastructure Added
- Raw output inspection: Logs tensor dimensions and output statistics
- Class detection tracking: Shows all detected classes in NMS output
- Low-confidence checking: Searches for any class 50 predictions regardless of confidence
- Multi-method analysis: Tests across all preprocessing methods
Code Changes Made
YOLOOnnxDetector.kt: Added comprehensive debugging indetectWithPreprocessing()- Debug flags:
DEBUG_SHINY_DETECTION, loweredCONFIDENCE_THRESHOLD - Enhanced logging for NMS output format analysis
Why NMS=True Was Chosen
The NMS=True version provides "drastically better results" for general detection, so reverting to NMS=False isn't ideal.
Proposed Solutions
Option 1: Hybrid Model Approach (Recommended)
- Primary model: Keep NMS=True for general detection performance
- Fallback model: Add NMS=False model specifically for rare classes like shiny icons
- Detection strategy: Run general detection first, then targeted detection for missing rare classes
Option 2: NMS Parameter Tuning
Re-export ONNX model with modified NMS parameters:
model.export(format='onnx', nms=True, max_det=500, conf=0.1) # Increase max detections, lower confidence
Option 3: Post-Processing Enhancement
- Export NMS=False model temporarily to verify shiny detection capability
- Implement custom NMS that preserves rare class detections
- Use class-aware confidence thresholds
Option 4: Model Re-training Consideration
If shiny icons are consistently low-confidence, consider:
- Augmenting training data with more shiny examples
- Adjusting class weights during training
- Using focal loss for rare classes
Next Steps (Prioritized)
Immediate (Next Session)
- Test NMS=False export to confirm model can detect shiny icons in raw output
- Document baseline performance comparison between NMS=True vs NMS=False
- Verify class mapping is correct in ONNX conversion
Short Term
- Implement hybrid approach with both models if NMS=False confirms shiny detection
- Optimize detection pipeline to minimize performance impact
- Add class-specific confidence thresholds
Long Term
- Model optimization: Fine-tune NMS parameters during export
- Training improvements: Address rare class detection in model training
- Performance monitoring: Track detection rates for all rare classes
Files Modified
YOLOOnnxDetector.kt: Added debugging infrastructure- Branch:
feature/debug-shiny-pokeball-detection - Commits: Multiple debugging iterations with detailed logging
Test Environment
- Device: Android device with ONNX Runtime
- Test image: Contains shiny icon detectable at 0.97 confidence by .pt model
- ONNX model:
best.onnxwith NMS=True, 95 classes, 300 max detections
Conclusion
The investigation successfully identified that the ONNX model with built-in NMS is capable of detecting objects effectively, but the aggressive NMS filtering (top 300 only) is preventing shiny icon detections from appearing in the final output. The model architecture and class mapping appear correct, as other classes are detected properly.
The solution requires either adjusting the NMS parameters during model export or implementing a hybrid detection approach to preserve rare class detections while maintaining the superior general performance of the NMS=True model.