Ejemplo n.º 1
0
def TestCombinedKmeans(train_data_matrix, train_labels_matrix, test_data,
                       test_labels):
    im_size = [240, 240]

    #Combined K-means
    pred_labels_kmeans = np.empty(
        [train_data_matrix.shape[0], train_data_matrix.shape[2]])
    for i in range(train_data_matrix.shape[2]):
        #normalize data
        train_data, test_data = seg.normalize_data(train_data_matrix[:, :, i],
                                                   test_data)
        #get optimized clusters (using 100 iterations and a learning rate of 0.1)
        _, _, w_final = prj.kmeans_no_plot(train_data,
                                           train_labels_matrix[:, i], 100, 0.1)
        #predict the labels
        temp_pred = prj.predicted_kmeans_test(w_final, test_data)

        #store labels for each training subject in one matrix
        pred_labels_kmeans[:, i] = temp_pred

    #decision fusion based on majority voting
    predicted_labels_kmeans_final = scipy.stats.mode(pred_labels_kmeans,
                                                     axis=1)[0].flatten()

    #calculate the error and dice
    err = util.classification_error(test_labels, predicted_labels_kmeans_final)
    dice = util.dice_multiclass(test_labels, predicted_labels_kmeans_final)
    predicted_mask = predicted_labels_kmeans_final.reshape(
        im_size[0], im_size[1])

    return predicted_mask, err, dice
Ejemplo n.º 2
0
def TestcombinedAtlases(train_labels_matrix, test_labels):
    im_size = [240, 240]
    #predict the test data labels
    predicted_labels, predicted_labels2_atlas = seg.segmentation_combined_atlas(
        train_labels_matrix)
    #calculate error and dice
    dice_atlas = util.dice_multiclass(test_labels, predicted_labels2_atlas)
    err_atlas = util.classification_error(test_labels, predicted_labels2_atlas)
    predicted_mask_atlas = predicted_labels2_atlas.reshape(
        im_size[0], im_size[1])
    return predicted_mask_atlas, err_atlas, dice_atlas,
Ejemplo n.º 3
0
def TestcombinedkNN(train_data_matrix, train_labels_matrix, test_data,
                    test_labels):
    im_size = [240, 240]

    #predict test data labels
    predicted_labels, predicted_labels2_knn = seg.segmentation_combined_knn(
        train_data_matrix, train_labels_matrix, test_data)
    #calculate error and dice
    dice_knn = util.dice_multiclass(test_labels, predicted_labels2_knn)
    err_knn = util.classification_error(test_labels, predicted_labels2_knn)
    predicted_mask_atlas = predicted_labels2_knn.reshape(
        im_size[0], im_size[1])
    return predicted_mask_atlas, err_knn, dice_knn
def segmentation_mymethod(train_data,
                          train_labels,
                          test_data,
                          test_labels,
                          task='brain',
                          method='nearest neighbour',
                          testDice=True):
    # segments the image based on your own method!
    # Input:
    # train_data_matrix   num_pixels x num_features x num_subjects matrix of
    # features
    # train_labels_matrix num_pixels x num_subjects matrix of labels
    # test_data           num_pixels x num_features test data
    # task           String corresponding to the segmentation task: either 'brain' or 'tissue'
    # Output:
    # predicted_labels    Predicted labels for the test slice

    #------------------------------------------------------------------#
    #TODO: Implement your method here
    if method == 'kmeans':
        predicted_labels = seg.kmeans_clustering(test_data, K=4)
    elif method == 'nearest neighbour':
        predicted_labels = seg.nn_classifier(train_data, train_labels,
                                             test_data)
    elif method == 'knn':
        predicted_labels = seg.knn_classifier(train_data,
                                              train_labels,
                                              test_data,
                                              k=4)
    elif method == 'atlas':
        predicted_labels = seg.segmentation_atlas(train_data, train_labels,
                                                  test_data)

    predicted_labels = predicted_labels.astype(bool)
    test_labels = test_labels.astype(bool)

    err = util.classification_error(test_labels, predicted_labels)
    print('Error:\n{}'.format(err))

    if testDice:
        dice = util.dice_multiclass(test_labels, predicted_labels)
        print('Dice coefficient:\n{}'.format(dice))
    #------------------------------------------------------------------#
    return predicted_labels
Ejemplo n.º 5
0
#Combined K-means
pred_labels_kmeans = np.empty(
    [train_data_matrix.shape[0], train_data_matrix.shape[2]])
print(train_data.shape)
for i in range(train_data_matrix.shape[2]):
    train_data, test_data = seg.normalize_data(train_data_matrix[:, :, i],
                                               test_data)

    _, _, w_final = prj.kmeans(train_data, train_labels_matrix[:, i], num_iter,
                               mu)

    temp_pred = prj.predicted_kmeans_test(w_final, test_data)

    print("Possible classes are: {}".format(np.unique(temp_pred)))
    tempdice = util.dice_multiclass(test_labels, temp_pred)
    temperr = util.classification_error(test_labels, temp_pred)

    print('Err {:.4f}, dice {:.4f}'.format(temperr, tempdice))
    pred_labels_kmeans[:, i] = temp_pred

#decision fusion
predicted_labels_kmeans_final = scipy.stats.mode(pred_labels_kmeans,
                                                 axis=1)[0].flatten()

#do a check which labels exist
print("Possible classes are: {}".format(
    np.unique(predicted_labels_kmeans_final)))

#calculate the error and dice
err = util.classification_error(test_labels, predicted_labels_kmeans_final)
Ejemplo n.º 6
0
def segmentation_demo():
    #only SECTION 2 is needed for what we want to do
    train_subject = 1
    test_subject = 2
    train_slice = 1
    test_slice = 1
    task = 'tissue'
    #SECTION 1 (this seciton has nothing to do with SECTION 2)

    #Load data from a train and testsubject
    train_data, train_labels, train_feature_labels = util.create_dataset(
        train_subject, train_slice, task)
    test_data, test_labels, test_feature_labels = util.create_dataset(
        test_subject, test_slice, task)

    util.scatter_data(train_data, train_labels, 0, 6)
    util.scatter_data(test_data, test_labels, 0, 6)

    predicted_labels = seg.segmentation_atlas(None, train_labels, None)

    err = util.classification_error(test_labels, predicted_labels)
    dice = util.dice_overlap(test_labels, predicted_labels)

    #Display results
    true_mask = test_labels.reshape(240, 240)
    predicted_mask = predicted_labels.reshape(240, 240)

    fig = plt.figure(figsize=(8, 8))
    ax1 = fig.add_subplot(111)
    ax1.imshow(true_mask, 'gray')
    ax1.imshow(predicted_mask, 'viridis', alpha=0.5)
    print('Subject {}, slice {}.\nErr {}, dice {}'.format(
        test_subject, test_slice, err, dice))

    ## SECTION 2:Compare methods
    num_images = 5
    num_methods = 3
    im_size = [240, 240]

    all_errors = np.empty([num_images, num_methods])
    all_errors[:] = np.nan
    all_dice = np.empty([num_images, num_methods])
    all_dice[:] = np.nan

    all_subjects = np.arange(5)  #list of all subjects [0, 1, 2, 3, 4]
    train_slice = 2
    task = 'tissue'
    all_data_matrix = np.empty(
        [train_data.shape[0], train_data.shape[1], num_images])
    all_labels_matrix = np.empty([train_labels.size, num_images])

    #Load datasets once
    print('Loading data for ' + str(num_images) + ' subjects...')

    for i in all_subjects:
        sub = i + 1
        train_data, train_labels, train_feature_labels = util.create_dataset(
            sub, train_slice, task)
        all_data_matrix[:, :, i] = train_data
        all_labels_matrix[:, i] = train_labels.flatten()

    print('Finished loading data.\nStarting segmentation...')

    #Go through each subject, taking i-th subject as the test
    for i in all_subjects:
        sub = i + 1
        #Define training subjects as all, except the test subject
        train_subjects = all_subjects.copy()
        train_subjects = np.delete(train_subjects, i)

        train_data_matrix = all_data_matrix[:, :, train_subjects]
        train_labels_matrix = all_labels_matrix[:, train_subjects]
        test_data = all_data_matrix[:, :, i]
        test_labels = all_labels_matrix[:, i]
        test_shape_1 = test_labels.reshape(im_size[0], im_size[1])

        fig = plt.figure(figsize=(15, 5))

        predicted_labels, predicted_labels2 = seg.segmentation_combined_atlas(
            train_labels_matrix)
        all_errors[i, 0] = util.classification_error(test_labels,
                                                     predicted_labels2)
        all_dice[i, 0] = util.dice_multiclass(test_labels, predicted_labels2)
        predicted_mask_1 = predicted_labels2.reshape(im_size[0], im_size[1])
        ax1 = fig.add_subplot(131)
        ax1.imshow(test_shape_1, 'gray')
        ax1.imshow(predicted_mask_1, 'viridis', alpha=0.5)
        text_str = 'Err {:.4f}, dice {:.4f}'.format(all_errors[i, 0],
                                                    all_dice[i, 0])
        ax1.set_xlabel(text_str)
        ax1.set_title('Subject {}: Combined atlas'.format(sub))

        predicted_labels, predicted_labels2 = seg.segmentation_combined_knn(
            train_data_matrix, train_labels_matrix, test_data)
        all_errors[i, 1] = util.classification_error(test_labels,
                                                     predicted_labels2)
        all_dice[i, 1] = util.dice_multiclass(test_labels, predicted_labels2)
        predicted_mask_2 = predicted_labels2.reshape(im_size[0], im_size[1])
        ax2 = fig.add_subplot(132)
        ax2.imshow(test_shape_1, 'gray')
        ax2.imshow(predicted_mask_2, 'viridis', alpha=0.5)
        text_str = 'Err {:.4f}, dice {:.4f}'.format(all_errors[i, 1],
                                                    all_dice[i, 1])
        ax2.set_xlabel(text_str)
        ax2.set_title('Subject {}: Combined k-NN'.format(sub))

        #OUR METHOD
        #predict the labels using our method
        predicted_labels_mymethod = segmentation_mymethod(train_data_matrix,
                                                          train_labels_matrix,
                                                          test_data,
                                                          num_iter=100,
                                                          mu=0.1)

        #determine error and dice (multiclass, since there are more classes)
        all_errors[i,
                   2] = util.classification_error(test_labels,
                                                  predicted_labels_mymethod)
        all_dice[i, 2] = util.dice_multiclass(test_labels,
                                              predicted_labels_mymethod)

        #reshape the predicted labels in order to plot the results
        predicted_mask_3 = predicted_labels_mymethod.reshape(
            im_size[0], im_size[1])

        #plot the predicted image over the real image
        plt.imshow(predicted_mask_3, 'viridis')
        ax3 = fig.add_subplot(133)
        ax3.imshow(test_shape_1, 'gray')
        ax3.imshow(predicted_mask_3, 'viridis', alpha=0.5)
        text_str = 'Err {:.4f}, dice {:.4f}'.format(all_errors[i, 2],
                                                    all_dice[i, 2])
        ax3.set_xlabel(text_str)
        ax3.set_title('Subject {}: My method'.format(sub))

        #save the figure after every loop (3 subimages/plots)
        fig.savefig("Results for test subject {}".format(sub), )
Ejemplo n.º 7
0
#Select data with certain features and normalize it
features = [1,4]
train_data,_ = seg.normalize_data(train_data[:, features])
test_data ,_= seg.normalize_data(test_data[:,features])
all_data_matrix, _ = seg.normalize_data(all_data_matrix[:, features, :])


#predicted_train = seg.kmeans_clustering(train_data, K=4)
if (task == 'tissue'):
    k = 4
else:
    k = 2
    
kmeans_cost, train_predicted, w_final =  prj.kmeans(train_data, train_labels k, mu = 0.1, num_iter = 5)

dice = util.dice_multiclass(train_labels, train_predicted)
error = util.classification_error(train_labels, train_predicted)

print("Dice score is {:.2f}".format(dice))
print("Error is {:.2f}".format(error))

#Use my method
#predicted_labels = prj.segmentation_mymethod(train_data, train_labels, test_data, task='tissue')
pred_labels_kmeans = prj.predicted_kmeans_test(w_final, test_data).T

_, pred_labels_cat = seg.segmentation_combined_atlas(train_labels, combining='mode')
_, pred_labels_cnn = seg.segmentation_combined_knn(all_data_matrix, all_labels_matrix, test_data, k=1)

pred_labels_cat = pred_labels_cat.T
pred_labels_cnn = pred_labels_cnn.T
Ejemplo n.º 8
0
    train_labels_matrix[:, i] = train_labels.flatten()

#select certain data:
train_data_matrix = all_data_matrix[:, :, train_subjects]
train_data_matrix = train_data_matrix[:, features, :]
test_data = test_data[:, features]
train_labels_matrix = train_labels_matrix[:, train_subjects]

#predict test data labels
predicted_labels, predicted_labels2_atlas = seg.segmentation_combined_atlas(
    train_labels_matrix)
predicted_labels, predicted_labels2_knn = seg.segmentation_combined_knn(
    train_data_matrix, train_labels_matrix, test_data)

#calculate error and dice
dice_atlas = util.dice_multiclass(test_labels, predicted_labels2_atlas)
err_atlas = util.classification_error(test_labels, predicted_labels2_atlas)

dice_knn = util.dice_multiclass(test_labels, predicted_labels2_knn)
err_knn = util.classification_error(test_labels, predicted_labels2_knn)

#needed for plotting the 'real' data and the predicted
test_shape = test_labels.reshape(im_size[0], im_size[1])

#Plot for combined atlas
predicted_mask_atlas = predicted_labels2_atlas.reshape(im_size[0], im_size[1])
fig, ax = plt.subplots()
ax.imshow(test_shape, 'gray')

ax.imshow(predicted_mask_atlas, 'viridis', alpha=0.5)
text_str = 'Err {:.4f}, dice {:.4f}'.format(err_atlas, dice_atlas)
def segmentation_demo():

    # Data name specification
    train_subject = 1
    test_subject = 2
    train_slice = 1
    test_slice = 1
    task = 'tissue'

    # Load data
    train_data, train_labels, train_feature_labels = util.create_dataset(
        train_subject, train_slice, task)
    test_data, test_labels, test_feature_labels = util.create_dataset(
        test_subject, test_slice, task)

    # Normalize and feed data through X_pca
    train_norm, _ = seg.normalize_data(train_data)
    Xpca, v, w, fraction_variance, ix = seg.mypca(train_norm)
    relevant_feature = int(np.sum(fraction_variance < 0.95)) + 1
    train_norm_ord = train_norm[:, ix]
    train_norm = train_norm_ord[:, :relevant_feature]

    # find the predicted labels (here: the train_labels)
    predicted_labels = seg.segmentation_atlas(None, train_labels, None)

    # Calculate the error and dice score of these predicted labels in comparison to test labels
    err = util.classification_error(test_labels, predicted_labels)
    dice = util.dice_multiclass(test_labels, predicted_labels)

    # Display results
    true_mask = test_labels.reshape(240, 240)
    predicted_mask = predicted_labels.reshape(240, 240)
    fig = plt.figure(figsize=(8, 8))
    ax1 = fig.add_subplot(111)
    ax1.imshow(true_mask, 'gray')
    ax1.imshow(predicted_mask, 'viridis', alpha=0.5)
    print('Subject {}, slice {}.\nErr {}, dice {}'.format(
        test_subject, test_slice, err, dice))

    # COMPARE METHODS
    num_images = 5
    num_methods = 3
    im_size = [240, 240]

    # make space for error and dice data
    all_errors = np.empty([num_images, num_methods])
    all_errors[:] = np.nan
    all_dice = np.empty([num_images, num_methods])
    all_dice[:] = np.nan

    # data name specification
    all_subjects = np.arange(num_images)
    train_slice = 1
    task = 'tissue'

    # make space for data
    all_data_matrix = np.empty(
        [train_norm.shape[0], train_norm.shape[1], num_images])
    all_labels_matrix = np.empty([train_labels.size, num_images])
    all_data_matrix_kmeans = np.empty(
        [train_norm.shape[0], train_norm.shape[1], num_images])
    all_labels_matrix_kmeans = np.empty([train_labels.size, num_images])

    # Load datasets once
    print('Loading data for ' + str(num_images) + ' subjects...')
    for i in all_subjects:
        sub = i + 1
        train_data, train_labels, train_feature_labels = util.create_dataset(
            sub, train_slice, task)
        train_norm, _ = seg.normalize_data(train_data)
        Xpca, v, w, fraction_variance, ix = seg.mypca(train_norm)
        relevant_labels = int(np.sum(fraction_variance < 0.95)) + 1
        train_norm_ord = train_norm[:, ix]
        train_norm = train_norm_ord[:, :relevant_labels]
        all_data_matrix[:, :, i] = train_norm
        all_labels_matrix[:, i] = train_labels.flatten()

    # Load datasets for kmeans
    print('Loading data for ' + str(num_images) + ' subjects...')
    for i in all_subjects:
        sub = i + 1
        train_data_kmeans, train_labels_kmeans, train_feature_labels_kmeans = create_dataset(
            sub, train_slice, task)
        train_norm_kmeans, _ = seg.normalize_data(train_data_kmeans)
        all_data_matrix_kmeans[:, :, i] = train_norm_kmeans
        all_labels_matrix_kmeans[:, i] = train_labels_kmeans.flatten()

    print('Finished loading data.\nStarting segmentation...')

    # Go through each subject, taking i-th subject as the test
    for i in np.arange(num_images):
        sub = i + 1

        # Define training subjects as all, except the test subject
        train_subjects = all_subjects.copy()
        train_subjects = np.delete(train_subjects, i)

        # Obtain data about the chosen amount of subjects
        train_data_matrix = all_data_matrix[:, :, train_subjects]
        train_labels_matrix = all_labels_matrix[:, train_subjects]
        test_data = all_data_matrix[:, :, i]
        test_labels = all_labels_matrix[:, i]
        test_shape_1 = test_labels.reshape(im_size[0], im_size[1])

        fig = plt.figure(figsize=(15, 5))

        # Get predicted labels from atlas method
        predicted_labels = seg.segmentation_combined_atlas(train_labels_matrix)
        all_errors[i, 0] = util.classification_error(test_labels,
                                                     predicted_labels)
        all_dice[i, 0] = util.dice_multiclass(test_labels, predicted_labels)

        # Plot atlas method
        predicted_mask_1 = predicted_labels.reshape(im_size[0], im_size[1])
        ax1 = fig.add_subplot(151)
        ax1.imshow(test_shape_1, 'gray')
        ax1.imshow(predicted_mask_1, 'viridis', alpha=0.5)
        text_str = 'Err {:.4f}, dice {:.4f}'.format(all_errors[i, 0],
                                                    all_dice[i, 0])
        ax1.set_xlabel(text_str)
        ax1.set_title('Subject {}: Combined atlas'.format(sub))

        # Get predicted labels from kNN method
        predicted_labels = seg.segmentation_combined_knn(train_data_matrix,
                                                         train_labels_matrix,
                                                         test_data,
                                                         k=10)
        all_errors[i, 1] = util.classification_error(test_labels,
                                                     predicted_labels)
        all_dice[i, 1] = util.dice_multiclass(test_labels, predicted_labels)

        # Plot kNN method
        predicted_mask_2 = predicted_labels.reshape(im_size[0], im_size[1])
        ax2 = fig.add_subplot(152)
        ax2.imshow(test_shape_1, 'gray')
        ax2.imshow(predicted_mask_2, 'viridis', alpha=0.5)
        text_str = 'Err {:.4f}, dice {:.4f}'.format(all_errors[i, 1],
                                                    all_dice[i, 1])
        ax2.set_xlabel(text_str)
        ax2.set_title('Subject {}: Combined k-NN'.format(sub))

        # Get predicted labels from my own method
        # all_data_matrix_bnb = np.empty([train_norm.shape[0], train_norm.shape[1], num_images])
        # all_labels_matrix_bnb = np.empty([train_labels.size, num_images])

        # for ii in all_subjects:
        #     sub = i + 1
        #     task = 'brain'
        #     train_data_bnb, train_labels_bnb, train_feature_labels_bnb = util.create_dataset(sub, train_slice, task)
        #     train_norm_bnb, _ = seg.normalize_data(train_data_bnb)
        #     Xpca, v, w, fraction_variance, ix = seg.mypca(train_norm_bnb)
        #     relevant_labels_bnb = int(np.sum(fraction_variance < 0.95)) + 1
        #     train_norm_ord_bnb = train_norm_bnb[:, ix]
        #     train_norm_bnb = train_norm_ord_bnb[:, :relevant_labels_bnb]
        #     all_data_matrix_bnb[:, :, ii] = train_norm_bnb
        #     all_labels_matrix_bnb[:, ii] = train_labels_bnb.flatten()
        #
        # qw, we, er = all_data_matrix.shape
        # for iii in np.arange(qw):
        #     for j in np.arange(er):
        #         if all_labels_matrix_bnb[iii, j] == 0:
        #             for k in np.arange(we):
        #                 all_data_matrix[iii, k, j] = 0

        # train_data_matrix = all_data_matrix[:, :, train_subjects]
        # test_data = all_data_matrix[:, :, i]

        train_data_matrix_kmeans = all_data_matrix_kmeans[:, :, train_subjects]
        train_labels_matrix_kmeans = all_labels_matrix[:, train_subjects]
        test_data_kmeans = all_data_matrix_kmeans[:, :, i]

        predicted_labels = segmentation_mymethod(train_data_matrix_kmeans,
                                                 train_labels_matrix_kmeans,
                                                 test_data_kmeans, task)
        all_errors[i, 2] = util.classification_error(test_labels,
                                                     predicted_labels)
        all_dice[i, 2] = util.dice_multiclass(test_labels, predicted_labels)

        # Plot my own method
        predicted_mask_3 = predicted_labels.reshape(im_size[0], im_size[1])
        ax3 = fig.add_subplot(153)
        ax3.imshow(test_shape_1, 'gray')
        ax3.imshow(predicted_mask_3, 'viridis', alpha=0.5)
        text_str = 'Err {:.4f}, dice {:.4f}'.format(all_errors[i, 2],
                                                    all_dice[i, 2])
        ax3.set_xlabel(text_str)
        ax3.set_title('Subject {}: My method'.format(sub))

        ax4 = fig.add_subplot(154)
        ax4.imshow(predicted_mask_3, 'viridis')
        text_str = 'Err {:.4f}, dice {:.4f}'.format(all_errors[i, 2],
                                                    all_dice[i, 2])
        ax4.set_xlabel(text_str)
        ax4.set_title('Subject {}: My method'.format(sub))

        ax5 = fig.add_subplot(155)
        ax5.imshow(test_shape_1, 'gray')
        text_str = 'Err {:.4f}, dice {:.4f}'.format(all_errors[i, 2],
                                                    all_dice[i, 2])
        ax5.set_xlabel(text_str)
        ax5.set_title('Subject {}: My method'.format(sub))