def directory_iterator(root): """ Iterator through a directory that contains: 1) individual subdirectories that contain SRH strips in alterating order 2) Bijection of specimens to directories """ pred_dict = defaultdict(list) for dirpath, dirname, files in os.walk(root): if "NIO" in dirpath: try: mosaic = import_srh_dicom(dirpath) patches = patch_generator(mosaic) normalized_dist = prediction(feedforward(patches, model)) pred_dict["specimen"].append(dirpath.split("/")[-1]) # select only the filename, remove root # append the predictions to each class key in the prediction dictionary for i in range(len(CLASS_NAMES)): pred_dict[CLASS_NAMES[i]].append(normalized_dist[i]) except: continue return pred_dict
def directory_iterator(root): mosaic_dirs = os.listdir(root) mosaic_dirs = sorted(mosaic_dirs) print(mosaic_dirs) counter = 0 mosaic_dict = {} for mosaic_dir in mosaic_dirs: counter += 1 mosaic = import_srh_dicom(os.path.join(root, mosaic_dir)) patches = patch_generator(mosaic) inference_mah = feedforward(patches, model=output_model) score_dict = mahalanobis_score_ensemble(maha_class, inference_mah) mosaic_dict[mosaic_dir] = score_dict print(counter) return mosaic_dict
plt.imshow(heatmap[:,:,class_index]) plt.show() def save_heatmap_image(file_name, heatmap, class_index, cmap='Greys_r'): """ Saves the greyscale heatmap image for specific class """ plt.imsave(file_name, heatmap[:,:,class_index], cmap='Greys_r', vmin=0,vmax=1) if __name__ == '__main__': # import model and specify image directory model = load_model("/home/todd/Desktop/RecPseudo_project/patches/cv_round2/recurmodel_kthfold_2.hdf5") image_dir = "/home/todd/Desktop/Recurrence_mosaics_figures/NIODS10070_2" # import image mosaic = import_srh_dicom(image_dir) image_size = mosaic.shape[0] # index map to allow for patch-tile mapping heatmap_indices = indices_map(mosaic, step_size = STEP_SIZE) # predict on each patch patch_dict = patch_dictionary(mosaic, model, heatmap_indices, image_preprocced=False, step_size = STEP_SIZE) # generate heatmap predictions heatmap = srh_heatmap(patch_dict, image_size, step_size = STEP_SIZE) plt.imshow(heatmap[:,:,1]) plt.show()
output_model = mahalanobis_model(model_path) val_batch, val_steps = validation_batch_steps(training_dir) train_generator = ImageDataGenerator( horizontal_flip=True, vertical_flip=True, preprocessing_function=cnn_preprocessing, data_format="channels_last").flow_from_directory( directory=training_dir, target_size=(img_rows, img_cols), color_mode='rgb', classes=None, class_mode='categorical', batch_size=val_batch, shuffle=False) mahalanobis_output = output_model.predict_generator(train_generator, steps=val_steps, verbose=1) maha_class = Class_conditional_dicts(layer_list=layer_outputs) maha_class.populate_mu_sigmas(mahalanobis_output, train_generator) mosaic_dict = directory_iterator("") export_mahalanobis_scores(mosaic_dict, layer_outputs, metric="mean") mosaic = import_srh_dicom("") patches = patch_generator(mosaic) inference_mah = feedforward(patches, model=output_model) mosaic_dict = mahalanobis_score_ensemble(maha_class, inference_mah)
args = parser.parse_args() CLASS_NAMES = [ 'ependymoma', 'greymatter', 'glioblastoma', 'lowgradeglioma', 'lymphoma', 'medulloblastoma', 'meningioma', 'metastasis', 'nondiagnostic', 'pilocyticastrocytoma', 'pituitaryadenoma', 'pseudoprogression', 'schwannoma', 'whitematter' ] if __name__ == "__main__": # load a trained SRH model deepSRHmodel = load_model(args.model) # import SRH mosaic specimen = import_srh_dicom(args.strip_directory) print("SRH mosaic size is: " + str(specimen.shape)) # generate preprocessed image patches patches = patch_generator(specimen, step_size=100, old_preprocess=True) del specimen # predict on patches specimen_prediction = prediction( feedforward(patch_dict=patches, model=deepSRHmodel)) # print diagnosis print("SRH specimen diagnosis is: " + CLASS_NAMES[np.argmax(specimen_prediction)] + " (probability = " + str(np.max(specimen_prediction)) + ")")
Function to return a list of directories that contain raw SRH strips """ dir_list = sorted(os.listdir(parent_dir)) directory_list = [] for directory in dir_list: directory_list.append(os.path.join(parent_dir, directory)) return directory_list if __name__ == "__main__": # root directory with directories of STRIPS from single mosaic src_dir = "/home/todd/Desktop/test_strips" # destination for ALL PATCHES dest_dir = "/home/todd/Desktop/test_patch_generator" # ordered list of strip directories ordered_dir_list = directory_list(src_dir) for strip_directory in ordered_dir_list: print(strip_directory) # import mosaic mosaic = import_srh_dicom(strip_directory, flatten=False) # returns the patches to save and patches to pass to CNN patches = patch_generator(mosaic, step_size=200) # save patches os.chdir(dest_dir) patch_saver(patches, src_dir=strip_directory)