Skip to content

Commit 0851e3d

Browse files
authored
[MedicalSeg] Fix lung precision problem (#2029)
1 parent 27cb17c commit 0851e3d

5 files changed

Lines changed: 25 additions & 27 deletions

File tree

contrib/MedicalSeg/configs/lung_coronavirus/vnet_lung_coronavirus_128_128_128_15k.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ model:
55
elu: False
66
in_channels: 1
77
num_classes: 3
8-
pretrained: null
8+
pretrained: https://bj.bcebos.com/paddleseg/dygraph/lung_coronavirus/vnet_lung_coronavirus_128_128_128_15k/pretrain/model.pdparams

contrib/MedicalSeg/medicalseg/core/val.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def evaluate(model,
139139
save_array(
140140
save_path=os.path.join(
141141
save_dir,
142-
str(iter) + "_" + str(np.mean(per_channel_dice))),
142+
str(iter)),
143143
save_content={
144144
'pred': pred.numpy(),
145145
'label': label.numpy(),

contrib/MedicalSeg/medicalseg/utils/download.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def download_file_and_uncompress(url,
118118
extrapath=None,
119119
extraname=None,
120120
print_progress=True,
121-
cover=False,
121+
cover=True,
122122
delete_file=True):
123123
if savepath is None:
124124
savepath = "."

contrib/MedicalSeg/tools/prepare.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ def uncompress_file(self, num_files, form):
139139
"""unzip all the file in the root directory"""
140140
files = glob.glob(os.path.join(self.dataset_root, "*.{}".format(form)))
141141

142-
assert len(files) == num_files, print(
143-
"The file directory should include {} compressed files, but there is only {}"
144-
.format(num_files, len(files)))
142+
assert len(files) == num_files, "The file directory should include {} compressed files, but there is only {}".format(num_files, len(files))
145143

146144
for f in files:
147145
extract_path = os.path.join(self.raw_data_path,
@@ -162,24 +160,23 @@ def load_medical_data(f):
162160

163161
# validate nii.gz on lung and mri with correct spacing_resample
164162
if filename.endswith((".nii", ".nii.gz", ".dcm")):
165-
itkimage = sitk.ReadImage(f)
166-
if itkimage.GetDimension() == 4:
167-
slicer = sitk.ExtractImageFilter()
168-
s = list(itkimage.GetSize())
169-
s[-1] = 0
170-
slicer.SetSize(s)
171-
for slice_idx in range(itkimage.GetSize()[-1]):
172-
slicer.SetIndex([0, 0, 0, slice_idx])
173-
sitk_volume = slicer.Execute(itkimage)
174-
images.append(sitk_volume)
163+
if "radiopaedia" in filename or "corona" in filename:
164+
f_nps = [nib.load(f).get_fdata(dtype=np.float32)]
175165
else:
176-
images = [itkimage]
177-
images = [sitk.DICOMOrient(img, 'LPS') for img in images]
178-
f_nps = [sitk.GetArrayFromImage(img) for img in images]
179-
180-
# if previous line not swap to xyz
181-
if f_nps[0].shape[0] != f_nps[0].shape[1]:
182-
f_nps = [np.transpose(f_np, [1, 2, 0]) for f_np in f_nps] # swap to xyz
166+
itkimage = sitk.ReadImage(f)
167+
if itkimage.GetDimension() == 4:
168+
slicer = sitk.ExtractImageFilter()
169+
s = list(itkimage.GetSize())
170+
s[-1] = 0
171+
slicer.SetSize(s)
172+
for slice_idx in range(itkimage.GetSize()[-1]):
173+
slicer.SetIndex([0, 0, 0, slice_idx])
174+
sitk_volume = slicer.Execute(itkimage)
175+
images.append(sitk_volume)
176+
else:
177+
images = [itkimage]
178+
images = [sitk.DICOMOrient(img, 'LPS') for img in images]
179+
f_nps = [sitk.GetArrayFromImage(img) for img in images]
183180

184181
elif filename.endswith(
185182
(".mha", ".mhd", "nrrd"

contrib/MedicalSeg/tools/preprocess_utils/values.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def normalize(image, min_val=None, max_val=None):
6464
return image
6565

6666

67-
def HUnorm(image, HU_min=-1000, HU_max=600, HU_nan=-2000):
67+
def HUnorm(image, HU_min=-1200, HU_max=600, HU_nan=-2000):
6868
"""
6969
Convert CT HU unit into uint8 values. First bound HU values by predfined min
7070
and max, and then normalize. Due to paddle.nn.conv3D doesn't support uint8, we need to convert
@@ -76,11 +76,12 @@ def HUnorm(image, HU_min=-1000, HU_max=600, HU_nan=-2000):
7676
HU_nan: float, value for nan in the raw CT image.
7777
"""
7878

79-
if not isinstance(image, np.ndarray):
79+
if not isinstance(image, np.ndarray):
8080
image = np.array(image)
8181
image = np.nan_to_num(image, copy=False, nan=HU_nan)
8282

83-
image = (image - HU_min) / (HU_max - HU_min)
84-
np.clip(image, 0, 1, out=image)
83+
# normalize to [0, 1]
84+
image = (image - HU_min) / ((HU_max - HU_min) / 255)
85+
np.clip(image, 0, 255, out=image)
8586

8687
return image

0 commit comments

Comments
 (0)