File size: 18,504 Bytes
4b6c5c7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | """
Dataset builder for door orientation detection.
All images — real and "synthetic" — are derived from the hand-crafted real crops.
"Synthetic" images are generated by applying heavy augmentations to the real crops
so that the entire dataset shares the same visual style (grayscale floorplan look).
Four classes (sill always at the bottom of the crop):
double — two arcs, one from each bottom corner
hinge_left — single arc from the bottom-left corner
hinge_right — single arc from the bottom-right corner
no_arc — no door arc visible (plain room interior / background)
Output layout (PyTorch ImageFolder + explicit train/test split):
<output_dir>/
train/ double/ hinge_left/ hinge_right/ no_arc/
test/ double/ hinge_left/ hinge_right/ no_arc/
Note: no_arc/ is optional — if raw_crops/no_arc/ is absent a warning is logged
and the builder skips it, keeping backward compatibility with 3-class datasets.
Usage:
python cnn_dataset_builder.py --real-crops real_crops_dataset/ \\
--output-dir mixed_dataset/ \\
--n-per-class 2000
"""
# ruff: noqa: N806
from __future__ import annotations
import argparse
import re
from collections import defaultdict
from collections.abc import Callable
from pathlib import Path
import cv2
import numpy as np
import torch
from loguru import logger
from PIL import Image
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision import transforms
from tqdm import tqdm
# ── constants ──────────────────────────────────────────────────────────────────
IMG_SIZE = 128 # px, square
TRAIN_SPLIT = 0.85 # fraction of samples used for training
LABELS = ("double", "hinge_left", "hinge_right", "no_arc")
# Horizontal flip label mapping. no_arc stays no_arc — a plain room interior
# looks the same mirrored. Aim for no_arc sample count ≈ each door class count.
_FLIP_MAP = {
"hinge_left": "hinge_right",
"hinge_right": "hinge_left",
"double": "double",
"no_arc": "no_arc",
}
# ── helpers ────────────────────────────────────────────────────────────────────
def _plan_id_from_path(p: Path) -> str:
"""Return the plan ID prefix (e.g. 'plan-32') from a crop filename."""
m = re.match(r"^(plan-\d+)", p.stem)
return m.group(1) if m else p.stem
def _split_paths_by_plan(
paths: list[Path],
rng: np.random.Generator,
test_fraction: float,
) -> tuple[list[Path], list[Path]]:
"""
Split paths into (train_paths, test_paths) ensuring all crops from a given
floorplan land in the same split (no data leakage across the sill boundary).
"""
by_plan: dict[str, list[Path]] = defaultdict(list)
for p in paths:
by_plan[_plan_id_from_path(p)].append(p)
plan_ids = sorted(by_plan.keys())
rng.shuffle(plan_ids) # type: ignore[arg-type]
n_test_plans = max(1, int(len(plan_ids) * test_fraction))
test_plans = set(plan_ids[:n_test_plans])
train_plans = set(plan_ids[n_test_plans:])
train_paths = [p for pid in sorted(train_plans) for p in by_plan[pid]]
test_paths = [p for pid in sorted(test_plans) for p in by_plan[pid]]
return train_paths, test_paths
def _to_grayscale(img_bgr: np.ndarray, size: int) -> np.ndarray:
gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) if img_bgr.ndim == 3 else img_bgr
return cv2.resize(gray, (size, size), interpolation=cv2.INTER_LINEAR)
def _add_clutter(img: np.ndarray, rng: np.random.Generator, size: int) -> None:
"""
Overlay sparse dark hollow rectangles simulating furniture / room labels.
Rectangles are kept in the upper 65% of the image so they don't cover the sill.
"""
s = size - 1
t = int(rng.choice([1, 1, 2]))
for _ in range(int(rng.integers(1, 4))):
w = int(rng.uniform(0.05, 0.20) * size)
h = int(rng.uniform(0.05, 0.15) * size)
x0 = int(rng.uniform(0.02, 0.85) * size)
y0 = int(rng.uniform(0.02, 0.60) * size) # stay away from sill area
cv2.rectangle(img, (x0, y0), (min(x0 + w, s), min(y0 + h, s)), 0, t)
# ── real-crop preprocessing & augmentation ─────────────────────────────────────
def preprocess_real_crop(gray: np.ndarray, size: int = IMG_SIZE) -> np.ndarray:
"""
CLAHE + resize — boosts local contrast so the arc geometry is more visible
on real crops that have low-contrast lines on a light background.
"""
enhanced = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)).apply(gray)
return cv2.resize(enhanced, (size, size), interpolation=cv2.INTER_LINEAR)
def augment_real_crop(
img_bgr: np.ndarray, rng: np.random.Generator, label: str, size: int = IMG_SIZE
) -> list[tuple[np.ndarray, str, str]]:
"""
Return (grayscale uint8, label, tag) triples for one real crop (x4):
- Original (CLAHE enhanced) → tag "orig"
- Rotation ±10° → tag "rot{angle}"
- Scale jitter (0.85-1.0, centre crop) → tag "sc{pct}"
- Horizontal flip with label swap → tag "hf"
"""
base = preprocess_real_crop(_to_grayscale(img_bgr, size), size)
results: list[tuple[np.ndarray, str, str]] = [(base.copy(), label, "orig")]
angle = float(rng.uniform(-10.0, 10.0))
M = cv2.getRotationMatrix2D((size / 2.0, size / 2.0), angle, 1.0)
results.append(
(
cv2.warpAffine(
base, M, (size, size), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE
),
label,
f"rot{abs(int(angle))}",
)
)
scale = float(rng.uniform(0.85, 1.0))
crop_sz = max(int(size * scale), 32)
off = (size - crop_sz) // 2
results.append(
(
cv2.resize(
base[off : off + crop_sz, off : off + crop_sz],
(size, size),
interpolation=cv2.INTER_LINEAR,
),
label,
f"sc{int(scale * 100)}",
)
)
results.append((cv2.flip(base, 1), _FLIP_MAP[label], "hf"))
return results
def synthesize_from_real_crop(
img_bgr: np.ndarray, rng: np.random.Generator, label: str, size: int = IMG_SIZE
) -> tuple[np.ndarray, str, str]:
"""
Generate one heavily-augmented image from a real crop for the synthetic batch.
Augmentations (all randomised):
1. Horizontal flip (50%) with label swap (hinge_left ↔ hinge_right)
2. Rotation ±10° (BORDER_REPLICATE — no black fill)
3. Scale jitter 0.70-1.0 (centre crop then resize)
4. Sparse dark clutter rectangles (50%)
5. Gaussian blur ∈ [0, 0.6]
6. Morphological erode (40%) or dilate (20%) — thins or thickens arc lines
Returns (grayscale uint8, label, tag) where tag encodes the applied transforms,
e.g. "hf_r7_s85_cl_er".
"""
base = preprocess_real_crop(_to_grayscale(img_bgr, size), size)
parts: list[str] = []
# 1. hflip
if rng.random() < 0.5:
base = cv2.flip(base, 1)
label = _FLIP_MAP[label]
parts.append("hf")
# 2. rotation ±10°
angle = float(rng.uniform(-10.0, 10.0))
M = cv2.getRotationMatrix2D((size / 2.0, size / 2.0), angle, 1.0)
base = cv2.warpAffine(
base, M, (size, size), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE
)
parts.append(f"r{abs(int(angle))}")
# 3. scale jitter (centre crop 70-100%)
scale = float(rng.uniform(0.70, 1.0))
crop_sz = max(int(size * scale), 32)
off = (size - crop_sz) // 2
base = cv2.resize(
base[off : off + crop_sz, off : off + crop_sz], (size, size), interpolation=cv2.INTER_LINEAR
)
parts.append(f"s{int(scale * 100)}")
# 4. sparse clutter
if rng.random() < 0.5:
_add_clutter(base, rng, size)
parts.append("cl")
# 5. blur
sigma = float(rng.uniform(0.0, 0.6))
if sigma > 0.3:
base = cv2.GaussianBlur(base, (0, 0), sigma)
parts.append("bl")
# 6. morph — bias toward erode (thinner lines) to match real crop arc widths
morph = rng.choice(["none", "erode", "dilate"], p=[0.40, 0.40, 0.20])
if morph != "none":
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
op = cv2.MORPH_ERODE if morph == "erode" else cv2.MORPH_DILATE
base = cv2.morphologyEx(base, op, kernel, iterations=1)
parts.append(morph[:2]) # "er" or "di"
return base, label, "_".join(parts)
# ── validation ─────────────────────────────────────────────────────────────────
def _plan_ids_in_split(split_dir: Path) -> set[str]:
"""Return plan IDs of real (non-synthetic) crops found under split_dir/{label}/."""
ids: set[str] = set()
for label in LABELS:
for p in (split_dir / label).glob("*.png"):
if p.name.startswith("synth_"):
continue
pid = _plan_id_from_path(p)
if pid.startswith("plan-"):
ids.add(pid)
return ids
def _source_plan_ids(real_crops_dir: Path) -> set[str]:
"""Return all plan IDs present in the labeled source crops directory."""
ids: set[str] = set()
for label in LABELS:
class_dir = real_crops_dir / label
if not class_dir.exists():
continue
for p in class_dir.glob("*.png"):
pid = _plan_id_from_path(p)
if pid.startswith("plan-"):
ids.add(pid)
return ids
def check_no_plan_leakage(output_dir: Path, real_crops_dir: Path) -> None:
"""
Verify that:
- No floorplan ID appears in both train and test splits.
- All floorplan IDs from the source crops are represented in the dataset.
Synthetic images (prefixed with "synth_") are skipped — they are generated from
train crops only and intentionally appear in both splits.
Raises AssertionError if any check fails.
"""
train_plans = _plan_ids_in_split(output_dir / "train")
test_plans = _plan_ids_in_split(output_dir / "test")
overlap = train_plans & test_plans
assert not overlap, f"Data leakage: plans in both splits: {sorted(overlap)}"
missing = _source_plan_ids(real_crops_dir) - (train_plans | test_plans)
assert not missing, f"Plans not represented in dataset: {sorted(missing)}"
logger.info(
f"Leakage check passed: {len(train_plans)} train plans, "
f"{len(test_plans)} test plans, 0 overlap."
)
# ── dataset generation ─────────────────────────────────────────────────────────
_SaveFn = Callable[[np.ndarray, str, str, str, str], None]
def _process_real_label(
label: str,
real_crops_dir: Path,
rng: np.random.Generator,
real_test_fraction: float,
size: int,
save_real: _SaveFn,
) -> list[Path]:
"""Split one label's crops by floorplan and write real train/test images. Returns train paths."""
class_dir = real_crops_dir / label
if not class_dir.exists():
logger.info(f" Warning: {class_dir} not found — skipping real crops for {label}")
return []
paths = sorted(class_dir.glob("*.png"))
train_paths, test_paths = _split_paths_by_plan(paths, rng, real_test_fraction)
for p in tqdm(test_paths, desc=f"real test/{label}"):
img_bgr = cv2.imread(str(p))
if img_bgr is None:
logger.info(f" Warning: could not read {p} — skipping")
continue
save_real(preprocess_real_crop(_to_grayscale(img_bgr, size), size), "test", label, p.stem, "orig")
for p in tqdm(train_paths, desc=f"real train/{label}"):
img_bgr = cv2.imread(str(p))
if img_bgr is None:
logger.info(f" Warning: could not read {p} — skipping")
continue
for aug_img, aug_label, tag in augment_real_crop(img_bgr, rng, label, size):
save_real(aug_img, "train", aug_label, p.stem, tag)
return train_paths
def _process_synth_label(
label: str,
crops: list[Path],
rng: np.random.Generator,
n_train: int,
n_test: int,
size: int,
save_synth: _SaveFn,
) -> None:
"""Generate synthetic images for one label from the provided train crop paths."""
if not crops:
logger.info(f" Warning: no real train crops for {label} — skipping synthetic generation")
return
for split, n in [("train", n_train), ("test", n_test)]:
for _ in tqdm(range(n), desc=f"synth {split}/{label}"):
p = crops[int(rng.integers(0, len(crops)))]
img_bgr = cv2.imread(str(p))
if img_bgr is None:
continue
aug_img, aug_label, tag = synthesize_from_real_crop(img_bgr, rng, label, size)
save_synth(aug_img, split, aug_label, p.stem, tag)
def generate_dataset(
n_synthetic_per_class: int,
output_dir: str | Path,
real_crops_dir: str | Path,
seed: int = 42,
size: int = IMG_SIZE,
train_split: float = TRAIN_SPLIT,
real_test_fraction: float = 0.20,
) -> None:
"""
Build a mixed dataset.
Real crops are split at the floorplan level (no individual-file shuffling)
to prevent data leakage between train and test:
- ~20% of floorplans held out as-is (CLAHE only) → test split
- ~80% of floorplans augmented x4 (augment_real_crop) → train split
Synthetic (augmented from real train crops via synthesize_from_real_crop):
- n_synthetic_per_class x train_split → train split
- n_synthetic_per_class x (1 - train_split) → test split
Args:
n_synthetic_per_class: number of synthetic images per class.
output_dir: root output directory.
real_crops_dir: directory with subdirs double/, hinge_left/, hinge_right/.
real_test_fraction: fraction of floorplans held out for the test split.
"""
output_dir = Path(output_dir)
real_crops_dir = Path(real_crops_dir)
for split in ("train", "test"):
for label in LABELS:
(output_dir / split / label).mkdir(parents=True, exist_ok=True)
rng = np.random.default_rng(seed)
# Per-label counter used as collision-breaker suffix for synthetic filenames
synth_counters: dict[str, int] = dict.fromkeys(LABELS, 0)
def _save_real(img: np.ndarray, split: str, label: str, stem: str, tag: str) -> None:
Image.fromarray(img).save(output_dir / split / label / f"{stem}_{tag}.png")
def _save_synth(img: np.ndarray, split: str, label: str, stem: str, tag: str) -> None:
idx = synth_counters[label]
synth_counters[label] += 1
Image.fromarray(img).save(output_dir / split / label / f"synth_{stem}_{tag}_{idx:04d}.png")
# ── real crops: split by floorplan, process ────────────────────────────────
train_real_paths: dict[str, list[Path]] = {
label: _process_real_label(label, real_crops_dir, rng, real_test_fraction, size, _save_real)
for label in LABELS
}
# ── synthetic crops (augmented from real train crops) ─────────────────────
n_train = int(n_synthetic_per_class * train_split)
n_test_synth = n_synthetic_per_class - n_train
for label in LABELS:
_process_synth_label(
label, train_real_paths[label], rng, n_train, n_test_synth, size, _save_synth
)
check_no_plan_leakage(output_dir, real_crops_dir)
logger.info(f"\nDataset written to {output_dir}")
for split in ("train", "test"):
for label in LABELS:
n = len(list((output_dir / split / label).glob("*.png")))
logger.info(f" {split}/{label}: {n}")
# ── dataloader ─────────────────────────────────────────────────────────────────
TRANSFORM = transforms.Compose(
[
transforms.Grayscale(num_output_channels=1),
transforms.Resize((IMG_SIZE, IMG_SIZE)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5], std=[0.5]),
]
)
def get_dataloaders(
dataset_dir: str | Path,
batch_size: int = 64,
num_workers: int = 4,
) -> tuple[DataLoader, DataLoader]:
"""
Return (train_loader, test_loader).
Classes sorted alphabetically: 0=double, 1=hinge_left, 2=hinge_right, 3=no_arc.
"""
dataset_dir = Path(dataset_dir)
train_ds = datasets.ImageFolder(dataset_dir / "train", transform=TRANSFORM)
test_ds = datasets.ImageFolder(dataset_dir / "test", transform=TRANSFORM)
kw = {
"batch_size": batch_size,
"num_workers": num_workers,
"pin_memory": torch.cuda.is_available(),
}
return (
DataLoader(train_ds, shuffle=True, **kw),
DataLoader(test_ds, shuffle=False, **kw),
)
# ── entry point ────────────────────────────────────────────────────────────────
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Door orientation dataset builder")
parser.add_argument(
"--real-crops",
type=Path,
default=Path(__file__).parent / "labeled_crops",
help="Directory containing double/, hinge_left/, hinge_right/, no_door/ subfolders",
)
parser.add_argument(
"--output-dir",
type=Path,
default=Path(__file__).parent / "mixed_dataset",
help="Output directory (default: mixed_dataset/)",
)
parser.add_argument(
"--n-per-class",
type=int,
default=2_000,
help="Number of synthetic images per class (default: 2000)",
)
args = parser.parse_args()
generate_dataset(
n_synthetic_per_class=args.n_per_class,
output_dir=args.output_dir,
real_crops_dir=args.real_crops,
)
|