Esempio n. 1
0
def easy_hard_data_classifier_test():
    train_data_easy, train_label_easy, test_data_easy, test_label_easy = generate_train_test(50, 'easy')
    train_data_hard, train_label_hard, test_data_hard, test_label_hard = generate_train_test(50, 'hard')

    predicted_labels_easy = seg.nn_classifier(train_data_easy, train_label_easy, test_data_easy)
    predicted_labels_hard = seg.nn_classifier(train_data_hard, train_label_hard, test_data_hard)

    err_easy = util.classification_error(test_label_easy, predicted_labels_easy)
    err_hard = util.classification_error(test_label_hard, predicted_labels_hard)

    print("easy: {0}, hard: {1}".format(err_easy, err_hard))
Esempio n. 2
0
def nn_classifier_test_brains(testDice=False):

    # Subject 1, slice 1 is the train data
    X, Y, feature_labels_train = util.create_dataset(1,1,'brain')
    N = 1000
    ix = np.random.randint(len(X), size=N)
    train_data = X[ix,:]
    train_labels = Y[ix,:]
    # Subject 3, slice 1 is the test data
    test_data, test_labels, feature_labels_test  = util.create_dataset(3,1,'brain')

    predicted_labels = seg.nn_classifier(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_overlap(test_labels, predicted_labels)
        print('Dice coefficient:\n{}'.format(dice))
    else:
        I = plt.imread('../data/dataset_brains/3_1_t1.tif')
        GT = plt.imread('../data/dataset_brains/3_1_gt.tif')
        gt_mask = GT>0
        gt_labels = gt_mask.flatten() # labels
        predicted_mask = predicted_labels.reshape(I.shape)
        fig = plt.figure(figsize=(15,5))
        ax1 = fig.add_subplot(131)
        ax1.imshow(I)
        ax2 = fig.add_subplot(132)
        ax2.imshow(predicted_mask)
        ax3  = fig.add_subplot(133)
        ax3.imshow(gt_mask)
Esempio n. 3
0
def easy_hard_data_classifier_test():
    #-------------------------------------------------------------------#
    #TODO: generate and classify (using nn_classifier) 2 pairs of datasets (easy and hard)
    # calculate classification error in each case
    EasytrainX, EasytrainY, EasytestX, EasytestY = generate_train_test(100, 'easy')
    HardtrainX, HardtrainY, HardtestX, HardtestY = generate_train_test(100, 'hard')

    predictedLabels_easy = seg.nn_classifier(EasytrainX, EasytrainY, EasytestX)
    class_error_easy = util.classification_error(EasytrainY, predictedLabels_easy)
    
    predictedLabels_hard = seg.nn_classifier(EasytrainX, EasytrainY, EasytestX)
    class_error_hard = util.classification_error(HardtrainX, HardtrainY, predictedLabels_hard)
    
    print("Easy task's error: {}".format(class_error_easy))
    print("Hard task's error: {}".format(class_error_hard))
    #-------------------------------------------------------------------#
    pass
Esempio n. 4
0
def easy_hard_data_classifier_test():
    #-------------------------------------------------------------------#
    #TODO: generate and classify (using nn_classifier) 2 pairs of datasets (easy and hard)
    # calculate classification error in each case
    trainXeasy, trainYeasy, testXeasy, testYeasy = generate_train_test(
        2, 'easy')
    trainXhard, trainYhard, testXhard, testYhard = generate_train_test(
        2, 'hard')

    predicted_labels_easy = seg.nn_classifier(trainXeasy, trainYeasy,
                                              testXeasy)
    predicted_labels_hard = seg.nn_classifier(trainXhard, trainYhard,
                                              testXhard)

    err_easy = util.classification_error(testYeasy, predicted_labels_easy)
    print(err_easy)
    err_hard = util.classification_error(testYhard, predicted_labels_hard)
    print(err_hard)
Esempio n. 5
0
def nn_classifier_test_samples():
    train_data, train_labels = seg.generate_gaussian_data(20)
    test_data, test_labels = seg.generate_gaussian_data(10)
    predicted_labels = seg.nn_classifier(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('True labels:\n{}'.format(test_labels))
    print('Predicted labels:\n{}'.format(predicted_labels))
    print('Error:\n{}'.format(err))
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