File size: 3,569 Bytes
69ad385
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
from collections import defaultdict
import json
import logging
from logging.handlers import TimedRotatingFileHandler
import os
import platform
from pathlib import Path
import sys
import shutil
from typing import List

pwd = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(pwd, "../../"))

import pandas as pd
from scipy.io import wavfile
import torch
from tqdm import tqdm

from toolbox.torch.utils.data.vocabulary import Vocabulary
from toolbox.torchaudio.models.cnn_audio_classifier.modeling_cnn_audio_classifier import WaveClassifierPretrainedModel


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--dataset", default="dataset.xlsx", type=str)

    parser.add_argument("--vocabulary_dir", default="vocabulary", type=str)
    parser.add_argument("--model_dir", default="best", type=str)

    parser.add_argument("--output_file", default="evaluation.xlsx", type=str)

    args = parser.parse_args()
    return args


def logging_config():
    fmt = "%(asctime)s - %(name)s - %(levelname)s  %(filename)s:%(lineno)d >  %(message)s"

    logging.basicConfig(format=fmt,
                        datefmt="%m/%d/%Y %H:%M:%S",
                        level=logging.DEBUG)
    stream_handler = logging.StreamHandler()
    stream_handler.setLevel(logging.INFO)
    stream_handler.setFormatter(logging.Formatter(fmt))

    logger = logging.getLogger(__name__)

    return logger


def main():
    args = get_args()

    logger = logging_config()

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    n_gpu = torch.cuda.device_count()
    logger.info("GPU available count: {}; device: {}".format(n_gpu, device))

    logger.info("prepare vocabulary, model")
    vocabulary = Vocabulary.from_files(args.vocabulary_dir)

    model = WaveClassifierPretrainedModel.from_pretrained(
        pretrained_model_name_or_path=args.model_dir,
    )
    model.to(device)
    model.eval()

    logger.info("read excel")
    df = pd.read_excel(args.dataset)
    result = list()

    total_correct = 0
    total_examples = 0

    progress_bar = tqdm(total=len(df), desc="Evaluation")
    for i, row in df.iterrows():
        filename = row["filename"]
        ground_true = row["labels"]

        sample_rate, waveform = wavfile.read(filename)
        waveform = waveform / (1 << 15)
        waveform = torch.tensor(waveform, dtype=torch.float32)
        waveform = torch.unsqueeze(waveform, dim=0)
        waveform = waveform.to(device)

        with torch.no_grad():
            logits = model.forward(waveform)
            probs = torch.nn.functional.softmax(logits, dim=-1)
            label_idx = torch.argmax(probs, dim=-1)

        label_idx = label_idx.cpu()
        probs = probs.cpu()

        label_idx = label_idx.numpy()[0]
        label_str = vocabulary.get_token_from_index(label_idx, namespace="labels")
        prob = probs[0][label_idx].numpy()

        correct = 1 if label_str == ground_true else 0
        row_ = dict(row)
        row_["predict"] = label_str
        row_["prob"] = prob
        row_["correct"] = correct
        result.append(row_)

        total_examples += 1
        total_correct += correct
        accuracy = total_correct / total_examples

        progress_bar.update(1)
        progress_bar.set_postfix({
            "accuracy": accuracy,
        })

    result = pd.DataFrame(result)
    result.to_excel(
        args.output_file,
        index=False
    )
    return


if __name__ == '__main__':
    main()