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 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 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)
tr_X = list(np.zeros(len(feature),dtype='int')) dimy = list(np.zeros(len(feature),dtype='int')) for i in range(len(feature)): tr_X[i], tr_y = GetAllData( dev_fd+'/'+feature[i], label_csv, agg_num, hop ) print(tr_X[i].shape) print(tr_y.shape) for i in range(len(feature)): dimy[i]=tr_X[i].shape[-1] #aud_utils.check_dimension(feature[i],dimy[i],'defaults.yaml') #tr_X=aud_utils.equalise(tr_X) for i in range(len(feature)): tr_X[i]=aud_utils.mat_3d_to_nd(model,tr_X[i]) print(tr_X[i].shape) dimx=tr_X[0].shape[-2] if prep=='dev': cross_validation=True else: cross_validation=False miz=aud_model.Functional_Model(input_neurons=input_neurons,cross_validation=cross_validation,dropout1=dropout1, act1=act1,act2=act2,act3=act3,nb_filter = nb_filter, filter_length=filter_length, num_classes=num_classes, model=model,dimx=dimx,dimy=dimy) np.random.seed(68) if cross_validation:
file_logger = FileLogger('out_{}.tsv'.format(model), ['step', 'tr_loss', 'te_loss', 'tr_acc', 'te_acc']) ############# EXTRACT FEATURES ##################### dropout, act1, act2,act3, input_neurons, epochs, batchsize, num_classes, _, _,\ loss,nb_filter, filter_length,pool_size, optimizer=seth.get_parameters() ############# LOAD DATA ########################### tr_X, tr_y = seth.get_train_data('logmel_trainx2.cpkl') v_X, v_y = seth.get_val_data('logmel_testx2.cpkl') dimx = tr_X.shape[-2] dimy = tr_X.shape[-1] tr_X = aud_utils.mat_3d_to_nd(model, tr_X) v_X = aud_utils.mat_3d_to_nd(model, v_X) print("Functional_model {}".format(model)) print("Activation 1 {} 2 {} 3 {}".format(act1, act2, act3)) print("Dropout {}".format(dropout)) print("Kernels {} Size {} Poolsize {}".format(nb_filter, filter_length, pool_size)) print("Loss {} Optimizer {}".format(loss, optimizer)) #train_x=np.array(tr_X) #train_y=np.array(tr_y) #test_x=np.array(v_X) #test_y=np.array(v_y) ##train_x, test_x, train_y, test_y = train_test_split( tr_X, tr_y, test_size=0.2, random_state=42) #print("Evaluation mode")