Views
No views yet
2dcheckpoint_best.pth, fold 0model.zip — nnU-Net trained-model folder (contains plans.json, dataset.json, fold_0/).1import zipfile, numpy as np, torch
2from PIL import Image
3from huggingface_hub import hf_hub_download
4from nnunetv2.inference.predict_from_raw_data import nnUNetPredictor
5
6zip_path = hf_hub_download("sklumpe/fibsemos-chlamydomonas-v1", "model.zip")
7zipfile.ZipFile(zip_path).extractall("model") # -> model/<trainer folder>/
8model_folder = "model/model" # folder with plans.json + dataset.json + fold_0
9
10pred = nnUNetPredictor(device=torch.device("cpu"), allow_tqdm=False)
11pred.initialize_from_trained_model_folder(model_folder, use_folds=(0,), checkpoint_name="checkpoint_best.pth")
12
13img = np.asarray(Image.open("image.png").convert("L"), dtype=np.float32)[None, None] # (c,z,y,x)
14seg = pred.predict_single_npy_array(img, {"spacing": (999.0, 1.0, 1.0)}, None, None, False).squeeze()