def fuse_labels(valGen, model, dataGenerator, outFile="TRAIN_MERGED.h5"): ''' Method to create dataset that contains labels from both datasets (SegTHOR & LCTSC) ''' #create instance of Plotter class plotter = Plotter() #create instance of FeatureLabelReader object, I will use "save_image_mask" method # reader = FeatureLabelReader() imgNum = 0 # while imgNum<config1["VALSIZE"]: while imgNum < 5981: #total number of slices in LCTSC file imgBatch, label = next(valGen) labelBatch = label["organ_output"] #denormalize image for plotting # imgBatchDeNorm = dataGenerator.denormalize(imgBatch) imgOriginal = dataGenerator.images #output of image is one-hot encoded, so take argmax #labelTrue = dataGenerator.morphological_operation(labelBatch.argmax(axis=-1).astype('uint8'),'open') labelOriginal = dataGenerator.labels #predict labels and apply inverse transformation labelPred = model.predict(imgBatch) labelPredUnZoom = dataGenerator.unzoom_label( labelPred, numClasses=5) #model predicts 5 labels, true labels contain 6 labelPredDeCrop = dataGenerator.uncrop_label(labelPredUnZoom, numClasses=5) #get selected index of predictions labelPredMorph = dataGenerator.morphological_operation( labelPredDeCrop.argmax(axis=-1).astype('uint8'), 'open') #create merged labels labelMerged = label_merger(labelOriginal, labelPredMorph) k = plotter.plot_label_prediction(imgOriginal, labelMerged, labelPredMorph) if k == 27: break elif k == ord('s'): print("Saving ", imgNum) FeatureLabelReader.save_image_mask(imgOriginal, labelMerged, fileName=outFile) #print("Processed ",imgNum) imgNum += imgBatch.shape[0]
def merged_datasets(valGen, model, dataGenerator, outFile="TRAIN_MERGED.h5"): ''' Method used to merge SegTHOR and LCTSC dataset SegTHOR has {'esophagus':1,'heart':2,'trachea':3,'aorta'} LCTSC has {'esophagus':1, 'heart':2, 'spinal cord':3 ,'L-R Lung':4,5} ''' #create instance of Plotter class plotter = Plotter() imgNum = 0 while imgNum < 5981: #total number of slices in LCTSC file imgBatch, label = next(valGen) imgOriginal = dataGenerator.images labelOriginal = dataGenerator.labels #I am assigning different value to spinal cord #labelOriginal[labelOriginal==3] = 6 labelOriginal[labelOriginal == 4] = 0 labelOriginal[labelOriginal == 5] = 0 labelOriginal[labelOriginal == 3] = 0 #look for slices that has non-zero label zSlices = np.array([ np.max(labelOriginal[idx, ...]) > 0 for idx in range(imgBatch.shape[0]) ], dtype=np.bool) print(imgOriginal.shape) imgSave = imgOriginal[zSlices, ...] labelSave = labelOriginal[zSlices, ...] print(imgSave.shape) if imgSave.shape[0] > 0: print("Saving ", imgNum) FeatureLabelReader.save_image_mask(imgSave, labelSave, fileName=outFile) #k = plotter.plot_slice_label(imgOriginal,labelOriginal) #if k==27: break imgNum += imgBatch.shape[0]
def compile_dataset(pathToFiles,fileName): myParser = FeatureLabelReader(pathToFiles=pathToFiles,structNameMap=prostate_struct) images = myParser.end_point_parser(dirPattern=["CT"],filePattern="*.dcm") labels = myParser.end_point_parser(dirPattern=["RTst"],filePattern="*.dcm") #now test compile dataset myParser.compile_dataset(imgFiles=images,labelFiles=labels,fileName=fileName)
def compile_dataset(pathToFiles, fileName): #test path in which feature label are same folder myParser = FeatureLabelReader(pathToFiles=pathToFiles) images = myParser.end_point_parser(filePath=pathToFiles, filePattern="Patient*.nii.gz") labels = myParser.end_point_parser(filePath=pathToFiles, filePattern="GT*.nii.gz") #now test compile dataset myParser.compile_dataset(imgFiles=images, labelFiles=labels, fileName=fileName, batchSize=1) #each nii contains ~220 slices
def compile_dataset(pathToFiles, fileName): myParser = FeatureLabelReader(pathToFiles=pathToFiles) images = myParser.end_point_parser(filePattern="*.dcm", dirPattern=["DICOM_anon"]) labels = myParser.end_point_parser(filePattern="liver_GT*.png", dirPattern=["Ground"]) #length has to be equal assert (images.__len__() == labels.__len__()) #now test compile dataset myParser.compile_dataset(imgFiles=images, labelFiles=labels, fileName=fileName)
def compile_dataset2(pathToFiles, fileName): #test path in which feature label are same folder myParser = FeatureLabelReader(pathToFiles=pathToFiles, structNameMap=lung_struct) images = myParser.end_point_parser(filePath=pathToFiles, filePattern="*.dcm", dirPattern=["Scan", "Train"]) labels = myParser.end_point_parser(filePath=pathToFiles, filePattern="*.dcm", dirPattern=["RTst", "Train"]) #now test compile dataset myParser.compile_dataset(imgFiles=images, labelFiles=labels, fileName=fileName)
def compile_dataset_unit_tester(): #test path in which feature label are seperated pathToFiles = "../../SegmentationDataSets/CHAOS/CT_data_batch1/" myParser = FeatureLabelReader(pathToFiles=pathToFiles, structNameMap=prostate_struct) images = myParser.end_point_parser(filePattern="i*.dcm") labels = myParser.end_point_parser(filePattern="liver_GT*.png") #length has to be equal assert (images.__len__() == labels.__len__()) #testing RS file format pathToFiles = "../../SegmentationDataSets/SBRT_Lung/" myParser = FeatureLabelReader(pathToFiles=pathToFiles, structNameMap=sbrt_lung_struct) images = myParser.end_point_parser(filePath=pathToFiles, filePattern="CT*.dcm") labels = myParser.end_point_parser(filePath=pathToFiles, filePattern="RS*.dcm") #now test compile dataset myParser.compile_dataset(imgFiles=images, labelFiles=labels, fileName="TRAIN_LUNG.h5") #test path in which CT and RS in different directory pathToFiles = "../TrainData/" images = myParser.end_point_parser(filePath=pathToFiles, dirPattern=["PROSTATE", "CT"], filePattern="*.dcm") labels = myParser.end_point_parser(filePath=pathToFiles, dirPattern=["PROSTATE", "RTst"], filePattern="*.dcm")
def end_point_unit_tester(): #test path in which feature label are seperates pathToFiles = "../../SegmentationDataSets/CHAOS/CT_data_batch1/" myParser = FeatureLabelReader(pathToFiles=pathToFiles) images = myParser.end_point_parser(filePattern="i*.dcm") labels = myParser.end_point_parser(filePattern="liver_GT*.png") #length has to be equal assert (images.__len__() == labels.__len__()) #test path in which feature label are same folder pathToFiles = "../../SegmentationDataSets/SegTHOR/train/" images = myParser.end_point_parser(filePath=pathToFiles, filePattern="Patient*.nii.gz") labels = myParser.end_point_parser(filePath=pathToFiles, filePattern="GT*.nii.gz") #length has to be equal assert (images.__len__() == labels.__len__()) #test path in which CT and RS in different directory pathToFiles = "../TrainData/" images = myParser.end_point_parser(filePath=pathToFiles, dirPattern=["PROSTATE", "CT"], filePattern="*.dcm") labels = myParser.end_point_parser(filePath=pathToFiles, dirPattern=["PROSTATE", "RTst"], filePattern="*.dcm") #test path in which CT and RS struct in same directory pathToFiles = "../../SegmentationDataSets/SBRT_Lung/" images = myParser.end_point_parser(filePath=pathToFiles, filePattern="CT*.dcm") labels = myParser.end_point_parser(filePath=pathToFiles, filePattern="RS*.dcm") #test only images pathToFiles = "../ValData/" images = myParser.end_point_parser(filePath=pathToFiles) print(images)