def random_walk_epoch(hdf_file, beta, threshold, random_walk=True):
    if not 'postprocessed' in hdf_file:
        logging.warning('Attempted to random walk for data file which '
                        'does not have postprocessed predictions from '
                        'previous epoch')
    else:
        #reopen data file in read mode
        data_fpath = hdf_file.filename
        hdf_file.close()
        hdf_file = h5py.File(data_fpath, 'r+')

        #get images from base data file
        base_data_file = h5py.File(
            os.path.join(os.path.dirname(hdf_file.filename), 'base_data.hdf5'),
            'r')
        images = np.array(base_data_file['images_train'])
        base_data_file.close()

        #get scribble data as output of previous epoch
        seeds = hdf_file['postprocessed']
        random_walked = hdf_file['random_walked']

        #get checkpoint metadata
        processed = random_walked.attrs.get('processed')
        processed_to = random_walked.attrs.get('processed_to')
        recursion = utils.get_recursion_from_hdf5(hdf_file)
        if not processed:
            #process in batches of 20
            #doesn't really make a time difference
            logging.info("Random walking for recursion {}".format(recursion))
            batch_size = 20
            for scr_idx in range(processed_to, len(seeds), batch_size):
                if random_walk:
                    logging.info(
                        'Random walking range {} to {} of recursion {}'.format(
                            scr_idx, scr_idx + batch_size - 1, recursion))
                    random_walked[scr_idx:scr_idx + batch_size, ...] = segment(
                        images[scr_idx:scr_idx + batch_size, ...],
                        seeds[scr_idx:scr_idx + batch_size, ...],
                        beta=beta,
                        threshold=threshold)
                else:
                    random_walked[scr_idx:scr_idx + batch_size,
                                  ...] = seeds[scr_idx:scr_idx + batch_size,
                                               ...]

                random_walked.attrs.modify('processed_to',
                                           scr_idx + batch_size)

            random_walked.attrs.modify('processed', True)

        #reopen in read mode
        hdf_file.close()
        hdf_file = h5py.File(data_fpath, 'r')
    return hdf_file
Beispiel #2
0
def postprocess_gt(data, images_train, scribbles_train=None):
    '''
    Postprocesses predictions of CNN to create ground truths for recursion
    :param data: Data of this recursion - e.g. if given data file for recursion n,
                 It will set up ground truths to be random walked for recursion n
    :param images_train: Numpy array of training images
    :param scribbles_train: Numpy array of weakly annotated images
    :return: data file with postprocessed ground truths
    '''

    #reopen in write mode
    data_fpath = data.filename
    data.close()
    data = h5py.File(data_fpath)

    #get recursions and previous progress
    recursion = utils.get_recursion_from_hdf5(data)
    predictions = np.array(data['predicted'])
    postprocessed = data['postprocessed']
    done = postprocessed.attrs.get('processed')
    if not done:
        processed_to = data['postprocessed'].attrs.get('processed_to')
        scr_max = len(images_train)

        #LOGGING: Print the postprocessing options
        if exp_config.postprocessing:
            logging.info("Postprocessing options chosen:")
            if exp_config.reinit:
                logging.info(
                    "\treinit = True\n\t\t\t\t\t"
                    "Reinitialising network weights on each recursion")
            if exp_config.keep_largest_cluster:
                logging.info(
                    "\tkeep_largest_blob = True\n\t\t\t\t\t"
                    "Keeping only largest cluster from ground truth prediction"
                )
            if exp_config.cnn_threshold:
                logging.info("\tcnn_threshold = {0}\n\t\t\t\t\t"
                             "Segmentation predictions of CNN with probability"
                             " of less than {0} are left unlabelled".format(
                                 exp_config.cnn_threshold))
            if exp_config.rw_intersection:
                logging.info("\trw_intersection = True\n\t\t\t\t\t"
                             "Random walker segmentation used as "
                             "upper bound")
            if exp_config.rw_reversion:
                logging.info(
                    "\trw_reversion = True\n\t\t\t\t\tIf after post processing the"
                    " network has predicted less than the original ground truth, "
                    "it will revert to the original ground truth to prevent "
                    "propagation of bad predictions")

            if exp_config.smooth_edges:
                logging.info(
                    "\t smooth_edges with sigma = {} and threshold = {}\n\t\t\t\t\t"
                    "Will use a gaussian filter to smooth edges of the segmentation"
                )
        else:
            logging.info("No postprocessing options enabled")

        for scr_idx in range(processed_to, scr_max, exp_config.batch_size):
            # get indices to process
            if scr_idx + exp_config.batch_size > scr_max:
                ind = list(range(scr_max - exp_config.batch_size, scr_max))
            else:
                ind = list(range(scr_idx, scr_idx + exp_config.batch_size))

            logging.info(
                'Postprocessing groundtruth of recursion {0} for images {1} to {2} '
                .format(recursion, ind[0], ind[-1]))

            mask_out = postprocess(np.array(predictions[ind, ...]),
                                   images_train[ind, ...],
                                   scribbles_train[ind, ...])

            #save to dataset
            postprocessed[ind, ...] = np.squeeze(mask_out)
            data['postprocessed'].attrs.modify('processed_to',
                                               scr_idx + exp_config.batch_size)
        data['postprocessed'].attrs.modify('processed', True)
        logging.info(
            'Finished postprocessing ground truths for recursion {}'.format(
                recursion))
    #reopen in read only mode
    data.close()
    data = h5py.File(data_fpath, 'r')
    return data
def load_different_recursion(data, step):
    recursion = utils.get_recursion_from_hdf5(data) + step
    return h5py.File(recursion_filepath(recursion, data_file=data))
Beispiel #4
0
def predict_next_gt(data, images_train, images_placeholder,
                    training_time_placeholder, keep_prob, logits, sess):
    '''
    Uses current network weights to segment images for next recursion
    After postprocessing, these are used as the ground truth for further training
    :param data: Data of the current recursion - if this is of recursion n, this function
                 predicts ground truths for recursion n + 1
    :param images_train: Numpy array of training images
    :param images_placeholder: Tensorflow placeholder for image feed
    :param training_time_placeholder: Boolean tensorflow placeholder
    :param logits: Logits operator for calculating segmentation mask probabilites
    :param sess: Tensorflow session
    :return: The data file for recursion n + 1
    '''
    #get recursion from filename
    recursion = utils.get_recursion_from_hdf5(data)

    new_recursion_fname = acdc_data.recursion_filepath(recursion + 1,
                                                       data_file=data)
    if not os.path.exists(new_recursion_fname):
        fpath = os.path.dirname(data.filename)
        data.close()
        data = acdc_data.create_recursion_dataset(fpath, recursion + 1)
    else:
        data.close()
        data = h5py.File(new_recursion_fname, 'r+')

    #attributes to track processing
    prediction = data['predicted']
    prediction_pre_cor = data['predicted_pre_cor']
    p_vals = data['p_vals']

    processed = data['predicted'].attrs.get('processed')
    if not processed:
        processed_to = data['predicted'].attrs.get('processed_to')
        scr_max = len(images_train)
        print("SCR max = " + str(scr_max))
        for scr_idx in range(processed_to, scr_max, exp_config.batch_size):
            if scr_idx + exp_config.batch_size > scr_max:
                print("Entered last")
                # At the end of the dataset
                # Must ensure feed_dict is 20 images long however
                ind = list(range(scr_max - exp_config.batch_size, scr_max))
            else:

                ind = list(range(scr_idx, scr_idx + exp_config.batch_size))

            x = np.expand_dims(np.array(images_train[ind, ...]), -1)

            feed_dict = {
                images_placeholder: x,
                training_time_placeholder: False,
                keep_prob: 0.5
            }

            temp = np.zeros((50, exp_config.batch_size, 212, 212, 5))

            ps = np.zeros((exp_config.batch_size, 212, 212))
            for k in range(50):

                temp[k, :, :, :, :] = sess.run(logits, feed_dict=feed_dict)

            temp_mean = np.squeeze(np.mean(temp, axis=0))

            conf = 0.05

            mask = tf.arg_max(temp_mean, dimension=-1)
            mask_out = sess.run(mask, feed_dict=feed_dict)

            pre_cor_mask = np.copy(mask_out)

            for t in range(exp_config.batch_size):
                for m in range(212):
                    for n in range(212):

                        estim = mask_out[t, m, n]

                        temp_mean[t, m, n, estim] = -1000000

                        best_ind = np.argmax(
                            np.squeeze(np.copy(temp_mean[t, m, n, :])))


#                        [st,p] = ttest_ind(np.squeeze(temp[:,t,m,n,estim]),np.squeeze(temp[:,t,m,n,best_ind]),equal_var=False)
#                        ps[t,m,n] = p
#                        if p > conf:
#                            mask_out[t,m,n] = 0

#            for k in range(5):
#                locs = np.where(mask_out==k)
#                if (locs[0].size!=0):
#
#                    temp_max_vals = np.copy(max_vals)
#                    temp_max_vals[locs] = -100
#                    temp_arg_max = temp_max_vals
#                    min_vals[locs]
#
#                    cur_vals = temp_mean[locs]
#                    cur_stds = temp_std[locs]
#                    min_vals = cur_vals - conf*cur_stds

#save to dataset
            for indice in range(len(ind)):
                prediction_pre_cor[ind[indice],
                                   ...] = np.squeeze(pre_cor_mask[indice, ...])
                prediction[ind[indice], ...] = np.squeeze(mask_out[indice,
                                                                   ...])
                p_vals[ind[indice], ...] = np.squeeze(ps[indice, ...])

            data['predicted'].attrs.modify('processed_to',
                                           scr_idx + exp_config.batch_size)

        if exp_config.reinit:
            logging.info("Initialising variables")
            sess.run(tf.global_variables_initializer())

        data['predicted'].attrs.modify('processed', True)
        logging.info(
            'Created unprocessed ground truths for recursion {}'.format(
                recursion + 1))

        #Reopen in read only mode
        data.close()
        data = h5py.File(new_recursion_fname, 'r')
    return data
def predict_next_gt(data2,
                    images_train,
                    images_placeholder,
                    training_time_placeholder,
                    keep_prob,
                    logits,
                    
                    sess,
                    random_walked):
    '''
    Uses current network weights to segment images for next recursion
    After postprocessing, these are used as the ground truth for further training
    :param data: Data of the current recursion - if this is of recursion n, this function
                 predicts ground truths for recursion n + 1
    :param images_train: Numpy array of training images
    :param images_placeholder: Tensorflow placeholder for image feed
    :param training_time_placeholder: Boolean tensorflow placeholder
    :param logits: Logits operator for calculating segmentation mask probabilites
    :param sess: Tensorflow session
    :return: The data file for recursion n + 1
    '''
    #get recursion from filename
    recursion = utils.get_recursion_from_hdf5(data2)

    new_recursion_fname = acdc_data.recursion_filepath(recursion + 1, data_file=data2)
    fpath = os.path.dirname(data2.filename)
    data = acdc_data.create_recursion_dataset(fpath, recursion + 1)

    #attributes to track processing
    prediction = data['predicted']
    logits_h5 = data['logits']
    pre_logits_h5 = data['pre_logits']
    predicted_pre_crf = data['predicted_pre_crf']
    processed = data['predicted'].attrs.get('processed')
    ran = data['random_walked']
    
    ran2 = data2['random_walked']
    means_h_2 = data2['means']
    p_vals = data2['p_vals']
    

    if not processed:
        processed_to = data['predicted'].attrs.get('processed_to')
        scr_max = len(images_train)
        print("SCR max = " + str(scr_max))
        for scr_idx in range(processed_to, scr_max, exp_config.batch_size):
            if scr_idx+exp_config.batch_size > scr_max:
                print("Entered last")
                # At the end of the dataset
                # Must ensure feed_dict is 20 images long however
                ind = list(range(scr_max - exp_config.batch_size, scr_max))
            else:
                
                ind = list(range(scr_idx, scr_idx + exp_config.batch_size))
                print(str(ind))
#            logging.info('Saving prediction after recursion {0} for images {1} to {2} '
#                         .format(ind[0], ind[-1]))
            x = np.expand_dims(np.array(images_train[ind, ...]), -1)

            
            temp_mean = np.array(means_h_2[ind, ...])
            logs = tf.nn.softmax(temp_mean)
            
            cur_rand = np.array(ran2[ind, ...])
            for t in range(exp_config.batch_size):
                for m in range(212):
                    for n in range(212):
                        if cur_rand[t,m,n] != 0:
                            logs[t,m,n,:] =0
                            logs[t,m,n,cur_rand[t,m,n]] = 1
                            
                    
            
            temp_maps = np.argmax(temp_mean, axis=-1)
            
            mask_out = apply_crf(logs,x)
            
            conf = 0.05
            temp_p = np.array(p_vals[ind, ...])
            for t in range(exp_config.batch_size):
                for m in range(212):
                    for n in range(212):
          

                        if temp_p[t,m,n] > conf:
                            mask_out[t,m,n] = 0
                            
                            
            for t in range(exp_config.batch_size):
                for m in range(212):
                    for n in range(212):
                        if cur_rand[t,m,n] != 0:
                            mask_out[t,m,n] = cur_rand[t,m,n]
            #save to dataset
            for indice in range(len(ind)):
                
                prediction[ind[indice], ...] = np.squeeze(mask_out[indice, ...])
                predicted_pre_crf[ind[indice], ...] = np.squeeze(temp_maps[indice, ...])
                logits_h5[ind[indice], ...] = np.squeeze(temp_mean[indice, ...])
                ran[ind[indice], ...] = np.squeeze(cur_rand[indice, ...])
             
            data['predicted'].attrs.modify('processed_to', scr_idx + exp_config.batch_size)

        if exp_config.reinit:
            logging.info("Initialising variables")
            sess.run(tf.global_variables_initializer())

        data['predicted'].attrs.modify('processed', True)
        logging.info('Created unprocessed ground truths for recursion {}'.format(recursion + 1))

        #Reopen in read only mode
        data.close()
        data = h5py.File(new_recursion_fname, 'r')
    return data
Beispiel #6
0
def predict_next_gt(data, images_train, images_placeholder,
                    training_time_placeholder, logits, sess):
    '''
    Uses current network weights to segment images for next recursion
    After postprocessing, these are used as the ground truth for further training
    :param data: Data of the current recursion - if this is of recursion n, this function
                 predicts ground truths for recursion n + 1
    :param images_train: Numpy array of training images
    :param images_placeholder: Tensorflow placeholder for image feed
    :param training_time_placeholder: Boolean tensorflow placeholder
    :param logits: Logits operator for calculating segmentation mask probabilites
    :param sess: Tensorflow session
    :return: The data file for recursion n + 1
    '''
    #get recursion from filename
    recursion = utils.get_recursion_from_hdf5(data)

    new_recursion_fname = acdc_data.recursion_filepath(recursion + 1,
                                                       data_file=data)
    if not os.path.exists(new_recursion_fname):
        fpath = os.path.dirname(data.filename)
        data.close()
        data = acdc_data.create_recursion_dataset(fpath, recursion + 1)
    else:
        data.close()
        data = h5py.File(new_recursion_fname, 'r+')

    #attributes to track processing
    prediction = data['predicted']
    processed = data['predicted'].attrs.get('processed')
    if not processed:
        processed_to = data['predicted'].attrs.get('processed_to')
        scr_max = len(images_train)
        print("SCR max = " + str(scr_max))
        for scr_idx in range(processed_to, scr_max, exp_config.batch_size):
            if scr_idx + exp_config.batch_size > scr_max:
                print("Entered last")
                # At the end of the dataset
                # Must ensure feed_dict is 20 images long however
                ind = list(range(scr_max - exp_config.batch_size, scr_max))
            else:

                ind = list(range(scr_idx, scr_idx + exp_config.batch_size))
                print(str(ind))


#            logging.info('Saving prediction after recursion {0} for images {1} to {2} '
#                         .format(ind[0], ind[-1]))
            x = np.expand_dims(np.array(images_train[ind, ...]), -1)

            feed_dict = {
                images_placeholder: x,
                training_time_placeholder: False
            }
            softmax = tf.nn.softmax(logits)

            print("softmax")
            #threshold output of cnn
            if exp_config.cnn_threshold:
                threshold = tf.constant(exp_config.cnn_threshold,
                                        dtype=tf.float32)
                s = tf.multiply(
                    tf.ones(shape=[exp_config.batch_size, 212, 212, 1]),
                    threshold)
                softmax = tf.concat([s, softmax[..., 1:]], axis=-1)
            print("threshold")
            # if exp_config.use_crf:
            #     #get unary from softmax
            #     unary = tf.multiply(-1, tf.log(softmax))
            #
            #calculate mask
            mask = tf.arg_max(softmax, dimension=-1)

            print("before sess")
            mask_out = sess.run(mask, feed_dict=feed_dict)
            print("after sess : " + str(mask_out))
            #save to dataset
            for indice in range(len(ind)):

                prediction[ind[indice], ...] = np.squeeze(mask_out[indice,
                                                                   ...])
                print("added " + str(ind[indice]))
            data['predicted'].attrs.modify('processed_to',
                                           scr_idx + exp_config.batch_size)

        if exp_config.reinit:
            logging.info("Initialising variables")
            sess.run(tf.global_variables_initializer())

        data['predicted'].attrs.modify('processed', True)
        logging.info(
            'Created unprocessed ground truths for recursion {}'.format(
                recursion + 1))

        #Reopen in read only mode
        data.close()
        data = h5py.File(new_recursion_fname, 'r')
    return data
def predict_next_gt(data, images_train, images_placeholder,
                    training_time_placeholder, keep_prob, logits,
                    network_output, rnn_in_placeholder, sess):
    '''
    Uses current network weights to segment images for next recursion
    After postprocessing, these are used as the ground truth for further training
    :param data: Data of the current recursion - if this is of recursion n, this function
                 predicts ground truths for recursion n + 1
    :param images_train: Numpy array of training images
    :param images_placeholder: Tensorflow placeholder for image feed
    :param training_time_placeholder: Boolean tensorflow placeholder
    :param logits: Logits operator for calculating segmentation mask probabilites
    :param sess: Tensorflow session
    :return: The data file for recursion n + 1
    '''
    #get recursion from filename
    recursion = utils.get_recursion_from_hdf5(data)

    new_recursion_fname = acdc_data.recursion_filepath(recursion + 1,
                                                       data_file=data)
    if not os.path.exists(new_recursion_fname):
        fpath = os.path.dirname(data.filename)
        data.close()
        data = acdc_data.create_recursion_dataset(fpath, recursion + 1)
    else:
        data.close()
        data = h5py.File(new_recursion_fname, 'r+')

    #attributes to track processing
    prediction = data['predicted']
    prediction_pre_cor = data['predicted_pre_cor']
    p_vals = data['p_vals']
    processed = data['predicted'].attrs.get('processed')
    if not processed:
        processed_to = data['predicted'].attrs.get('processed_to')
        scr_max = len(images_train)
        print("SCR max = " + str(scr_max))
        for scr_idx in range(processed_to, scr_max):
            ind = np.copy(scr_idx)

            x = np.expand_dims(
                np.expand_dims(np.array(images_train[ind, ...]), -1), 0)

            feed_dict = {
                images_placeholder: x,
                training_time_placeholder: False,
                keep_prob: 0.5
            }

            ps = np.zeros((212, 212))
            temp, temp_mean = average_outputs(sess, network_output,
                                              images_placeholder, x, keep_prob)

            logits_out = np.squeeze(
                sess.run(logits,
                         feed_dict={
                             images_placeholder: x,
                             rnn_in_placeholder: temp_mean
                         }))

            mask = tf.arg_max(logits_out, dimension=-1)
            mask_out = np.squeeze(sess.run(mask, feed_dict=feed_dict))

            pre_cor_mask = np.copy(mask_out)

            #            for m in range(212):
            #                for n in range(212):
            #
            #
            #                    estim = mask_out[m,n]
            #
            #
            #                    temp_mean[0,m,n,estim] = -1000000
            #
            #                    best_ind = np.argmax(np.squeeze(np.copy(temp_mean[0,m,n,:])))
            #                    [st,p] = ttest_ind(np.squeeze(temp[:,0,m,n,estim]),np.squeeze(temp[:,0,m,n,best_ind]),equal_var=False)
            #                    ps[m,n] = p
            #                    if p > conf:
            #                        mask_out[m,n] = 0

            prediction_pre_cor[ind, ...] = np.squeeze(pre_cor_mask)
            prediction[ind, ...] = np.squeeze(mask_out)
            p_vals[ind, ...] = np.squeeze(ps)
            data['predicted'].attrs.modify('processed_to',
                                           scr_idx + exp_config.batch_size)

        data['predicted'].attrs.modify('processed', True)
        logging.info(
            'Created unprocessed ground truths for recursion {}'.format(
                recursion + 1))

        #Reopen in read only mode
        data.close()
        data = h5py.File(new_recursion_fname, 'r')
    return data
def predict_next_gt(data, images_train, images_placeholder,
                    training_time_placeholder, keep_prob, logits,
                    network_output, rnn_in_placeholder, sess):
    '''
    Uses current network weights to segment images for next recursion
    After postprocessing, these are used as the ground truth for further training
    :param data: Data of the current recursion - if this is of recursion n, this function
                 predicts ground truths for recursion n + 1
    :param images_train: Numpy array of training images
    :param images_placeholder: Tensorflow placeholder for image feed
    :param training_time_placeholder: Boolean tensorflow placeholder
    :param logits: Logits operator for calculating segmentation mask probabilites
    :param sess: Tensorflow session
    :return: The data file for recursion n + 1
    '''
    #get recursion from filename
    recursion = utils.get_recursion_from_hdf5(data)

    new_recursion_fname = acdc_data.recursion_filepath(recursion + 1,
                                                       data_file=data)
    if not os.path.exists(new_recursion_fname):
        fpath = os.path.dirname(data.filename)
        data.close()
        data = acdc_data.create_recursion_dataset(fpath, recursion + 1)
    else:
        data.close()
        data = h5py.File(new_recursion_fname, 'r+')

    #attributes to track processing
    prediction = data['predicted']
    soft_o = data['predicted_pre_crf']
    prediction_pre_cor = data['network_output']

    processed = data['predicted'].attrs.get('processed')
    if not processed:
        processed_to = data['predicted'].attrs.get('processed_to')
        scr_max = len(images_train)
        print("SCR max = " + str(scr_max))
        for scr_idx in range(processed_to, scr_max):
            ind = np.copy(scr_idx)

            x = np.expand_dims(
                np.expand_dims(np.array(images_train[ind, ...]), -1), 0)

            feed_dict = {
                images_placeholder: x,
                training_time_placeholder: False
            }

            temp_mean = sess.run(network_output, feed_dict={images_pl: x})
            temp_soft = sess.run(tf.nn.softmax(temp_mean))
            logits_out = np.squeeze(
                sess.run(logits,
                         feed_dict={
                             images_placeholder: x,
                             rnn_in_placeholder: temp_mean
                         }))

            mask = tf.arg_max(logits_out, dimension=-1)
            mask_out = np.squeeze(sess.run(mask, feed_dict=feed_dict))

            prediction_pre_cor[ind, ...] = np.squeeze(temp_mean)
            prediction[ind, ...] = np.squeeze(mask_out)
            soft_o[ind, ...] = np.squeeze(temp_soft)
            data['predicted'].attrs.modify('processed_to',
                                           scr_idx + exp_config.batch_size)

        data['predicted'].attrs.modify('processed', True)
        logging.info(
            'Created unprocessed ground truths for recursion {}'.format(
                recursion + 1))

        #Reopen in read only mode
        data.close()
        data = h5py.File(new_recursion_fname, 'r')
    return data