def create_weights_from_masks(mask_folder, output_folder, NUM_PROCS=8):
    """Creates weight image for all masks in mask_folder and saves to output folder.

    This process uses paralell processing to speed this up.

    Parameters
    ----------
    mask_folder : str
        Path to mask folder
    output_folder : str
        Desired output folder
    NUM_PROCS : int, optional
        How many threads to spawn

    """
    from multiprocessing import Pool
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    masks = get_all_files(mask_folder)
    input_pool = []
    for iMask in masks:
        image_path = os.path.basename(iMask)
        output_path = os.path.join(output_folder, image_path)
        input_pool.append([iMask, output_path])

    pool = Pool(processes=NUM_PROCS)
    pool.map(process_one, input_pool)
Esempio n. 2
0
 def _get_image_paths(self, path):
     return fileops.get_all_files(path)
if __name__ == "__main__":
    # TODO: Add argparse
    """
    image_test = '/home/adamf/data/Karolinska/images/Nuclei_3.png'
    mq = MakeQuery('karolinska-188312', 'karolinska', 'v3')
    response = mq.get_prediction([image_test])
    mask_response = response['predictions'][0]['classes']
    org_image, _ = load_image(image_test, size=(512, 512))

    def apply_tinge(original, prediction):
        # Load and make the predicted cells a bit more red
        original = np.stack([original.copy()] * 3, 2)
        x_idx, y_idx = np.where(prediction)
        original[x_idx, y_idx, 1] /= 5
        original[x_idx, y_idx, 2] /= 5
        plot_image_from_array(original, show_type='matplotlib')

    # Simple argmax prediction
    apply_tinge(org_image, mask_response)

    # More strict predictions
    mask_probability = np.array(response['predictions'][0]['probabilities'])
    for i_level in [0.25, 0.5, 0.75]:
        apply_tinge(org_image, mask_probability[:, :, 1] > i_level)
    """
    all_images = get_all_files("/home/adamf/data/Karolinska/images/")
    mq = MakeQuery('karolinska-188312', 'karolinska', 'v3')
    n_images = len(all_images)
    for i, image in enumerate(all_images):
        response = mq.get_prediction([image])
        '--output_location',
        help='Path to save the output prediction',
        required=True
    )
    parser.add_argument(
        '--energy_threshold',
        help='Which energy contour to plot',
        default=0,
        type=int
    )
    args = parser.parse_args()

    if not os.path.exists(args.output_location):
        os.makedirs(args.output_location)

    all_images = get_all_files(args.image_folder)
    mask_predictor = get_predictor(args.mask_model)
    watershed_predictor = get_predictor(args.watershed_model)
    n_images = len(all_images)
    for i, image in enumerate(all_images):
        # Get a mask prediction
        safe = image_to_websafe_mask(image)
        mask_response = mask_predictor(safe)
        predicted_mask = np.squeeze(mask_response['classes'])

        # Pipe to watershed prediction
        safe = image_to_websafe_watershed(image, predicted_mask)
        mask_response = watershed_predictor(safe)
        predicted_energy = np.squeeze(mask_response['classes'])

        img = Image.fromarray(apply_tinge(image, predicted_energy, args.energy_threshold))
Esempio n. 5
0
from src.lib.imgops import create_weight_file
from src.lib.fileops import get_all_files

for ifile in get_all_files("/home/adamf/data/Karolinska/labels"):
    create_weight_file(ifile, "/home/adamf/data/Karolinska/weights_10_25")
Esempio n. 6
0
    parser.add_argument(
        '--prediction_model',
        help='Path to the prediction model (folder with .pb file)',
        required=True)
    parser.add_argument('--image_folder',
                        help='Path to folder containing images to segment',
                        required=True)
    parser.add_argument(
        '--mask_folder',
        help='Path to folder containing segment masks (binary)',
        required=True)
    parser.add_argument('--output_location',
                        help='Path to save the output prediction',
                        required=True)
    args = parser.parse_args()

    if not os.path.exists(args.output_location):
        os.makedirs(args.output_location)

    all_images = get_all_files(args.image_folder)
    all_masks = get_all_files(args.mask_folder)
    predict = get_predictor(args.prediction_model)
    n_images = len(all_images)
    for i, (image, mask) in enumerate(zip(all_images, all_masks)):
        safe = image_to_websafe(image, mask)
        response = predict(safe)
        predicted_energy = np.squeeze(response['classes'])
        img = Image.fromarray(apply_tinge(image, predicted_energy))
        save_path = os.path.join(args.output_location, os.path.basename(image))
        img.save(save_path)
Esempio n. 7
0
 def _get_image_paths(self, path):
     """Fetches all files in a given path."""
     return fileops.get_all_files(path)