Ejemplo n.º 1
0
def main(model):
    qa = None
    if model == 'lstm':
        import lstm
        qa = lstm.LSTM()
    else:
        import svm
        qa = svm.SVM()
    if qa == None:
        qa = svm.SVM()
    qa.train_save()
Ejemplo n.º 2
0
    def __init__(self, fakeways=[], arp_for_unknowns=False, wide=False):
        # These are "fake gateways" -- we'll answer ARPs for them with MAC
        # of the switch they're connected to.
        self.fakeways = set(fakeways)

        self.startTime = time.time()
        self.svm = svm.SVM()

        # If True, we create "wide" matches.  Otherwise, we create "narrow"
        # (exact) matches.
        self.wide = wide

        # If this is true and we see a packet for an unknown
        # host, we'll ARP for it.
        self.arp_for_unknowns = arp_for_unknowns

        # (dpid,IP) -> expire_time
        # We use this to keep from spamming ARPs
        self.outstanding_arps = {}

        # (dpid,IP) -> [(expire_time,buffer_id,in_port), ...]
        # These are buffers we've gotten at this datapath for this IP which
        # we can't deliver because we don't know where they go.
        self.lost_buffers = {}

        # For each switch, we map IP addresses to Entries
        self.arpTable = {}

        # This timer handles expiring stuff
        self._expire_timer = Timer(5, self._handle_expiration, recurring=True)

        core.listen_to_dependencies(self)
Ejemplo n.º 3
0
def getHumanDetector(winSize):
    svm4human = svm.SVM()
    svm4human.setLinearSVM()

    detector4human = detector.Detector(winSize)
    detector4human.setClassifier(svm4human)

    dataSet = []
    labels = []

    # positive samples
    posSamples = imageProcessor.loadImages(IN_CLASS_HUMAN_SET_TRAIN)
    dataSet += posSamples
    labels += [1 for i in range(len(posSamples))]

    posSamples = imageProcessor.loadImages(MIT_HUMAN_SET)
    dataSet += posSamples
    labels += [1 for i in range(len(posSamples))]

    # negative samples
    negSamples = imageProcessor.loadImages(IN_CLASS_BACKGROUND_SET)
    dataSet += negSamples
    labels += [0 for i in range(len(negSamples))]

    # negSamples = imageProcessor.loadImages(IN_CLASS_CAR_SET_TEST)
    # dataSet += negSamples
    # labels += [0 for i in range(len(negSamples))]

    detector4human.train(dataSet, labels)

    return detector4human
Ejemplo n.º 4
0
def main():
    namer = lambda name: name + '.norm'
    normalize(('data/spambase.data', ), namer, delimiter=",")
    data, labels = load_data(('data/spambase.data.norm', ),
                             delimiter=",",
                             label_map={
                                 0.0: -1.0,
                                 1.0: 1.0
                             })
    indices = range(len(data))
    shuffle(indices)
    train_errors = []
    test_errors = []
    step = len(data) / NUM_FOLDS
    for k in range(NUM_FOLDS):
        train_indices = indices[0:k * step] + indices[(k + 1) * step:-1]
        test_indices = indices[k * step:(k + 1) * step]
        learner = svm.SVM(data=data[train_indices],
                          labels=labels[train_indices],
                          tolerance=0.01,
                          epsilon=0.001,
                          cost=0.75,
                          kernel=svm.linear_kernel)
        learner.train()
        train_error = learner.test(data[train_indices], labels[train_indices])
        test_error = learner.test(data[test_indices], labels[test_indices])
        train_errors.append(train_error)
        test_errors.append(test_error)
        print "fold %d: train_error=%.6f test_error=%.6f" % (
            k + 1, train_error, test_error)
    train_error = sum(train_errors) / float(NUM_FOLDS)
    test_error = sum(test_errors) / float(NUM_FOLDS)
    print "average: train_error=%.6f test_error=%.6f" % (train_error,
                                                         test_error)
Ejemplo n.º 5
0
 def medium_mouse_click(event):
     if (event.y >= TOP and event.y <= ORIGEN[1] and event.x >= ORIGEN[0]):
         canvas = event.widget
         color = ''
         flag = False
         pointX = float(event.x - ORIGEN[0]) / DESPL_X
         pointY = float(ORIGEN[1] - event.y) / DESPL_Y
         xx, yy = svm.concatPoints(red_points, blue_points)
         alfa, w, b, S = svm.SVM(xx, yy)
         e = svm.verifyPoint(xx, yy, alfa, b, [pointX, pointY])
         print e
         if (e <= 0.0):
             color = "red"
             if (e >= -1.0):
                 flag = True
         else:
             color = "blue"
             if (e <= 1.0):
                 flag = True
         canvas.create_oval(event.x - POINT_RADIO,
                            event.y - POINT_RADIO,
                            event.x + POINT_RADIO,
                            event.y + POINT_RADIO,
                            fill=color,
                            width=1)
         if (color == "red"):
             red_points.append((pointX, pointY))
         elif (color == "blue"):
             blue_points.append((pointX, pointY))
         if (flag == True):
             svmAction()
Ejemplo n.º 6
0
def rescale(Clist = [0.1, 1, 10, 100], data = None, **args) :

    chain = composite.Chain([preproc.Rescale(), svm.SVM(**args)])
    if Clist is not None :
        param = modelSelection.Param(chain, 'classifier.C', Clist)
        return modelSelection.ModelSelector(param)
    else :
        return chain
Ejemplo n.º 7
0
 def svm(self):
     svr = svm.SVM("train.csv")
     clf = svr.train_with_features([0, 1])
     print clf
     if self.p_flag == "p":
         print svr.predict
         predictions = clf.predict(svr.predict)
         self.binning(svr.predict_original, predictions)
Ejemplo n.º 8
0
def gaussianSelect(gammaList = [0.01, 0.05, 0.1, 0.3, 1, 2], **args) :

    measure = 'balancedSuccessRate'
    if 'measure' in args :
	measure = args['measure']
    k = ker.Gaussian()
    param = modelSelection.Param(svm.SVM(k),
				 'kernel.gamma', gammaList)
    return modelSelection.ModelSelector(param, measure = measure)
Ejemplo n.º 9
0
def svmAction():
    tempRed = deepcopy(red_points)
    tempBlue = deepcopy(blue_points)
    resetCanvas()
    copyPoints(tempRed, tempBlue)
    plotPoints()
    x, y = svm.concatPoints(red_points, blue_points)
    alfa, w, b, S = svm.SVM(x, y)
    plotSVM(w, b, S)
Ejemplo n.º 10
0
def main():
    # create some random rectangles
    print "Generating rectangles..."
    rectangles = []
    for x in range(0, 24):
        for y in range(0, 24):
            for width in range(5, 14):
                for height in range(5, 14):
                    area = width * height
                    if area >= 130 and area <= 170 \
                    and x + width <= 28 and y + height <= 28:
                        top_left = (x, y)
                        bottom_right = (x + width, y + height)
                        rectangles.append((top_left, bottom_right))
    rectangles = sample(rectangles, 100)

    # prepare the training data
    print "Loading images..."
    images, labels = load_mnist(dataset="training", path="data")
    print "Preparing training data..."
    train_indices = sample(range(len(images)), len(images) / 10)
    train_data = prep_data(images[train_indices], rectangles)
    train_labels = labels[train_indices].reshape(len(train_indices))

    # train learners
    print "Training..."
    learners = {}
    for pair in combinations(range(10), 2):
        digit_1, digit_2 = pair
        indices = np.where((train_labels == digit_1)
                           | (train_labels == digit_2))[0]
        data = train_data[indices]
        labels = train_labels[indices].copy()
        labels[labels == digit_1] = -1
        labels[labels == digit_2] = 1
        learners[pair] = learner = svm.SVM(data=data,
                                           labels=labels,
                                           tolerance=0.001,
                                           epsilon=0.001,
                                           cost=0.75,
                                           kernel=svm.linear_kernel)
        learner.train()

    # prepare the testing data
    print "Preparing testing data..."
    images, labels = load_mnist(dataset="testing", path="data")
    test_indices = list(set(range(len(images))) - set(train_indices))
    test_data = prep_data(images[test_indices], rectangles)
    test_labels = labels[test_indices].reshape(len(test_indices))

    # test the learners
    print "Testing..."
    train_error = test(learners, train_data, train_labels)
    test_error = test(learners, test_data, test_labels)
    print "train_error=%.6f test_error=%.6f" % (train_error, test_error)
Ejemplo n.º 11
0
def rescaleGaussian(gammaList = [0.01, 0.05, 0.1, 0.3, 1, 2], data = None, **args) :

    k = ker.Gaussian()
    s=svm.SVM(k)
    chain = composite.Chain([preproc.Rescale(), s])
    if gammaList is not None :
        param = modelSelection.Param(chain,
                                     'classifier.kernel.gamma', gammaList)
        return modelSelection.ModelSelector(param)
    else :
        return chain
Ejemplo n.º 12
0
def custom_SVM_test_function(X_train, y_train, X_test):
    data_dict = dataset_reader.build_data_dict(X_train, y_train)

    svm = custom_svm.SVM(data=data_dict, kernel=Kernel.linear(), c=1)
    svm.fit()

    y_pred = []
    print(X_test[0])

    for x in X_test:
        y_pred.append(svm.predict(x))

    return y_pred
Ejemplo n.º 13
0
 def __init__(self, k_max, Cs, weights):
     """
     Initializes the class. k_max is the maximum k_spectrum kernel to consider.
     Cs is the array of regulizer parametters to use for each kernel. The are 
     averaged.
     weights are the weights to use in the combiaison.
     """
     def kernel(X, Y):
         ret = weights[2] * spectrum.k_spectrum(X, Y, k=2)
         for i in range(3, k_max+1):
             ret += weights[i] * spectrum.k_spectrum(X, Y, k=i)
         return ret
     clf = svm.SVM(kernel=kernel, C=np.mean(Cs))
     self.clf = clf
Ejemplo n.º 14
0
def main():
    iris_data = datasets.load_iris()
    train = iris_data["data"][:100]
    target = iris_data["target"][:100]

    f1_scores = np.array([])
    for train_index, test_index in StratifiedKFold(n_splits=5, shuffle=True).split(train, target):
        X_train, X_test = train[train_index], train[test_index]
        y_train, y_test = target[train_index], target[test_index]
        
        model = svm.SVM()
        model.fit(X_train, y_train)
        
        predict = model.predict(X_test)
        f1_scores = np.append(f1_scores, calc_F1(predict, y_test))

    print("F-measure : ", f1_scores)
    print("F-measure average : ", f1_scores.mean())    
Ejemplo n.º 15
0
 def fit(self,X,y,gamma=1,landmarks=None,learning_rate=1,epochs=1000,C=1):
     '''
     Fits the data to the SVM using an rbf kernel
     gamma (1/sigma**2) can be speficied by user (default=1)
     the number of landmarks can also be specified by user (default is len(X))
         if the number of landmarks is specified, the landmarks are randomly chosen from X
     Other parameters for the SVM can also be specified here (and are passed to the linear SVM)
     '''
     self.gamma = gamma
     # remove row on constants 1 if present (saves memory and a tiny bit of time)
     if (X[0,0]==1 and len(np.unique(X[:,0]))==1):
         X = X[:,1:]
     # generate and store landmarks
     if (landmarks==None or landmarks>=len(X)):
         self.landmarks = X
     else:
         self.landmarks = X[np.random.choice(np.arange(len(X),dtype=int),landmarks,replace=False)]
     # generate matrix of features
     X = self.transform(X)
     # fit to a linear SVM
     self.svm = svm.SVM()
     # no x0 appears to perform better
     self.svm.fit(X, y, learning_rate, epochs,C,add_x0=False)
Ejemplo n.º 16
0
Archivo: main.py Proyecto: yx0119/SVM
def run(X, y, train_rate, kernel, C, max_iter, gama):
    f_log.write("this is para{train_set_rate:" + str(train_rate) + " kernel:" +
                str(kernel) + " C:" + str(C) + " max_iter:" + str(max_iter) +
                " gama:" + str(gama) + "}" + "\n")
    n = len(X)
    xx = int(n * train_rate)
    train_X_set = X[:xx]
    train_y_set = y[:xx]
    test_X_set = X[xx:]
    test_y_set = y[xx:]
    svm_Model = svm.SVM(kernel, C)
    svm_Model.set_gama(gama)
    alpha, b = svm_Model.train(train_X_set, train_y_set, max_iter)
    f_log.write("alpha:" + str(alpha) + "\n")
    f_log.write("b:" + str(b) + "\n")
    f_log.write("train_rate:" + str(train_rate) + "\n")
    if kernel == 'linear':
        w = np.dot(train_X_set.T, np.multiply(alpha, train_y_set))
        np.save('./linear_w.npy', w)
        np.save('./linear_b.npy', b)
    else:
        np.save('./rbf_alpha.npy', alpha)
        np.save('./rbf_b.npy', b)
    predictor = svm.Predictor(kernel, alpha, b, train_y_set, train_X_set, gama)
Ejemplo n.º 17
0
import matplotlib.pyplot as plt
import pandas as pd
import utils
# learner imports
import svm
import knn

#Main Script Run
#This script will show as an example of the use of a KNN and SVM learners
#Created by Elijah Flinders

#svm setup and training
print("*********************************************************************")
print("Creating and testing SVM on it's own dataset. Support Vector Machine")
print("*********************************************************************")
svm = svm.SVM(10000, 0.000001)
svm.fit()
print("Finished running the SVM!\n")

#KNN setup and prediction
print("************************************************")
print("Creating and testing KNN. K-th Nearest Neighbor")
print("************************************************")
knnTester = knn.KNN()

# load the Iris data set and convert to specific type
dataset = knnTester.loadCsvListKnn('iris.csv')
for i in range(len(dataset[0]) - 1):
    knnTester.colToFloat(dataset, i)

# convert columns to ints
Ejemplo n.º 18
0
                    best_batch = minibatch[m]
                    best_alpha = alpha[a]

        print("When batch size is {0}, alpha is {1}, test error is {2}".format(
            best_batch, best_alpha,
            utils.classification_error(model.predict(Xtest), ytest)))

    elif Model == 'svm':

        # standardize X
        X, mu, sigma = utils.standardize_cols(X)
        Xvalid, _, _ = utils.standardize_cols(Xvalid, mu, sigma)
        Xtest, _, _ = utils.standardize_cols(Xtest, mu, sigma)

        # SVM
        model = svm.SVM()
        minibatch = [500, 1000, 1500]
        alpha = [0.01, 0.001, 0.0001]
        min_val_error = 1
        best_batch = 0
        best_alpha = 0

        for m in range(3):
            for a in range(3):
                val_error = []
                # cross validation
                for train, validate in kf.split(X, y):
                    model.fit(X[train],
                              y[train],
                              epoch=100,
                              minibatch=minibatch[m],
Ejemplo n.º 19
0
def linear():
    x1, y1, x2, y2 = get_linear_outlier_training_examples()
    x = np.vstack((x1, x2))
    y = np.hstack((y1, y2))
    model = svm.SVM(kernel='linear', C=0.0)
    model.fit(x, y)
    for p in x1:
        plt.scatter(p[0],
                    p[1],
                    c='blue',
                    marker='o',
                    alpha=1,
                    edgecolors='none')
    for p in x2:
        plt.scatter(p[0],
                    p[1],
                    c='red',
                    marker='o',
                    alpha=1,
                    edgecolors='none')
    w, b = model.get_model()
    print(w)
    print(b)

    k = float(-w[0:1, 0:1] / w[0:1, 1:2])
    intercept = float(b / w[0:1, 1:2])
    print(k, intercept)
    p1 = [0, 10]
    p2 = [float(b), k * 10 + intercept]
    plt.plot(p1, p2, c='black')

    x1, y1, x2, y2 = get_linear_outlier_test_examples()
    x = np.vstack((x1, x2))
    y = np.hstack((y1, y2))
    succ = 0
    total = 0
    s_set = set()
    for i in range(x.shape[0]):
        total += 1
        pred = model.predict(x[i])
        if pred == y[i]:
            s_set.add(i)
            succ += 1
    print('accuracy:', succ / total)

    c = 0
    for p in x1:
        if c in s_set:
            plt.scatter(p[0],
                        p[1],
                        c='blue',
                        marker='^',
                        alpha=1,
                        edgecolors='none')
        else:
            plt.scatter(p[0],
                        p[1],
                        c='black',
                        marker='^',
                        alpha=1,
                        edgecolors='none')
        c += 1
    for p in x2:
        if c in s_set:
            plt.scatter(p[0],
                        p[1],
                        c='red',
                        marker='^',
                        alpha=1,
                        edgecolors='none')
        else:
            plt.scatter(p[0],
                        p[1],
                        c='black',
                        marker='^',
                        alpha=1,
                        edgecolors='none')
        c += 1

    plt.grid(True)
    plt.show(block=True)
Ejemplo n.º 20
0
import svm
import pandas as pd
from sklearn import datasets
from sklearn import preprocessing
from sklearn import metrics
from sklearn.model_selection import train_test_split

if __name__ == '__main__':
    train_df = pd.read_csv("sprint4/wdbc.data", header=None)
    y = train_df[1]
    X = train_df.drop([0, 1], axis=1)
    scaler = preprocessing.StandardScaler()
    X_std = scaler.fit_transform(X)
    y = list(map(lambda l: 1 if l == 'M' else -1, y))
    X_train , X_test , y_train , y_test =\
     train_test_split(X_std, y, test_size=0.2, random_state=0)
    # ラベルは1, -1

    # スケーリング

    clf = svm.SVM()
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    print("Confusion Matrix")
    print(metrics.confusion_matrix(y_test, y_pred))
    print(metrics.classification_report(y_test, y_pred))
Ejemplo n.º 21
0
train_data = data[:train_len]
train_tar = target[:train_len]
valid_data = data[train_len:train_len + valid_len]
valid_tar = target[train_len:train_len + valid_len]
test_data = data[train_len + valid_len:]
test_tar = target[train_len + valid_len:]

data = {
    'X_train': train_data,  # training data
    'y_train': train_tar,  # training labels
    'X_val': valid_data,  # validation data
    'y_val': valid_tar  # validation labels
}

model = svm.SVM(input_dim=D, hidden_dim=200, reg=0.005)

solver = solver.Solver(model,
                       data,
                       update_rule='adam',
                       optim_config={
                           'learning_rate': 1e-3,
                       },
                       lr_decay=1,
                       num_epochs=3000,
                       batch_size=500,
                       print_every=1)
solver.train()

acc = solver.check_accuracy(test_data, test_tar)
Ejemplo n.º 22
0
def polynomial():
    x1, y1, x2, y2 = get_polynomial_training_examples()
    x = np.vstack((x1, x2))
    y = np.hstack((y1, y2))
    model = svm.SVM(kernel='polynomial', C=0.0)
    model.fit(x, y)
    for p in x1:
        plt.scatter(p[0],
                    p[1],
                    c='blue',
                    marker='o',
                    alpha=1,
                    edgecolors='none')
    for p in x2:
        plt.scatter(p[0],
                    p[1],
                    c='red',
                    marker='o',
                    alpha=1,
                    edgecolors='none')
    w, b = model.get_model()
    print(w)
    print(b)

    x1, y1, x2, y2 = get_polynomial_test_examples()
    x = np.vstack((x1, x2))
    y = np.hstack((y1, y2))
    succ = 0
    total = 0
    s_set = set()
    for i in range(x.shape[0]):
        total += 1
        pred = model.predict(x[i])
        if pred == y[i]:
            s_set.add(i)
            succ += 1
    print('accuracy:', succ / total)

    c = 0
    for p in x1:
        if c in s_set:
            plt.scatter(p[0],
                        p[1],
                        c='blue',
                        marker='^',
                        alpha=1,
                        edgecolors='none')
        else:
            plt.scatter(p[0],
                        p[1],
                        c='black',
                        marker='^',
                        alpha=1,
                        edgecolors='none')
        c += 1
    for p in x2:
        if c in s_set:
            plt.scatter(p[0],
                        p[1],
                        c='red',
                        marker='^',
                        alpha=1,
                        edgecolors='none')
        else:
            plt.scatter(p[0],
                        p[1],
                        c='black',
                        marker='^',
                        alpha=1,
                        edgecolors='none')
        c += 1

    plt.grid(True)
    plt.show(block=True)
Ejemplo n.º 23
0
            fig.add_trace(
                go.Scatter3d(x=data_reduced.T[0][i:i + 100],
                             y=data_reduced.T[1][i:i + 100],
                             z=data_reduced.T[2][i:i + 100],
                             mode='markers',
                             marker=dict(size=3)))

        fig.show()

#####################################################
# Classify
#####################################################
print('\nAttempt: Classification...')

# Init classifier
classifier = svm.SVM()

# Fit to training data
W = classifier.learn(data_reduced, y_train)

if (params['resub']):
    # Classify on resub
    classifier.predict(data_reduced, y_train)
else:
    # Classify on test data

    # Smush features together
    features_ravel = np.empty((1, 2048))
    for feat in test_features:
        features_ravel = np.append(features_ravel, feat, axis=0)
    features_ravel = features_ravel[1:]
Ejemplo n.º 24
0
import pandas as pd
import scipy.io as spy
import matplotlib.pyplot as plt
import matplotlib.colors as mplcolors
import numpy as np
import svm

if __name__ == "__main__":
    data = spy.loadmat('ressources/ex6data1.mat')
    X = np.array(data['X'])
    print(X.mean(axis=0), X[:, 1].mean())
    print(X.std(axis=0))
    labels = np.array(data['y'])
    labels[labels == 0] = -1
    print('lab ', labels.shape)
    y = ['red' if i == 1 else 'blue' for i in labels]
    colors = ['red', 'blue']
    plt.scatter(x=X[:, 0], y=X[:, 1], c=y)
    plt.show()
    my_svm = svm.SVM(learn_rate=0.01, reg_param=0.03, epoch=1000)
    my_svm.fit(X, labels, normalize=True)
    my_svm.run_svm()
    print(my_svm.get_prediction(X, labels))
Ejemplo n.º 25
0
def rbf():
    x1, y1, x2, y2 = get_rbf_training_examples()
    x = np.vstack((x1, x2))
    y = np.hstack((y1, y2))
    model = svm.SVM(kernel='rbf', C=0.0)
    model.fit(x, y)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    circle = Circle(xy=(0.0, 0.0), radius=5, alpha=0.3)
    ax.add_patch(circle)
    for p in x1:
        plt.scatter(p[0],
                    p[1],
                    c='blue',
                    marker='o',
                    alpha=1,
                    edgecolors='none')
    for p in x2:
        plt.scatter(p[0],
                    p[1],
                    c='red',
                    marker='o',
                    alpha=1,
                    edgecolors='none')
    w, b = model.get_model()
    print(w)
    print(b)

    x1, y1, x2, y2 = get_rbf_test_examples()
    x = np.vstack((x1, x2))
    y = np.hstack((y1, y2))
    succ = 0
    total = 0
    s_set = set()
    for i in range(x.shape[0]):
        total += 1
        pred = model.predict(x[i])
        if pred == y[i]:
            s_set.add(i)
            succ += 1
    print('accuracy:', succ / total)

    c = 0
    for p in x1:
        if c in s_set:
            plt.scatter(p[0],
                        p[1],
                        c='blue',
                        marker='^',
                        alpha=1,
                        edgecolors='none')
        else:
            plt.scatter(p[0],
                        p[1],
                        c='black',
                        marker='^',
                        alpha=1,
                        edgecolors='none')
        c += 1
    for p in x2:
        if c in s_set:
            plt.scatter(p[0],
                        p[1],
                        c='red',
                        marker='^',
                        alpha=1,
                        edgecolors='none')
        else:
            plt.scatter(p[0],
                        p[1],
                        c='black',
                        marker='^',
                        alpha=1,
                        edgecolors='none')
        c += 1

    plt.grid(True)
    plt.show(block=True)
Ejemplo n.º 26
0
inst_logR.begin_alg()
param_classif = inst_logR.get_param()
#print param_classif

inst_dtree = decisiontreeNoZScoreKBest.DecisionTree(data_reader.copy(),
                                                    class_values)

print """
----------------
	"""
print("Decision Tree Classifier algorithm...")
inst_dtree.begin_alg()
param_classif = inst_dtree.get_param()
#print param_classif

inst_svm = svm.SVM(data_reader.copy(), class_values)
print """
----------------
	"""
print("Support Vector Machines(SVM) algorithm...")
inst_svm.begin_alg()
param_weight = inst_svm.get_param()
#print param_classif

print """

-------
testing data...ZeroR
-------
"""
Ejemplo n.º 27
0
import cv2
import data
import hog
import svm as SupportVectorMachine
import objectdetector as od
import evaluate
import pickle
import math

train_data, train_data_mirrored, train_labels = data.get_train_data()

svm = SupportVectorMachine.SVM("trained_svm.data")
if not svm.isTrained:
    svm.train(train_data, train_labels)

mirrored_svm = SupportVectorMachine.SVM("trained_mirrored_svm.data")
if not mirrored_svm.isTrained:
    mirrored_svm.train(train_data_mirrored, train_labels)

hog = hog.HOGDescriptor()

objectDetector = od.ObjectDetector(scales=10,
                                   scaling=0.8,
                                   stride=(6, 5),
                                   detection_threshold=0.6,
                                   overlap_threshold=0.5)

while True:
    command = input("\nType a command\n")
    arguments = command.split()
Ejemplo n.º 28
0
def extract(data, cl=1):
    x = []
    y = []

    for d in data:
        if d.val == cl:
            x.append(d.x)
            y.append(d.y)

    return x, y


linData = dr.readData('linsep.txt')
print(linData)

linsvm = svm.SVM(linData)
w, b = linsvm.train()

print(w)
print(b)

correctRate = linsvm.test(linData)

print('percent correct')
print(correctRate)

px, py = extract(linData, +1)
nx, ny = extract(linData, -1)
lx = np.linspace(0, .6, 100)
ly = []
for x in lx:
Ejemplo n.º 29
0
__author__ = 'yujinke'
import numpy as np

import cv2

import matplotlib as plt

import svm
import os
import cPickle

dir_T = "./Char_classify/T"
dir_F = "./Char_classify/F"

dir_folder = "./HasPlate"
model = svm.SVM(C=1.67, gamma=0.0383)
model.load('digits_svm.dat')

# f = open("./data.pkl", 'rb')
# training_data, validation_data, test_data = cPickle.load(f)
# samples_train, labels_train= test_data
#
# for i in range(100):
#
#      print "result",model.predict_single(samples_train[i]),labels_train[i]
#      samples_train[i]=  samples_train[i]*255
#
#      P  = samples_train[i].astype(np.uint8)
#
#      P =P.reshape((28,28))
#
Ejemplo n.º 30
0
def main():
    qa = svm.SVM()
    qa.train_save()
    sent = '澳大利亚 是 南半球 面积 第 几 大 的 国家'
    print(extract_word(sent))
    qa.forward(sent)