def trainSVM_models_1vs1(model_ubm, tr_data, modeldir_patient_svm, ParametersSVM): """ This function will produce the 1vs1 multiclass SVM models It will first create the supervectors from the data and then feed them to make_svm_model for selecting c parameter and producing a SVM model with best parameter :param model_ubm: UBM model to adapt the sequences. :param tr_data: training data :param modeldir_patient_svm: model directory to save the trained SVM model :param ParametersSVM: parameters to be used for selecting relevance factor, and supervector creation. :return: nothing """ for i in range(1,5,1): for j in range(i+1,5,1): data1 = getattr(tr_data, str('data_g' + str(i))) data2 = getattr(tr_data, str('data_g' + str(j))) r_final = 5 # Select what ratio of each class should be present for training dataset. # Make supervectors if ParametersSVM.equal_number_of_sequences == 1: if max(data1[0]) < max(data2[0]): num_of_seq = int(max(data1[0])) else: num_of_seq = int(max(data2[0])) super_vector1 = make_super_vector( data1[0], data1[1], r_final, num_of_seq, model_ubm, ParametersSVM) super_vector2 = make_super_vector( data2[0], data2[1], r_final, num_of_seq, model_ubm, ParametersSVM) else: num_of_seq = int(max(data1[0])) super_vector1 = make_super_vector( data1[0], data1[1], r_final, num_of_seq, model_ubm, ParametersSVM) num_of_seq = int(max(data2[0])) super_vector2 = make_super_vector( data2[0], data2[1], r_final, num_of_seq, model_ubm, ParametersSVM) fulltrset_sv = np.concatenate([super_vector1, super_vector2], axis=1) labels = np.concatenate([np.ones(np.size(super_vector1, 1)), np.zeros(np.size(super_vector2, 1))]) del super_vector1, super_vector2 remove_NaNs(fulltrset_sv, labels) print('Building SVM models for grade', i, 'vs', j) save_file_name_svm = path.join(modeldir_patient_svm, ('SVM_model_' + str(i) + '_' + str(j))) model_svm = make_svm_model(fulltrset_sv, labels) model_svm.rel_factor=r_final fid = open(save_file_name_svm,'wb') pickle.dump(model_svm, fid) fid.close()
def classify_1vs1(data, model_ubm, modeldir_patient_svm, ParametersSVM): prob_epoch = np.zeros((6, int(max(data[0])))) dec_class = np.zeros((6, int(max(data[0])))) a = 0 for i in range(1, 5, 1): for j in range(i + 1, 5, 1): load_file_name_svm = os.path.join( modeldir_patient_svm, ('SVM_model_' + str(i) + '_' + str(j))) fid = open(load_file_name_svm, 'rb') model_svm = pickle.load(fid) fid.close() super_vectors = make_super_vector(data[0], data[1], model_svm.rel_factor, int(max(data[0])), model_ubm, ParametersSVM, test_flag=1) Dzero = (np.where(super_vectors[1] == 0)) dec_class[a, :] = model_svm.predict(super_vectors[0].T) prob_epoch[a, :] = model_svm.predict_proba(super_vectors[0].T)[:, 1] prob_epoch[a, Dzero] = np.nan dec_class[a, Dzero] = 0 a = a + 1 if ParametersSVM.prob_based_dec == 1: final_class_epoch = max(prob_epoch) else: final_class_epoch = get_class(dec_class) return final_class_epoch, dec_class, prob_epoch
def classify_1vs1(data, model_ubm, modeldir_patient_svm, ParametersSVM): prob_epoch = np.zeros((6,int(max(data[0])))) dec_class = np.zeros((6,int(max(data[0])))) a=0 for i in range(1,5,1): for j in range(i+1,5,1): load_file_name_svm = os.path.join(modeldir_patient_svm, ('SVM_model_' + str(i) + '_' + str(j))) fid = open(load_file_name_svm,'rb') model_svm = pickle.load(fid) fid.close() super_vectors = make_super_vector(data[0], data[1], model_svm.rel_factor, int(max(data[0])), model_ubm, ParametersSVM, test_flag=1) Dzero=(np.where(super_vectors[1]==0)) dec_class[a,:] = model_svm.predict(super_vectors[0].T) prob_epoch[a,:] = model_svm.predict_proba(super_vectors[0].T)[:,1] prob_epoch[a, Dzero] = np.nan dec_class[a, Dzero] = 0 a=a+1 if ParametersSVM.prob_based_dec==1: final_class_epoch = max(prob_epoch) else: final_class_epoch = get_class(dec_class) return final_class_epoch, dec_class, prob_epoch
def trainSVM_models_crammer_singer(model_ubm, tr_data, modeldir_patient_svm, ParametersSVM): """ This function will train a multiclass SVM based on crammers_singer implementation. It will give all the data from all the grades and then finally train a SVM model. :param model_ubm: UBM model to adapt the sequences. :param tr_data: training data :param modeldir_patient_svm: model directory to save the trained SVM model :param ParametersSVM: parameters to be used for selecting relevance factor, and supervector creation. :return: nothing """ targets = np.arange(1, ParametersSVM.num_of_classes + 1, 1) # Concatenate the data of all classes for lv in range(0, ParametersSVM.num_of_classes, 1): data_temp = getattr(tr_data, str('data_g' + str(targets[lv]))) if lv == 0: data = np.asarray(data_temp) else: data[0] = np.concatenate([data[0], data_temp[0] + np.max(data[0])], axis=0) data[1] = np.concatenate([data[1], data_temp[1]], axis=1) num_of_seq = int(max(data[0])) r_final = 5 fulltrset_sv = make_super_vector(data[0], data[1], r_final, num_of_seq, model_ubm, ParametersSVM) labels = np.concatenate([ np.ones((int(max(tr_data.data_g1[0])))), 2 * np.ones( (int(max(tr_data.data_g2[0])))), 3 * np.ones( (int(max(tr_data.data_g3[0])))), 4 * np.ones( (int(max(tr_data.data_g4[0])))) ]) # Do k-fold internal cross validation to select best C parameter parameters = [{'C': [1, 10, 100, 1000]}] kf = StratifiedKFold(labels, n_folds=3) gscv = GridSearchCV(LinearSVC(multi_class='crammer_singer'), parameters, cv=kf) model_svm = gscv.fit(fulltrset_sv.T, labels) print('best score =', gscv.best_score_, 'with C ', gscv.best_params_) save_file_name_svm = path.join(modeldir_patient_svm, ('SVM_model_CS')) model_svm.rel_factor = r_final fid = open(save_file_name_svm, 'wb') pickle.dump(model_svm, fid) fid.close()
def classify_builtin_1vs1(data, model_ubm, modeldir_patient_svm, ParametersSVM): result=result_class(4,int(max(data[0]))) load_file_name_svm = os.path.join(modeldir_patient_svm, ('SVM_model_CS')) fid = open(load_file_name_svm,'rb') model_svm = pickle.load(fid) fid.close() super_vectors = make_super_vector(data[0], data[1], model_svm.rel_factor, int(max(data[0])), model_ubm, ParametersSVM, test_flag=1) result.final_dec_epoch = model_svm.predict(super_vectors[0].T) result.dis_epoch = model_svm.decision_function(super_vectors[0].T) Dzero=(np.where(super_vectors[1]==0))[0] result.final_dec_epoch[Dzero]=0 prob_epoch=0 return result
def classify_crammer_singer(data, model_ubm, modeldir_patient_svm, ParametersSVM): load_file_name_svm = os.path.join(modeldir_patient_svm, ('SVM_model_CS')) fid = open(load_file_name_svm,'rb') model_svm = pickle.load(fid) fid.close() super_vectors = make_super_vector(data[0], data[1], model_svm.rel_factor, int(max(data[0])), model_ubm, ParametersSVM, test_flag=1) final_class_epoch = model_svm.predict(super_vectors[0].T) dec_dist = model_svm.decision_function(super_vectors[0].T) Dzero=(np.where(super_vectors[1]==0)) final_class_epoch[Dzero]=0 prob_epoch=0 return final_class_epoch, dec_dist, prob_epoch
def classify_1vsRest(data, model_ubm, modeldir_patient_svm, ParametersSVM): result = result_class(4, int(max(data[0]))) a = 0 for i in range(1, 5, 1): load_file_name_svm = os.path.join(modeldir_patient_svm, ('SVM_model_' + str(i) + '_rest')) fid = open(load_file_name_svm, 'rb') model_svm = pickle.load(fid) fid.close() super_vectors = make_super_vector(data[0], data[1], model_svm.rel_factor, int(max(data[0])), model_ubm, ParametersSVM, test_flag=1) Dzero = np.where(super_vectors[1] == 0)[0] result.dec_epoch[a, :] = model_svm.predict(super_vectors[0].T) result.dis_epoch[a, :] = model_svm.decision_function( super_vectors[0].T) result.prob_epoch[a, :] = model_svm.predict_proba( super_vectors[0].T)[:, 1] result.dec_epoch[a, np.where(result.dec_epoch == 0)] = 5 result.dec_epoch[a, np.where(result.dec_epoch == 1)] = i result.prob_epoch[a, Dzero] = np.nan result.dec_epoch[a, Dzero] = 0 result.dis_epoch[a, Dzero] = np.nan a = a + 1 if ParametersSVM.final_dec is 'prob': result.final_dec_epoch = np.argmax(result.prob_epoch, axis=0) + 1 result.final_dec_epoch[Dzero] = 0 elif ParametersSVM.final_dec is 'distance': result.final_dec_epoch = np.argmin(result.dis_epoch, axis=0) + 1 result.final_dec_epoch[Dzero] = 0 return result
def trainSVM_models_builtin_1vs1(model_ubm, tr_data, modeldir_patient_svm, ParametersSVM): """ This function will train a multiclass SVM based on crammers_singer implementation. It will give all the data from all the grades and then finally train a SVM model. :param model_ubm: UBM model to adapt the sequences. :param tr_data: training data :param modeldir_patient_svm: model directory to save the trained SVM model :param ParametersSVM: parameters to be used for selecting relevance factor, and supervector creation. :return: nothing """ targets = np.arange(1,ParametersSVM.num_of_classes+1,1) # Concatenate the data of all classes for lv in range(0,ParametersSVM.num_of_classes,1): data_temp=getattr(tr_data, str('data_g' + str(targets[lv]))) if lv == 0: data=np.asarray(data_temp) else: data[0] = np.concatenate([data[0], data_temp[0]+np.max(data[0])], axis=0) data[1] = np.concatenate([data[1], data_temp[1]], axis=1) num_of_seq = int(max(data[0])) r_final=5 fulltrset_sv = make_super_vector(data[0], data[1], r_final, num_of_seq, model_ubm, ParametersSVM) labels = np.concatenate([np.ones((int(max(tr_data.data_g1[0])))), 2*np.ones((int(max(tr_data.data_g2[0])))), 3*np.ones((int(max(tr_data.data_g3[0])))), 4*np.ones((int(max(tr_data.data_g4[0]))))]) # Do k-fold internal cross validation to select best C parameter parameters = [{'C': [1, 10, 100, 1000]}] kf = StratifiedKFold(labels, n_folds=3) gscv = GridSearchCV(SVC(probability=True), parameters, cv=kf) model_svm = gscv.fit(fulltrset_sv.T, labels) print('best score =', gscv.best_score_ , 'with C ', gscv.best_params_) save_file_name_svm = path.join(modeldir_patient_svm, ('SVM_model_CS')) model_svm.rel_factor=r_final fid = open(save_file_name_svm,'wb') pickle.dump(model_svm, fid) fid.close()
def classify_builtin_1vs1(data, model_ubm, modeldir_patient_svm, ParametersSVM): result = result_class(4, int(max(data[0]))) load_file_name_svm = os.path.join(modeldir_patient_svm, ('SVM_model_CS')) fid = open(load_file_name_svm, 'rb') model_svm = pickle.load(fid) fid.close() super_vectors = make_super_vector(data[0], data[1], model_svm.rel_factor, int(max(data[0])), model_ubm, ParametersSVM, test_flag=1) result.final_dec_epoch = model_svm.predict(super_vectors[0].T) result.dis_epoch = model_svm.decision_function(super_vectors[0].T) Dzero = (np.where(super_vectors[1] == 0))[0] result.final_dec_epoch[Dzero] = 0 prob_epoch = 0 return result
def classify_crammer_singer(data, model_ubm, modeldir_patient_svm, ParametersSVM): load_file_name_svm = os.path.join(modeldir_patient_svm, ('SVM_model_CS')) fid = open(load_file_name_svm, 'rb') model_svm = pickle.load(fid) fid.close() super_vectors = make_super_vector(data[0], data[1], model_svm.rel_factor, int(max(data[0])), model_ubm, ParametersSVM, test_flag=1) final_class_epoch = model_svm.predict(super_vectors[0].T) dec_dist = model_svm.decision_function(super_vectors[0].T) Dzero = (np.where(super_vectors[1] == 0)) final_class_epoch[Dzero] = 0 prob_epoch = 0 return final_class_epoch, dec_dist, prob_epoch
def classify_1vsRest(data, model_ubm, modeldir_patient_svm, ParametersSVM): result=result_class(4,int(max(data[0]))) a=0 for i in range(1,5,1): load_file_name_svm = os.path.join(modeldir_patient_svm, ('SVM_model_' + str(i) + '_rest')) fid = open(load_file_name_svm,'rb') model_svm = pickle.load(fid) fid.close() super_vectors = make_super_vector(data[0], data[1], model_svm.rel_factor, int(max(data[0])), model_ubm, ParametersSVM, test_flag=1) Dzero = np.where(super_vectors[1]==0)[0] result.dec_epoch[a,:] = model_svm.predict(super_vectors[0].T) result.dis_epoch[a,:] = model_svm.decision_function(super_vectors[0].T) result.prob_epoch[a,:] = model_svm.predict_proba(super_vectors[0].T)[:,1] result.dec_epoch[a,np.where(result.dec_epoch==0)]=5 result.dec_epoch[a,np.where(result.dec_epoch==1)]=i result.prob_epoch[a, Dzero] = np.nan result.dec_epoch[a, Dzero] = 0 result.dis_epoch[a, Dzero] = np.nan a=a+1 if ParametersSVM.final_dec is 'prob': result.final_dec_epoch = np.argmax(result.prob_epoch, axis=0)+1 result.final_dec_epoch[Dzero]=0 elif ParametersSVM.final_dec is 'distance': result.final_dec_epoch = np.argmin(result.dis_epoch, axis=0)+1 result.final_dec_epoch[Dzero]=0 return result
def trainSVM_models_1vsRest(model_ubm, tr_data, modeldir_patient_svm, ParametersSVM): """ This function will produce the 1vsRest SVM models. It will first create the supervectors from the data and then feed them to make_svm_model for selecting c parameter and producing a SVM model with best parameter :param model_ubm: UBM model to adapt the sequences. :param tr_data: training data :param modeldir_patient_svm: model directory to save the trained SVM model :param ParametersSVM: parameters to be used for selecting relevance factor, and supervector creation. :return: nothing """ targets = np.arange(1, ParametersSVM.num_of_classes + 1, 1) for i in range(1, 5, 1): data1 = getattr(tr_data, str('data_g' + str(i))) # Concatenate the data of all classes other then the class(i) classes_rest = np.where(targets != i)[0] for lv in range(0, np.size(classes_rest), 1): data2_temp = getattr( tr_data, str('data_g' + str(targets[classes_rest[lv]]))) if lv == 0: data2 = np.asarray(data2_temp) else: data2[0] = np.concatenate( [data2[0], data2_temp[0] + np.max(data2[0])], axis=0) data2[1] = np.concatenate([data2[1], data2_temp[1]], axis=1) del data2_temp r_final = 5 # Select what ratio of each class should be present for training dataset. # Make supervectors if ParametersSVM.equal_number_of_sequences == 1: if max(data1[0]) < max(data2[0]): num_of_seq = int(max(data1[0])) else: num_of_seq = int(max(data2[0])) data_shuffle = 1 super_vector1 = make_super_vector( data1[0], data1[1], r_final, num_of_seq, model_ubm, ParametersSVM, data_shuffle, test_flag=0, ) super_vector2 = make_super_vector(data2[0], data2[1], r_final, num_of_seq, model_ubm, ParametersSVM) else: num_of_seq = int(max(data1[0])) super_vector1 = make_super_vector(data1[0], data1[1], r_final, num_of_seq, model_ubm, ParametersSVM) num_of_seq = int(max(data2[0])) super_vector2 = make_super_vector(data2[0], data2[1], r_final, num_of_seq, model_ubm, ParametersSVM) fulltrset_sv = np.concatenate([super_vector1, super_vector2], axis=1) labels = np.concatenate([ np.ones(np.size(super_vector1, 1)), np.zeros(np.size(super_vector2, 1)) ]) del super_vector1, super_vector2 remove_NaNs(fulltrset_sv, labels) print('Building SVM models for grade', i, 'vs_rest') save_file_name_svm = path.join(modeldir_patient_svm, ('SVM_model_' + str(i) + '_rest')) model_svm = make_svm_model_1vsRest(fulltrset_sv, labels) model_svm.rel_factor = r_final fid = open(save_file_name_svm, 'wb') pickle.dump(model_svm, fid) fid.close()
def trainSVM_models_1vs1(model_ubm, tr_data, modeldir_patient_svm, ParametersSVM): """ This function will produce the 1vs1 multiclass SVM models It will first create the supervectors from the data and then feed them to make_svm_model for selecting c parameter and producing a SVM model with best parameter :param model_ubm: UBM model to adapt the sequences. :param tr_data: training data :param modeldir_patient_svm: model directory to save the trained SVM model :param ParametersSVM: parameters to be used for selecting relevance factor, and supervector creation. :return: nothing """ for i in range(1, 5, 1): for j in range(i + 1, 5, 1): data1 = getattr(tr_data, str('data_g' + str(i))) data2 = getattr(tr_data, str('data_g' + str(j))) r_final = 5 # Select what ratio of each class should be present for training dataset. # Make supervectors if ParametersSVM.equal_number_of_sequences == 1: if max(data1[0]) < max(data2[0]): num_of_seq = int(max(data1[0])) else: num_of_seq = int(max(data2[0])) super_vector1 = make_super_vector(data1[0], data1[1], r_final, num_of_seq, model_ubm, ParametersSVM) super_vector2 = make_super_vector(data2[0], data2[1], r_final, num_of_seq, model_ubm, ParametersSVM) else: num_of_seq = int(max(data1[0])) super_vector1 = make_super_vector(data1[0], data1[1], r_final, num_of_seq, model_ubm, ParametersSVM) num_of_seq = int(max(data2[0])) super_vector2 = make_super_vector(data2[0], data2[1], r_final, num_of_seq, model_ubm, ParametersSVM) fulltrset_sv = np.concatenate([super_vector1, super_vector2], axis=1) labels = np.concatenate([ np.ones(np.size(super_vector1, 1)), np.zeros(np.size(super_vector2, 1)) ]) del super_vector1, super_vector2 remove_NaNs(fulltrset_sv, labels) print('Building SVM models for grade', i, 'vs', j) save_file_name_svm = path.join( modeldir_patient_svm, ('SVM_model_' + str(i) + '_' + str(j))) model_svm = make_svm_model(fulltrset_sv, labels) model_svm.rel_factor = r_final fid = open(save_file_name_svm, 'wb') pickle.dump(model_svm, fid) fid.close()
def trainSVM_models_1vsRest(model_ubm, tr_data, modeldir_patient_svm, ParametersSVM): """ This function will produce the 1vsRest SVM models. It will first create the supervectors from the data and then feed them to make_svm_model for selecting c parameter and producing a SVM model with best parameter :param model_ubm: UBM model to adapt the sequences. :param tr_data: training data :param modeldir_patient_svm: model directory to save the trained SVM model :param ParametersSVM: parameters to be used for selecting relevance factor, and supervector creation. :return: nothing """ targets = np.arange(1,ParametersSVM.num_of_classes+1,1) for i in range(1,5,1): data1 = getattr(tr_data, str('data_g' + str(i))) # Concatenate the data of all classes other then the class(i) classes_rest = np.where(targets != i)[0] for lv in range(0,np.size(classes_rest),1): data2_temp=getattr(tr_data, str('data_g' + str(targets[classes_rest[lv]]))) if lv == 0: data2=np.asarray(data2_temp) else: data2[0] = np.concatenate([data2[0], data2_temp[0]+np.max(data2[0])], axis=0) data2[1] = np.concatenate([data2[1], data2_temp[1]], axis=1) del data2_temp r_final = 5 # Select what ratio of each class should be present for training dataset. # Make supervectors if ParametersSVM.equal_number_of_sequences == 1: if max(data1[0]) < max(data2[0]): num_of_seq = int(max(data1[0])) else: num_of_seq = int(max(data2[0])) data_shuffle=1 super_vector1 = make_super_vector(data1[0], data1[1], r_final, num_of_seq, model_ubm, ParametersSVM, data_shuffle, test_flag=0,) super_vector2 = make_super_vector(data2[0], data2[1], r_final, num_of_seq, model_ubm, ParametersSVM) else: num_of_seq = int(max(data1[0])) super_vector1 = make_super_vector(data1[0], data1[1], r_final, num_of_seq, model_ubm, ParametersSVM) num_of_seq = int(max(data2[0])) super_vector2 = make_super_vector(data2[0], data2[1], r_final, num_of_seq, model_ubm, ParametersSVM) fulltrset_sv = np.concatenate([super_vector1, super_vector2], axis=1) labels = np.concatenate([np.ones(np.size(super_vector1, 1)), np.zeros(np.size(super_vector2, 1))]) del super_vector1, super_vector2 remove_NaNs(fulltrset_sv, labels) print('Building SVM models for grade', i, 'vs_rest') save_file_name_svm = path.join(modeldir_patient_svm, ('SVM_model_' + str(i) + '_rest')) model_svm = make_svm_model_1vsRest(fulltrset_sv, labels) model_svm.rel_factor = r_final fid = open(save_file_name_svm,'wb') pickle.dump(model_svm, fid) fid.close()