Esempio n. 1
0
def plot():
    C = [1, 10, 100, 1000]
    test_accuracy = []
    word_acr = []
    X_train, y_train = read_train_struct()
    X_test, y_test = read_test_struct()

    for i in C:
        y_train = y_train.ravel()
        clf = train(X_train, y_train, i / len(y_train))
        y_pred, score = test(clf, X_test, y_test)
        test_accuracy.append(score * 100)
        y_train = y_train.reshape(len(y_train, ))
        given_words, pred_words = form_words(y_test, y_pred)
        w_acc = word_accuracy(given_words, pred_words)
        word_acr.append(w_acc * 100)

    mp.figure(1)
    mp.title('Letter wise Accuracy vs C - SVM-MC ')
    mp.plot(C, test_accuracy)
    mp.ylabel('Accuracy')
    mp.xlabel('C')
    mp.figure(2)
    mp.plot(C, word_acr)
    mp.title('Word wise Accuracy vs C - SVM-MC ')
    mp.ylabel('Accuracy')
    mp.xlabel('C')
Esempio n. 2
0
def plot():
    C = [1, 10]
    test_accuracy = []
    word_acr = []
    X_train, y_train = read_train_struct()
    X_test, y_test = read_test_struct()
    for i in C:
        train(i)
        test()
        my_path = os.path.abspath(os.path.dirname(__file__))
        path = os.path.join(my_path, "test.outtags")
        y_pred = np.loadtxt(path, usecols=(0, ))
        y_test = y_test.reshape(len(y_test), )
        test_acc = get_test_accuracy(y_test, y_pred)
        test_accuracy.append(test_acc * 100)
        given_words, pred_words = form_words(y_test, y_pred)
        w_acc = word_accuracy(given_words, pred_words)
        word_acr.append(w_acc * 100)

    mp.figure(1)
    mp.plot(C, test_accuracy)
    mp.ylabel('Accuracy')
    mp.xlabel('C')
    mp.figure(2)
    mp.plot(C, word_acr)
    mp.ylabel('Accuracy')
    mp.xlabel('C')
Esempio n. 3
0
import translation as t 
import rotation as r
import readInput as ri
import SVM_MC as svm
import os.path
import numpy as np
import matplotlib.pyplot as mp

my_path = os.path.abspath(os.path.dirname(__file__))
path = os.path.join(my_path, "../../data/transform.txt")


f = open(path, 'r')
#raw_data = file.read()
x=[0,50]
X_train ,y_train=ri.read_train_struct();
X_test,y_test=ri.read_test_struct()
test_accuracy =[]
word_acr =[]
train_data={}

for i in range(len(X_train)):
    train_data[i+1]=y_train[i]


for num in x:
    x_trans=[]
    y_trans=[]
    for i in range(num):
        line=next(f)
        line=line[0:len(line)-1]
Esempio n. 4
0
def tamper(model):
    test_acc = []
    wrd_acr = []
    y_pred = []

    X_train, y_train = ri.read_train_struct()
    X_test, y_test = ri.read_test_struct()
    for num in x:
        print(num)
        X_trans = copy.deepcopy(X_train)
        for i in range(num):
            line = next(f)
            line = line.split(' ')
            offset = []
            c = X_trans[int(line[1]), :]
            example = np.array(c).reshape(16, 8)
            if (line[0] == 'r'):
                alpha = float(line[2])
                x_result = r.rotate(example, alpha)
                x_result = x_result.reshape(128, )
            elif (line[0] == 't'):
                offset.append(int(line[2]))
                offset.append(int(line[3]))
                x_result = t.translate(example, offset)
                x_result = x_result.reshape(128, )
            x_max_old = 0
            x_min_old = 255
            x_min_new = 0
            x_max_new = 1
            new_value = restore_range(x_result, x_max_old, x_min_old,
                                      x_min_new, x_max_new)
            X_trans[int(line[1])] = new_value
        #training
        if (num == 0):
            if (model == 'svm'):
                clf = svm.train(X_train, y_train, 1000 / len(y_train))
            else:
                x_y = X_train, y_train
                optimize.get_params(x_y)
                a = np.loadtxt("best_Weights_tampered", usecols=(0, ))
                W = np.array(a[:26 * 128].reshape(26, 128))
                T = np.array(a[26 * 128:26 * 128 + 26 * 26].reshape(26, 26))
        #training with more than 1 transformation
        else:
            if (model == 'svm'):
                clf = svm.train(X_trans, y_train, 1000 / len(y_train))
            else:
                x_y = X_trans, y_train
                print(type(x_y))
                optimize.get_params(x_y)
                a = np.loadtxt("best_Weights_tampered", usecols=(0, ))
                W = np.array(a[:26 * 128].reshape(26, 128))
                T = np.array(a[26 * 128:26 * 128 + 26 * 26].reshape(26, 26))

        #testing
        if (model == 'svm'):
            y_pred, score = svm.test(clf, X_test, y_test)
        else:
            y_pred = decode.max_sum(X_test, W, T)
            y_pred = [y + 1 for y in y_pred]
            y_test = y_test.reshape(26198, )
            y_pred = np.array(y_pred).reshape(len(y_pred, ))
            print((y_test))
            print((y_pred))
            score = max_sum_decode.get_test_accuracy(y_test, y_pred)
        test_acc.append(score * 100)
        y_test = y_test.reshape(len(y_test, ))
        given_words, pred_words = svm.form_words(y_test, y_pred)
        w_acc = svm.word_accuracy(given_words, pred_words)
        wrd_acr.append(w_acc * 100)
    return test_acc, wrd_acr