def test_get_data(): # train=True data_generator = DataGenerator(dataset, feature_extractor, folds=['all'], batch_size=2, shuffle=False) X, Y = data_generator.get_data() assert np.allclose(X, np.concatenate((X_gt[0], X_gt[1], X_gt[2]), axis=0)) assert np.allclose(Y, np.concatenate((Y_gt[0], Y_gt[1], Y_gt[2]), axis=0)) # train=False data_generator = DataGenerator(dataset, feature_extractor, folds=['all'], batch_size=2, shuffle=False, train=False) X, Y = data_generator.get_data() assert type(X) is list assert type(Y) is list assert len(X) == 3 assert len(Y) == 3 for j in range(3): assert np.allclose(X[j], X_gt[j]) assert np.allclose(Y[j], Y_gt[j])
def test_get_data_batch(): data_generator = DataGenerator(dataset, feature_extractor, folds=['all'], batch_size=2, shuffle=False) # check length assert len(data_generator) == 2 # Batch 1 X1, Y1 = data_generator.get_data_batch(0) assert X1.shape[0] == len(X_gt[0]) + len(X_gt[1]) assert X1.shape[0] == Y1.shape[0] assert np.allclose(Y1, np.concatenate((Y_gt[0], Y_gt[1]), axis=0)) assert np.allclose(X1, np.concatenate((X_gt[0], X_gt[1]), axis=0)) # Batch 2 X2, Y2 = data_generator.get_data_batch(1) assert X2.shape[0] == len(X_gt[2]) assert X2.shape[0] == Y2.shape[0] assert np.allclose(Y2, Y_gt[2]) assert np.allclose(X2, X_gt[2]) # Train=False data_generator = DataGenerator(dataset, feature_extractor, folds=['all'], batch_size=2, shuffle=False, train=False) # check length assert len(data_generator) == 2 # Batch 1 X1, Y1 = data_generator.get_data_batch(0) assert type(X1) is list assert type(Y1) is list assert len(X1) == 2 assert len(Y1) == 2 assert np.allclose(X1[0], X_gt[0]) assert np.allclose(Y1[0], Y_gt[0]) assert np.allclose(X1[1], X_gt[1]) assert np.allclose(Y1[1], Y_gt[1]) # Batch 2 X2, Y2 = data_generator.get_data_batch(1) assert type(X2) is list assert type(Y2) is list assert len(X2) == 1 assert len(Y2) == 1 assert np.allclose(X2[0], X_gt[2]) assert np.allclose(Y2[0], Y_gt[2])
def test_convert_audio_path_to_features_path(): data_generator = DataGenerator(dataset, feature_extractor, folds=['all'], shuffle=False) features_path = os.path.join(dataset_path, 'features') audio_path = os.path.join(dataset_path, 'audio') audio_file = os.path.join(audio_path, 'test.wav') features_file = data_generator.convert_audio_path_to_features_path( audio_file, features_path) assert features_file == os.path.join(features_path, 'test.npy') # subfolder='original' features_file = data_generator.convert_audio_path_to_features_path( audio_file, features_path, subfolder='original') assert features_file == os.path.join(features_path, 'original', 'test.npy') # list audio_file = [ os.path.join(features_path, 'test1.wav'), os.path.join(features_path, 'test2.wav') ] features_file = data_generator.convert_audio_path_to_features_path( audio_file, features_path) assert type(features_file) is list assert len(features_file) == 2 assert features_file[0] == os.path.join(features_path, 'test1.npy') assert features_file[1] == os.path.join(features_path, 'test2.npy')
def test_convert_features_path_to_audio_path(): data_generator = DataGenerator(dataset, feature_extractor, folds=['all'], shuffle=False) features_path = os.path.join(dataset_path, 'features') audio_path = os.path.join(dataset_path, 'audio') features_file = os.path.join(features_path, 'test.npy') audio_file = data_generator.convert_features_path_to_audio_path( features_file, features_path) assert audio_file == os.path.join(audio_path, 'test.wav') # sr=22050 sr = 22050 audio_path = os.path.join(dataset_path, 'audio22050') features_file = os.path.join(features_path, 'test.npy') audio_file = data_generator.convert_features_path_to_audio_path( features_file, features_path, sr=sr) assert audio_file == os.path.join(audio_path, 'test.wav') # list features_file = [ os.path.join(features_path, 'test1.npy'), os.path.join(features_path, 'test2.npy') ] audio_path = os.path.join(dataset_path, 'audio') audio_file = data_generator.convert_features_path_to_audio_path( features_file, features_path) assert type(audio_file) is list assert len(audio_file) == 2 assert audio_file[0] == os.path.join(audio_path, 'test1.wav') assert audio_file[1] == os.path.join(audio_path, 'test2.wav')
def test_paths_remove_aug_subfolder(): data_generator = DataGenerator(dataset, feature_extractor, folds=['all'], shuffle=False) audio_path = os.path.join(dataset_path, 'audio') audio_file = os.path.join(audio_path, 'original', 'test.wav') new_audio_file = data_generator.paths_remove_aug_subfolder(audio_file) assert new_audio_file == os.path.join(audio_path, 'test.wav')
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]
def test_init(): # not a Dataset with pytest.raises(AttributeError): data_generator = DataGenerator("dataset", feature_extractor, folds=['all']) # not a downloaded dataset download_file = os.path.join(dataset_path, 'download.txt') _clean(download_file) with pytest.raises(AttributeError): data_generator = DataGenerator(dataset, feature_extractor, folds=['all']) dataset.set_as_downloaded() # not a FeatureExtractor with pytest.raises(AttributeError): data_generator = DataGenerator(dataset, "feature_extractor", folds=['all']) # not extracted features features_path = feature_extractor.get_features_path(dataset) _clean(features_path) with pytest.raises(AttributeError): data_generator = DataGenerator(dataset, feature_extractor, folds=['all']) feature_extractor.extract(dataset) # check audio_file_list data_generator = DataGenerator(dataset, feature_extractor, folds=['all'], shuffle=False) audio_path = dataset.audio_path assert len(data_generator.audio_file_list) == len(audio_files) for filename, afl in zip(audio_files, data_generator.audio_file_list): assert afl['file_original'] == os.path.join(audio_path, filename) assert afl['sub_folder'] == 'original'
def test_get_data_from_file(): # train=True data_generator = DataGenerator(dataset, feature_extractor, folds=['all'], batch_size=2, shuffle=False) for j in range(3): X, Y = data_generator.get_data_from_file(j) assert np.allclose(X, X_gt[j]) assert np.allclose(Y, Y_gt[j])
def test_transform(): # minmax X1 = np.random.randn(100, 32, 128) scaler = Scaler(normalizer="minmax") scaler.fit(X1) X1_scaled = scaler.transform(X1) assert np.amin(X1_scaled) == -1.0 assert np.amax(X1_scaled) == 1.0 # standard X2 = np.random.randn(100, 32, 128) scaler = Scaler(normalizer="standard") scaler.fit(X2) X2_scaled = scaler.transform(X2) X2_scaled_flat = np.reshape(X2_scaled, (-1, X2.shape[-1])) assert X2_scaled_flat.shape[1] == X2.shape[-1] mean = np.mean(X2_scaled_flat, axis=0) std = np.std(X2_scaled_flat, axis=0) assert np.allclose(mean, np.zeros(128), rtol=0.001, atol=0.001) assert np.allclose(std, np.ones(128), rtol=0.001, atol=0.001) # list of scalers scaler = Scaler(normalizer=["minmax", "standard"]) X_list = [X1, X2] scaler.fit(X_list) X_list_scaled = scaler.transform(X_list) assert type(X_list_scaled) is list assert len(X_list_scaled) == 2 assert np.allclose(X_list_scaled[0], X1_scaled, rtol=0.001, atol=0.001) assert np.allclose(X_list_scaled[1], X2_scaled, rtol=0.001, atol=0.001) # DataGenerator feature_extractor = MelSpectrogram() feature_extractor.extract(dataset) data_generator = DataGenerator(dataset, feature_extractor, folds=["all"]) scaler = Scaler(normalizer="minmax") scaler.fit(data_generator) data_generator.set_scaler(scaler) X, _ = data_generator.get_data() assert np.amin(X) == -1.0 assert np.amax(X) == 1.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']
def test_fit(): # Array, expect same results than partial_fit X = np.random.randn(100, 32, 128) scaler = Scaler(normalizer="minmax") scaler.fit(X) assert type(scaler.scaler) is list assert len(scaler.scaler) == 1 assert scaler.scaler[0][0] == np.amin(X) assert scaler.scaler[0][1] == np.amax(X) # DataGenerator feature_extractor = MelSpectrogram() feature_extractor.extract(dataset) data_generator = DataGenerator(dataset, feature_extractor, folds=["all"]) scaler = Scaler(normalizer="minmax") scaler.fit(data_generator) X, _ = data_generator.get_data() assert type(scaler.scaler) is list assert len(scaler.scaler) == 1 assert scaler.scaler[0][0] == np.amin(X) assert scaler.scaler[0][1] == np.amax(X)
def start_training(status, fold_ix, normalizer, model_path, epochs, early_stopping, optimizer_ix, learning_rate, batch_size, considered_improvement, n_clicks_train, dataset_ix): global data_generator_train global data_generator_val if status == 'TRAINING': if fold_ix is None: return [True, 'Please select a Fold', 'danger', ""] if optimizer_ix is None: return [True, 'Please select an Optimizer', 'danger', ""] dataset_name = options_datasets[dataset_ix]['label'] fold_name = dataset.fold_list[fold_ix] params_dataset = params['datasets'][dataset_name] optimizer = options_optimizers[optimizer_ix]['label'] use_validate_set = True if dataset_name in ['TUTSoundEvents2017', 'ESC50', 'ESC10']: # When have less data, don't use validation set. use_validate_set = False folds_train, folds_val, _ = evaluation_setup( fold_name, dataset.fold_list, params_dataset['evaluation_mode'], use_validate_set=use_validate_set) data_generator_train = DataGenerator( dataset, feature_extractor, folds=folds_train, batch_size=params['train']['batch_size'], shuffle=True, train=True, scaler=None) scaler = Scaler(normalizer=normalizer) print('Fitting scaler ...') scaler.fit(data_generator_train) print('Done!') # Pass scaler to data_gen_train to be used when data # loading data_generator_train.set_scaler(scaler) data_generator_val = DataGenerator(dataset, feature_extractor, folds=folds_val, batch_size=batch_size, shuffle=False, train=False, scaler=scaler) exp_folder_fold = conv_path(os.path.join(model_path, fold_name)) mkdir_if_not_exists(exp_folder_fold, parents=True) scaler_path = os.path.join(exp_folder_fold, 'scaler.pickle') save_pickle(scaler, scaler_path) train_arguments = { 'epochs': epochs, 'early_stopping': early_stopping, 'optimizer': optimizer, 'learning_rate': learning_rate, 'batch_size': batch_size, 'considered_improvement': considered_improvement } with graph.as_default(): model_container.train(data_generator_train, data_generator_val, weights_path=exp_folder_fold, label_list=dataset.label_list, **train_arguments) model_container.load_model_weights(exp_folder_fold) return [True, "Model trained", 'success', 'True'] else: raise dash.exceptions.PreventUpdate
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]])
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': ""}, ""]
def main(): # Parse arguments parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter ) parser.add_argument( '-od', '--origin_dataset', type=str, help='dataset name (e.g. UrbanSound8k, ESC50, URBAN_SED, SONYC_UST)', default='UrbanSound8k' ) parser.add_argument( '-ofold', '--origin_fold_name', type=str, help='origin fold name', default='fold1') parser.add_argument( '-d', '--dataset', type=str, help='dataset name (e.g. UrbanSound8k, ESC50, URBAN_SED, SONYC_UST)', default='ESC50' ) parser.add_argument( '-fold', '--fold_name', type=str, help='destination fold name', default='fold1') 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( '-s', '--models_path', type=str, help='path to save the trained model', default='../trained_models' ) 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_dataset = params['datasets'][args.dataset] params_features = params['features'] params_model = params['models'][args.model] # Load origin model model_path_origin = os.path.join(args.models_path, args.model, args.origin_dataset) model_class = get_available_models()[args.model] metrics = ['accuracy'] if args.dataset in sed_datasets: metrics = ['sed'] model_container = model_class( model=None, model_path=model_path_origin, metrics=metrics ) model_container.load_model_weights( os.path.join(model_path_origin, args.origin_fold_name)) kwargs = {} if args.dataset in sed_datasets: kwargs = {'sequence_hop_time': params_features['sequence_hop_time']} # Get and init dataset class dataset_class = get_available_datasets()[args.dataset] dataset_path = os.path.join(args.path, params_dataset['dataset_path']) dataset = dataset_class(dataset_path, **kwargs) 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] ) print('Features shape: ', features.get_shape()) # Check if features were extracted if not features.check_if_extracted(dataset): print('Extracting features ...') features.extract(dataset) print('Done!') use_validate_set = True if args.dataset in ['TUTSoundEvents2017', 'ESC50', 'ESC10']: # When have less data, don't use validation set. use_validate_set = False folds_train, folds_val, _ = evaluation_setup( args.fold_name, dataset.fold_list, params_dataset['evaluation_mode'], use_validate_set=use_validate_set ) data_gen_train = DataGenerator( dataset, features, folds=folds_train, batch_size=params['train']['batch_size'], shuffle=True, train=True, scaler=None ) scaler = Scaler(normalizer=params_model['normalizer']) print('Fitting features ...') scaler.fit(data_gen_train) print('Done!') data_gen_train.set_scaler(scaler) data_gen_val = DataGenerator( dataset, features, folds=folds_val, batch_size=params['train']['batch_size'], shuffle=False, train=False, scaler=scaler ) # Fine-tune model n_classes = len(dataset.label_list) layer_where_to_cut = -2 model_container.fine_tuning(layer_where_to_cut, new_number_of_classes=n_classes, new_activation='sigmoid', freeze_source_model=True) model_container.model.summary() # Set paths model_folder = os.path.join( args.models_path, args.model, args.origin_dataset+'_ft_'+args.dataset) exp_folder = os.path.join(model_folder, args.fold_name) mkdir_if_not_exists(exp_folder, parents=True) # Save model json and scaler model_container.save_model_json(model_folder) save_pickle(scaler, os.path.join(exp_folder, 'scaler.pickle')) # Train model model_container.train( data_gen_train, data_gen_val, label_list=dataset.label_list, weights_path=exp_folder, sequence_time_sec=params_features['sequence_hop_time'], **params['train'])
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 save the trained model', default='../trained_models') parser.add_argument('--aug', dest='augmentation', action='store_true') parser.add_argument('--no-aug', dest='augmentation', action='store_false') parser.set_defaults(augmentation=False) 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_dataset = params['datasets'][args.dataset] params_features = params['features'] params_model = params['models'][args.model] # Get and init dataset class dataset_class = get_available_datasets()[args.dataset] 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') # Data augmentation if args.augmentation: # Define the augmentations augmentations = params['data_augmentations'] # Initialize AugmentedDataset dataset = AugmentedDataset(dataset, params['features']['sr'], augmentations) # Process all files print('Doing data augmentation ...') dataset.process() print('Done!') # 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]) print('Features shape: ', features.get_shape()) # Check if features were extracted if not features.check_if_extracted(dataset): print('Extracting features ...') features.extract(dataset) print('Done!') use_validate_set = True if args.dataset in ['TUTSoundEvents2017', 'ESC50', 'ESC10']: # When have less data, don't use validation set. use_validate_set = False folds_train, folds_val, _ = evaluation_setup( args.fold_name, dataset.fold_list, params_dataset['evaluation_mode'], use_validate_set=use_validate_set) data_gen_train = DataGenerator(dataset, features, folds=folds_train, batch_size=params['train']['batch_size'], shuffle=True, train=True, scaler=None) scaler = Scaler(normalizer=params_model['normalizer']) print('Fitting scaler ...') scaler.fit(data_gen_train) print('Done!') # Pass scaler to data_gen_train to be used when data # loading data_gen_train.set_scaler(scaler) data_gen_val = DataGenerator(dataset, features, folds=folds_val, batch_size=params['train']['batch_size'], shuffle=False, train=False, scaler=scaler) # Define model features_shape = features.get_shape() n_frames_cnn = features_shape[1] n_freq_cnn = features_shape[2] n_classes = len(dataset.label_list) model_class = get_available_models()[args.model] metrics = ['classification'] if args.dataset in sed_datasets: metrics = ['sed'] if args.dataset in tagging_datasets: metrics = ['tagging'] model_container = model_class(model=None, model_path=None, n_classes=n_classes, n_frames_cnn=n_frames_cnn, n_freq_cnn=n_freq_cnn, metrics=metrics, **params_model['model_arguments']) model_container.model.summary() # Set paths model_folder = os.path.join(args.models_path, args.model, args.dataset) exp_folder = os.path.join(model_folder, args.fold_name) mkdir_if_not_exists(exp_folder, parents=True) # Save model json and scaler model_container.save_model_json(model_folder) save_pickle(scaler, os.path.join(exp_folder, 'scaler.pickle')) # data_train = data_gen_train.get_data() # data_val = data_gen_val.get_data() # Train model model_container.train( data_gen_train, data_gen_val, # data_train, data_val, label_list=dataset.label_list, weights_path=exp_folder, **params['train'], sequence_time_sec=params_features['sequence_hop_time'])
import os import numpy as np import ast import soundfile as sf from tensorflow.compat.v1 import get_default_graph from sklearn.decomposition import PCA import base64 import dash import dash_bootstrap_components as dbc from dash.dependencies import Input, Output, State dataset = Dataset("") feature_extractor = FeatureExtractor() data_generator_train = DataGenerator(dataset, feature_extractor, []) data_generator_val = DataGenerator(dataset, feature_extractor, []) data_generator_test = DataGenerator(dataset, feature_extractor, []) X_pca = np.zeros((1, 4)) X = np.zeros((1, 128, 64)) Y = np.zeros((1, 10)) file_names = [] graph = get_default_graph() # VIS TAB def conv_path(file_or_folder): return os.path.join(os.path.dirname(__file__), file_or_folder)