My Task:
Actually, original training data is in 3D with shape 49x496x512 for both Images and labels. And I have to create 2D images and labels out of it and then train a UNet to segment each class (3 eye fluids).
here is the code for converting 2D from slices of 3D data:
# give list of all oct images and references from the provided ROOT folder
def preprocess_oct_images_in_numpy(dir:str):
references = list() #only for training data
oct_images =list()
for subdir, dirs, files in os.walk(dir):
for file in files:
filepath = subdir + os.sep + file
if filepath.endswith("reference.mhd"):
references.append(filepath)
elif filepath.endswith("oct.mhd"):
oct_images.append(filepath)
references = sorted(references)
oct_images = sorted(oct_images)
updated_image_list =[]
updated_label_list =[] #only for training data
# creating 2D image (496x512) from each slice of 3D Image (49x496x512)
# For image
for data in range(len(oct_images)):
image_numpy_3D = sitk.GetArrayFromImage(sitk.ReadImage(oct_images[data], sitk.sitkFloat32)) # order z,y,x
for idx in range(image_numpy_3D.shape[0]):
updated_image_list.append(image_numpy_3D[idx,:,:])
# for label
for index in range(len(references)):
label_numpy_3D = sitk.GetArrayFromImage(sitk.ReadImage(references[index], sitk.sitkFloat32)) # order z,y,x
for idx in range(label_numpy_3D.shape[0]):
updated_label_list.append(label_numpy_3D[idx,:,:])
return updated_image_list, updated_label_list
This is what one of my training data (image and label) before and after transform (in 2D) look like:
Thanks so here I have created multi one hot encoding for labels within the dataset class (I have 2nd data set class as well for tranformation) and with this I get label shape in 4x496x512 and after transform (with padding) it converted to shape 4x512x512
class OCTDataset(Dataset):
"OCT Scan data set"
def __init__(self, images, references):
super(OCTDataset, self).__init__()
self.images = images
self.references = references
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
img = self.images[idx]
label = self.references[idx]
# manual multi one hot encoding
new_label = np.zeros((4, 496, 512),dtype=np.float32) # 4 here is 4 classes including background and 3 eye fluids
for iRow in range(label.shape[0]):
for iCol in range(label.shape[1]):
if label[iRow,iCol] == 1:
new_label[iRow,iCol,1] = 1
if label[iRow,iCol] == 2:
new_label[iRow,iCol,2] = 2
if label[iRow,iCol] == 3:
new_label[iRow,iCol,3] = 3
return img, new_label
How can I check if its not multi class segmentation and not multilabel segmentation?
May be in 2D it seems like multi class but when one have to rebuild from 2D to 3D again in that case may be its multi label segmentation (because of overlapping of identified fluid regions), is it correct?
Many thanks!