def show_data_augment(index): images_list = get_all_images_list("train") # Check index is valid if index >= len(images_list): print("Index %s out of range. Max index is %s" % (index, len(images_list) - 1)) exit(-1) file_path, filename = images_list[index] dicom_data = get_dicom_data(file_path) image = tf.convert_to_tensor(dicom_data.pixel_array, dtype=tf.float32) image = tf.reshape(image, (1, image_size, image_size, 1)) # Display the final image fig = plt.figure(figsize=(18, 12)) fig.canvas.set_window_title("Show data augment %s" % filename) show_subplot(plt, 1, image, "Original Image") show_subplot(plt, 2, data_augment.get_tall_image(image), "Tall Image") show_subplot(plt, 3, data_augment.get_large_image(image), "Large Image") show_subplot(plt, 4, data_augment.get_zoomed_image(image), "Zoomed Image") show_subplot(plt, 5, data_augment.get_mirror_image(image), "Mirror Image") show_subplot(plt, 6, data_augment.rotate_and_zoom_image(image), "Rotated and Zoomed Image") show_subplot(plt, 7, data_augment.smooth_image(image), "Smooth Image") show_subplot(plt, 8, data_augment.change_image_brightness(image), "Bright Image") plt.show()
def show_true_mask(index): images_list = get_all_images_list("train") # Check index is valid if index >= len(images_list): print("Index %s out of range. Max index is %s" % (index, len(images_list) - 1)) exit(-1) file_path, filename = images_list[index] dicom_data = get_dicom_data(file_path) # True mask is only known for the training dataset mask = get_true_mask(filename) if np.max(mask) == 0: print("No Pneumothorax on %s" % filename) exit(0) pixels = np.array(dicom_data.pixel_array) * (1 - .5 * mask) # Display the final image fig = plt.figure(figsize=(18, 12)) fig.canvas.set_window_title("Show True Mask of %s" % filename) plt.imshow(pixels) plt.show()
def show_prediction(folder, index): images_list = get_all_images_list(folder) # Check index is valid if index >= len(images_list): print("Index %s out of range. Max index is %s" % (index, len(images_list) - 1)) exit(-1) filepath, filename = images_list[index] dicom_data = get_dicom_data(filepath) # Display the final image fig = plt.figure(figsize=(18, 12)) fig.canvas.set_window_title("Show Predicted Mask of %s" % filename) plt.subplot(1, 2, 1) pixels = dicom_data.pixel_array if (folder == "train"): true_mask = get_true_mask(filename) pixels = np.array(pixels) * (1 - .5 * true_mask) plt.imshow(pixels) plt.subplot(1, 2, 2) predictions = get_segmentation_prediction(filepath) plt.imshow(predictions) plt.show()
def get_segmentation_prediction(filepath): "Returns the prediction as a numpy array of shape (image_size, image_size) for a given filepath" dicom_data = get_dicom_data(filepath) image = format_pixel_array_for_tf(dicom_data.pixel_array) predicted_logits = unet.predict(image, steps=1) predictions = tf.convert_to_tensor(predicted_logits, dtype=tf.float32) predictions = tf.image.resize(predicted_logits, (image_size, image_size), align_corners=True) predictions = sess.run(predictions) predictions = np.reshape(predictions, (image_size, image_size)) return predictions
def get_classification_prediction(filepath): """ Returns the prediction as a numpy array of shape (image_size, image_size) for a given filepath\n Please note that due a bug loading the models, get_classification_prediction currenly uses the body models. Please fix the bug and use the head models of the hydra """ dicom_data = get_dicom_data(filepath) image = format_pixel_array_for_tf(dicom_data.pixel_array) predictions = [model.predict(image, steps=1) for model in all_models] predictions = [p[0][1] for p in predictions] return predictions
def training_generator(graph, starting_index=0): """ Yields a tuple (image, true_mask). Image is a tensor of shape (1, tf_image_size, tf_image_size, 3) and true_mask is a tensor of shape (1, image_size, image_size)\n Due to the way generators work, it is required to specify the graph to work on """ images_list = images_list_root[starting_index:] for (filepath, filename) in images_list: with graph.as_default(): dicom_data = get_dicom_data(filepath) image = format_pixel_array_for_tf(dicom_data.pixel_array) true_mask = get_true_mask(filename) true_mask = tf.convert_to_tensor(true_mask, dtype=tf.float32) true_mask = tf.reshape(true_mask, (1, image_size, image_size, 1)) yield ([image], [true_mask])
def show_data(folder, index): images_list = get_all_images_list(folder) # Check index is valid if index >= len(images_list): print("Index %s out of range. Max index is %s" % (index, len(images_list) - 1)) exit(-1) file_path, filename = images_list[index] dicom_data = get_dicom_data(file_path) # Display the data and image through matplotlib plt.imshow(dicom_data.pixel_array) print("File: %s" % filename) print(dicom_data) plt.show()
def training_generator(graph, data_augment_technique="none", starting_index=0): """ Yields a tuple (image, is_there_pneumothorax). Image is a tensor of shape (1, tf_image_size, tf_image_size, 3) and true_mask is a tensor of shape (1, 2)\n Due to the way generators work, it is required to specify the graph to work on\n data_augment_technique is the name of the technique used by data_augmentation (see data_augment.py -> random_data_augment) """ images_list = images_list_root[starting_index:] for (filepath, filename) in images_list: with graph.as_default(): dicom_data = get_dicom_data(filepath) image = dicom_data.pixel_array image = format_pixel_array_for_tf( image, apply_data_augment_technique=data_augment_technique) is_there_pneumothorax = get_image_label(filename) is_there_pneumothorax = [[ 1 - is_there_pneumothorax, is_there_pneumothorax ]] is_there_pneumothorax = tf.convert_to_tensor( is_there_pneumothorax, dtype=tf.float32) yield ([image], [is_there_pneumothorax])