File size: 2,431 Bytes
3656aff | 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 | """Write fp16 ``.npy`` frames and an index CSV for one dataset.
Training reads this cache when ``paths.frame_cache/<dataset>/index.csv``
exists and otherwise reads the source files. Nothing is downloaded.
Workers then load a small array instead of decoding the source.
``python -m src.cache_frames --dataset nasa``
"""
from __future__ import annotations
import argparse
import re
from pathlib import Path
import numpy as np
import pandas as pd
_SAMPLE_CHARS = re.compile(r"[^A-Za-z0-9._-]+")
def _safe_name(sample_id: str) -> str:
cleaned = _SAMPLE_CHARS.sub("_", sample_id.strip())
return cleaned or "frame"
def cache_dataset(dataset: str, config: dict) -> Path:
"""Read one local dataset and write fp16 frames plus ``index.csv``."""
from src.data_loader import load_image
from src.train import load_training_frame
name = str(dataset).strip().lower()
root = Path(config["paths"]["frame_cache"])
frame, _excluded = load_training_frame(name, config)
dest_dir = root / name / "frames"
dest_dir.mkdir(parents=True, exist_ok=True)
rows: list[dict[str, str]] = []
for row in frame.itertuples(index=False):
array = np.asarray(load_image(row.image_path), dtype=np.float16)
path = dest_dir / f"{_safe_name(str(row.sample_id))}.npy"
np.save(path, array)
rows.append(
{
"sample_id": str(row.sample_id),
"cache_path": path.resolve().as_posix(),
"source_path": str(row.image_path),
"dataset": name,
"dtype": "float16",
}
)
index = root / name / "index.csv"
pd.DataFrame(rows).to_csv(index, index=False)
print(f"Wrote {index} ({len(rows)} fp16 frames). Not a model result.")
return index
def main(argv: list[str] | None = None) -> Path:
"""``python -m src.cache_frames --config config/config.yaml --dataset nasa``."""
from src.config import load_config
from src.train import _DATASETS
parser = argparse.ArgumentParser(
description="Cache one dataset as fp16 npy frames. Does not download imagery."
)
parser.add_argument("--config", type=Path, default=Path("config/config.yaml"))
parser.add_argument("--dataset", required=True, choices=_DATASETS)
args = parser.parse_args(argv)
return cache_dataset(args.dataset, load_config(args.config))
if __name__ == "__main__":
main()
|