Beispiel #1
0
                labels=None,
                transform=Compose([
                    LoadAudio(),
                    audio_transform,
                    DropFields(("audio", "filename", "sr")),
                ]),
                clean_transform=Compose([
                    LoadAudio(),
                    MapLabels(class_map=class_map),
                ]),
            ),
            shuffle=False,
            batch_size=args.batch_size,
            collate_fn=make_collate_fn(
                {"signal": audio_transform.padding_value}),
            **loader_kwargs)

        model = TwoDimensionalCNNClassificationModel(experiment,
                                                     device=args.device)
        model.load_best_model(fold)
        model.eval()

        val_preds = model.predict(test_loader)

        all_predictions += val_preds / config.data._n_folds

result = pd.DataFrame(all_predictions,
                      columns=get_class_names_from_classmap(class_map))
result["fname"] = test_df.fname

result.to_csv(args.output_df, index=False)
Beispiel #2
0
        best_metric = max(scores)
        experiment.register_result("fold{}.metric".format(fold), best_metric)

        torch.save(
            model.state_dict(),
            os.path.join(experiment.checkpoints, "fold_{}".format(fold),
                         "final_model.pth"))

        # predictions
        model.load_best_model(fold)

        # validation

        val_preds = model.predict(valid_loader)
        val_predictions_df = pd.DataFrame(
            val_preds, columns=get_class_names_from_classmap(class_map))
        val_predictions_df["fname"] = train_df.fname[valid].values
        val_predictions_df.to_csv(os.path.join(
            experiment.predictions, "val_preds_fold_{}.csv".format(fold)),
                                  index=False)
        del val_predictions_df

        # test
        test_loader = torch.utils.data.DataLoader(
            SoundDataset(audio_files=[
                os.path.join(args.test_data_dir, fname)
                for fname in test_df.fname.values
            ],
                         transform=Compose([
                             LoadAudio(),
                             STFT(n_fft=config.data.n_fft,
    collate_fn=make_collate_fn({"signal": audio_transform.padding_value}),
)

all_probs = []
all_labels = []

with torch.no_grad():

    for sample in loader:

        signal, labels = (sample["signal"].to(args.device),
                          sample["labels"].to(args.device).float())

        probs, nonpooled = model(signal)

        all_probs.extend(probs.data.cpu().numpy())
        all_labels.extend(labels.data.cpu().numpy())

all_probs = np.array(all_probs)
all_labels = np.array(all_labels)

scores = all_labels * np.expand_dims(all_probs, -1)
mean_scores = scores.sum(axis=0) / all_labels.sum(axis=0)

classnames = get_class_names_from_classmap(class_map)

pd.options.display.max_rows = 100

print()
print(pd.DataFrame({"classname": classnames, "scores": mean_scores}))