예제 #1
0
def next_patch_balanced(big_img, big_lbl, patch_shape, index, indices, full_indices):
    finished = False

    img = get_patch_from_3d_data(big_img,patch_shape,full_indices[indices[index]])
    lbl = get_patch_from_3d_data(big_lbl,patch_shape,full_indices[indices[index]])
    
    if index==(len(indices)-1):
        finished = True
    else:
        index+=1

    patch_tupla = random_transform_couple((img,lbl))

    return patch_tupla, index, finished
예제 #2
0
def get_chosen_indices(lbl, patch_shape, repetitions, proportion_background):
    '''Return the corner indices for each of the selected patches for each image'''

    full_indices = compute_patch_indices(lbl.shape, patch_shape)

    index_distribution = {'background': [], 'target': []}

    index_num = 0

    for index in full_indices:

        patch_lbl = get_patch_from_3d_data(lbl, patch_shape, index)

        if has_labels(patch_lbl):
            index_distribution['target'].append(index_num)
        else:
            index_distribution['background'].append(index_num)

        index_num += 1

    background_indices = list(
        np.random.choice(list(index_distribution['background']),
                         size=repetitions * int(
                             np.round(proportion_background *
                                      len(index_distribution['target']))),
                         replace=True))

    indices = background_indices
    for _ in range(repetitions):
        indices = indices + index_distribution['target']

    np.random.shuffle(indices)

    return indices, full_indices
예제 #3
0
def next_patch(img, lbl, patch_shape, indices, index, expand):
    '''
    Given an image, return the following patch that has not been processed yet.

    Params:
    img: image in numpy array
    patch_shape: desired shape of the patch
    indices: localizations of the different patches we are going to get. These
             are calculated with function compute_patch_indices.
    index: which index on indices are we using in this iteration.
    expand: whether to expand dimension of image True/False

    Returns: 
    img: actual patch of the image, numpy array
    indices: vector of indices
    index: next index to be processed
    finished: True/False whether if we have finished with the current image patches or not
    '''

    if not indices is None:
        img = get_patch_from_3d_data(data=img,
                                     patch_shape=patch_shape,
                                     patch_index=indices[index])
        lbl = get_patch_from_3d_data(data=lbl,
                                     patch_shape=patch_shape,
                                     patch_index=indices[index])
        index += 1
    else:
        indices = compute_patch_indices(image_shape=img.shape,
                                        patch_size=patch_shape)
        img = get_patch_from_3d_data(data=img,
                                     patch_shape=patch_shape,
                                     patch_index=indices[0])
        lbl = get_patch_from_3d_data(data=lbl,
                                     patch_shape=patch_shape,
                                     patch_index=indices[0])
        index = 1

    if index == len(indices):
        finished = True
    else:
        finished = False

    if expand:
        img = np.expand_dims(img, axis=0)

    return img, lbl, indices, index, finished
예제 #4
0
def next_patch_balanced(big_img, big_lbl, patch_shape, index, indices,
                        full_indices):
    '''Given the input image and its label, return the next patch that the input pipeline has to return'''
    finished = False

    img = get_patch_from_3d_data(big_img, patch_shape,
                                 full_indices[indices[index]])
    lbl = get_patch_from_3d_data(big_lbl, patch_shape,
                                 full_indices[indices[index]])

    if index == (len(indices) - 1):
        finished = True
    else:
        index += 1

    patch_tupla = random_transform_couple((img, lbl))

    return patch_tupla, index, finished
예제 #5
0
def image_to_patches(img, patch_size):
    '''
    img: image as numpy array
    patch_shape: tuple of patch shape (for example: (126,126,45))

    returns: list of images (patches) created of the chosen size
    '''

    indices = compute_patch_indices(img.shape, patch_size)

    images = []

    for index in indices:
        images.append(get_patch_from_3d_data(img, patch_size, index))

    return images
예제 #6
0
def patch_wise_prediction(output, data, patch_shape,
                          overlap=0, batch_size=1):

    """
    :param batch_size:
    :param model:
    :param data:
    :param overlap:
    :return:
    """
    predictions = []
    indices = compute_patch_indices(data.shape[-3:],
                                    patch_size=patch_shape,
                                    overlap=overlap,
                                    start=0)
    batch = []
    i = 0
    pbar = tqdm(enumerate(indices),
                unit='patches',
                total=len(indices),
                desc='scan_patches')

    sess = tf.get_default_session()

    for j, _ in pbar:
        while len(batch) < batch_size:
            patch = get_patch_from_3d_data(data[0],
                                           patch_shape=patch_shape,
                                           patch_index=indices[i])
            batch.append(patch)
            i += 1
        prediction = sess.run(output,
                              feed_dict={'img:0': np.asarray(batch)})
        batch = []
        for predicted_patch in prediction:
            predictions.append(predicted_patch)
    output_shape = [int(output.shape[1])] + list(data.shape[-3:])
    return reconstruct_from_patches(predictions,
                                    patch_indices=indices,
                                    data_shape=output_shape)