#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Downsample experiment: train 3 models (RetFound/ResNet/ViT) on ADAM/AIROGS/PAPILA at 100/50/25/10/5% training data fractions (stratified per class, train only), evaluate on the FULL val/test set. Produces a learning-vs-data curve. Output layout: results/downsample////{metrics.json, ...} """ import os, sys, csv, json, math, time, argparse, subprocess, random, shutil from collections import defaultdict from itertools import product PROJ = "/mnt/tidal-alsh-share2/dataset/qinshengqian/research/c3/GPT-Image" DSROOT = f"{PROJ}/Dataset" CODE = f"{PROJ}/Code" RETF = f"{CODE}/RETFound" CFP = f"{PROJ}/weights/pretrained/RETFound_mae_natureCFP.pth" RESULTS = f"{PROJ}/results/downsample" PY = "/root/miniconda3/envs/retfound/bin/python" TORCHRUN = "/root/miniconda3/envs/retfound/bin/torchrun" GPUS = [0, 1, 2, 3, 4, 5, 6, 7] TMPROOT = "/tmp/downsample_exp" # local, not JuiceFS (symlinks can be slow on FUSE) SEED = 42 FRACTIONS = [1.0, 0.5, 0.25, 0.10, 0.05] DATASETS = { "adam": ("AMD/adamdataset", 2, None), "airogs": ("Glaucoma/eyepacs-airogs-light", 2, 30), "papila": ("Glaucoma/papila-retinal-fundus-images", 2, None), } CLASS_NAMES = {"adam": "NonAMD,AMD", "airogs": "NRG,RG", "papila": "healthy,glaucoma"} MODELS = ["retfound", "resnet", "vit"] def make_downsampled_dir(dsk, frac): """Symlink full val/test + stratified-downsampled train into a temp dir. Returns (data_path, n_train).""" rel, nc, max_ep = DATASETS[dsk] src = os.path.join(DSROOT, rel) dst = os.path.join(TMPROOT, f"{dsk}_{int(frac*100)}") shutil.rmtree(dst, ignore_errors=True) os.makedirs(dst) rows = list(csv.DictReader(open(os.path.join(src, "labels.csv")))) by = defaultdict(list) for r in rows: if r["split"] == "train": by[r["label"]].append(r["filepath"]) rnd = random.Random(SEED) chosen = [] # relative paths like "train/0/img.jpg" for lab in sorted(by, key=int): files = sorted(by[lab]) rnd.shuffle(files) n = max(1, int(round(len(files) * frac))) chosen.extend(files[:n]) # symlink val + test entirely for sp in ["val", "test"]: os.symlink(os.path.join(src, sp), os.path.join(dst, sp)) # build train from chosen files only os.makedirs(os.path.join(dst, "train")) for fp in chosen: parts = fp.split("/") # ["train", "