def predictTraitValue(self, imagelist_file='image_list.txt', class_label=1, outfile='output', norm=-1, debug=False): """ Calls external functions to obtain predicted value between 0 and 1 for given image, updates class values Args: imagelist_file: file that contains the list of image/images to be processed class_label: label given images as either positive or negative for SVM classification outfile: filename of output for Dense SIFT analysis Return: True if successful """ with open(imagelist_file, 'w') as f: f.write('tmp.jpg\n') cv2.imwrite('tmp.jpg', self.norm_image) hog_histogram(imagelist_file, class_label, outfile, norm, debug) prob_y, prob_x = svm_read_problem(outfile) model_file = models[self.trait] model = load_model(model_file) self.p_val = predict(prob_y, prob_x, model)[2][0][0] return True
def load_feature_row_file(feature_file, rows_file): """ Reads a libSVM-formatted feature file and corresponding row IDs and returns an adjacency dictionary and IDs. Parameters ---------- feature_file : str Path to a LibSVM-formatted feature file. row_file : str Path to a file containing a newline-separated list of gene IDs associated with each row of feature_file. Returns ------- data : list(dict) A list of feature vector dicts where elements are accessed by gene ID. IDs : list(str) Gene IDs corresponding to each row of `data`. """ labels, data = ll.svm_read_problem(feature_file) IDs = [] with open(rows_file, "r") as f: for line in f: IDs.append(line.strip()) return data, IDs
def liblinear_classifier(svm_input=None, y=[], x=[]): """调用训练好的liblinear分类器做垃圾过滤 """ svm_model = load_model(SVM_MODEL_FILE) if svm_input: y, x = svm_read_problem(svm_input) p_label, p_acc, p_val = predict(y, x, svm_model, "-q") return p_label
def liblinear_classifier(svm_input=None, y=[], x=[]): """调用训练好的liblinear分类器做垃圾过滤 """ svm_model = load_model(SVM_MODEL_FILE) if svm_input: y, x = svm_read_problem(svm_input) p_label, p_acc, p_val = predict(y, x, svm_model, "-q") return p_label
def run_classifier(train_file, test_file): count_one=0 y_train, x_train = svm_read_problem(train_file) counter=0 while counter<len(y_train): if y_train[counter]==-1: count_one=count_one+1 counter=counter+1 w1=count_one/float(len(y_train)) #w1=0.95 # Extra credit #w1=0.95 # Extra credit param='-s 0 -w1 '+str(w1)+' -w-1 '+str(1-w1) #param='-s 0' # Extra Credit model = train(y_train, x_train, param) y_test, x_test = svm_read_problem(test_file) p_labels, p_acc, p_vals = predict(y_test, x_test, model, '-b 1') accuracy = p_acc[0] index=0 if model.label[0]==1: index=0 elif model.label[1]==1: index=1 counter=0 prob_list=[] while counter<len(p_vals): prob_list.append(p_vals[counter][index]) counter=counter+1 output_tup=(p_labels, y_test, prob_list) return output_tup
def read_multiple_days(start_day, end_day): all_y = [] all_x = [] if start_day > end_day: return if start_day < 0: return if end_day > 120: return for day in range(start_day, end_day): path = "%s/Day%d.svm" % (base_path, day) (y, x) = ll.svm_read_problem(path) all_y.extend(y) all_x.extend(x) # print "loaded data from days %d to %d" % (start_day, end_day) return (all_y, all_x)
def main(): if __name__ == "__main__": y, x = svm_read_problem(feature_file, return_scipy=True) # train:test = 7:3 train_X = x[:14000] train_y = y[:14000] test_X = x[14000:] test_y = y[14000:] prob = problem(train_y, train_X) param = parameter("-c 1 -s 2") model = train(prob, param) p_labs, p_acc, p_vals = predict(test_y, test_X, model) accuracy, precision, recall = metrics_result(test_y, p_labs) print print "accuracy: ", accuracy print "precision: ", precision print "recall: ", recall
def predictTraitValue(self, imagelist_file='image_list.txt', class_label=1, outfile='output', norm=-1, debug=False): """ Calls external functions to obtain predicted value between 0 and 1 for given image, updates class values Args: imagelist_file: file that contains the list of image/images to be processed class_label: label given images as either positive or negative for SVM classification outfile: filename of output for Dense SIFT analysis Return: True if successful """ with open(imagelist_file, 'w') as f: f.write('tmp.jpg\n') cv2.imwrite('tmp.jpg', self.norm_image) hog_histogram(imagelist_file, class_label, outfile, norm, debug) prob_y, prob_x = svm_read_problem(outfile) model_file = models[self.trait] model = load_model(model_file) self.p_val = predict(prob_y, prob_x, model)[2][0][0] return True
def liblinear_predict(problem_filepath, model_filepath): """ Using LibLinear to predict result of a problem Returns ------- (ids, labels) """ # Reading a problem ids, x = liblinearutil.svm_read_problem(problem_filepath) print "len(x) = ", len(x) # Preparing a model model = liblinearutil.load_model(model_filepath) # Predicting y = [-2] * len(x) p_label, p_acc, p_val = liblinearutil.predict(y, x, model) return (ids, p_label)
def train(instance_file, model_file, param): y, x = ll.svm_read_problem(instance_file) prob = ll.problem(y, x) m = ll.train(prob, param) ll.save_model(model_file, m) print 'done training', model_file
def readData(file, return_scipy=False): from liblinearutil import svm_read_problem y, X = svm_read_problem(file + ".svm", return_scipy=return_scipy) tag = open(file + ".tag").readlines() return y, X, tag
import liblinearutil import numpy as np import math train_data = liblinearutil.svm_read_problem( './data/gisette/gisette.train.scaled') test_data = liblinearutil.svm_read_problem( './data/gisette/gisette.test.scaled') def get_params(s, e, C): return ['-s', s, '-e', e, '-c', C, '-q'] def get_k_fold(k, fold, dataset): result = np.array_split(dataset, k) left = np.concatenate( result[:fold]) if len(result[:fold]) > 0 else np.empty((0, )) right = np.concatenate( result[fold + 1:]) if len(result[fold + 1:]) > 0 else np.empty((0, )) return np.concatenate((left, right)), result[fold] def validation(k, data_x, data_y, s, e, C): accuracies = [] params = get_params(s, e, C) print('s = {}, e = {}, C = {}'.format(s, e, C)) for fold in range(k): train_x, test_x = get_k_fold(k, fold, data_x) train_y, test_y = get_k_fold(k, fold, data_y) m = liblinearutil.train(train_y, train_x, params)
def train(word_dict): get_feature(word_dict, "data/train.dat", "data/train.format") get_feature(word_dict, "data/test.dat", "data/test.format") train_y, train_x = linear.svm_read_problem("data/train.format") model = linear.train(train_y, train_x) linear.save_model("model.dat", model)
def train_model(): """训练模型 """ y, x = svm_read_problem(TRAIN_INPUT_FILE) m = train(y, x, '-c 4') save_model(SVM_MODEL_FILE, m)
def load_day(day): path = "%s/Day%d.svm" % (base_path, day) (y, x) = ll.svm_read_problem(path) return (y, x)
def predict(instance_file, model_file, param): y, x = ll.svm_read_problem(instance_file) prob = ll.problem(y, x) m = ll.load_model(model_file) dist = ll.predict(y, x, m, param)[2] print dist[0]
#coding: utf-8 import liblinearutil import outputLIBSVMformat train_label, train_data = liblinearutil.svm_read_problem("./train_libsvmFormat.txt") #カーネル関数は線型 model = liblinearutil.train(train_label, train_data, "-s 3") test_label, test_data = liblinearutil.svm_read_problem("./test_libsvmFormat.txt") p_label, p_acc, p_val = liblinearutil.predict(test_label, test_data, model)
def train_model(): """训练模型 """ y, x = svm_read_problem(TRAIN_INPUT_FILE) m = train(y, x, "-c 4") save_model(SVM_MODEL_FILE, m)