nnUNet icon indicating copy to clipboard operation
nnUNet copied to clipboard

Questions about labels

Open boundaryT opened this issue 2 years ago • 3 comments

hello! I am using MM-WHS2017 dataset for training and it has labels [205,420,500,550,600,820,850 ],as nnunet needs continuous labels so what should I do with it? Thanks

boundaryT avatar Mar 18 '22 11:03 boundaryT

Simply convert the labels to [0, 1, 2, 3, 4, ...]?

FabianIsensee avatar Mar 28 '22 07:03 FabianIsensee

@boundaryT I have the same issue. Did you find a fast way to convert the labels? If so, could you please share? Thanks a lot!

annewiet avatar Apr 23 '22 20:04 annewiet

@boundaryT I have the same issue. Did you find a fast way to convert the labels? If so, could you please share? Thanks a lot!

for m ,file in enumerate(labels_list): if True: print(m) img=nib.load(os.path.join(mask_path,file)) img_data=img.get_data().copy() affine=img.affine.copy() hdr=img.header.copy() new_label=np.zeros((1,img_data.shape[0],img_data.shape[1],img_data.shape[2]),dtype=np.int32) for i in range(len(labels)): new_label[0][img_data==labels[i]]=int(relabels[i]) new_label=new_label.squeeze() save_img = nib.Nifti1Image(new_label, affine, hdr) nib.save(save_img,os.path.join(save_path,file))

boundaryT avatar Apr 27 '22 08:04 boundaryT

Yeah just open the segmentations with any tool, modify the labels and then save again. That's not hard. Example:

seg = sitk.ReadImage(FILE)
seg_npy = sitk.GetArrayFromImage(seg)
seg_converted = np.zeros_like(seg_npy)
seg_converted[seg_npy == 205] = 1
seg_converted[seg_npy == 420] = 2
seg_converted[seg_npy == 500] = 3
[...]
seg_new_itk = sitk.GetImageFromArray(seg_converted)
seg_new_itk.CopyInformation(seg)
sitk.WriteImage(seg_new_itk, NEW_FILE)

FabianIsensee avatar Aug 23 '22 08:08 FabianIsensee