• 2 min read

Industrial Vision Agent

A casting-defect inspector: a ViT classifies the part, Grad-CAM and a U-Net locate the defect, and a Gemini agent writes the report.

PyTorchVision TransformersGrad-CAMU-NetGeminiMCP

Input, Grad-CAM attention and U-Net segmentation for a defective and an OK casting

What it is

You give it a photo of a metal casting. A Gemini agent runs up to three checks and writes an inspection report.

StepToolOutput
ClassifyViT-Tinydefective or OK, with confidence
LocateResNet18 + Grad-CAMboxes around the suspicious regions
MeasureU-Netpercent of the surface covered by the defect

The agent only runs steps 2 and 3 when step 1 says the part is defective. Every number in the report comes from a tool call, and the report includes the full tool trace, so a reviewer can see exactly what the system looked at.

A terminal run of the inspector agent on a defective casting

The same tools are exposed four ways: a CLI, a FastAPI service, a Gradio app and an MCP server.

Segmentation without pixel labels

The dataset only says whether each part is defective. It has no masks. So the pipeline makes its own:

  1. run Grad-CAM on the classifier for each defective image;
  2. threshold the heatmap into a mask, which gave 3,758 pseudo-masks;
  3. train a U-Net, written from scratch, on those masks.

Its 0.706 Dice is measured against the Grad-CAM pseudo-masks, not hand-drawn ones, so it shows how well the U-Net learned the heatmaps. It isn’t a true defect-segmentation score.

Results

From the held-out test split, recorded in models/metrics.json:

ModelTest F1Notes
ResNet18 baseline92.4%
ViT-Tiny, fine-tuned99.67%recall 99.56%, precision 99.78%
MobileNetV3, distilled from ResNet1899.11%beats its teacher with 94.9% fewer parameters
ViT-Tiny, INT822.2 MB → 6.3 MB (71.7% smaller), 1.34x faster
U-Net on pseudo-masksDice 0.706see above

The whole six-stage pipeline (classifiers, distillation, pseudo-masks, U-Net, quantization) trained on one NVIDIA A10 in about 48 minutes. It can resume after a crash.

Weights are on Hugging Face.

Back to top