def segmentation(args): segment_index_arr, smallest_segments, root_dir, links, active_nodes, coarsening_mode, percent, \ active_nodes_arr, min_n, max_n, G_size = args for segment_index in segment_index_arr: i = 0 segment = smallest_segments[segment_index] components_active_nodes = active_nodes_arr[segment_index] root_dir_component = root_dir + percent + '/' score_dir = root_dir_component + 'score' + str(segment[0]) + '_' + str( segment[1]) + '.txt' remain_factor = round(1 - float(percent) / 100, 2) if remain_factor * max_n < min_n: goal_szie = round(remain_factor * max_n) else: goal_szie = float(min_n) G_p = int((1 - (goal_szie / G_size[segment_index])) * 100) # print('G_p: ' + str(G_p)) # print('goal_szie: ' + str(goal_szie)) # print('G_size: ' + str(G_size[segment_index])) # seg_score_dir, seg_active_dir, coarse_input_dir, coarse_output_dir, map_dir, temp_dir = get_comp_seg_dir( root_dir_component, i, segment) sub_active_node = get_sub_link_score(components_active_nodes, score_dir, seg_score_dir, seg_active_dir) start = time.time() coarse_net(coarsening_mode, coarse_input_dir, seg_score_dir, str(G_p), coarse_output_dir, map_dir, temp_dir) elapsed = (time.time() - start) # print('end of coarse_net') gephi_graph = root_dir + percent + '/graph' + str(i) + '_' + str( segment[0]) + '_' + str(segment[1]) + '.txt' gephi_nodes = root_dir + percent + '/nodes' + str(i) + '_' + str( segment[0]) + '_' + str(segment[1]) + '.txt' # print('get graph') coarse_link, coarse_node_label, coarse_link_weight, coarse_labels = getgraph.main( coarse_output_dir, map_dir, seg_active_dir, gephi_graph, gephi_nodes) # coarse_link, coarse_node_label, coarse_link_weight, coarse_labels = read_gephi(gephi_graph, gephi_nodes, # coarsening_mode, comp_links_dir, # comp_nodes_dir, seg_active_dir) # print('component_feature_time') component_feature_time = fe.extract_feature(coarse_link, coarse_node_label, coarse_link_weight, coarsening_mode) prefix = root_dir + percent + '/' if not os.path.exists(prefix): os.makedirs(prefix) feature_dir = prefix + 'feature' + str(segment_index) + '.txt' with open(feature_dir, 'w') as f: for items in component_feature_time: f.write(str(items) + '\t') f.close() # print('parallel finished..') return 0
def test_recog(): with open(CLASSIFIER_PATH, 'rb') as file: svm_model, class_names = pickle.load(file) dataset = get_dataset('Dataset/test') paths, labels = get_image_paths_and_labels(dataset) for i in range(len(paths)): img = cv2.imread(paths[i]) img = img[..., ::-1] emb_array = extract_feature(img, trained_model) predictions = svm_model.predict_proba(emb_array) best_class_indices = np.argmax(predictions, axis=1)[0] best_name = class_names[best_class_indices] print(best_name, labels[i])
def test_mood_of_song(path): print(path) #getting song name song_name = '' for char in path[::-1]: if char == '\\': break else: song_name = char + song_name messagebox.showinfo( "Feature Extraction", '\n Extracting features of selected song : {0}'.format(song_name)) print('\n Extracting features of selected song : {0}'.format(song_name)) print('....') print('....') print('....') print('....') feature_extraction.extract_feature(path) #unpickling our object infile = open('logistic_classifier', 'rb') new_clf = pickle.load(infile) infile.close() #finding mood of our song original_dataset = pd.read_csv('dataset.csv').values[:, 2:] original_dataset = original_dataset new_song_data = pd.read_csv('test_song_mood.csv').values[:, 2:] X = np.concatenate((original_dataset, new_song_data)) #scaling our features scaler = StandardScaler() X = scaler.fit_transform(X) mood = new_clf.predict(X[-1:]) print('\nMood of Song is : {0}'.format(mood)) return mood
def segmentation(args): segment_index_arr, smallest_segments, root_dir, data_dir, active_nodes, coarsening_mode = args score_dir = data_dir + 'score.txt' for segment_index in segment_index_arr: i = 0 segment = smallest_segments[segment_index] try: components_active_nodes = active_nodes[segment[0]] except KeyError: components_active_nodes = [] seg_score_dir, seg_active_dir, coarse_input_dir, coarse_output_dir, map_dir, temp_dir = get_comp_seg_dir( data_dir, i, segment) sub_active_node = get_sub_link_score(components_active_nodes, score_dir, seg_score_dir, seg_active_dir) start = time.time() coarse_net(coarsening_mode, coarse_input_dir, seg_score_dir, percent, coarse_output_dir, map_dir, temp_dir) elapsed = (time.time() - start) # print('end of coarse_net') gephi_graph = root_dir + percent + '/graph' + str(i) + '_' + str( segment[0]) + '_' + str(segment[1]) + '.txt' gephi_nodes = root_dir + percent + '/nodes' + str(i) + '_' + str( segment[0]) + '_' + str(segment[1]) + '.txt' # print('get graph') coarse_link, coarse_node_label, coarse_link_weight, coarse_labels = getgraph.main( coarse_output_dir, map_dir, seg_active_dir, gephi_graph, gephi_nodes) # comp_links_dir = data_dir + '/coarse.txt' # 'newInx_links.txt' # comp_nodes_dir = data_dir + 'newInx_nodes.txt' # coarse_link, coarse_node_label, coarse_link_weight, coarse_labels = read_gephi(gephi_graph, gephi_nodes, # coarsening_mode, comp_links_dir, # comp_nodes_dir, seg_active_dir) # print('component_feature_time') component_feature_time = fe.extract_feature(coarse_link, coarse_node_label, coarse_link_weight, coarsening_mode) prefix = root_dir + percent + '/' if not os.path.exists(prefix): os.makedirs(prefix) feature_dir = prefix + 'feature' + str(segment_index) + '.txt' with open(feature_dir, 'w') as f: for items in component_feature_time: f.write(str(items) + '\t') f.close() # print('parallel finished..') return 0
def test_recog(): with open(args.classifier_path, 'rb') as file: svm_model, class_names = pickle.load(file) dataset = get_dataset('data/processed_data/validation') paths, labels = get_image_paths_and_labels(dataset) for i in range(len(paths)): img = cv2.imread(paths[i]) img = img[..., ::-1] emb_array = extract_feature(img, trained_model) predictions = svm_model.predict_proba(emb_array) best_class_indices = np.argmax(predictions, axis=1)[0] best_name = class_names[best_class_indices] yhat = class_names[labels[i]] print(best_name, yhat)
continue frame = imutils.resize(frame, width=720) boxes, labels, probs = face_detect.predict( frame, args.candidate_size / 2, args.threshold) print(boxes.size(0)) for i in range(boxes.size(0)): try: box = boxes[i, :] box = box.numpy() box = box.astype(int) cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), (0, 0, 255), 2) cropped_face = frame[box[1]:box[3], box[0]:box[2]] emb_array = extract_feature(cropped_face, face_emb) predictions = svm_model.predict_proba(emb_array) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] if best_class_probabilities > 0.50: name = class_names[best_class_indices[0]] else: name = "Unknown" cv2.putText(frame, "ID: " + name, (box[0], box[1] - 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 100), 2) fpstext = "Inference speed: " + str(1/(time.time() - t0))[:5] + " FPS" cv2.putText(frame, fpstext,
from feature_extraction import extract_feature from regression_model import Hotness from regression_model import create_feature # Extract feature from audio extract_feature('train') extract_feature('test') # Calculate hotness of artists and composers hotness = Hotness() hotness.compute() # Create feature vector create_feature('train') create_feature('test')
''' File: spikie_train.py Author: Congwen Yang Date: 07/11/2018 - convert those features into spike trains ''' from feature_extraction import extract_feature import numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt #get proprocessed feature matrix p1 = "training_data.p" p2 = "testing_data.p" spectrum_all_1 = extract_feature(p1) spectrum_all_2 = extract_feature(p2) #set constance parameters in the regular spiking neruron Vrest = -60 Vth = -40 Vpeak = 35 C = 100 K = 0.7 detla_T = 0.1 a = 0.03 b = -2 c = -50 d = 100 Uo = 0 T = 200
def main(argv): genres = ['50s', '60s', '70s', '80s', '90s'] # 'country', 'past_century' feature_names = ['unigram', 'bigram', 'trigram', 'interval'] # powerset of features (2^4 = 16) p_set = list( itertools.chain.from_iterable( itertools.combinations(feature_names, r) for r in range(len(feature_names) + 1))) # preprocess all midi's for genre in genres: # build list of filenames filenames = [] for ext in ['.mid', '.kar']: for f in os.listdir('data/' + genre + '/'): if f.endswith(ext): filenames.append(os.path.join('data/' + genre + '/', f)) print("preprocessing " + genre + " music files...") for filename in filenames: base = os.path.basename(filename) name = os.path.splitext(base) # preprocess midi if it doesn't have a corresponding .p file if not os.path.exists('preprocess_pickle/' + genre + '/' + name[0] + '.p'): print("\tpreprocessing " + name[0] + "...", end='') # read the midi file pattern = midi.read_midifile(filename) # preprocess the midi file out = pre.preprocess(pattern) # pickle the output pickle.dump( out, open("preprocess_pickle/" + genre + "/" + name[0] + ".p", "wb")) print(" done.") # otherwise skip else: print("\tskipping " + name[0] + ".") continue # build list of filenames filenames = [] for genre in genres: for f in os.listdir('preprocess_pickle/' + genre + '/'): if f.endswith('.p'): filenames.append( os.path.join('preprocess_pickle/' + genre + '/', f)) # make label vector labels = [] for genre in genres: n = len( glob.glob(os.path.join('preprocess_pickle/' + genre + '/', '*.p'))) labels.extend([genre] * n) X = numpy.array(filenames) y = numpy.array(labels) kf = KFold(len(y), n_folds=10, shuffle=True) # cross validation here results_m = [] for train_index, test_index in kf: X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index] # extract features for both sets of data for s in ['training', 'test']: if s == 'training': fs = X_train else: fs = X_test # extract features for feature in feature_names: # extract feature print("extracting " + feature + " feature for " + s + " set...", end='') # extract top m n-grams (and intervals) output = fe.extract_feature(fs, feature, s) # pickle the output pickle.dump( output, open("feature_pickle/" + s + "/" + feature + ".p", "wb")) print(" done.") # put together sets of features for feature_set in p_set[1:]: print("putting together the " + ", ".join(feature_set) + " feature set...", end='') feature_mat = [] for feature in feature_set: feature_vec = pickle.load( open("./feature_pickle/" + s + '/' + feature + ".p", "rb")) feature_mat.extend(feature_vec) # transpose feature_mat_t = map(lambda *a: list(a), *feature_mat) # pickle the output pickle.dump( feature_mat_t, open( "feature_set_pickle/" + s + "/" + "_".join(feature_set) + ".p", "wb")) print(" done.") models = ["svm", "logistic regression", "bayes", "lda"] # use the training set to train models for model in models: for feature_set in p_set[1:]: print("training " + model + " model using the " + ", ".join(feature_set) + " feature set...", end='') # load pickled feature set (could have easily not pickled it and just made it here...) feature_mat = pickle.load( open( "feature_set_pickle/training/" + "_".join(feature_set) + ".p", "rb")) # train model clf = train_model(model, feature_mat, y_train) # pickle the output pickle.dump( clf, open( 'model_pickle/' + model + '/' + "_".join(feature_set) + '.p', "wb")) print(" done.") # evaluate the models on the test set results = [] for model in models: for feature_set in p_set[1:]: # load pickled feature model clf = pickle.load( open( "model_pickle/" + model + '/' + "_".join(feature_set) + ".p", "rb")) # load pickled feature set feature_mat = pickle.load( open( "feature_set_pickle/test/" + "_".join(feature_set) + ".p", "rb")) # get result y_t, y_p, c_m = predict_genre(clf, feature_mat, y_test) # keep track of results internally results.append((model, feature_set, (y_t, y_p, c_m))) results_m.append(results) # final output results_t = map(lambda *a: list(a), *results_m) out = [] for t in results_t: y_ts = [] y_ps = [] conf_m = numpy.zeros((len(genres), len(genres))) for model, feature_set, (y_t, y_p, c_m) in t: y_ts.extend(y_t) y_ps.extend(y_p) conf_m = conf_m + c_m acc = accuracy_score(y_ts, y_ps) f1 = f1_score(y_ts, y_ps, average='weighted') out.append((t[0][0], t[0][1], (acc, f1, conf_m))) for exp in out: print(exp)