|
| 1 | +import os |
| 2 | + |
| 3 | +import h5py |
| 4 | +from math import ceil |
| 5 | + |
| 6 | +from micro_sam.sam_annotator import image_series_annotator, annotator_3d |
| 7 | + |
| 8 | + |
| 9 | +DATA_ROOT = "/home/anwai/data/lucchi/lucchi_test.h5" |
| 10 | +EMBEDDING_ROOT = "/home/anwai/embeddiings/test/" |
| 11 | +OUTPUT_ROOT = "/home/anwai/data/lucchi/outputs/" |
| 12 | + |
| 13 | + |
| 14 | +def _get_volume(volume_path): |
| 15 | + """Getting the lucchi test volume""" |
| 16 | + with h5py.File(volume_path, "r") as f: |
| 17 | + raw = f["raw"][:] |
| 18 | + labels = f["labels"][:] |
| 19 | + |
| 20 | + return raw, labels |
| 21 | + |
| 22 | + |
| 23 | +def segment_volume(input_volume, embedding_path): |
| 24 | + """Load the entire volume in the tool for segmentation. |
| 25 | + """ |
| 26 | + assert input_volume.ndim == 3 |
| 27 | + annotator_3d( |
| 28 | + image=input_volume, |
| 29 | + embedding_path=embedding_path, |
| 30 | + model_type="vit_b_em_organelles", |
| 31 | + tile_shape=None, |
| 32 | + halo=None, |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def segment_each_slice(input_volume, embedding_dir, output_folder): |
| 37 | + """Load each slice from the volume one-by-one in the tool for segmentation. |
| 38 | + """ |
| 39 | + assert input_volume.ndim == 3 |
| 40 | + |
| 41 | + all_slices = [each_slice for each_slice in input_volume] |
| 42 | + image_series_annotator( |
| 43 | + images=all_slices, |
| 44 | + output_folder=output_folder, |
| 45 | + model_type="vit_b_em_organelles", |
| 46 | + embedding_path=embedding_dir, |
| 47 | + tile_shape=None, |
| 48 | + halo=None, |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def segment_each_n_slices(z_batch, input_volume, embedding_dir, output_folder): |
| 53 | + """Load n slices from the volume one-by-one in the tool for segmentation. |
| 54 | + """ |
| 55 | + assert input_volume.ndim == 3 |
| 56 | + |
| 57 | + n_z_slices = input_volume.shape[0] |
| 58 | + all_z_idxx = int(ceil(n_z_slices / z_batch)) |
| 59 | + |
| 60 | + all_per_n_slices_volumes = [] |
| 61 | + for z_id in range(all_z_idxx): |
| 62 | + z_start = z_id * z_batch |
| 63 | + z_stop = min((z_id + 1) * z_batch, n_z_slices) |
| 64 | + |
| 65 | + batch_volume = input_volume[z_start: z_stop] |
| 66 | + all_per_n_slices_volumes.append(batch_volume) |
| 67 | + |
| 68 | + print(f"We split the volume into {len(all_per_n_slices_volumes)} sub-volumes.") |
| 69 | + image_series_annotator( |
| 70 | + images=all_per_n_slices_volumes, |
| 71 | + output_folder=output_folder, |
| 72 | + model_type="vit_b_em_organelles", |
| 73 | + embedding_path=embedding_dir, |
| 74 | + tile_shape=None, |
| 75 | + halo=None, |
| 76 | + is_volumetric=True, |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +def main(): |
| 81 | + raw, _ = _get_volume(DATA_ROOT) |
| 82 | + |
| 83 | + # segment_volume( |
| 84 | + # input_volume=raw, |
| 85 | + # embedding_path=os.path.join(EMBEDDING_ROOT, "lucchi_3d_volume") |
| 86 | + # ) |
| 87 | + |
| 88 | + # segment_each_slice( |
| 89 | + # input_volume=raw, |
| 90 | + # embedding_dir=os.path.join(EMBEDDING_ROOT, "lucchi_2d_per_slice"), |
| 91 | + # output_folder=os.path.join(OUTPUT_ROOT, "per_slice_segmentation") |
| 92 | + # ) |
| 93 | + |
| 94 | + segment_each_n_slices( |
| 95 | + z_batch=15, |
| 96 | + input_volume=raw, |
| 97 | + embedding_dir=os.path.join(EMBEDDING_ROOT, "lucchi_3d_per_n_slices"), |
| 98 | + output_folder=os.path.join(OUTPUT_ROOT, "per_n_slices") |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + main() |
0 commit comments