Download scripts/download_fitzpatrick.py from Gowtham25/hc-mts-training-data: direct link, hf CLI and curl.
- Browser
- Download file 2.97 kB
-
https://huggingface.co/datasets/Gowtham25/hc-mts-training-data/resolve/main/scripts/download_fitzpatrick.py
- Command line
-
hf download hf://datasets/Gowtham25/hc-mts-training-data/scripts/download_fitzpatrick.py
-
curl -L -o download_fitzpatrick.py https://huggingface.co/datasets/Gowtham25/hc-mts-training-data/resolve/main/scripts/download_fitzpatrick.py
2.97 kB
| #!/usr/bin/env python3 | |
| """Download Fitzpatrick17k images from the url column in the official CSV.""" | |
| from __future__ import annotations | |
| import csv | |
| import os | |
| import sys | |
| import time | |
| import urllib.request | |
| from concurrent.futures import ThreadPoolExecutor, as_completed | |
| from pathlib import Path | |
| ROOT = Path("/opt/dlami/nvme/workspace/skin_disease_classification") | |
| CSV_PATH = Path(os.environ.get("FITZ_CSV", ROOT / "data/raw/fitzpatrick17k/fitzpatrick17k.csv")) | |
| DEST = Path(os.environ.get("FITZ_DEST", ROOT / "data/raw/fitzpatrick17k/images")) | |
| WORKERS = int(os.environ.get("FITZ_WORKERS", "16")) | |
| UA = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/128.0.0.0 Safari/537.36" | |
| def one(md5: str, url: str) -> str: | |
| if not url or not md5: | |
| return "skip-empty" | |
| ext = ".jpg" | |
| lower = url.lower().split("?")[0] | |
| if lower.endswith(".png"): | |
| ext = ".png" | |
| dest = DEST / f"{md5}{ext}" | |
| if dest.exists() and dest.stat().st_size > 2000: | |
| return "skip" | |
| tmp = dest.with_suffix(dest.suffix + ".part") | |
| last = None | |
| for attempt in range(3): | |
| try: | |
| req = urllib.request.Request(url, headers={"User-Agent": UA, "Referer": url}) | |
| with urllib.request.urlopen(req, timeout=40) as resp, open(tmp, "wb") as fh: | |
| fh.write(resp.read()) | |
| if tmp.stat().st_size < 2000: | |
| raise OSError("too small") | |
| tmp.replace(dest) | |
| return "ok" | |
| except Exception as exc: # noqa: BLE001 | |
| last = exc | |
| time.sleep(0.8 * (attempt + 1)) | |
| if tmp.exists(): | |
| tmp.unlink(missing_ok=True) | |
| return f"fail {md5} {last}" | |
| def main() -> int: | |
| DEST.mkdir(parents=True, exist_ok=True) | |
| rows = [] | |
| with CSV_PATH.open(newline="", encoding="utf-8") as fh: | |
| reader = csv.DictReader(fh) | |
| for row in reader: | |
| md5 = (row.get("md5hash") or row.get("md5") or "").strip() | |
| url = (row.get("url") or "").strip() | |
| rows.append((md5, url)) | |
| print(f"CSV rows={len(rows)} dest={DEST}", flush=True) | |
| ok = skip = fail = 0 | |
| fails = [] | |
| with ThreadPoolExecutor(max_workers=WORKERS) as pool: | |
| futs = [pool.submit(one, md5, url) for md5, url in rows] | |
| for i, fut in enumerate(as_completed(futs), 1): | |
| result = fut.result() | |
| if result == "ok": | |
| ok += 1 | |
| elif result.startswith("skip"): | |
| skip += 1 | |
| else: | |
| fail += 1 | |
| if len(fails) < 30: | |
| fails.append(result) | |
| if i % 500 == 0 or i == len(futs): | |
| print(f"[{i}/{len(futs)}] ok={ok} skip={skip} fail={fail}", flush=True) | |
| fail_path = ROOT / "logs" / "fitzpatrick_failures.txt" | |
| fail_path.write_text("\n".join(fails) + "\n") | |
| print(f"done ok={ok} skip={skip} fail={fail} sample_fails={len(fails)}", flush=True) | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |