from __future__ import annotations from dataclasses import dataclass, field from pathlib import Path from typing import Any, Optional import yaml @dataclass class SourceEntry: """One logical source (dataset). May scan multiple folders via `images_dirs`.""" id: str images_dirs: list[str] = field(default_factory=list) recursive: bool = True extensions: list[str] = field(default_factory=lambda: [".jpg", ".jpeg", ".png", ".webp"]) split: str = "train" # train | test captions_file: Optional[str] = None captions_format: str = "none" # none | equifashion_json_list | deepfashion_json_dict | facad_jsonl @dataclass class DedupConfig: enabled: bool = True hash_size: int = 16 @dataclass class QualityConfig: enabled: bool = False min_short_side_ratio: float = 0.35 @dataclass class TextConfig: seed: int = 42 typo_prob: float = 0.03 token_dropout_prob: float = 0.12 truncate_tail_prob: float = 0.08 @dataclass class EnrichConfig: fabric_patch: int = 128 workers: int = 0 @dataclass class PackageConfig: relative_paths: bool = True @dataclass class PipelineConfig: output_root: Path target_size: int = 512 sources: list[SourceEntry] = field(default_factory=list) raw_manifest_jsonl: Optional[str] = None dedup: DedupConfig = field(default_factory=DedupConfig) quality: QualityConfig = field(default_factory=QualityConfig) taxonomy: dict[str, Any] = field(default_factory=dict) text: TextConfig = field(default_factory=TextConfig) enrich: EnrichConfig = field(default_factory=EnrichConfig) package: PackageConfig = field(default_factory=PackageConfig) @property def work_dir(self) -> Path: return self.output_root / "work" @property def images512_dir(self) -> Path: return self.output_root / "images_512" # EquiFashion_DB-like output layout @property def train_dir(self) -> Path: return self.output_root / "train" @property def test_dir(self) -> Path: return self.output_root / "test" @property def train_sketch_dir(self) -> Path: return self.output_root / "train_sketch" @property def train_fabric_dir(self) -> Path: return self.output_root / "train_fabric" @property def test_sketch_dir(self) -> Path: return self.output_root / "test_sketch" @property def test_fabric_dir(self) -> Path: return self.output_root / "test_fabric" @property def sketch_dir(self) -> Path: return self.output_root / "sketch" @property def fabric_dir(self) -> Path: return self.output_root / "fabric" def _collect_image_dirs(s: dict[str, Any]) -> list[str]: """YAML may use `images_dir` (string or list) and/or `images_dirs` (list).""" out: list[str] = [] if s.get("images_dirs") is not None: v = s["images_dirs"] if isinstance(v, list): out.extend(str(x).strip() for x in v if str(x).strip()) elif isinstance(v, str) and v.strip(): out.append(v.strip()) v = s.get("images_dir") if v is not None: if isinstance(v, list): out.extend(str(x).strip() for x in v if str(x).strip()) elif isinstance(v, str) and v.strip(): out.append(v.strip()) seen: set[str] = set() uniq: list[str] = [] for d in out: if d not in seen: seen.add(d) uniq.append(d) return uniq def _parse_sources(raw: list[dict[str, Any]]) -> list[SourceEntry]: out: list[SourceEntry] = [] for s in raw or []: out.append( SourceEntry( id=str(s.get("id", "source")), images_dirs=_collect_image_dirs(s), recursive=bool(s.get("recursive", True)), extensions=list(s.get("extensions", [".jpg", ".jpeg", ".png", ".webp"])), split=str(s.get("split", "train")), captions_file=s.get("captions_file"), captions_format=str(s.get("captions_format", "none")), ) ) return out def load_config(path: Path) -> PipelineConfig: with open(path, encoding="utf-8") as f: raw = yaml.safe_load(f) or {} out_root = Path(raw.get("output_root", "./processed_equifashion")).resolve() ded = raw.get("dedup") or {} qual = raw.get("quality") or {} txt = raw.get("text") or {} enr = raw.get("enrich") or {} pkg = raw.get("package") or {} return PipelineConfig( output_root=out_root, target_size=int(raw.get("target_size", 512)), sources=_parse_sources(raw.get("sources") or []), raw_manifest_jsonl=raw.get("raw_manifest_jsonl"), dedup=DedupConfig(enabled=bool(ded.get("enabled", True)), hash_size=int(ded.get("hash_size", 16))), quality=QualityConfig( enabled=bool(qual.get("enabled", False)), min_short_side_ratio=float(qual.get("min_short_side_ratio", 0.35)), ), taxonomy=dict(raw.get("taxonomy") or {}), text=TextConfig( seed=int(txt.get("seed", 42)), typo_prob=float(txt.get("typo_prob", 0.03)), token_dropout_prob=float(txt.get("token_dropout_prob", 0.12)), truncate_tail_prob=float(txt.get("truncate_tail_prob", 0.08)), ), enrich=EnrichConfig( fabric_patch=int(enr.get("fabric_patch", 128)), workers=int(enr.get("workers", 0)), ), package=PackageConfig(relative_paths=bool(pkg.get("relative_paths", True))), ) def write_default_config(dest: Path) -> None: here = Path(__file__).resolve().parent / "defaults.yaml" dest.parent.mkdir(parents=True, exist_ok=True) dest.write_text(here.read_text(encoding="utf-8"), encoding="utf-8")