def test(md, csv_file, model): # load name of wavs to be classified with open(csv_file, 'rb') as f: reader = csv.reader(f) lis = list(reader) # do classification for each file y_pred = [] te_y = [] for li in lis: na = li[1] #audio evaluation name fe_path = eva_fd + '/' + feature + '/' + na + '.f' info_path = label_csv + '/' + na + '.csv' with open(info_path, 'rb') as g: reader2 = csv.reader(g) lis2 = list(reader2) tags = lis2[-2][1] y = np.zeros(num_classes) for ch in tags: y[lb_to_id[ch]] = 1 te_y.append(y) X0 = cPickle.load(open(fe_path, 'rb')) X0 = aud_utils.mat_2d_to_3d(X0, agg_num, hop) X0 = aud_utils.mat_3d_to_nd(model, X0) # predict p_y_pred = md.predict(X0) p_y_pred = np.mean(p_y_pred, axis=0) # shape:(n_label) y_pred.append(p_y_pred) return np.array(te_y), np.array(y_pred)
def GetAllData(fe_fd, csv_file, agg_num, hop): """ Input: Features folder(String), CSV file(String), agg_num(Integer), hop(Integer). Output: Loaded features(Numpy Array) and labels(Numpy Array). Loads all the features saved as pickle files. """ # read csv with open( csv_file, 'rb') as f: reader = csv.reader(f) lis = list(reader) # init list X3d_all = [] y_all = [] i=0 for li in lis: # load data [na, lb] = li[0].split('\t') na = na.split('/')[1][0:-4] path = fe_fd + '/' + na + '.f' X = aud_feature.load(path) # reshape data to (n_block, n_time, n_freq) i+=1 X3d = aud_utils.mat_2d_to_3d( X, agg_num, hop ) X3d_all.append( X3d ) y_all += [ lb_to_id[lb] ] * len( X3d ) print "Features loaded",i print 'All files loaded successfully' # concatenate list to array X3d_all = np.concatenate( X3d_all ) y_all = np.array( y_all ) return X3d_all, y_all
def get_train_data(self): """ Loads all the features saved as pickle files. Input: Features folder(str), CSV file(str) Output: Loaded features(np array) and labels(np array). """ feature = self.feature fe_fd = cfg.dev_fd + '/' + feature csv_file = cfg.meta_train_csv agg_num = self.agg_num hop = self.hop # read csv with open(csv_file, 'rb') as f: reader = csv.reader(f) lis = list(reader) # init list X3d_all = [] y_all = [] i = 0 for li in lis: # load data na = li[1] path = fe_fd + '/' + na + '.f' info_path = cfg.label_csv + '/' + na + '.csv' with open(info_path, 'rb') as g: reader2 = csv.reader(g) lis2 = list(reader2) tags = lis2[-2][1] y = np.zeros(len(cfg.labels)) for ch in tags: y[cfg.lb_to_id[ch]] = 1 X = aud_feature.load(path) # reshape data to (n_block, n_time, n_freq) i += 1 X3d = aud_utils.mat_2d_to_3d(X, agg_num, hop) X3d_all.append(X3d) y_all += [y] * len(X3d) print "Features loaded", i print 'All files loaded successfully' # concatenate list to array X3d_all = np.concatenate(X3d_all) y_all = np.array(y_all) return X3d_all, y_all
def get_test_predictions(self, md): model = self.model feature = self.feature fe_fd = cfg.eva_fd + '/' + feature csv_file = cfg.txt_eva_path agg_num = self.agg_num hop = self.hop eva_file = cfg.eva_file # load name of wavs to be classified with open(csv_file, 'rb') as f: reader = csv.reader(f) lis = list(reader) # do classification for each file names = [] pred_lbs = [] for li in lis: names.append(li[0]) na = li[0][6:-4] #audio evaluation name fe_path = fe_fd + '/' + na + '.f' X0 = cPickle.load(open(fe_path, 'rb')) X0 = aud_utils.mat_2d_to_3d(X0, agg_num, hop) X0 = aud_utils.mat_3d_to_nd(model, X0) # predict p_y_preds = md.predict(X0) # probability, size: (n_block,label) preds = np.argmax(p_y_preds, axis=-1) # size: (n_block) b = scipy.stats.mode(preds) pred = int(b[0]) pred_lbs.append(cfg.id_to_lb[pred]) pred = [] # write out result for i1 in xrange(len(names)): fname = names[i1] + '\t' + pred_lbs[i1] + '\n' pred.append(fname) print 'write out finished!' truth = open(eva_file, 'r').readlines() pred = [i.split('\t')[1].split('\n')[0] for i in pred] truth = [i.split('\t')[1] for i in truth] pred.sort() truth.sort() return truth, pred
def GetAllData(fe_fd, csv_file, agg_num, hop): """ Input: Features folder(String), CSV file(String), agg_num(Integer), hop(Integer). Output: Loaded features(Numpy Array) and labels(Numpy Array). Loads all the features saved as pickle files. """ # read csv with open(csv_file, 'rb') as f: reader = csv.reader(f) lis = list(reader) # init list X3d_all = [] y_all = [] i = 0 for li in lis: # load data na = li[1] path = fe_fd + '/' + na + '.f' info_path = label_csv + '/' + na + '.csv' with open(info_path, 'rb') as g: reader2 = csv.reader(g) lis2 = list(reader2) tags = lis2[-2][1] y = np.zeros(len(labels)) for ch in tags: y[lb_to_id[ch]] = 1 try: X = cPickle.load(open(path, 'rb')) except Exception as e: print 'Error while parsing', path continue # reshape data to (n_block, n_time, n_freq) i += 1 X3d = aud_utils.mat_2d_to_3d(X, agg_num, hop) X3d_all.append(X3d) y_all += [y] * len(X3d) print "Features loaded", i print 'All files loaded successfully' # concatenate list to array X3d_all = np.concatenate(X3d_all) y_all = np.array(y_all) return X3d_all, y_all
def get_test_predictions(self, md): model = self.model feature = self.feature fe_fd = cfg.eva_fd + '/' + feature csv_file = cfg.meta_test_csv agg_num = self.agg_num hop = self.hop # load name of wavs to be classified with open(csv_file, 'rb') as f: reader = csv.reader(f) lis = list(reader) # do classification for each file y_pred = [] te_y = [] for li in lis: na = li[1] #audio evaluation name fe_path = fe_fd + '/' + na + '.f' info_path = cfg.label_csv + '/' + na + '.csv' with open(info_path, 'rb') as g: reader2 = csv.reader(g) lis2 = list(reader2) tags = lis2[-2][1] y = np.zeros(len(cfg.labels)) for ch in tags: y[cfg.lb_to_id[ch]] = 1 te_y.append(y) X0 = cPickle.load(open(fe_path, 'rb')) X0 = aud_utils.mat_2d_to_3d(X0, agg_num, hop) X0 = aud_utils.mat_3d_to_nd(model, X0) # predict p_y_pred = md.predict(X0) p_y_pred = np.mean(p_y_pred, axis=0) # shape:(n_label) y_pred.append(p_y_pred) return np.array(te_y), np.array(y_pred)
def get_train_data(self): """ Loads all the features saved as pickle files. Input: Features folder(str), CSV file(str) Output: Loaded features(np array) and labels(np array). """ feature = self.feature fe_fd = cfg.dev_fd + '/' + feature csv_file = cfg.label_csv agg_num = self.agg_num hop = self.hop # read csv with open(csv_file, 'rb') as f: reader = csv.reader(f) lis = list(reader) # init list X3d_all = [] y_all = [] i = 0 for li in lis: # load data [na, lb] = li[0].split('\t') na = na.split('/')[1][0:-4] path = fe_fd + '/' + na + '.f' X = aud_feature.load(path) # reshape data to (n_block, n_time, n_freq) i += 1 X3d = aud_utils.mat_2d_to_3d(X, agg_num, hop) X3d_all.append(X3d) y_all += [cfg.lb_to_id[lb]] * len(X3d) print "Features loaded", i print 'All files loaded successfully' # concatenate list to array X3d_all = np.concatenate(X3d_all) y_all = np.array(y_all) return X3d_all, y_all