def extract_dicom_images_patient(src_dir): target_dir = settings.NDSB3_EXTRACTED_IMAGE_DIR print("Dir: ", src_dir) dir_path = settings.NDSB3_RAW_SRC_DIR + src_dir + "/" patient_id = src_dir slices = load_patient(dir_path) print(len(slices), "\t", slices[0].SliceThickness, "\t", slices[0].PixelSpacing) print("Orientation: ", slices[0].ImageOrientationPatient) assert slices[0].ImageOrientationPatient == [1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000] pixels = get_pixels_hu(slices) image = pixels print(image.shape) invert_order = slices[1].ImagePositionPatient[2] > slices[0].ImagePositionPatient[2] print("Invert order: ", invert_order, " - ", slices[1].ImagePositionPatient[2], ",", slices[0].ImagePositionPatient[2]) pixel_spacing = slices[0].PixelSpacing pixel_spacing.append(slices[0].SliceThickness) image = helpers.rescale_patient_images(image, pixel_spacing, settings.TARGET_VOXEL_MM) if not invert_order: image = numpy.flipud(image) for i in range(image.shape[0]): patient_dir = target_dir + patient_id + "/" if not os.path.exists(patient_dir): os.mkdir(patient_dir) img_path = patient_dir + "img_" + str(i).rjust(4, '0') + "_i.png" org_img = image[i] img, mask = helpers.get_segmented_lungs(org_img.copy()) org_img = helpers.normalize_hu(org_img) cv2.imwrite(img_path, org_img * 255) cv2.imwrite(img_path.replace("_i.png", "_m.png"), mask * 255)
def process_image(src_path): patient_id = src_path[-14:-4] print("Patient: ", patient_id) dst_dir = settings.LUNA16_EXTRACTED_IMAGE_DIR + patient_id + "/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) #print("Img array: ", img_array.shape) origin = numpy.array(itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) #print("Origin (x,y,z): ", origin) direction = numpy.array(itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) #print("Direction: ", direction) spacing = numpy.array(itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) #print("Spacing (x,y,z): ", spacing) rescale = spacing / settings.TARGET_VOXEL_MM #print("Rescale: ", rescale) img_array = helpers.rescale_patient_images(img_array, spacing, settings.TARGET_VOXEL_MM) img_list = [] for i in range(img_array.shape[0]): img = img_array[i] seg_img, mask = helpers.get_segmented_lungs(img.copy()) img_list.append(seg_img) img = normalize(img) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_i.png", img * 255) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_m.png", mask * 255)
def extract_dicom_images_patient(src_dir): target_dir = settings.NDSB3_EXTRACTED_IMAGE_DIR #print("Dir: ", src_dir) settings.log.info("Patient: {0}".format(src_dir)) dir_path = settings.NDSB3_RAW_SRC_DIR + src_dir + "/" patient_id = src_dir slices = load_patient(dir_path) settings.log.info( "Slice number: {0} \t thickness - {1} \t x and y spacing - {2}".format( len(slices), slices[0].SliceThickness, slices[0].PixelSpacing)) #print(len(slices), "\t", slices[0].SliceThickness, "\t", slices[0].PixelSpacing) settings.log.info("Orientation: {0}".format( slices[0].ImageOrientationPatient)) #print("Orientation: ", slices[0].ImageOrientationPatient) #assert slices[0].ImageOrientationPatient == [1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000] cos_value = (slices[0].ImageOrientationPatient[0]) cos_degree = round(math.degrees(math.acos(cos_value)), 2) pixels = get_pixels_hu(slices) image = pixels settings.log.info("image shape: {0}".format(image.shape)) invert_order = slices[1].ImagePositionPatient[2] > slices[ 0].ImagePositionPatient[2] settings.log.info("Invert order: {0} - {1:.6f}, {2:.6f}".format( invert_order, slices[1].ImagePositionPatient[2], slices[0].ImagePositionPatient[2])) #print("Invert order: ", invert_order, " - ", slices[1].ImagePositionPatient[2], ",", slices[0].ImagePositionPatient[2]) pixel_spacing = slices[0].PixelSpacing pixel_spacing.append(slices[0].SliceThickness) image = helpers.rescale_patient_images(image, pixel_spacing, settings.TARGET_VOXEL_MM, verbose=True) if not invert_order: image = numpy.flipud(image) for i in range(image.shape[0]): patient_dir = target_dir + patient_id + "/" if not os.path.exists(patient_dir): os.makedirs(patient_dir) img_path = patient_dir + "img_" + str(i).rjust(4, '0') + "_i.png" org_img = image[i] # if there exists slope,rotation image with corresponding degree if cos_degree > 0.0: org_img = cv_flip(org_img, org_img.shape[1], org_img.shape[0], cos_degree) img, mask = helpers.get_segmented_lungs(org_img.copy()) org_img = helpers.normalize_hu(org_img) cv2.imwrite(img_path, org_img * 255) cv2.imwrite(img_path.replace("_i.png", "_m.png"), mask * 255)
def extract_dicom_images_patient(src_dir): #directory where the extracted images will go target_dir = settings.NDSB3_EXTRACTED_IMAGE_DIR print("Dir: ", src_dir) #directory of the patient (folder name = patientID) dir_path = settings.NDSB3_RAW_SRC_DIR + src_dir + "/" patient_id = src_dir #sorts slices by acquisition, obtains thickness, applies to all slices, then returns array of slices slices = load_patient(dir_path) print(len(slices), "\t", slices[0].SliceThickness, "\t", slices[0].PixelSpacing) print("Orientation: ", slices[0].ImageOrientationPatient) #assert slices[0].ImageOrientationPatient == [1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000] cos_value = (slices[0].ImageOrientationPatient[0]) cos_degree = round(math.degrees(math.acos(cos_value)), 2) #convert to image pixels = get_pixels_hu(slices) image = pixels print(image.shape) #check if order is inverted invert_order = slices[1].ImagePositionPatient[2] > slices[ 0].ImagePositionPatient[2] print("Invert order: ", invert_order, " - ", slices[1].ImagePositionPatient[2], ",", slices[0].ImagePositionPatient[2]) #rescale images based on pixel spacing pixel_spacing = slices[0].PixelSpacing pixel_spacing.append(slices[0].SliceThickness) image = helpers.rescale_patient_images(image, pixel_spacing, settings.TARGET_VOXEL_MM) if not invert_order: image = numpy.flipud(image) #go through the height of the image (rows = [0]) for i in range(image.shape[0]): patient_dir = target_dir + patient_id + "/" if not os.path.exists(patient_dir): os.mkdir(patient_dir) #write image out as .png file img_path = patient_dir + "img_" + str(i).rjust(4, '0') + "_i.png" #original image is the one just written to folder org_img = image[i] # if there exists slope,rotation image with corresponding degree if cos_degree > 0.0: org_img = cv_flip(org_img, org_img.shape[1], org_img.shape[0], cos_degree) img, mask = helpers.get_segmented_lungs(org_img.copy()) #normalize hounsfield units org_img = helpers.normalize_hu(org_img) cv2.imwrite(img_path, org_img * 255) cv2.imwrite(img_path.replace("_i.png", "_m.png"), mask * 255)
def process_image(src_path): """Load the '.mhd' file, extract the 3D numpy array, rescale the data, write out each slice as '.png' and each segmented mask as '.png' """ patient_id = ntpath.basename(src_path).replace( ".mhd", "") #extract patient id from filename print("Patient: ", patient_id) dst_dir = settings.LUNA16_EXTRACTED_IMAGE_DIR + patient_id + "/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) print("Img array: ", img_array.shape) origin = numpy.array( itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) print("Origin (x,y,z): ", origin) direction = numpy.array( itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) print("Direction: ", direction) spacing = numpy.array( itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) print("Spacing (x,y,z): ", spacing) rescale = spacing / settings.TARGET_VOXEL_MM print("Rescale: ", rescale) img_array = helpers.rescale_patient_images(img_array, spacing, settings.TARGET_VOXEL_MM) img_list = [] for i in range(img_array.shape[0]): img = img_array[i] seg_img, mask = helpers.get_segmented_lungs( img.copy()) #find the segmented image and the mask img_list.append(seg_img) img = normalize(img) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_i.png", img * 255) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_m.png", mask * 255)
def process_image(src_path): patient_id = ntpath.basename(src_path).replace(".mhd", "") print("Patient: ", patient_id) dst_dir = settings.LUNA16_EXTRACTED_IMAGE_DIR + patient_id + "/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) print("Img array: ", img_array.shape) origin = numpy.array( itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) print("Origin (x,y,z): ", origin) direction = numpy.array( itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) print("Direction: ", direction) spacing = numpy.array( itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) print("Spacing (x,y,z): ", spacing) rescale = spacing / settings.TARGET_VOXEL_MM print("Rescale: ", rescale) #Rescale our scan img_array = helpers.rescale_patient_images(img_array, spacing, settings.TARGET_VOXEL_MM) img_list = [] #Iterate on each frame for i in range(img_array.shape[0]): img = img_array[i] #Get the segmented lungs, so we know the regions of interest, using Segmentation seg_img, mask = helpers.get_segmented_lungs(img.copy()) img_list.append(seg_img) img = normalize(img) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_i.png", img * 255) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_m.png", mask * 255)
def process_image(src_path): patient_id = ntpath.basename(src_path).replace(".mhd", "") print("Patient: ", patient_id) if patient_id == 'LKDS-00384': return dst_dir = settings.TEST_EXTRACTED_IMAGE_DIR + patient_id + "/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) print("Img array: ", img_array.shape) origin = numpy.array( itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) print("Origin (x,y,z): ", origin) direction = numpy.array( itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) print("Direction: ", direction) spacing = numpy.array( itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) print("Spacing (x,y,z): ", spacing) rescale = spacing / settings.TARGET_VOXEL_MM print("Rescale: ", rescale) #归一化,体素间距为1,z保留512个最多 img_array = helpers.rescale_patient_images(img_array, spacing, settings.TARGET_VOXEL_MM) img_list = [] for i in range(img_array.shape[0]): img = img_array[i] seg_img, mask = helpers.get_segmented_lungs(img.copy()) img_list.append(seg_img) img = normalize(img) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_i.png", img * 255) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_m.png", mask * 255)
def fetch_image(src_path_file,TARGET_VOXEL_MM=1): """Load the '.mhd' file, extract the 3D numpy array, rescale the data, and normalize. Args: src_path_file: (string) the complete path (dir + filename) to the .mhd file to load TARGET_VOXEL_MM: the size of each voxel (mm) after rescaling of the data Returns: img_array: (np array) 3D array of image data. After rescaling and normalizing """ patient_id = ntpath.basename(src_path_file).replace(".mhd", "") #extract patient id from filename print("Patient: ", patient_id) itk_img = SimpleITK.ReadImage(src_path_file) img_array = SimpleITK.GetArrayFromImage(itk_img) #Extract actual array data #origin = np.array(itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) #direction = np.array(itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) spacing = np.array(itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) #rescale = spacing / TARGET_VOXEL_MM img_array = helpers.rescale_patient_images(img_array, spacing, TARGET_VOXEL_MM) img_array = normalize(img_array) return img_array
def process_image(src_path): patient_id = os.path.basename(src_path).replace(".mhd", "") settings.log.info("Patient: {0}".format(patient_id)) dst_dir = settings.LUNA16_EXTRACTED_IMAGE_DIR + patient_id + "/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) settings.log.info("Img array: {0}".format(img_array.shape)) origin = numpy.array( itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) settings.log.info("Origin (x,y,z): {0}".format(origin)) direction = numpy.array( itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) settings.log.info("Direction: {0}".format(direction)) spacing = numpy.array( itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) settings.log.info("Spacing (x,y,z): {0}".format(spacing)) rescale = spacing / settings.TARGET_VOXEL_MM settings.log.info("Rescale: {0}".format(rescale)) img_array = helpers.rescale_patient_images(img_array, spacing, settings.TARGET_VOXEL_MM) img_list = [] for i in range(img_array.shape[0]): img = img_array[i] seg_img, mask = helpers.get_segmented_lungs(img.copy()) img_list.append(seg_img) img = normalize(img) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_i.png", img * 255) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_m.png", mask * 255)
def extract_dicom_images_patient(src_dir): target_dir = settings.NDSB3_EXTRACTED_IMAGE_DIR print("Dir: ", src_dir) dir_path = settings.NDSB3_RAW_SRC_DIR + src_dir + "/" patient_id = src_dir slices = load_patient(dir_path) print(len(slices), "\t", slices[0].SliceThickness, "\t", slices[0].PixelSpacing) print("Orientation: ", slices[0].ImageOrientationPatient) assert slices[0].ImageOrientationPatient == [ 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000 ] pixels = get_pixels_hu(slices) image = pixels print(image.shape) invert_order = slices[1].ImagePositionPatient[2] > slices[ 0].ImagePositionPatient[2] print("Invert order: ", invert_order, " - ", slices[1].ImagePositionPatient[2], ",", slices[0].ImagePositionPatient[2]) pixel_spacing = slices[0].PixelSpacing pixel_spacing.append(slices[0].SliceThickness) image = helpers.rescale_patient_images(image, pixel_spacing, settings.TARGET_VOXEL_MM) if not invert_order: image = numpy.flipud(image) for i in range(image.shape[0]): patient_dir = target_dir + patient_id + "/" if not os.path.exists(patient_dir): os.mkdir(patient_dir) img_path = patient_dir + "img_" + str(i).rjust(4, '0') + "_i.png" org_img = image[i] img, mask = helpers.get_segmented_lungs(org_img.copy()) org_img = helpers.normalize_hu(org_img) cv2.imwrite(img_path, org_img * 255) cv2.imwrite(img_path.replace("_i.png", "_m.png"), mask * 255)
def predict_cubes(patient_ids, z0, model_path, magnification=1, flip=False, holdout_no=-1, ext_name="", fold_count=2): sw = helpers.Stopwatch.start_new() all_predictions_csv = [] for patient_index, patient_id in enumerate(reversed(patient_ids)): if "metadata" in patient_id: continue if "labels" in patient_id: continue patient_img = helpers.load_patient_images(patient_id, LUNA16_EXTRACTED_IMAGE_DIR, "*_i.png", []) if magnification != 1: patient_img = helpers.rescale_patient_images( patient_img, (1, 1, 1), magnification) patient_mask = helpers.load_patient_images(patient_id, LUNA16_EXTRACTED_IMAGE_DIR, "*_m.png", []) if magnification != 1: patient_mask = helpers.rescale_patient_images(patient_mask, (1, 1, 1), magnification, is_mask_image=True) # patient_img = patient_img[:, ::-1, :] # patient_mask = patient_mask[:, ::-1, :] step = PREDICT_STEP CROP_SIZE = CUBE_SIZE # CROP_SIZE = 48 predict_volume_shape_list = [0, 0, 0] for dim in range(3): dim_indent = 0 while dim_indent + CROP_SIZE < patient_img.shape[dim]: predict_volume_shape_list[dim] += 1 dim_indent += step predict_volume_shape = (predict_volume_shape_list[0], predict_volume_shape_list[1], predict_volume_shape_list[2]) predict_volume = numpy.zeros(shape=predict_volume_shape, dtype=float) done_count = 0 skipped_count = 0 batch_size = 128 batch_list = [] batch_list_coords = [] patient_predictions_csv = [] annotation_index = 0 if z0 < 0: z0 = 0 z1 = predict_volume_shape[0] else: z1 = z0 + 1 for z in range(z0, z1): for y in range(0, predict_volume_shape[1]): for x in range(0, predict_volume_shape[2]): #if cube_img is None: cube_img = patient_img[z * step:z * step + CROP_SIZE, y * step:y * step + CROP_SIZE, x * step:x * step + CROP_SIZE] cube_mask = patient_mask[z * step:z * step + CROP_SIZE, y * step:y * step + CROP_SIZE, x * step:x * step + CROP_SIZE] if cube_mask.sum() < 2000: skipped_count += 1 else: if flip: cube_img = cube_img[:, :, ::-1] if CROP_SIZE != CUBE_SIZE: cube_img = helpers.rescale_patient_images2( cube_img, (CUBE_SIZE, CUBE_SIZE, CUBE_SIZE)) # helpers.save_cube_img("c:/tmp/cube.png", cube_img, 8, 4) # cube_mask = helpers.rescale_patient_images2(cube_mask, (CUBE_SIZE, CUBE_SIZE, CUBE_SIZE)) img_prep = prepare_image_for_net3D(cube_img) batch_list.append(img_prep) batch_list_coords.append((z, y, x)) if len(batch_list) % batch_size == 0: batch_data = numpy.vstack(batch_list) p = model.predict(batch_data, batch_size=batch_size) for i in range(len(p[0])): p_z = batch_list_coords[i][0] p_y = batch_list_coords[i][1] p_x = batch_list_coords[i][2] nodule_chance = p[0][i][0] predict_volume[p_z, p_y, p_x] = nodule_chance if nodule_chance > P_TH: p_z = p_z * step + CROP_SIZE / 2 p_y = p_y * step + CROP_SIZE / 2 p_x = p_x * step + CROP_SIZE / 2 p_z_perc = round( float(p_z) / patient_img.shape[0], 4) p_y_perc = round( float(p_y) / patient_img.shape[1], 4) p_x_perc = round( float(p_x) / patient_img.shape[2], 4) diameter_mm = round(p[1][i][0], 4) # diameter_perc = round(2 * step / patient_img.shape[2], 4) diameter_perc = round( 2 * step / patient_img.shape[2], 4) diameter_perc = round( diameter_mm / patient_img.shape[2], 4) nodule_chance = round(nodule_chance, 4) patient_predictions_csv_line = [ annotation_index, p_x, p_y, p_z, p_x_perc, p_y_perc, p_z_perc, diameter_perc, nodule_chance, diameter_mm ] patient_predictions_csv.append( patient_predictions_csv_line) all_predictions_csv.append( [patient_id] + patient_predictions_csv_line) annotation_index += 1 batch_list = [] batch_list_coords = [] done_count += 1 df = pandas.DataFrame(patient_predictions_csv, columns=[ "anno_index", "ax", "ay", "az", "coord_x", "coord_y", "coord_z", "diameter", "nodule_chance", "diameter_mm" ]) filter_patient_nodules_predictions(df, patient_id, CROP_SIZE * magnification) return df
def predict_cubes(model_path, continue_job, only_patient_id=None, luna16=False, magnification=1, flip=False, train_data=True, holdout_no=-1, ext_name="", fold_count=2): if luna16: dst_dir = settings.LUNA_NODULE_DETECTION_DIR else: dst_dir = settings.NDSB3_NODULE_DETECTION_DIR if not os.path.exists(dst_dir): os.makedirs(dst_dir) holdout_ext = "" # if holdout_no is not None: # holdout_ext = "_h" + str(holdout_no) if holdout_no >= 0 else "" flip_ext = "" if flip: flip_ext = "_flip" dst_dir += "predictions" + str(int( magnification * 10)) + holdout_ext + flip_ext + "_" + ext_name + "/" if not os.path.exists(dst_dir): os.makedirs(dst_dir) sw = helpers.Stopwatch.start_new() model = step2_train_nodule_detector.get_net(input_shape=(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE, 1), load_weight_path=model_path) if not luna16: if train_data: labels_df = pandas.read_csv("resources/stage1_labels.csv") labels_df.set_index(["id"], inplace=True) else: #labels_df = pandas.read_csv("resources/stage2_sample_submission.csv") labels_df = pandas.read_csv("resources/tc_sample_submission.csv") labels_df.set_index(["id"], inplace=True) patient_ids = [] for file_name in os.listdir(settings.NDSB3_EXTRACTED_IMAGE_DIR): if not os.path.isdir(settings.NDSB3_EXTRACTED_IMAGE_DIR + file_name): continue patient_ids.append(file_name) all_predictions_csv = [] for patient_index, patient_id in enumerate(reversed(patient_ids)): if not luna16: if patient_id not in labels_df.index: continue if "metadata" in patient_id: continue if only_patient_id is not None and only_patient_id != patient_id: continue if holdout_no is not None and train_data: patient_fold = helpers.get_patient_fold(patient_id) patient_fold %= fold_count if patient_fold != holdout_no: continue print(patient_index, ": ", patient_id) csv_target_path = dst_dir + patient_id + ".csv" if continue_job and only_patient_id is None: if os.path.exists(csv_target_path): continue patient_img = helpers.load_patient_images( patient_id, settings.NDSB3_EXTRACTED_IMAGE_DIR, "*_i.png", []) if magnification != 1: patient_img = helpers.rescale_patient_images( patient_img, (1, 1, 1), magnification) patient_mask = helpers.load_patient_images( patient_id, settings.NDSB3_EXTRACTED_IMAGE_DIR, "*_m.png", []) if magnification != 1: patient_mask = helpers.rescale_patient_images(patient_mask, (1, 1, 1), magnification, is_mask_image=True) # patient_img = patient_img[:, ::-1, :] # patient_mask = patient_mask[:, ::-1, :] step = PREDICT_STEP CROP_SIZE = CUBE_SIZE # CROP_SIZE = 48 predict_volume_shape_list = [0, 0, 0] for dim in range(3): dim_indent = 0 while dim_indent + CROP_SIZE < patient_img.shape[dim]: predict_volume_shape_list[dim] += 1 dim_indent += step predict_volume_shape = (predict_volume_shape_list[0], predict_volume_shape_list[1], predict_volume_shape_list[2]) predict_volume = numpy.zeros(shape=predict_volume_shape, dtype=float) print("Predict volume shape: ", predict_volume.shape) done_count = 0 skipped_count = 0 batch_size = 128 batch_list = [] batch_list_coords = [] patient_predictions_csv = [] cube_img = None annotation_index = 0 for z in range(0, predict_volume_shape[0]): for y in range(0, predict_volume_shape[1]): for x in range(0, predict_volume_shape[2]): #if cube_img is None: cube_img = patient_img[z * step:z * step + CROP_SIZE, y * step:y * step + CROP_SIZE, x * step:x * step + CROP_SIZE] cube_mask = patient_mask[z * step:z * step + CROP_SIZE, y * step:y * step + CROP_SIZE, x * step:x * step + CROP_SIZE] if cube_mask.sum() < 2000: skipped_count += 1 else: if flip: cube_img = cube_img[:, :, ::-1] if CROP_SIZE != CUBE_SIZE: cube_img = helpers.rescale_patient_images2( cube_img, (CUBE_SIZE, CUBE_SIZE, CUBE_SIZE)) # helpers.save_cube_img("c:/tmp/cube.png", cube_img, 8, 4) # cube_mask = helpers.rescale_patient_images2(cube_mask, (CUBE_SIZE, CUBE_SIZE, CUBE_SIZE)) img_prep = prepare_image_for_net3D(cube_img) batch_list.append(img_prep) batch_list_coords.append((z, y, x)) if len(batch_list) % batch_size == 0: batch_data = numpy.vstack(batch_list) p = model.predict(batch_data, batch_size=batch_size) for i in range(len(p[0])): p_z = batch_list_coords[i][0] p_y = batch_list_coords[i][1] p_x = batch_list_coords[i][2] nodule_chance = p[0][i][0] predict_volume[p_z, p_y, p_x] = nodule_chance if nodule_chance > P_TH: p_z = p_z * step + CROP_SIZE / 2 p_y = p_y * step + CROP_SIZE / 2 p_x = p_x * step + CROP_SIZE / 2 p_z_perc = round( p_z / patient_img.shape[0], 4) p_y_perc = round( p_y / patient_img.shape[1], 4) p_x_perc = round( p_x / patient_img.shape[2], 4) diameter_mm = round(p[1][i][0], 4) # diameter_perc = round(2 * step / patient_img.shape[2], 4) diameter_perc = round( 2 * step / patient_img.shape[2], 4) diameter_perc = round( diameter_mm / patient_img.shape[2], 4) nodule_chance = round(nodule_chance, 4) #patient_predictions_csv_line = [annotation_index, p_x_perc, p_y_perc, p_z_perc, diameter_perc, nodule_chance, diameter_mm] patient_predictions_csv_line = [ annotation_index, p_x_perc, p_y_perc, p_z_perc, diameter_perc, nodule_chance, diameter_mm, p_x, p_y, p_z ] #patient_predictions_csv_line = [annotation_index, p_x, p_y, p_z, diameter_perc, nodule_chance, diameter_mm] patient_predictions_csv.append( patient_predictions_csv_line) all_predictions_csv.append( [patient_id] + patient_predictions_csv_line) annotation_index += 1 batch_list = [] batch_list_coords = [] done_count += 1 if done_count % 10000 == 0: print("Done: ", done_count, " skipped:", skipped_count) df = pandas.DataFrame(patient_predictions_csv, columns=[ "anno_index", "coord_x", "coord_y", "coord_z", "diameter", "nodule_chance", "diameter_mm", "abs_x", "abs_y", "abs_z" ]) filter_patient_nodules_predictions(df, patient_id, CROP_SIZE * magnification) df.to_csv(csv_target_path, index=False) # cols = ["anno_index", "nodule_chance", "diamete_mm"] + ["f" + str(i) for i in range(64)] # df_features = pandas.DataFrame(patient_features_csv, columns=cols) # for index, row in df.iterrows(): # if row["diameter_mm"] < 0: # print("Dropping") # anno_index = row["anno_index"] # df_features.drop(df_features[df_features["anno_index"] == anno_index].index, inplace=True) # # df_features.to_csv(csv_target_path_features, index=False) # df = pandas.DataFrame(all_predictions_csv, columns=["patient_id", "anno_index", "coord_x", "coord_y", "coord_z", "diameter", "nodule_chance", "diameter_mm"]) # df.to_csv("c:/tmp/tmp2.csv", index=False) print(predict_volume.mean()) print("Done in : ", sw.get_elapsed_seconds(), " seconds")
def predict_cubes(path, model_path, magnification=1, holdout_no=-1, ext_name="", fold_count=2): dst_dir = settings.LUNA_NODULE_DETECTION_DIR if not os.path.exists(dst_dir): os.makedirs(dst_dir) holdout_ext = "" dst_dir += "predictions" + str(int( magnification * 10)) + holdout_ext + "_" + ext_name + "/" if not os.path.exists(dst_dir): os.makedirs(dst_dir) sw = helpers.Stopwatch.start_new() model = step2_train_nodule_detector.get_net(input_shape=(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE, 1), load_weight_path=model_path) patient_id = path all_predictions_csv = [] if holdout_no is not None: patient_fold = helpers.get_patient_fold(patient_id) patient_fold %= fold_count print(": ", patient_id) csv_target_path = dst_dir + patient_id + ".csv" print(patient_id) try: patient_img = helpers.load_patient_images(patient_id + '_Preprocessed', '', "*_i.png", []) except: print('Please Re-Process the dicom file again') if magnification != 1: patient_img = helpers.rescale_patient_images(patient_img, (1, 1, 1), magnification) patient_mask = helpers.load_patient_images(patient_id + '_Preprocessed', '', "*_m.png", []) if magnification != 1: patient_mask = helpers.rescale_patient_images(patient_mask, (1, 1, 1), magnification, is_mask_image=True) # patient_img = patient_img[:, ::-1, :] # patient_mask = patient_mask[:, ::-1, :] step = PREDICT_STEP CROP_SIZE = CUBE_SIZE # CROP_SIZE = 48 predict_volume_shape_list = [0, 0, 0] for dim in range(3): dim_indent = 0 while dim_indent + CROP_SIZE < patient_img.shape[dim]: predict_volume_shape_list[dim] += 1 dim_indent += step predict_volume_shape = (predict_volume_shape_list[0], predict_volume_shape_list[1], predict_volume_shape_list[2]) predict_volume = numpy.zeros(shape=predict_volume_shape, dtype=float) print("Predict volume shape: ", predict_volume.shape) done_count = 0 skipped_count = 0 batch_size = 128 batch_list = [] batch_list_coords = [] patient_predictions_csv = [] cube_img = None annotation_index = 0 for z in range(0, predict_volume_shape[0]): for y in range(0, predict_volume_shape[1]): for x in range(0, predict_volume_shape[2]): #if cube_img is None: cube_img = patient_img[z * step:z * step + CROP_SIZE, y * step:y * step + CROP_SIZE, x * step:x * step + CROP_SIZE] cube_mask = patient_mask[z * step:z * step + CROP_SIZE, y * step:y * step + CROP_SIZE, x * step:x * step + CROP_SIZE] if cube_mask.sum() < 2000: skipped_count += 1 if CROP_SIZE != CUBE_SIZE: cube_img = helpers.rescale_patient_images2( cube_img, (CUBE_SIZE, CUBE_SIZE, CUBE_SIZE)) # helpers.save_cube_img("c:/tmp/cube.png", cube_img, 8, 4) # cube_mask = helpers.rescale_patient_images2(cube_mask, (CUBE_SIZE, CUBE_SIZE, CUBE_SIZE)) img_prep = prepare_image_for_net3D(cube_img) batch_list.append(img_prep) batch_list_coords.append((z, y, x)) if len(batch_list) % batch_size == 0: batch_data = numpy.vstack(batch_list) p = model.predict(batch_data, batch_size=batch_size) for i in range(len(p[0])): p_z = batch_list_coords[i][0] p_y = batch_list_coords[i][1] p_x = batch_list_coords[i][2] nodule_chance = p[0][i][0] predict_volume[p_z, p_y, p_x] = nodule_chance if nodule_chance > P_TH: p_z = p_z * step + CROP_SIZE / 2 p_y = p_y * step + CROP_SIZE / 2 p_x = p_x * step + CROP_SIZE / 2 p_z_perc = round(p_z / patient_img.shape[0], 4) p_y_perc = round(p_y / patient_img.shape[1], 4) p_x_perc = round(p_x / patient_img.shape[2], 4) diameter_mm = round(p[1][i][0], 4) # diameter_perc = round(2 * step / patient_img.shape[2], 4) diameter_perc = round( 2 * step / patient_img.shape[2], 4) diameter_perc = round( diameter_mm / patient_img.shape[2], 4) nodule_chance = round(nodule_chance, 4) patient_predictions_csv_line = [ annotation_index, p_x_perc, p_y_perc, p_z_perc, diameter_perc, nodule_chance, diameter_mm ] patient_predictions_csv.append( patient_predictions_csv_line) all_predictions_csv.append( [patient_id] + patient_predictions_csv_line) annotation_index += 1 batch_list = [] batch_list_coords = [] done_count += 1 if done_count % 10000 == 0: print("Done: ", done_count, " skipped:", skipped_count) df = pandas.DataFrame(patient_predictions_csv, columns=[ "anno_index", "coord_x", "coord_y", "coord_z", "diameter", "nodule_chance", "diameter_mm" ]) print("Started Filtering") print(all_predictions_csv) #print(batch_data) filter_patient_nodules_predictions(df, patient_id, CROP_SIZE * magnification) df.to_csv(csv_target_path, index=False) # cols = ["anno_index", "nodule_chance", "diamete_mm"] + ["f" + str(i) for i in range(64)] # df_features = pandas.DataFrame(patient_features_csv, columns=cols) # for index, row in df.iterrows(): # if row["diameter_mm"] < 0: # print("Dropping") # anno_index = row["anno_index"] # df_features.drop(df_features[df_features["anno_index"] == anno_index].index, inplace=True) # # df_features.to_csv(csv_target_path_features, index=False) # df = pandas.DataFrame(all_predictions_csv, columns=["patient_id", "anno_index", "coord_x", "coord_y", "coord_z", "diameter", "nodule_chance", "diameter_mm"]) # df.to_csv("c:/tmp/tmp2.csv", index=False) print(predict_volume.mean()) print("Done in : ", sw.get_elapsed_seconds(), " seconds")
def process_image(src_path): patient_id = ntpath.basename(src_path).replace(".mhd", "") #df_patient = annotations[annotations['seriesuid'] == patient_id] #print("Patient: ", patient_id) dst_dir = traindata_path + patient_id + "/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) print("Img array: ", img_array.shape) origin = numpy.array( itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) print("Origin (x,y,z): ", origin) direction = numpy.array( itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) print("Direction: ", direction) spacing = numpy.array( itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) print("Spacing (x,y,z): ", spacing) rescale = spacing / setting.TARGET_VOXEL_MM print("Rescale: ", rescale) #calc real origin flip_direction_x = False flip_direction_y = False if round(direction[0]) == -1: origin[0] *= -1 direction[0] = 1 flip_direction_x = True print("Swappint x origin") if round(direction[4]) == -1: origin[1] *= -1 direction[4] = 1 flip_direction_y = True print("Swappint y origin") print("Direction: ", direction) assert abs(sum(direction) - 3) < 0.01 try: img_array = helpers.rescale_patient_images(img_array, spacing, setting.TARGET_VOXEL_MM) except: print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") img_list = [] lung_mask = lung_segmentation.segment_HU_scan_elias(img_array) for i in range(img_array.shape[0]): #img = img_array[i] #img = normalize(img) #mask = lung_mask[i] #cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_i.png", img * 255) #cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_m.png", mask * 255) #orgin kaggle ranking 2 code #nodule_mask = numpy.zeros([512,512]) img = img_array[i] seg_img, mask = helpers.get_segmented_lungs(img.copy()) img_list.append(seg_img) img = normalize(img) #img = normalize(img) #img[mask==0] = 0 cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_i.png", img * 255) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_m.png", mask * 255)
def predict_cubes(model_path, continue_job, only_patient_id=None, luna16=False, magnification=1, flip=False, train_data=True, holdout_no=-1, ext_name="", fold_count=2): if luna16: dst_dir = settings.LUNA_NODULE_DETECTION_DIR else: dst_dir = settings.NDSB3_NODULE_DETECTION_DIR if not os.path.exists(dst_dir): os.makedirs(dst_dir) holdout_ext = "" # if holdout_no is not None: # holdout_ext = "_h" + str(holdout_no) if holdout_no >= 0 else "" flip_ext = "" if flip: flip_ext = "_flip" dst_dir += "predictions" + str(int(magnification * 10)) + holdout_ext + flip_ext + "_" + ext_name + "/" if not os.path.exists(dst_dir): os.makedirs(dst_dir) sw = helpers.Stopwatch.start_new() model = step2_train_nodule_detector.get_net(input_shape=(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE, 1), load_weight_path=model_path) if not luna16: if train_data: labels_df = pandas.read_csv("resources/stage1_labels.csv") labels_df.set_index(["id"], inplace=True) else: labels_df = pandas.read_csv("resources/stage2_sample_submission.csv") labels_df.set_index(["id"], inplace=True) patient_ids = [] for file_name in os.listdir(settings.NDSB3_EXTRACTED_IMAGE_DIR): if not os.path.isdir(settings.NDSB3_EXTRACTED_IMAGE_DIR + file_name): continue patient_ids.append(file_name) all_predictions_csv = [] for patient_index, patient_id in enumerate(reversed(patient_ids)): if not luna16: if patient_id not in labels_df.index: continue if "metadata" in patient_id: continue if only_patient_id is not None and only_patient_id != patient_id: continue if holdout_no is not None and train_data: patient_fold = helpers.get_patient_fold(patient_id) patient_fold %= fold_count if patient_fold != holdout_no: continue print(patient_index, ": ", patient_id) csv_target_path = dst_dir + patient_id + ".csv" if continue_job and only_patient_id is None: if os.path.exists(csv_target_path): continue patient_img = helpers.load_patient_images(patient_id, settings.NDSB3_EXTRACTED_IMAGE_DIR, "*_i.png", []) if magnification != 1: patient_img = helpers.rescale_patient_images(patient_img, (1, 1, 1), magnification) patient_mask = helpers.load_patient_images(patient_id, settings.NDSB3_EXTRACTED_IMAGE_DIR, "*_m.png", []) if magnification != 1: patient_mask = helpers.rescale_patient_images(patient_mask, (1, 1, 1), magnification, is_mask_image=True) # patient_img = patient_img[:, ::-1, :] # patient_mask = patient_mask[:, ::-1, :] step = PREDICT_STEP CROP_SIZE = CUBE_SIZE # CROP_SIZE = 48 predict_volume_shape_list = [0, 0, 0] for dim in range(3): dim_indent = 0 while dim_indent + CROP_SIZE < patient_img.shape[dim]: predict_volume_shape_list[dim] += 1 dim_indent += step predict_volume_shape = (predict_volume_shape_list[0], predict_volume_shape_list[1], predict_volume_shape_list[2]) predict_volume = numpy.zeros(shape=predict_volume_shape, dtype=float) print("Predict volume shape: ", predict_volume.shape) done_count = 0 skipped_count = 0 batch_size = 128 batch_list = [] batch_list_coords = [] patient_predictions_csv = [] cube_img = None annotation_index = 0 for z in range(0, predict_volume_shape[0]): for y in range(0, predict_volume_shape[1]): for x in range(0, predict_volume_shape[2]): #if cube_img is None: cube_img = patient_img[z * step:z * step+CROP_SIZE, y * step:y * step + CROP_SIZE, x * step:x * step+CROP_SIZE] cube_mask = patient_mask[z * step:z * step+CROP_SIZE, y * step:y * step + CROP_SIZE, x * step:x * step+CROP_SIZE] if cube_mask.sum() < 2000: skipped_count += 1 else: if flip: cube_img = cube_img[:, :, ::-1] if CROP_SIZE != CUBE_SIZE: cube_img = helpers.rescale_patient_images2(cube_img, (CUBE_SIZE, CUBE_SIZE, CUBE_SIZE)) # helpers.save_cube_img("c:/tmp/cube.png", cube_img, 8, 4) # cube_mask = helpers.rescale_patient_images2(cube_mask, (CUBE_SIZE, CUBE_SIZE, CUBE_SIZE)) img_prep = prepare_image_for_net3D(cube_img) batch_list.append(img_prep) batch_list_coords.append((z, y, x)) if len(batch_list) % batch_size == 0: batch_data = numpy.vstack(batch_list) p = model.predict(batch_data, batch_size=batch_size) for i in range(len(p[0])): p_z = batch_list_coords[i][0] p_y = batch_list_coords[i][1] p_x = batch_list_coords[i][2] nodule_chance = p[0][i][0] predict_volume[p_z, p_y, p_x] = nodule_chance if nodule_chance > P_TH: p_z = p_z * step + CROP_SIZE / 2 p_y = p_y * step + CROP_SIZE / 2 p_x = p_x * step + CROP_SIZE / 2 p_z_perc = round(p_z / patient_img.shape[0], 4) p_y_perc = round(p_y / patient_img.shape[1], 4) p_x_perc = round(p_x / patient_img.shape[2], 4) diameter_mm = round(p[1][i][0], 4) # diameter_perc = round(2 * step / patient_img.shape[2], 4) diameter_perc = round(2 * step / patient_img.shape[2], 4) diameter_perc = round(diameter_mm / patient_img.shape[2], 4) nodule_chance = round(nodule_chance, 4) patient_predictions_csv_line = [annotation_index, p_x_perc, p_y_perc, p_z_perc, diameter_perc, nodule_chance, diameter_mm] patient_predictions_csv.append(patient_predictions_csv_line) all_predictions_csv.append([patient_id] + patient_predictions_csv_line) annotation_index += 1 batch_list = [] batch_list_coords = [] done_count += 1 if done_count % 10000 == 0: print("Done: ", done_count, " skipped:", skipped_count) df = pandas.DataFrame(patient_predictions_csv, columns=["anno_index", "coord_x", "coord_y", "coord_z", "diameter", "nodule_chance", "diameter_mm"]) filter_patient_nodules_predictions(df, patient_id, CROP_SIZE * magnification) df.to_csv(csv_target_path, index=False) # cols = ["anno_index", "nodule_chance", "diamete_mm"] + ["f" + str(i) for i in range(64)] # df_features = pandas.DataFrame(patient_features_csv, columns=cols) # for index, row in df.iterrows(): # if row["diameter_mm"] < 0: # print("Dropping") # anno_index = row["anno_index"] # df_features.drop(df_features[df_features["anno_index"] == anno_index].index, inplace=True) # # df_features.to_csv(csv_target_path_features, index=False) # df = pandas.DataFrame(all_predictions_csv, columns=["patient_id", "anno_index", "coord_x", "coord_y", "coord_z", "diameter", "nodule_chance", "diameter_mm"]) # df.to_csv("c:/tmp/tmp2.csv", index=False) print(predict_volume.mean()) print("Done in : ", sw.get_elapsed_seconds(), " seconds")