def load_classifier(neighbours, blur_scale, c=None, gamma=None, verbose=0): classifier_file = 'classifier_%s_%s.dat' \ % (blur_scale, neighbours) classifier_path = DATA_FOLDER + classifier_file if exists(classifier_file): if verbose: print 'Loading classifier...' classifier = Classifier(filename=classifier_path, \ neighbours=neighbours, verbose=verbose) elif c != None and gamma != None: if verbose: print 'Training new classifier...' classifier = Classifier(c=c, gamma=gamma, neighbours=neighbours, \ verbose=verbose) learning_set = load_learning_set(neighbours, blur_scale, \ verbose=verbose) classifier.train(learning_set) classifier.save(classifier_path) else: raise Exception('No soft margin and gamma specified.') return classifier
if len(argv) < 3: print 'Usage: python %s NEIGHBOURS BLUR_SCALE' % argv[0] exit(1) neighbours = int(argv[1]) blur_scale = float(argv[2]) suffix = '_%s_%s' % (blur_scale, neighbours) if not os.path.exists(RESULTS_FOLDER): os.mkdir(RESULTS_FOLDER) classifier_file = DATA_FOLDER + 'classifier%s.dat' % suffix results_file = '%sresult%s.txt' % (RESULTS_FOLDER, suffix) # Load learning set and test set learning_set = load_learning_set(neighbours, blur_scale, verbose=1) test_set = load_test_set(neighbours, blur_scale, verbose=1) # Perform a grid-search to find the optimal values for C and gamma C = [float(2**p) for p in xrange(-5, 16, 2)] Y = [float(2**p) for p in xrange(-15, 4, 2)] results = [] best = (0, ) i = 0 for c in C: for y in Y: classifier = Classifier(c=c, gamma=y, neighbours=neighbours, verbose=1) classifier.train(learning_set) result = classifier.test(test_set)
if len(argv) < 3: print 'Usage: python %s NEIGHBOURS BLUR_SCALE' % argv[0] exit(1) neighbours = int(argv[1]) blur_scale = float(argv[2]) suffix = '_%s_%s' % (blur_scale, neighbours) if not os.path.exists(RESULTS_FOLDER): os.mkdir(RESULTS_FOLDER) classifier_file = DATA_FOLDER + 'classifier%s.dat' % suffix results_file = '%sresult%s.txt' % (RESULTS_FOLDER, suffix) # Load learning set and test set learning_set = load_learning_set(neighbours, blur_scale, verbose=1) test_set = load_test_set(neighbours, blur_scale, verbose=1) # Perform a grid-search to find the optimal values for C and gamma C = [float(2 ** p) for p in xrange(-5, 16, 2)] Y = [float(2 ** p) for p in xrange(-15, 4, 2)] results = [] best = (0,) i = 0 for c in C: for y in Y: classifier = Classifier(c=c, gamma=y, neighbours=neighbours, verbose=1) classifier.train(learning_set) result = classifier.test(test_set)