|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +from glob import glob |
| 4 | +from subprocess import run |
| 5 | + |
| 6 | +import imageio.v3 as imageio |
| 7 | + |
| 8 | +from tqdm import tqdm |
| 9 | + |
| 10 | +DATA_ROOT = "/scratch/projects/nim00007/sam/datasets" |
| 11 | +EXP_ROOT = "/scratch/projects/nim00007/sam/experiments/cellpose" |
| 12 | + |
| 13 | +DATASETS = ( |
| 14 | + "covid-if", |
| 15 | + "deepbacs", |
| 16 | + "hpa", |
| 17 | + "livecell", |
| 18 | + "lizard", |
| 19 | + "mouse-embryo", |
| 20 | + "plantseg-ovules", |
| 21 | + "plantseg-root", |
| 22 | + "tissuenet", |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +def load_cellpose_model(): |
| 27 | + from cellpose import models |
| 28 | + |
| 29 | + device, gpu = models.assign_device(True, True) |
| 30 | + model = models.Cellpose(gpu=gpu, model_type="cyto", device=device) |
| 31 | + return model |
| 32 | + |
| 33 | + |
| 34 | +def run_cellpose_segmentation(datasets, job_id): |
| 35 | + dataset = datasets[job_id] |
| 36 | + experiment_folder = os.path.join(EXP_ROOT, dataset) |
| 37 | + |
| 38 | + prediction_folder = os.path.join(experiment_folder, "predictions") |
| 39 | + os.makedirs(prediction_folder, exist_ok=True) |
| 40 | + |
| 41 | + image_paths = sorted(glob(os.path.join(DATA_ROOT, dataset, "test", "image*.tif"))) |
| 42 | + model = load_cellpose_model() |
| 43 | + |
| 44 | + for path in tqdm(image_paths, desc=f"Segmenting {dataset} with cellpose"): |
| 45 | + fname = os.path.basename(path) |
| 46 | + out_path = os.path.join(prediction_folder, fname) |
| 47 | + if os.path.exists(out_path): |
| 48 | + continue |
| 49 | + image = imageio.imread(path) |
| 50 | + if image.ndim == 3: |
| 51 | + assert image.shape[-1] == 3 |
| 52 | + image = image.mean(axis=-1) |
| 53 | + assert image.ndim == 2 |
| 54 | + seg = model.eval(image, diameter=None, flow_threshold=None, channels=[0, 0])[0] |
| 55 | + assert seg.shape == image.shape |
| 56 | + imageio.imwrite(out_path, seg, compression=5) |
| 57 | + |
| 58 | + |
| 59 | +def submit_array_job(datasets): |
| 60 | + n_datasets = len(datasets) |
| 61 | + cmd = ["sbatch", "-a", f"0-{n_datasets-1}", "cellpose_baseline.sbatch"] |
| 62 | + run(cmd) |
| 63 | + |
| 64 | + |
| 65 | +def evaluate_dataset(dataset): |
| 66 | + from micro_sam.evaluation.evaluation import run_evaluation |
| 67 | + |
| 68 | + gt_paths = sorted(glob(os.path.join(DATA_ROOT, dataset, "test", "label*.tif"))) |
| 69 | + experiment_folder = os.path.join(EXP_ROOT, dataset) |
| 70 | + pred_paths = sorted(glob(os.path.join(experiment_folder, "predictions", "*.tif"))) |
| 71 | + assert len(gt_paths) == len(pred_paths), f"{len(gt_paths)}, {len(pred_paths)}" |
| 72 | + result_path = os.path.join(experiment_folder, "cellpose.csv") |
| 73 | + run_evaluation(gt_paths, pred_paths, result_path) |
| 74 | + |
| 75 | + |
| 76 | +def evaluate_segmentations(datasets): |
| 77 | + for dataset in datasets: |
| 78 | + # we skip livecell, which has already been processed by cellpose |
| 79 | + if dataset == "livecell": |
| 80 | + continue |
| 81 | + evaluate_dataset(dataset) |
| 82 | + |
| 83 | + |
| 84 | +def check_results(datasets): |
| 85 | + for ds in datasets: |
| 86 | + # we skip livecell, which has already been processed by cellpose |
| 87 | + if ds == "livecell": |
| 88 | + continue |
| 89 | + result_path = os.path.join(EXP_ROOT, ds, "cellpose.csv") |
| 90 | + if not os.path.exists(result_path): |
| 91 | + print("Cellpose results missing for", ds) |
| 92 | + print("All checks passed") |
| 93 | + |
| 94 | + |
| 95 | +def main(): |
| 96 | + parser = argparse.ArgumentParser() |
| 97 | + parser.add_argument("--segment", "-s", action="store_true") |
| 98 | + parser.add_argument("--evaluate", "-e", action="store_true") |
| 99 | + parser.add_argument("--check", "-c", action="store_true") |
| 100 | + parser.add_argument("--datasets", nargs="+") |
| 101 | + args = parser.parse_args() |
| 102 | + |
| 103 | + job_id = os.environ.get("SLURM_ARRAY_TASK_ID", None) |
| 104 | + |
| 105 | + if args.datasets is None: |
| 106 | + datasets = DATASETS |
| 107 | + else: |
| 108 | + datasets = args.datasets |
| 109 | + assert all(ds in DATASETS for ds in datasets) |
| 110 | + |
| 111 | + if job_id is not None: |
| 112 | + run_cellpose_segmentation(datasets, int(job_id)) |
| 113 | + elif args.segment: |
| 114 | + submit_array_job(datasets) |
| 115 | + elif args.evaluate: |
| 116 | + evaluate_segmentations(datasets) |
| 117 | + elif args.check: |
| 118 | + check_results(datasets) |
| 119 | + else: |
| 120 | + raise ValueError("Doing nothing") |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + main() |
0 commit comments