Skip to content

Commit a52ea61

Browse files
authored
Merge pull request #31 from neheller/training-set-release-candidate
Training Set Release
2 parents 1925600 + e4726f1 commit a52ea61

File tree

2,498 files changed

+20061924
-14704632
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,498 files changed

+20061924
-14704632
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ kits21/data/*/imaging.nii.gz
33

44
.idea
55

6+
kits21/data/*/segmentation_samples*
7+
regenerates.sh
68
kits21/data/*/*segmentation_samples*
79

810
*.egg-info

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The official repository of the 2021 Kidney and Kidney Tumor Segmentation Challenge
44

5-
**Current dataset version: `2.2`** (see [changelog](changelog.md))
5+
**Current dataset version: `2.2.1`** (see [changelog](changelog.md))
66

77
<img src="https://kits21.kits-challenge.org/public/site_media/figures/rendering.png" width="400" />
88

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
This file is meant to keep track of which annotations were changed in each dataset version. Broadly, the *major* version will be incremented with any change to the postprocessing code that causes every region to be regenerated (starting at `v1.x.x`). The *minor* version will be incremented with the addition of new **imaging**, and the *patch* version will be incremented with new **annotations** for the existing imaging.
44

5+
## [2.2.1] - July 1st, 2021: Training Set Release
6+
7+
- Edits to nearly every case
8+
59
## [2.2] - July 1st, 2021
610

711
- Code for evaluation of segmentations has been added. Have a look at kits21/evaluation/readme.md

kits21/annotation/cache.json

Lines changed: 488 additions & 473 deletions
Large diffs are not rendered by default.

kits21/annotation/import.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import argparse
22
import shutil
33
from pathlib import Path
4+
import json
45

56
import nibabel as nib
67
import numpy as np
@@ -75,12 +76,26 @@ def update_raw(delineation_path, case_id, in_test_set):
7576
if not destination_parent.exists():
7677
destination_parent.mkdir()
7778

79+
custom_hilums = None
80+
if (destination_parent / "meta.json").exists():
81+
with (destination_parent / "meta.json").open() as f:
82+
old_meta = json.loads(f.read())
83+
if "custom_hilums" in old_meta:
84+
custom_hilums = old_meta["custom_hilums"]
85+
7886
# Get source directory
7987
src = delineation_path.parent.parent.parent.parent
8088

8189
# Copy all annotation files to destination
8290
shutil.copytree(str(src), str(destination_parent), dirs_exist_ok=True)
8391

92+
if custom_hilums is not None:
93+
with (destination_parent / "meta.json").open() as f:
94+
new_meta = json.loads(f.read())
95+
with (destination_parent / "meta.json").open('w') as f:
96+
new_meta["custom_hilums"] = custom_hilums
97+
f.write(json.dumps(new_meta, indent=2))
98+
8499

85100
def get_localization(delineation_path):
86101
return get_most_recent_save(delineation_path.parent.parent / "localization")
@@ -140,8 +155,11 @@ def run_import(delineation_path):
140155
# Path to underlying CT scan stored as .nii.gz
141156
image_path = get_image_path(case_id, in_test_set)
142157

158+
meta_path = image_path.parent / "raw" / "meta.json"
159+
meta = load_json(meta_path)
160+
143161
# Compute and save segmentation based on delineation
144-
seg_nib = delineation_to_seg(region_type, image_path, delineation_path, localization)
162+
seg_nib = delineation_to_seg(region_type, image_path, delineation_path, meta, localization)
145163
save_segmentation(case_id, region_type, delineation_path, seg_nib, in_test_set)
146164

147165

kits21/annotation/postprocessing.py

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import numpy as np
55
import nibabel as nib
66
from PIL import Image, ImageDraw
7+
from numpy.core.fromnumeric import cumsum
78
import torch
89
import torch.nn.functional
910
from scipy import signal
@@ -591,7 +592,7 @@ def apply_hilum_to_slice(thresholded_c, blur_c, threshold, ind, hlm):
591592

592593
# TODO allow for custom hilums to be specified in dln
593594
# Polygons will be allowed for logged-in users
594-
def add_renal_hilum(thresholded_c, blr_c, threshold, lzn, side, cbox):
595+
def add_renal_hilum(thresholded_c, blr_c, threshold, lzn, side, cbox, custom_hilums):
595596
first_hilum_slice = None
596597
last_hilum_slice = None
597598
for ann in lzn["annotations"]:
@@ -613,8 +614,17 @@ def add_renal_hilum(thresholded_c, blr_c, threshold, lzn, side, cbox):
613614
if last_hilum_slice is None or frame > last_hilum_slice:
614615
last_hilum_slice = frame - cbox["zmin"]
615616

616-
if first_hilum_slice is not None and last_hilum_slice is not None:
617-
for ind in range(max(first_hilum_slice, 0), min(last_hilum_slice+1, thresholded_c.shape[0]-1)):
617+
for ind in range(thresholded_c.shape[0]):
618+
if "slice_{}".format(ind) in custom_hilums:
619+
for hlm in custom_hilums["slice_{}".format(ind)]:
620+
apply_hilum_to_slice(thresholded_c, blr_c, threshold, ind, hlm)
621+
elif (
622+
(
623+
first_hilum_slice is not None and ind >= first_hilum_slice
624+
) and (
625+
last_hilum_slice is not None and ind <= last_hilum_slice
626+
)
627+
):
618628
# TODO send dln here and use custom hilum if possible
619629
hlm = find_hilum_in_slice(thresholded_c[ind].copy(), side)
620630
apply_hilum_to_slice(thresholded_c, blr_c, threshold, ind, hlm)
@@ -633,7 +643,7 @@ def get_side(cbox):
633643
return "right"
634644

635645

636-
def generate_segmentation(region_type, cropped_img, cropped_drw, step=1, affine=None, lzn=None, cbox=None):
646+
def generate_segmentation(region_type, cropped_img, cropped_drw, step=1, affine=None, lzn=None, cbox=None, custom_hilums={}):
637647
# Interpolate drawings
638648
cropped_drw = interpolate_drawings(cropped_drw, step)
639649

@@ -660,7 +670,7 @@ def generate_segmentation(region_type, cropped_img, cropped_drw, step=1, affine=
660670
blr_c = blr_d.to("cpu").numpy()
661671
if region_type == "kidney":
662672
side = get_side(cbox)
663-
thresholded_c = add_renal_hilum(thresholded_c, blr_c, threshold, lzn, side, cbox)
673+
thresholded_c = add_renal_hilum(thresholded_c, blr_c, threshold, lzn, side, cbox, custom_hilums)
664674

665675
# Bring result back to cpu memory
666676
return thresholded_c
@@ -676,7 +686,37 @@ def inflate_seg_to_image_size(cbox, cropped_seg):
676686
return seg_np
677687

678688

679-
def delineation_to_seg(region_type, image_path, delineation_path, localization_path=None):
689+
def get_custom_hilums(meta, cbox):
690+
ret = {}
691+
if "custom_hilums" not in meta:
692+
return ret
693+
694+
for ch in meta["custom_hilums"]:
695+
if ch["slice_index"] < cbox["zmin"] or ch["slice_index"] > cbox["zmax"]:
696+
continue
697+
698+
dct_key = "slice_{}".format(ch["slice_index"] - cbox["zmin"])
699+
if dct_key not in ret:
700+
ret[dct_key] = []
701+
702+
for hlm in ch["hilums"]:
703+
ret[dct_key] += [
704+
[
705+
(
706+
hlm[0][0] - cbox["xmin"],
707+
hlm[0][1] - cbox["ymin"]
708+
),
709+
(
710+
hlm[1][0] - cbox["xmin"],
711+
hlm[1][1] - cbox["ymin"]
712+
)
713+
]
714+
]
715+
716+
return ret
717+
718+
719+
def delineation_to_seg(region_type, image_path, delineation_path, meta, localization_path=None):
680720
# Read and parse delination and (maybe) localization from file
681721
lzn = None
682722
if region_type == "kidney":
@@ -694,9 +734,12 @@ def delineation_to_seg(region_type, image_path, delineation_path, localization_p
694734
# Generate the drawing made by the annotator
695735
cropped_drw = generate_cropped_drawing_interior(cbox, dln)
696736

737+
# Get any custom hilums within the containing box
738+
custom_hilums = get_custom_hilums(meta, cbox)
739+
697740
# Apply heuristics to infer segmentation based on drawing and image
698741
cropped_seg = generate_segmentation(
699-
region_type, cropped_img, cropped_drw, cbox["step"], img_nib.affine, lzn, cbox
742+
region_type, cropped_img, cropped_drw, cbox["step"], img_nib.affine, lzn, cbox, custom_hilums
700743
)
701744

702745
# Undo cropping to get final segmentation
538 Bytes
Binary file not shown.
48 Bytes
Binary file not shown.
359 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)