예제 #1
0
def update_plot2D(samples_per_class, x_select, y_select, active_tab, fold_ix,
                  model_path, dataset_ix, sr):
    global X
    global X_pca
    global Y
    global file_names
    global feature_extractor
    print('start visualization')
    if (active_tab == 'tab_visualization'):
        fold_name = dataset.fold_list[fold_ix]
        exp_folder_fold = conv_path(os.path.join(model_path, fold_name))
        scaler_path = os.path.join(exp_folder_fold, 'scaler.pickle')
        scaler = load_pickle(scaler_path)

        dataset_name = options_datasets[dataset_ix]['label']
        params_dataset = params['datasets'][dataset_name]
        folds_train, folds_val, _ = evaluation_setup(
            fold_name, dataset.fold_list, params_dataset['evaluation_mode'])
        print(feature_extractor)
        data_gen_train = DataGenerator(
            dataset,
            feature_extractor,
            folds=folds_train,
            batch_size=params['train']['batch_size'],
            shuffle=True,
            train=False,
            scaler=scaler)
        X_list, Y_list = data_gen_train.get_data()
        file_names = data_gen_train.audio_file_list
        # file_names = data_gen_train.convert_features_path_to_audio_path(
        #     file_names, sr=sr)
        Xt = []
        Yt = []
        for j in range(len(X_list)):
            ix = int(len(X_list[j]) / 2) if len(X_list[j]) > 1 else 0
            Xj = np.expand_dims(X_list[j][ix], 0)
            Yj = np.expand_dims(Y_list[j][ix], 0)
            Xt.append(Xj)
            Yt.append(Yj)
        X = np.concatenate(Xt, axis=0)
        Yt = np.concatenate(Yt, axis=0)
        with graph.as_default():
            model_container.load_model_weights(exp_folder_fold)
            X_emb = model_container.get_intermediate_output(-2, X)
            # output_select

        pca = PCA(n_components=4)
        pca.fit(X_emb)
        X_pca = pca.transform(X_emb)

    print('pca', X_pca.shape, Yt.shape)
    figure2D = generate_figure2D(X_pca,
                                 Yt,
                                 dataset.label_list,
                                 pca_components=[x_select, y_select],
                                 samples_per_class=samples_per_class)
    return [figure2D]
예제 #2
0
def test_load_pickle():
    filename = "./test.pickle"
    obj = [np.zeros((2, 1))]
    _clean(filename)
    save_pickle(obj, filename)
    obj_loaded = load_pickle(filename)
    assert type(obj_loaded) is list
    assert len(obj_loaded) == 1
    assert np.allclose(obj, obj_loaded)
    _clean(filename)
예제 #3
0
def evaluate_model(n_clicks, fold_ix, model_path):
    global X_test
    global X_pca_test
    global file_names_test
    global Y_test
    global predictions
    global data_generator_test
    print('Change tab evaluation')
    # if (active_tab == "tab_evaluation") and (fold_ix is not None):
    if (n_clicks is not None) and (fold_ix is not None):
        print('Start evaluation')
        fold_name = dataset.fold_list[fold_ix]
        exp_folder_fold = conv_path(os.path.join(model_path, fold_name))
        scaler_path = os.path.join(exp_folder_fold, 'scaler.pickle')
        scaler = load_pickle(scaler_path)

        data_generator_test = DataGenerator(
            dataset,
            feature_extractor,
            folds=[fold_name],
            batch_size=params['train']['batch_size'],
            shuffle=True,
            train=False,
            scaler=scaler)
        print('Loading data...')
        X_test, Y_test = data_generator_test.get_data()
        print('Done')
        print(len(X_test), len(Y_test))
        with graph.as_default():
            model_container.load_model_weights(exp_folder_fold)
            results = model_container.evaluate((X_test, Y_test),
                                               label_list=dataset.label_list)
        results = results['classification'].results()

        accuracy = results['overall']['accuracy']
        class_wise = results['class_wise']
        metrics = []
        for label in dataset.label_list:
            metrics.append(class_wise[label]['accuracy']['accuracy'])
        print(metrics)
        figure_metrics = generate_figure_metrics(dataset.label_list, metrics)
        msg = "Accuracy in fold %s is %1.2f" % (fold_name, accuracy)
        return [msg, figure_metrics]

    return ['Pa']
예제 #4
0
def main():
    # Parse arguments
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument(
        '-d',
        '--dataset',
        type=str,
        help='dataset name (e.g. UrbanSound8k, ESC50, URBAN_SED, SONYC_UST)',
        default='UrbanSound8k')
    parser.add_argument(
        '-f',
        '--features',
        type=str,
        help='features name (e.g. Spectrogram, MelSpectrogram, Openl3)',
        default='MelSpectrogram')
    parser.add_argument('-p',
                        '--path',
                        type=str,
                        help='path to the parameters.json file',
                        default='../')
    parser.add_argument(
        '-m',
        '--model',
        type=str,
        help='model name (e.g. MLP, SB_CNN, SB_CNN_SED, A_CRNN, VGGish)',
        default='SB_CNN')
    parser.add_argument('-fold',
                        '--fold_name',
                        type=str,
                        help='fold name',
                        default='fold1')
    parser.add_argument('-s',
                        '--models_path',
                        type=str,
                        help='path to load the trained model',
                        default='../trained_models')
    parser.add_argument(
        '-ft',
        '--fine_tuning',
        type=str,
        help='fine-tuned dataset name (e.g. UrbanSound8k, ESC50, URBAN_SED)',
    )
    args = parser.parse_args()

    print(__doc__)

    if args.dataset not in get_available_datasets():
        raise AttributeError('Dataset not available')

    if args.features not in get_available_features():
        raise AttributeError('Features not available')

    if args.model not in get_available_models():
        raise AttributeError('Model not available')

    # Get parameters
    parameters_file = os.path.join(args.path, 'parameters.json')
    params = load_json(parameters_file)
    params_features = params['features']

    dataset_name = (args.dataset
                    if args.fine_tuning is None else args.fine_tuning)
    params_dataset = params['datasets'][dataset_name]

    # Get and init dataset class
    dataset_class = get_available_datasets()[dataset_name]
    dataset_path = os.path.join(args.path, params_dataset['dataset_path'])
    dataset = dataset_class(dataset_path)

    if args.fold_name not in dataset.fold_list:
        raise AttributeError('Fold not available')

    # Get and init feature class
    features_class = get_available_features()[args.features]
    features = features_class(
        sequence_time=params_features['sequence_time'],
        sequence_hop_time=params_features['sequence_hop_time'],
        audio_win=params_features['audio_win'],
        audio_hop=params_features['audio_hop'],
        sr=params_features['sr'],
        **params_features[args.features])

    # Check if features were extracted
    if not features.check_if_extracted(dataset):
        print('Extracting features ...')
        features.extract(dataset)
        print('Done!')

    # Set paths
    if args.fine_tuning is None:
        dataset_path = args.dataset
    else:
        dataset_path = args.dataset + '_ft_' + args.fine_tuning

    model_folder = os.path.join(args.models_path, args.model, dataset_path)
    exp_folder = os.path.join(model_folder, args.fold_name)

    # Load scaler
    scaler_file = os.path.join(exp_folder, 'scaler.pickle')
    scaler = load_pickle(scaler_file)

    # Init data generator
    data_gen_test = DataGenerator(dataset,
                                  features,
                                  folds=[args.fold_name],
                                  batch_size=params['train']['batch_size'],
                                  shuffle=False,
                                  train=False,
                                  scaler=scaler)

    # Load model and best weights
    model_class = get_available_models()[args.model]
    metrics = ['classification']
    if dataset_name in sed_datasets:
        metrics = ['sed']
    if args.dataset in tagging_datasets:
        metrics = ['tagging']

    model_container = model_class(model=None,
                                  model_path=model_folder,
                                  metrics=metrics)
    model_container.load_model_weights(exp_folder)

    kwargs = {}
    if dataset_name in sed_datasets:
        kwargs = {
            'sequence_time_sec': params_features['sequence_hop_time'],
            'metric_resolution_sec': 1.0
        }
    results = model_container.evaluate(data_gen_test,
                                       label_list=dataset.label_list,
                                       **kwargs)

    print(results[metrics[0]])
예제 #5
0
def generate_demo(n_clicks, list_of_contents, fold_ix, model_path,
                  list_of_names, list_of_dates, sr):
    print('generate demo')
    ctx = dash.callback_context
    button_id = ctx.triggered[0]['prop_id'].split('.')[0]
    print(button_id, n_clicks)
    if (n_clicks is not None) & (button_id == 'btn_run_demo'):
        fold_name = dataset.fold_list[fold_ix]
        exp_folder_fold = conv_path(os.path.join(model_path, fold_name))
        scaler_path = os.path.join(exp_folder_fold, 'scaler.pickle')
        scaler = load_pickle(scaler_path)

        data_generator_test = DataGenerator(
            dataset,
            feature_extractor,
            folds=[fold_name],
            batch_size=params['train']['batch_size'],
            shuffle=True,
            train=False,
            scaler=scaler)

        n_files = len(data_generator_test.audio_file_list)
        ix = np.random.randint(n_files)

        fold_name = dataset.fold_list[fold_ix]
        exp_folder_fold = conv_path(os.path.join(model_path, fold_name))

        X_features, Y_file = data_generator_test.get_data_from_file(ix)

        with graph.as_default():
            model_container.load_model_weights(exp_folder_fold)
            Y_features = model_container.model.predict(X_features)

        fig_demo = generate_figure_features(X_features, Y_features,
                                            dataset.label_list)

        audio_file = data_generator_test.audio_file_list[ix]
        audio_data, sr = sf.read(audio_file['file_original'])

        class_ix = np.argmax(Y_file[0])
        file_label = dataset.label_list[class_ix]

        return [
            fig_demo, {
                'autoPlay': False,
                'src': encode_audio(audio_data, sr)
            },
            'ground-truth: %s' % file_label
        ]

    if button_id == 'upload-data':
        fold_name = dataset.fold_list[fold_ix]
        exp_folder_fold = conv_path(os.path.join(model_path, fold_name))
        scaler_path = os.path.join(exp_folder_fold, 'scaler.pickle')
        scaler = load_pickle(scaler_path)

        filename = conv_path('upload.wav')
        data = list_of_contents.encode("utf8").split(b";base64,")[1]
        with open(filename, "wb") as fp:
            fp.write(base64.decodebytes(data))

        X_feat = feature_extractor.calculate(filename)
        X_feat = scaler.transform(X_feat)
        with graph.as_default():
            Y_t = model_container.model.predict(X_feat)

        label_list = dataset.label_list
        figure_features = generate_figure_features(X_feat, Y_t, label_list)
        return [
            figure_features, {
                'autoPlay': False,
                'src': list_of_contents
            }, ""
        ]

    X_feat = np.zeros((10, 128, 64))
    Y_t = np.zeros((10, 10))
    label_list = [] * 10
    figure_features = generate_figure_features(X_feat, Y_t, label_list)

    return [figure_features, {'autoPlay': False, 'src': ""}, ""]