예제 #1
0
def svm_train_test(input_arg):

    label_weights = None
    best = input_arg[1]
    testset = input_arg[0]
    output_probabilities = True  # Or False!
    try:
        # Create SVMClassifier with hyper-parameters
        svm_best = SVMClassifier(shrinking=True,
                                 kernel=best[0],
                                 degree=best[1],
                                 gamma=best[2],
                                 coef0=best[3],
                                 C=best[4],
                                 label_weights=label_weights,
                                 output_probabilities=output_probabilities)
    except Exception as inst:
        print "Error while instantiating SVMClassifier (required hyper-parameters are probably missing)"
        print inst
        sys.exit()

    finaltrainset = datasets['finaltrainset']
    svm_best.train(finaltrainset)
    outputs, costs = svm_best.test(testset)
    return (outputs, costs)
예제 #2
0
def find_best_model(hyperparams_grid,datasets):
    
    best_val_error = np.inf
    validset = datasets['validset']
    trainset = datasets['trainset']
    output_probabilities = True 
    label_weights = None
    
    
    for params in hyperparams_grid:
        try:
            # Create SVMClassifier with hyper-parameters
            svm = SVMClassifier(shrinking=True, kernel=params[0],degree=params[1],gamma=params[2],coef0=params[3],C=params[4],label_weights=label_weights, output_probabilities=output_probabilities)
        except Exception as inst:
            print "Error while instantiating SVMClassifier (required hyper-parameters are probably missing)"
            print inst
            sys.exit()

        svm.train(trainset)
        outputs, costs = svm.test(validset)
    
        errors = compute_error_mean_and_sterror(costs)
        error = errors[0]
    
        if error < best_val_error:
           best_val_error = error
           best_hyperparams = params
    return best_hyperparams   
예제 #3
0
def svm_model(dataset_directory, dataset_name, params, datasets):
    start_time = time.clock()
    use_weights = False
    output_probabilities = True
    label_weights = None
    if use_weights:
        label_weights = finaltrainset.metadata['label_weights']
    else:
        label_weights = None

    output_probabilities = True  # Or False!
    try:
        # Create SVMClassifier with hyper-parameters
        svm = SVMClassifier(shrinking=True,
                            kernel=params[0],
                            degree=params[1],
                            gamma=params[2],
                            coef0=params[3],
                            C=params[4],
                            label_weights=label_weights,
                            output_probabilities=output_probabilities)
    except Exception as inst:
        print "Error while instantiating SVMClassifier (required hyper-parameters are probably missing)"
        print inst
        sys.exit()
    #finaltrainset = datasets['finaltrainset']
    #svm.train(finaltrainset)
    #testset = datasets['testset']
    #outputs, costs = svm.test(testset)
    dice_mean = train_and_test(svm, datasets)
    end_time = time.clock()
    processing_time = end_time - start_time
    return [dice_mean, processing_time]
예제 #4
0
def svm_train_test(input_arg):


    label_weights = None
    best = input_arg[1]
    testset = input_arg[0]    
    output_probabilities = True # Or False!
    try:
        # Create SVMClassifier with hyper-parameters
        svm_best = SVMClassifier(shrinking=True, kernel=best[0],degree=best[1],gamma=best[2],coef0=best[3],C=best[4],label_weights=label_weights, output_probabilities=output_probabilities)
    except Exception as inst:
        print "Error while instantiating SVMClassifier (required hyper-parameters are probably missing)"
        print inst
        sys.exit()
   

    finaltrainset = datasets['finaltrainset']
    svm_best.train(finaltrainset)
    outputs, costs = svm_best.test(testset)
    return (outputs,costs)
예제 #5
0
    for gamma in gammas:
        for coef0 in coef0s:
            hyperparams_grid.append(['sigmoid', 3, gamma, coef0, C])

if use_weights:
    label_weights = finaltrainset.metadata['label_weights']
else:
    label_weights = None
    
output_probabilities = True # Or False!

print "Pretraining..."
for params in hyperparams_grid:
    try:
        # Create SVMClassifier with hyper-parameters
        svm = SVMClassifier(shrinking=True, kernel=params[0],degree=params[1],gamma=params[2],coef0=params[3],C=params[4],label_weights=label_weights, output_probabilities=output_probabilities)
    except Exception as inst:
        print "Error while instantiating SVMClassifier (required hyper-parameters are probably missing)"
        print inst
        sys.exit()

    svm.train(trainset)
    outputs, costs = svm.test(validset)
    
    errors = compute_error_mean_and_sterror(costs)
    error = errors[0]
    
    if error < best_val_error:
        best_val_error = error
        best_hyperparams = params
예제 #6
0
    """
if use_weights:
    label_weights = finaltrainset.metadata['label_weights']
else:
    label_weights = None

output_probabilities = True  # Or False!

print "Pretraining..."
for params in hyperparams_grid:
    try:
        # Create SVMClassifier with hyper-parameters
        svm = SVMClassifier(shrinking=True,
                            kernel=params[0],
                            degree=params[1],
                            gamma=params[2],
                            coef0=params[3],
                            C=params[4],
                            label_weights=label_weights,
                            output_probabilities=output_probabilities)
    except Exception as inst:
        print "Error while instantiating SVMClassifier (required hyper-parameters are probably missing)"
        print inst
        sys.exit()

    svm.train(trainset)
    outputs, costs = svm.test(validset)

    errors = compute_error_mean_and_sterror(costs)
    error = errors[0]

    if error < best_val_error:
예제 #7
0
def svm_model(datasets):
    print "Setting hyperparameters gridsearch..."
    best_hyperparams = None
    best_val_error = np.inf

    finaltrainset = datasets['finaltrainset']
    trainset = datasets['trainset']
    validset = datasets['validset']
    testset = datasets['testset']
    lbl = datasets['ground_truth']

    output_probabilities = True # Or False!
    kernels = ['rbf','sigmoid']
    #degrees = [1,2,3,4,5,7,10,15]
    gammas = [0.01,0.1,1,5,10,50,100,200,500,1000]
    #coef0s = [-10,-1,-0.1,-0.01,0,0.001,0.01,0.1,1,2,5,10,20]
    Cs = [1,5,10,25,50,75,100,200,500,1000,1500]

    hyperparams_grid = []
    # Rbf kernel parameters
    start_time = time.clock()
    for gamma in gammas:
        for C in Cs:
            hyperparams_grid.append(['rbf', 3, gamma, 0, C])
    
    use_weights = None               
    if use_weights:
        label_weights = finaltrainset.metadata['label_weights']
    else:
        label_weights = None
        
    output_probabilities = False # Or False!

    print "Pretraining..."
    for params in hyperparams_grid:
        try:
            # Create SVMClassifier with hyper-parameters
            svm = SVMClassifier(shrinking=True, kernel=params[0],degree=params[1],gamma=params[2],coef0=params[3],C=params[4],label_weights=label_weights, output_probabilities=output_probabilities)
        except Exception as inst:
            print "Error while instantiating SVMClassifier (required hyper-parameters are probably missing)"
            print inst
            sys.exit()
        svm.train(trainset)
        outputs, costs = svm.test(validset)
        
        errors = compute_error_mean_and_sterror(costs)
        error = errors[0]
        
        if error < best_val_error:
            best_val_error = error
            best_hyperparams = params

    print
    print 'Classification error on valid set : ' + str(best_val_error)
    print

    print "Training..."
    # Train SVM with best hyperparams on train + validset
    #gamma = 5.0
    #C = 1
    #best_hyperparams = ['rbf',3,gamma,0,C]
    best_svm = SVMClassifier(shrinking=True, kernel=best_hyperparams[0],degree=best_hyperparams[1],gamma=best_hyperparams[2],coef0=best_hyperparams[3],C=best_hyperparams[4],label_weights=label_weights, output_probabilities=output_probabilities)
    best_svm.train(finaltrainset)

    print 'Testing...'
    outputs, costs = best_svm.test(testset)
    end_time = time.clock()
    processing_time = end_time - start_time
    
    errors = compute_error_mean_and_sterror(costs)
    error = errors[0]

    print
    print 'Classification error on test set : ' + str(error)
    print "****************************************"


    # Evaluation (compute_statistics.py)
    id_to_class = {}
    for label, id in testset.class_to_id.iteritems():
        id_to_class[id] = label
        

    auto_lbl = np.array([int(id_to_class[output[0]]) for output in outputs]) # Predicted labels

    len_bg = testset.metadata['len_bg']
    lbl = np.append(lbl, [0]*len_bg)
    auto_lbl = np.append(auto_lbl, [0]*len_bg)
    (dice, jaccard, precision, recall) = compute_statistics.compute_eval_multilabel_metrics(auto_lbl, lbl)
    dice = dice[~np.isnan(dice)]
    return [dice.mean(), processing_time]