|
| 1 | +import os |
| 2 | +from glob import glob |
| 3 | + |
| 4 | +import imageio.v3 as imageio |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +from sklearn.model_selection import train_test_split |
| 8 | + |
| 9 | +ROOT = "/scratch-grete/projects/nim00007/data/deepbacs" |
| 10 | + |
| 11 | + |
| 12 | +def download_deepbacs(): |
| 13 | + from torch_em.data.datasets import get_deepbacs_loader |
| 14 | + get_deepbacs_loader(ROOT, "train", bac_type="mixed", download=True, patch_shape=(256, 256), batch_size=1) |
| 15 | + get_deepbacs_loader(ROOT, "test", bac_type="mixed", download=True, patch_shape=(256, 256), batch_size=1) |
| 16 | + |
| 17 | + |
| 18 | +# old code from Anwai |
| 19 | +def get_deepbacs_test_images(): |
| 20 | + root = ROOT |
| 21 | + output_root = "/scratch-grete/projects/nim00007/sam/ood/LM/deepbacs" |
| 22 | + |
| 23 | + def write_split(images, labels, split): |
| 24 | + out_folder = os.path.join(output_root, split) |
| 25 | + os.makedirs(out_folder, exist_ok=True) |
| 26 | + for ii, (im, lab) in enumerate(zip(images, labels)): |
| 27 | + out_im = os.path.join(out_folder, f"image_{ii:04}.tif") |
| 28 | + out_lab = os.path.join(out_folder, f"labels_{ii:04}.tif") |
| 29 | + im, lab = imageio.imread(im), imageio.imread(lab) |
| 30 | + imageio.imwrite(out_im, im) |
| 31 | + imageio.imwrite(out_lab, lab) |
| 32 | + |
| 33 | + root_imgs = glob(os.path.join(root, "mixed", "test", "source", "*")) |
| 34 | + root_gts = glob(os.path.join(root, "mixed", "test", "target", "*")) |
| 35 | + np.random.seed(0) |
| 36 | + |
| 37 | + val_images = np.random.choice(root_imgs, size=5, replace=False).tolist() |
| 38 | + val_labels = [gt_p for gt_p in root_gts if os.path.basename(gt_p) in [os.path.basename(x) for x in val_images]] |
| 39 | + |
| 40 | + test_images = [ip for ip in root_imgs if ip not in val_images] |
| 41 | + test_labels = [gp for gp in root_gts if gp not in val_labels] |
| 42 | + |
| 43 | + write_split(val_images, val_labels, "val") |
| 44 | + write_split(test_images, test_labels, "test") |
| 45 | + |
| 46 | + |
| 47 | +# new simplified code |
| 48 | +def get_deepbacs_test_images_new(): |
| 49 | + root = ROOT |
| 50 | + output_root = "/scratch-grete/projects/nim00007/sam/ood/LM/deepbacs" |
| 51 | + |
| 52 | + def write_split(images, labels, split): |
| 53 | + out_folder = os.path.join(output_root, split) |
| 54 | + os.makedirs(out_folder, exist_ok=True) |
| 55 | + for ii, (im, lab) in enumerate(zip(images, labels)): |
| 56 | + out_im = os.path.join(out_folder, f"image_{ii:04}.tif") |
| 57 | + out_lab = os.path.join(out_folder, f"labels_{ii:04}.tif") |
| 58 | + im, lab = imageio.imread(im), imageio.imread(lab) |
| 59 | + imageio.imwrite(out_im, im) |
| 60 | + imageio.imwrite(out_lab, lab) |
| 61 | + |
| 62 | + images = sorted(glob(os.path.join(root, "mixed", "test", "source", "*"))) |
| 63 | + labels = sorted(glob(os.path.join(root, "mixed", "test", "target", "*"))) |
| 64 | + |
| 65 | + test_images, val_images, test_labels, val_labels = train_test_split( |
| 66 | + images, labels, test_size=0.15, random_state=42 |
| 67 | + ) |
| 68 | + |
| 69 | + write_split(val_images, val_labels, "val") |
| 70 | + write_split(test_images, test_labels, "test") |
| 71 | + |
| 72 | + |
| 73 | +def main(): |
| 74 | + # download_deepbacs() |
| 75 | + get_deepbacs_test_images_new() |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + main() |
0 commit comments