Add trained steel ROI gate and hybrid validation
Browse files- inspection/surface_gate.py +27 -2
- models/steel_surface_detector.pt +3 -0
- training/README.md +10 -2
- training/prepare_surface_gate_dataset.py +53 -24
- training/requirements.txt +0 -1
- training/train_surface_gate_detector.py +4 -2
- yolo11n.pt +3 -0
inspection/surface_gate.py
CHANGED
|
@@ -188,18 +188,43 @@ class SteelSurfaceGate:
|
|
| 188 |
roi_area_ratio=roi_area_ratio,
|
| 189 |
)
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
return SurfaceGateResult(
|
| 192 |
is_steel=True,
|
| 193 |
confidence=float(best_candidate["confidence"]),
|
| 194 |
-
mode="detector",
|
| 195 |
label=str(best_candidate["label"]),
|
| 196 |
reason=(
|
| 197 |
-
f"Steel-surface detector localized an inspection ROI with {float(best_candidate['confidence']):.0%} confidence
|
|
|
|
| 198 |
),
|
| 199 |
metrics={
|
| 200 |
"boxes_detected": float(len(candidates)),
|
| 201 |
"roi_area_ratio": round(roi_area_ratio, 4),
|
| 202 |
"selector_score": round(float(best_candidate["selector_score"]), 4),
|
|
|
|
| 203 |
},
|
| 204 |
roi_bbox=best_candidate["bbox"],
|
| 205 |
roi_area_ratio=roi_area_ratio,
|
|
|
|
| 188 |
roi_area_ratio=roi_area_ratio,
|
| 189 |
)
|
| 190 |
|
| 191 |
+
roi_x, roi_y, roi_width, roi_height = best_candidate["bbox"]
|
| 192 |
+
roi_image = image[roi_y:roi_y + roi_height, roi_x:roi_x + roi_width]
|
| 193 |
+
heuristic_result = self._evaluate_with_heuristic(roi_image)
|
| 194 |
+
if not heuristic_result.is_steel:
|
| 195 |
+
return SurfaceGateResult(
|
| 196 |
+
is_steel=False,
|
| 197 |
+
confidence=float(best_candidate["confidence"]),
|
| 198 |
+
mode="detector+heuristic",
|
| 199 |
+
label=str(best_candidate["label"]),
|
| 200 |
+
reason=(
|
| 201 |
+
"Frame skipped because the localized ROI did not pass the steel-surface texture validation step. "
|
| 202 |
+
"Reduce background content and center the actual material."
|
| 203 |
+
),
|
| 204 |
+
metrics={
|
| 205 |
+
"boxes_detected": float(len(candidates)),
|
| 206 |
+
"roi_area_ratio": round(roi_area_ratio, 4),
|
| 207 |
+
"selector_score": round(float(best_candidate["selector_score"]), 4),
|
| 208 |
+
**heuristic_result.metrics,
|
| 209 |
+
},
|
| 210 |
+
roi_bbox=best_candidate["bbox"],
|
| 211 |
+
roi_area_ratio=roi_area_ratio,
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
return SurfaceGateResult(
|
| 215 |
is_steel=True,
|
| 216 |
confidence=float(best_candidate["confidence"]),
|
| 217 |
+
mode="detector+heuristic",
|
| 218 |
label=str(best_candidate["label"]),
|
| 219 |
reason=(
|
| 220 |
+
f"Steel-surface detector localized an inspection ROI with {float(best_candidate['confidence']):.0%} confidence, "
|
| 221 |
+
"and the ROI passed surface-texture validation."
|
| 222 |
),
|
| 223 |
metrics={
|
| 224 |
"boxes_detected": float(len(candidates)),
|
| 225 |
"roi_area_ratio": round(roi_area_ratio, 4),
|
| 226 |
"selector_score": round(float(best_candidate["selector_score"]), 4),
|
| 227 |
+
**heuristic_result.metrics,
|
| 228 |
},
|
| 229 |
roi_bbox=best_candidate["bbox"],
|
| 230 |
roi_area_ratio=roi_area_ratio,
|
models/steel_surface_detector.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fcf2d6d9662e8ee5765db2d27f313b4155e3688ea946f335af9dd01e4fff91b4
|
| 3 |
+
size 5436442
|
training/README.md
CHANGED
|
@@ -34,7 +34,9 @@ Positive steel sources:
|
|
| 34 |
|
| 35 |
Automatic non-steel/background source:
|
| 36 |
|
| 37 |
-
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
This avoids making you manually collect a non-steel dataset.
|
| 40 |
|
|
@@ -54,7 +56,13 @@ cd "/Users/ravindranadhm/Documents/Projects/steel-surface-inspection copy"
|
|
| 54 |
venv/bin/python training/prepare_surface_gate_dataset.py
|
| 55 |
```
|
| 56 |
|
| 57 |
-
This default mode is the fastest path because it avoids
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
If you want a stronger detector after the first run, rebuild with Severstal included:
|
| 60 |
|
|
|
|
| 34 |
|
| 35 |
Automatic non-steel/background source:
|
| 36 |
|
| 37 |
+
- `torchvision` datasets downloaded automatically
|
| 38 |
+
- default quick path: `CIFAR10`
|
| 39 |
+
- optional stronger but heavier path: `STL10`
|
| 40 |
|
| 41 |
This avoids making you manually collect a non-steel dataset.
|
| 42 |
|
|
|
|
| 56 |
venv/bin/python training/prepare_surface_gate_dataset.py
|
| 57 |
```
|
| 58 |
|
| 59 |
+
This default mode is the fastest path because it avoids the larger Severstal archive and uses `CIFAR10` as an automatic non-steel source.
|
| 60 |
+
|
| 61 |
+
If you want stronger natural-scene negatives later:
|
| 62 |
+
|
| 63 |
+
```bash
|
| 64 |
+
venv/bin/python training/prepare_surface_gate_dataset.py --negative-source stl10
|
| 65 |
+
```
|
| 66 |
|
| 67 |
If you want a stronger detector after the first run, rebuild with Severstal included:
|
| 68 |
|
training/prepare_surface_gate_dataset.py
CHANGED
|
@@ -3,7 +3,6 @@ from __future__ import annotations
|
|
| 3 |
import argparse
|
| 4 |
import json
|
| 5 |
import random
|
| 6 |
-
import shutil
|
| 7 |
import zipfile
|
| 8 |
from pathlib import Path
|
| 9 |
|
|
@@ -12,18 +11,11 @@ import numpy as np
|
|
| 12 |
import yaml
|
| 13 |
from huggingface_hub import snapshot_download
|
| 14 |
from tqdm import tqdm
|
| 15 |
-
|
| 16 |
-
try:
|
| 17 |
-
from datasets import load_dataset
|
| 18 |
-
except Exception as exc: # pragma: no cover - import guard for guidance
|
| 19 |
-
raise SystemExit(
|
| 20 |
-
"The `datasets` package is required for this script. Install training/requirements.txt first."
|
| 21 |
-
) from exc
|
| 22 |
|
| 23 |
|
| 24 |
SEVERSTAL_REPO = "rohanath/severstal-steel-detection"
|
| 25 |
NEU_REPO = "LiuErXiao/NEU_valid"
|
| 26 |
-
IMAGENETTE_REPO = "frgfm/imagenette"
|
| 27 |
|
| 28 |
|
| 29 |
def parse_args() -> argparse.Namespace:
|
|
@@ -76,6 +68,12 @@ def parse_args() -> argparse.Namespace:
|
|
| 76 |
default=1.0,
|
| 77 |
help="How many pure background negatives to generate per steel image.",
|
| 78 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
parser.add_argument("--seed", type=int, default=42)
|
| 80 |
return parser.parse_args()
|
| 81 |
|
|
@@ -187,19 +185,44 @@ def save_image(path: Path, image: np.ndarray) -> None:
|
|
| 187 |
cv2.imwrite(str(path), image)
|
| 188 |
|
| 189 |
|
| 190 |
-
def export_negative_pool(
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
rng.
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
|
| 205 |
def build_split(
|
|
@@ -293,7 +316,7 @@ def main() -> None:
|
|
| 293 |
ensure_clean_dir(labels_dir)
|
| 294 |
|
| 295 |
neu_raw = extract_first_zip(NEU_REPO, raw_root / "neu")
|
| 296 |
-
negative_dir = raw_root / "
|
| 297 |
ensure_clean_dir(negative_dir)
|
| 298 |
|
| 299 |
steel_paths: list[Path] = []
|
|
@@ -309,7 +332,12 @@ def main() -> None:
|
|
| 309 |
if not steel_paths:
|
| 310 |
raise RuntimeError("No steel images were collected for the detector dataset.")
|
| 311 |
|
| 312 |
-
negative_paths = export_negative_pool(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
if not negative_paths:
|
| 314 |
raise RuntimeError("No negative background images were collected.")
|
| 315 |
|
|
@@ -343,6 +371,7 @@ def main() -> None:
|
|
| 343 |
{
|
| 344 |
"steel_sources": len(steel_paths),
|
| 345 |
"negative_pool": len(negative_paths),
|
|
|
|
| 346 |
"splits": summary,
|
| 347 |
"dataset_yaml": str(yaml_path),
|
| 348 |
},
|
|
|
|
| 3 |
import argparse
|
| 4 |
import json
|
| 5 |
import random
|
|
|
|
| 6 |
import zipfile
|
| 7 |
from pathlib import Path
|
| 8 |
|
|
|
|
| 11 |
import yaml
|
| 12 |
from huggingface_hub import snapshot_download
|
| 13 |
from tqdm import tqdm
|
| 14 |
+
from torchvision.datasets import CIFAR10, STL10
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
SEVERSTAL_REPO = "rohanath/severstal-steel-detection"
|
| 18 |
NEU_REPO = "LiuErXiao/NEU_valid"
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
def parse_args() -> argparse.Namespace:
|
|
|
|
| 68 |
default=1.0,
|
| 69 |
help="How many pure background negatives to generate per steel image.",
|
| 70 |
)
|
| 71 |
+
parser.add_argument(
|
| 72 |
+
"--negative-source",
|
| 73 |
+
default="cifar10",
|
| 74 |
+
choices=["cifar10", "stl10", "auto"],
|
| 75 |
+
help="Automatic non-steel image source. `cifar10` is the fastest default.",
|
| 76 |
+
)
|
| 77 |
parser.add_argument("--seed", type=int, default=42)
|
| 78 |
return parser.parse_args()
|
| 79 |
|
|
|
|
| 185 |
cv2.imwrite(str(path), image)
|
| 186 |
|
| 187 |
|
| 188 |
+
def export_negative_pool(
|
| 189 |
+
target_dir: Path,
|
| 190 |
+
limit: int,
|
| 191 |
+
rng: random.Random,
|
| 192 |
+
negative_source: str,
|
| 193 |
+
) -> tuple[list[Path], str]:
|
| 194 |
+
source_loaders = {
|
| 195 |
+
"cifar10": lambda root: CIFAR10(root=str(root), train=True, download=True),
|
| 196 |
+
"stl10": lambda root: STL10(root=str(root), split="train", download=True),
|
| 197 |
+
}
|
| 198 |
+
if negative_source == "auto":
|
| 199 |
+
datasets_to_try = [("cifar10", source_loaders["cifar10"]), ("stl10", source_loaders["stl10"])]
|
| 200 |
+
else:
|
| 201 |
+
datasets_to_try = [(negative_source, source_loaders[negative_source])]
|
| 202 |
+
|
| 203 |
+
last_error: Exception | None = None
|
| 204 |
+
for dataset_name, loader in datasets_to_try:
|
| 205 |
+
try:
|
| 206 |
+
dataset = loader(target_dir / "_torchvision_cache")
|
| 207 |
+
samples = list(range(len(dataset)))
|
| 208 |
+
rng.shuffle(samples)
|
| 209 |
+
|
| 210 |
+
output_paths: list[Path] = []
|
| 211 |
+
for index, sample_index in enumerate(samples[:limit]):
|
| 212 |
+
sample_image, _ = dataset[sample_index]
|
| 213 |
+
image = cv2.cvtColor(np.array(sample_image), cv2.COLOR_RGB2BGR)
|
| 214 |
+
destination = target_dir / f"{dataset_name}_{index:05d}.jpg"
|
| 215 |
+
save_image(destination, center_crop_and_resize(image, 640))
|
| 216 |
+
output_paths.append(destination)
|
| 217 |
+
|
| 218 |
+
if output_paths:
|
| 219 |
+
return output_paths, dataset_name
|
| 220 |
+
except Exception as exc: # pragma: no cover - download/runtime fallback
|
| 221 |
+
last_error = exc
|
| 222 |
+
|
| 223 |
+
raise RuntimeError(
|
| 224 |
+
"Unable to download an automatic negative-image source via torchvision."
|
| 225 |
+
) from last_error
|
| 226 |
|
| 227 |
|
| 228 |
def build_split(
|
|
|
|
| 316 |
ensure_clean_dir(labels_dir)
|
| 317 |
|
| 318 |
neu_raw = extract_first_zip(NEU_REPO, raw_root / "neu")
|
| 319 |
+
negative_dir = raw_root / "negatives"
|
| 320 |
ensure_clean_dir(negative_dir)
|
| 321 |
|
| 322 |
steel_paths: list[Path] = []
|
|
|
|
| 332 |
if not steel_paths:
|
| 333 |
raise RuntimeError("No steel images were collected for the detector dataset.")
|
| 334 |
|
| 335 |
+
negative_paths, negative_source = export_negative_pool(
|
| 336 |
+
negative_dir,
|
| 337 |
+
args.max_negatives,
|
| 338 |
+
rng,
|
| 339 |
+
args.negative_source,
|
| 340 |
+
)
|
| 341 |
if not negative_paths:
|
| 342 |
raise RuntimeError("No negative background images were collected.")
|
| 343 |
|
|
|
|
| 371 |
{
|
| 372 |
"steel_sources": len(steel_paths),
|
| 373 |
"negative_pool": len(negative_paths),
|
| 374 |
+
"negative_source": negative_source,
|
| 375 |
"splits": summary,
|
| 376 |
"dataset_yaml": str(yaml_path),
|
| 377 |
},
|
training/requirements.txt
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
datasets
|
| 2 |
huggingface_hub
|
| 3 |
Pillow
|
| 4 |
PyYAML
|
|
|
|
|
|
|
| 1 |
huggingface_hub
|
| 2 |
Pillow
|
| 3 |
PyYAML
|
training/train_surface_gate_detector.py
CHANGED
|
@@ -23,7 +23,7 @@ def parse_args() -> argparse.Namespace:
|
|
| 23 |
)
|
| 24 |
parser.add_argument("--epochs", type=int, default=18)
|
| 25 |
parser.add_argument("--imgsz", type=int, default=512)
|
| 26 |
-
parser.add_argument("--batch", default=16)
|
| 27 |
parser.add_argument("--device", default="")
|
| 28 |
parser.add_argument("--workers", type=int, default=4)
|
| 29 |
parser.add_argument("--project", default="training/runs")
|
|
@@ -42,12 +42,14 @@ def main() -> None:
|
|
| 42 |
if not data_path.exists():
|
| 43 |
raise FileNotFoundError(f"Dataset YAML not found: {data_path}")
|
| 44 |
|
|
|
|
|
|
|
| 45 |
model = YOLO(args.model)
|
| 46 |
results = model.train(
|
| 47 |
data=str(data_path),
|
| 48 |
epochs=args.epochs,
|
| 49 |
imgsz=args.imgsz,
|
| 50 |
-
batch=
|
| 51 |
device=args.device or None,
|
| 52 |
workers=args.workers,
|
| 53 |
project=args.project,
|
|
|
|
| 23 |
)
|
| 24 |
parser.add_argument("--epochs", type=int, default=18)
|
| 25 |
parser.add_argument("--imgsz", type=int, default=512)
|
| 26 |
+
parser.add_argument("--batch", type=float, default=16)
|
| 27 |
parser.add_argument("--device", default="")
|
| 28 |
parser.add_argument("--workers", type=int, default=4)
|
| 29 |
parser.add_argument("--project", default="training/runs")
|
|
|
|
| 42 |
if not data_path.exists():
|
| 43 |
raise FileNotFoundError(f"Dataset YAML not found: {data_path}")
|
| 44 |
|
| 45 |
+
batch = int(args.batch) if float(args.batch).is_integer() else float(args.batch)
|
| 46 |
+
|
| 47 |
model = YOLO(args.model)
|
| 48 |
results = model.train(
|
| 49 |
data=str(data_path),
|
| 50 |
epochs=args.epochs,
|
| 51 |
imgsz=args.imgsz,
|
| 52 |
+
batch=batch,
|
| 53 |
device=args.device or None,
|
| 54 |
workers=args.workers,
|
| 55 |
project=args.project,
|
yolo11n.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0ebbc80d4a7680d14987a577cd21342b65ecfd94632bd9a8da63ae6417644ee1
|
| 3 |
+
size 5613764
|