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 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_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)