R&D prototype · open/synthetic data

ITS AI XAI - Explainable PCB Defect Detection

TL;DR. Every defect verdict ships with a faithful explanation, not just a score. A two-stage pipeline fuses 603 SECOM sensor features with YOLO visual inspection through an XGBoost meta-classifier, hitting F1 ≈0.95 on a 236-sample hold-out at 67.3 ms end-to-end CPU latency, while SHAP attributions validated against the model's own importances (Pearson > 0.95) make the explanations demonstrably faithful. Scope is stated up front: a solo R&D system built and validated entirely on open SECOM and DeepPCB benchmarks (sensor-image pairing is synthetic), not on real-factory data.
≈0.95 meta-classifier F1 XGBoost meta-classifier, n=236 hold-out
67.3 ms end-to-end latency CPU /predict
~96% defect recall FAIL class, n=236 hold-out
15.6 ms explanation latency SHAP /explain
System architecture diagram
Multimodal fusion pipeline architecture: 603 sensor features processed by a Random Forest and a feature-selection module, fused with YOLOv11 visual detection through an XGBoost meta-classifier (F1 ≈0.95), feeding a SHAP / Grad-CAM / counterfactual explainability layer for decision support.

Problem

PCB manufacturing quality control needs defect detection that engineers can trust and act on, not just a black-box pass/fail verdict. Sensor telemetry alone (603 raw features) and visual inspection alone each miss defects the other catches, and end-to-end deep models do not give engineers the interpretable, per-prediction feature attribution they need for root-cause analysis and process correction. The system had to combine both signal types, stay fast enough for inline use, and explain every decision in a way a process engineer could use to adjust the line — and it was built and benchmarked entirely on open/synthetic data (open SECOM sensor data + open DeepPCB images, paired synthetically), not on data from any real factory.

What I built

  • Built end-to-end as the solo developer: the multimodal fusion architecture, the XGBoost meta-classifier that fuses Random Forest (sensor) and YOLO (vision) outputs into 8 fusion features, the FastAPI backend, and the Streamlit frontend (design choices below).
  • Engineered the feature-reduction pipeline that cuts the 603 raw SECOM sensor features to 164 (-72.8%) via tiered missing-value handling and importance-based selection, improving prediction quality and uncertainty calibration.
  • Developed the explainability layer surfaced through a 4-tab Streamlit dashboard: SHAP waterfall attribution and a counterfactual what-if interface, with Plotly visualizations for defect analysis.

Design decisions

  1. XGBoost meta-classifier over end-to-end deep learning

    Context: interpretable predictions are needed for root-cause analysis. Options weighed: an end-to-end neural network vs. a two-stage fusion with an XGBoost meta-classifier. Rationale: XGBoost enables SHAP feature attribution served by a dedicated /explain endpoint at 15.6 ms per request, whereas deep learning would require expensive gradient-based explanations; the tree approach delivered the accuracy targets at 67.3 ms end-to-end /predict CPU latency.

  2. SHAP attribution with consistency validation against the model

    Context: a single explanation method may carry biases, and the feature-importance ranking needs to be trustworthy. Rationale: SHAP feature-importance rankings were validated against the Random Forest model's own built-in importance scores, reaching a Pearson correlation above 0.95, giving confidence the explanations reflect what the model actually learned; outputs are kept domain-agnostic so real sensor names can be mapped in later.

    Three-stage XAI flow: a PASS/DEFECT verdict, a SHAP waterfall attribution validated against the model's own importances (Pearson > 0.95), and a counterfactual showing the minimal change to flip the verdict.
    Every prediction carries a faithful explanation: a SHAP waterfall whose feature ranking matches the model's own built-in importances (Pearson > 0.95), plus a counterfactual that turns the verdict into an actionable process change.
  3. Dimensionality reduction of the 603-feature sensor stream

    The SECOM sensor stream carries 603 features (590 original + 13 engineered). A tiered pipeline (removing >90%-missing features, KNN/median imputation for the rest, and importance-based selection) narrowed this to 164 features (-72.8%), which cut dimensionality and inference cost while actually improving SECOM prediction quality and uncertainty calibration.

  4. Two-stream fusion: Random Forest (sensor) + YOLO (vision)

    Sensor telemetry is handled by a Random Forest and visual inspection by a YOLO detector (the trained YOLOv8m benchmarked at mAP@50 93.51% on the open DeepPCB dataset; a lighter YOLOv11n is used at inference time); the XGBoost meta-classifier fuses both streams into 8 features so defects visible in only one modality are still caught, reaching F1 ≈0.95 on the 236-sample hold-out test set.

In operation

XGBoost training pipeline
XGBoost meta-classifier training pipeline: data preprocessing, cross-validation, hyperparameter tuning, and evaluation, fusing Random Forest sensor analysis with YOLOv11 visual detection to reach the ≈0.95 F1-score.
SHAP waterfall explanation
SHAP waterfall visualization showing each feature's contribution to a single prediction: red bars push toward DEFECT, blue bars push toward PASS, and the cumulative effect determines the final classification.
Counterfactual analysis interface
Interactive counterfactual interface for what-if exploration: it identifies the minimal parameter changes needed to flip a prediction, surfacing actionable process-optimization recommendations.
Explainability output (SHAP attributions)
SHAP explainability output for a single prediction: per-feature attributions and the resulting feature-importance ranking.

Engineering foothold

ClaimEvidence
Meta-classifier F1-score of 0.9462 on the 236-sample hold-out test set (+26% above the F1 >= 0.75 target)FINAL_VALIDATION_REPORT.md lines 462, 468, 505-518: F1 0.9462 measured on a 236-sample hold-out test set (70/15/15 stratified split; 5-fold CV for model selection), target F1 >= 0.75 so +26% margin. Confirmed in README.md line 14/39.
End-to-end /predict latency of 67.3 ms on CPU (44-45% under the <120 ms target)FINAL_VALIDATION_REPORT.md lines 604-635: mean 67.3 ms over 10 runs (P50 66.5, P95 78.2, P99 101.0) on CPU-based inference, end-to-end /predict including SECOM RF, YOLO, meta-classifier, and API overhead; target <120 ms (PASS -44%). Confirmed README.md lines 14/40/257.
YOLOv8m visual detection mAP@50 of 93.51% on the open DeepPCB benchmarkFINAL_VALIDATION_REPORT.md lines 325-341: YOLOv8m (medium) trained on 999 DeepPCB image pairs, validated on 501, mAP@50 93.51% (also mAP@50-95 63.50%, precision 89.74%, recall 90.44%). The 93.51% belongs to YOLOv8m, while the inference model is YOLOv11n (lines 632, API_IMPLEMENTATION_SUMMARY.md line 52).
Meta-classifier defect (FAIL) recall of 95.91% on the 236-sample test setFINAL_VALIDATION_REPORT.md lines 214-220, 429, 469, 508: meta-classifier recall 0.9591 on the 236-sample test set (211 of 220 FAIL caught, 9 missed); this is the fused meta-classifier's defect-class recall, not YOLO's. Confirmed README.md lines 21/245.
SHAP feature-attribution latency of 15.6 ms per /explain requestFINAL_VALIDATION_REPORT.md line 599: /explain (SHAP explanations) measured at 15.6 ms, 100% success.
SHAP consistency validated against Random Forest built-in importance (Pearson > 0.95)FINAL_VALIDATION_REPORT.md line 823: SHAP feature-importance rankings match the Random Forest built-in importance scores at Pearson correlation > 0.95; the XAI method table (lines 815-819) lists only SHAP, Counterfactual, and Grad-CAM (no LIME, which is not implemented).
Target-vs-achieved scorecard: F1 ≈0.95 (target 0.75), end-to-end /predict 67.3 ms (target 120 ms), defect recall ~96%, SHAP /explain 15.6 ms, YOLOv8m mAP@50 93.51% on open DeepPCB; P50/P95/P99 latency strip.
Validation scorecard on open SECOM + DeepPCB: F1 ≈0.95 on a 236-sample hold-out (+26% over the 0.75 target) at 67.3 ms end-to-end CPU latency (44% under the 120 ms target), with ~96% defect recall and 15.6 ms SHAP explanations.
Feature selection pipeline
Automated feature-selection pipeline reducing 603 raw sensor features to 164 via tiered missing-value handling (>90%-missing removal, KNN/median imputation) and importance-based selection.

Tech stack

ML / AI
XGBoost (meta-classifier), Random Forest (sensor stream), YOLO (visual inspection - YOLOv8m benchmark, YOLOv11n at inference), scikit-learn, imbalanced-learn (SMOTE), Importance-based feature reduction (603 to 164)
Explainability (XAI)
SHAP (waterfall attribution), Counterfactual analysis
Dashboard / Frontend
Streamlit (4 interactive tabs), Plotly (visualizations)
API / Backend
FastAPI, Uvicorn (REST API, 7 endpoints), pandas, numpy, OpenCV
Data (open / synthetic)
SECOM (open semiconductor-manufacturing sensor dataset), DeepPCB (open PCB defect image benchmark), Synthetic SECOM-DeepPCB pairing (statistical matching)

← All projects