Пример #1
0
def main(filepath, model_type, output_dir, gpu_index, evaluate, mode):
    songname = filepath.split('/')[-1].split('.')[0]
    model_path = './MSnet/pretrain_model/MSnet_'+str(model_type)

    if gpu_index is not None:
        with torch.cuda.device(gpu_index):
            est_arr = MeExt(filepath, model_type=model_type, model_path=model_path, GPU=True, mode=mode)
    else:
        est_arr = MeExt(filepath, model_type=model_type, model_path=model_path, GPU=False, mode=mode)

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    print('Save the result in '+output_dir+'/'+songname+'.txt')
    np.savetxt(output_dir+'/'+songname+'.txt', est_arr)
    if evaluate is not None:
        import pandas as pd
        from MSnet.utils import melody_eval
        if 'csv' in evaluate:
            ycsv = pd.read_csv(evaluate, names = ["time", "freq"])
            gtt = ycsv['time'].values
            gtf = ycsv['freq'].values
            ref_arr = np.concatenate((gtt[:,None], gtf[:,None]), axis=1)
        elif 'txt' in evaluate:
            ref_arr = np.loadtxt(evaluate)
        else:
            print("Error: Wrong type of ground truth. The file must be '.txt' or '.csv' ")
            return None
        eval_arr = melody_eval(ref_arr, est_arr)
        print(songname, ' | VR: {:.2f}% VFA: {:.2f}% RPA: {:.2f}% RCA: {:.2f}% OA: {:.2f}%'.format(
        eval_arr[0], eval_arr[1], eval_arr[2], eval_arr[3], eval_arr[4]))
Пример #2
0
def main(data_dir, model_type, output_dir, gpu_index, dataset='Mdb_vocal'):
    model_path = './MSnet/pretrain_model/MSnet_' + str(model_type)
    if 'Mdb_vocal' in dataset:
        _, _, songlist = get_split_lists_vocal()
    elif 'Mdb_melody2' in dataset:
        _, _, songlist = get_split_lists()
    else:
        print('Error: Wrong type of dataset, Must be Mdb_vocal or Mdb_melody2')

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    with open(output_dir + '/' + str(dataset) + '_result.csv', 'w',
              newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['Songname', 'VR', 'VFA', 'RPA', 'RCA', 'OA'])
        avg_arr = [0, 0, 0, 0, 0]
        for songname in songlist:
            if 'Mdb_vocal' in dataset:
                filepath = data_dir + '/Audio/' + songname + '/' + songname + '_MIX.wav'
                ypath = data_dir + '/Annotations/Melody_Annotations/MELODY2/' + songname + '_MELODY2.csv'
                lpath = data_dir + 'Annotations/Instrument_Activations/SOURCEID/' + songname + '_SOURCEID.lab'
                ref_arr = select_vocal_track(ypath, lpath)
            elif 'Mdb_melody2' in dataset:
                filepath = data_dir + '/Audio/' + songname + '/' + songname + '_MIX.wav'
                ypath = data_dir + '/Annotations/Melody_Annotations/MELODY2/' + songname + '_MELODY2.csv'
                ref_arr = csv2ref(ypath)
            else:
                print('No add dataset')

            if gpu_index is not None:
                with torch.cuda.device(gpu_index):
                    est_arr = MeExt(filepath,
                                    model_type=model_type,
                                    model_path=model_path,
                                    GPU=True,
                                    mode='std',
                                    gid=gpu_index)
            else:
                est_arr = MeExt(filepath,
                                model_type=model_type,
                                model_path=model_path,
                                GPU=False,
                                mode='std')
            # np.savetxt(output_dir+'/'+songname+'.txt', est_arr)

            eval_arr = melody_eval(ref_arr, est_arr)
            avg_arr += eval_arr
            writer.writerow([
                songname, eval_arr[0], eval_arr[1], eval_arr[2], eval_arr[3],
                eval_arr[4]
            ])
        avg_arr /= len(songlist)
        writer.writerow([
            'Avg', avg_arr[0], avg_arr[1], avg_arr[2], avg_arr[3], avg_arr[4]
        ])