Esempio n. 1
0
    def __init__(self, X_train = None, Y_train = None , bins=256,  show_train_acc=False, show_images=False, file = None):
        if X_train != None :
            print 'computing a priory log likelihoods...'
            self.a_priory = np.sum(Y_train, axis = 0)/ Y_train.shape[0]

            self.bins = bins
            self.cat_num = Y_train.shape[1]

            self.ccls_pos, self.ccls_neg = compute_ccls_per_category(self.bins, X_train, Y_train, show_images)
            
            if show_train_acc :
                print 'Train:'
                prob_pos, prob_neg = compute_probabilities(self.bins, X_train, self.cat_num, self.ccls_pos, self.ccls_neg, self.a_priory)
                train_output = prob_pos > prob_neg
                cnn.print_error_rate_per_category(train_output, Y_train, thr = 0.5)
                print
        elif file != None :
            with open(file, 'r') as f :
                other = pickle.load(f)
                self.a_priory = other.a_priory
                self.bins = other.bins
                self.cat_num = other.cat_num
                self.ccls_pos = other.ccls_pos
                self.ccls_neg = other.ccls_neg
        else:
            print "Tried to init an fih without train data or other fih or a pickle file specified"
Esempio n. 2
0
 def predict(self, X_test, show_accuracy = False, Y_test = None) :
     output = np.zeros((X_test.shape[0], self.cat_num))
     prob_pos, prob_neg = compute_probabilities(self.bins, X_test, self.cat_num, self.ccls_pos, self.ccls_neg, self.a_priory)
     output = np.exp(prob_pos) / (np.exp(prob_pos) + np.exp(prob_neg))
     
     output[np.isnan(output)] = 0 #This has been fixed by taking low probabilities instead of 0 probabilities, but this works better.
     
     if show_accuracy :
         print 'Test:'
         if Y_test != None :
             cnn.print_error_rate_per_category(output, Y_test, thr = 0.5)
         else :
             print 'Error: Could not print accuracy: Y_test not specified'
     return output
Esempio n. 3
0
def runNet_cat(imageType, saveDir):
    name = saveDir + 'stressVSrelacs' + '_' + imageType + '.cnn'

    temp = sys.stdout

    with open(saveDir + 'stressVSrelacs' + '_' + imageType + '.txt', 'w') as sys.stdout:
        X_train, Y_train_all, X_test, Y_test_all = cnn.load_normalized_data(imageType)

        Y_train = cnn.get_categorical_data_stress(Y_train_all)
        Y_test  = cnn.get_categorical_data_stress(Y_test_all)

        model = cnn.build_empty_softmax_model(X_train.shape, Y_train.shape)

        model.fit(X_train, Y_train, 
            batch_size = 32, nb_epoch = 50,
            validation_data= (X_test, Y_test))

        cnn.save_weights(model, weights_filename = name)
        
        output = model.predict(X_test)
        cnn.print_error_rate_per_category(output, Y_test)




    name = saveDir + 'categories' + '_' + imageType + '.cnn'
    with open(saveDir + 'categories' + '_' + imageType + '.txt', 'w') as sys.stdout:
        X_train, Y_train_all, X_test, Y_test_all = cnn.load_normalized_data(imageType)

        cat_lists = [[6],[5],[11],[10],[4],[7],[9],[8]]
        Y_train = cnn.get_categorical_data_cats(Y_train_all, cat_lists)
        Y_test  = cnn.get_categorical_data_cats(Y_test_all, cat_lists)

        model = cnn.build_empty_softmax_model(X_train.shape, Y_train.shape)

        model.fit(X_train, Y_train, 
            batch_size = 32, nb_epoch = 50,
            validation_data= (X_test, Y_test))

        cnn.save_weights(model, weights_filename = name)
        
        output = model.predict(X_test)
        cnn.print_error_rate_per_category(output, Y_test)

    sys.stdout = temp
Esempio n. 4
0
    def predict(self, X_test, show_accuracy=False, Y_test=None):
        output = np.zeros((X_test.shape[0], self.cat_num))
        prob_pos, prob_neg = compute_probabilities(self.bins, X_test,
                                                   self.cat_num, self.ccls_pos,
                                                   self.ccls_neg,
                                                   self.a_priory)
        output = np.exp(prob_pos) / (np.exp(prob_pos) + np.exp(prob_neg))

        output[np.isnan(
            output
        )] = 0  #This has been fixed by taking low probabilities instead of 0 probabilities, but this works better.

        if show_accuracy:
            print 'Test:'
            if Y_test != None:
                cnn.print_error_rate_per_category(output, Y_test, thr=0.5)
            else:
                print 'Error: Could not print accuracy: Y_test not specified'
        return output
Esempio n. 5
0
def runNet(imageType, cats, saveDir):
    # Make the filename
    catName = str(cats)
    name = saveDir + catName + '_' + imageType + '.cnn'
   
    temp = sys.stdout
    with open(saveDir + catName + '_' + imageType + '.txt', 'w') as sys.stdout:
        X_train, Y_train_all, X_test, Y_test_all = cnn.load_normalized_data(imageType)

        Y_train = Y_train_all[:, cats]
        Y_test = Y_test_all[:, cats]

        model = cnn.build(X_train, Y_train, X_test, Y_test, epochs = 50)

        cnn.save_weights(model, weights_filename = name)

        output = model.predict(X_test)
        cnn.print_error_rate_per_category(output, Y_test)
    sys.stdout = temp
Esempio n. 6
0
def mlp(X_train, Y_train, X_test, Y_test) :
    X_train = np.sum(X_train, axis = (1,3))
    X_train = X_train/128
    X_train -= 65
    X_train /= 256

    X_test = np.sum(X_test, axis = (1,3))
    X_test = X_test/128
    X_test -= 65
    X_test /= 256

    print 'Building model...'
    model = build_empty_model(X_train.shape, Y_train.shape)
    
    model.fit(X_train, Y_train, batch_size = 32, nb_epoch = 200, validation_data= (X_test, Y_test))

    print 'predicting output'
    output = model.predict(X_test)

    cnn.print_error_rate_per_category(output, Y_test)
Esempio n. 7
0
def bayes(X_train, Y_train, X_test, Y_test, pyramid_height = 1, max_bins = 256, show_train_acc = True) :
    print 'computing a priory log likelihoods...'
    a_priory = np.sum(Y_train, axis = 0)/ Y_train.shape[0]


    output = np.zeros((pyramid_height , Y_test.shape[0], Y_test.shape[1]))

    for n in range(pyramid_height ) :
        bins = max_bins/ 2**n
        print 'binsize: ' + str(bins)
        
        ccls_pos, ccls_neg = compute_ccls_per_category(bins, X_train, Y_train)
        
        print 'Test:'
        prob_pos, prob_neg = compute_probabilities(bins, X_test, Y_test, ccls_pos, ccls_neg, a_priory)
        output[n] = prob_pos > prob_neg
        cnn.print_error_rate_per_category(output[n], Y_test, thr = 0.5)

        if show_train_acc :
            print 'Train:'
            prob_pos, prob_neg = compute_probabilities(bins, X_train, Y_train, ccls_pos, ccls_neg, a_priory)
            train_output = prob_pos > prob_neg
            cnn.print_error_rate_per_category(train_output, Y_train, thr = 0.5)
            print

    if pyramid_height != 1 :
        print 'Performance with all bin sizes together:'
        compressed_output = np.sum(output, axis = 0)
        compressed_output = compressed_output/pyramid_height 
        cnn.print_error_rate_per_category(compressed_output, Y_test, thr = 0.5)
Esempio n. 8
0
    def __init__(self,
                 X_train=None,
                 Y_train=None,
                 bins=256,
                 show_train_acc=False,
                 show_images=False,
                 file=None):
        if X_train != None:
            print 'computing a priory log likelihoods...'
            self.a_priory = np.sum(Y_train, axis=0) / Y_train.shape[0]

            self.bins = bins
            self.cat_num = Y_train.shape[1]

            self.ccls_pos, self.ccls_neg = compute_ccls_per_category(
                self.bins, X_train, Y_train, show_images)

            if show_train_acc:
                print 'Train:'
                prob_pos, prob_neg = compute_probabilities(
                    self.bins, X_train, self.cat_num, self.ccls_pos,
                    self.ccls_neg, self.a_priory)
                train_output = prob_pos > prob_neg
                cnn.print_error_rate_per_category(train_output,
                                                  Y_train,
                                                  thr=0.5)
                print
        elif file != None:
            with open(file, 'r') as f:
                other = pickle.load(f)
                self.a_priory = other.a_priory
                self.bins = other.bins
                self.cat_num = other.cat_num
                self.ccls_pos = other.ccls_pos
                self.ccls_neg = other.ccls_neg
        else:
            print "Tried to init an fih without train data or other fih or a pickle file specified"
Esempio n. 9
0
def mlp(X_train, Y_train, X_test, Y_test):
    X_train = np.sum(X_train, axis=(1, 3))
    X_train = X_train / 128
    X_train -= 65
    X_train /= 256

    X_test = np.sum(X_test, axis=(1, 3))
    X_test = X_test / 128
    X_test -= 65
    X_test /= 256

    print 'Building model...'
    model = build_empty_model(X_train.shape, Y_train.shape)

    model.fit(X_train,
              Y_train,
              batch_size=32,
              nb_epoch=200,
              validation_data=(X_test, Y_test))

    print 'predicting output'
    output = model.predict(X_test)

    cnn.print_error_rate_per_category(output, Y_test)
Esempio n. 10
0
def bayes(X_train,
          Y_train,
          X_test,
          Y_test,
          pyramid_height=1,
          max_bins=256,
          show_train_acc=True,
          show_images=False):
    print 'computing a priory log likelihoods...'
    a_priory = np.sum(Y_train, axis=0) / Y_train.shape[0]

    output = np.zeros((pyramid_height, Y_test.shape[0], Y_test.shape[1]))

    for n in range(pyramid_height):
        bins = max_bins / 2**n
        print 'binsize: ' + str(bins)

        ccls_pos, ccls_neg = compute_ccls_per_category(bins, X_train, Y_train,
                                                       show_images)

        print 'Test:'
        prob_pos, prob_neg = compute_probabilities(bins, X_test,
                                                   Y_test.shape[1], ccls_pos,
                                                   ccls_neg, a_priory)
        output[n] = prob_pos > prob_neg
        cnn.print_error_rate_per_category(output[n], Y_test, thr=0.5)

        if show_train_acc:
            print 'Train:'
            prob_pos, prob_neg = compute_probabilities(bins, X_train,
                                                       Y_train.shape[1],
                                                       ccls_pos, ccls_neg,
                                                       a_priory)
            train_output = prob_pos > prob_neg
            cnn.print_error_rate_per_category(train_output, Y_train, thr=0.5)
            print

    if pyramid_height != 1:
        print 'Performance with all bin sizes together:'
        compressed_output = np.sum(output, axis=0)
        compressed_output = compressed_output / pyramid_height
        cnn.print_error_rate_per_category(compressed_output, Y_test, thr=0.5)