forked from twosixlabs/armory-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_spectrogram_classification.py
More file actions
158 lines (131 loc) · 5.48 KB
/
audio_spectrogram_classification.py
File metadata and controls
158 lines (131 loc) · 5.48 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
General audio classification scenario using spectrograms
This way of approaching the scenario requires augmenting the label set of the data.
The baseline audio classification scenario does not support this so a new scenario
was created.
Scenario contributed by: MITRE Corporation
"""
import logging
import numpy as np
from tqdm import tqdm
from armory.utils.config_loading import (
load_dataset,
load_model,
load_attack,
)
from armory.utils import metrics
from armory.scenarios.base import Scenario
logger = logging.getLogger(__name__)
def segment(x, y, n_time_bins):
"""
Return segmented batch of spectrograms and labels
x is of shape (N,241,T), representing N spectrograms, each with 241 frequency bins
and T time bins that's variable, depending on the duration of the corresponding
raw audio.
The model accepts a fixed size spectrogram, so data needs to be segmented for a
fixed number of time_bins.
"""
x_seg, y_seg = [], []
for xt, yt in zip(x, y):
n_seg = int(xt.shape[1] / n_time_bins)
xt = xt[:, : n_seg * n_time_bins]
for ii in range(n_seg):
x_seg.append(xt[:, ii * n_time_bins : (ii + 1) * n_time_bins])
y_seg.append(yt)
x_seg = np.array(x_seg)
x_seg = np.expand_dims(x_seg, -1)
y_seg = np.array(y_seg)
return x_seg, y_seg
class AudioSpectrogramClassificationTask(Scenario):
def _evaluate(self, config: dict) -> dict:
"""
Evaluate a config file for classification robustness against attack.
"""
model_config = config["model"]
classifier, preprocessing_fn = load_model(model_config)
n_tbins = 100 # number of time bins in spectrogram input to model
task_metric = metrics.categorical_accuracy
# Train ART classifier
if not model_config["weights_file"]:
classifier.set_learning_phase(True)
logger.info(
f"Fitting model {model_config['module']}.{model_config['name']}..."
)
fit_kwargs = model_config["fit_kwargs"]
train_data_generator = load_dataset(
config["dataset"],
epochs=fit_kwargs["nb_epochs"],
split_type="train",
preprocessing_fn=preprocessing_fn,
)
for cnt, (x, y) in tqdm(enumerate(train_data_generator)):
x_seg, y_seg = segment(x, y, n_tbins)
classifier.fit(
x_seg,
y_seg,
batch_size=config["dataset"]["batch_size"],
nb_epochs=1,
verbose=True,
)
if (cnt + 1) % train_data_generator.batches_per_epoch == 0:
# evaluate on validation examples
val_data_generator = load_dataset(
config["dataset"],
epochs=1,
split_type="validation",
preprocessing_fn=preprocessing_fn,
)
cnt = 0
validation_accuracies = []
for x_val, y_val in tqdm(val_data_generator):
x_val_seg, y_val_seg = segment(x_val, y_val, n_tbins)
y_pred = classifier.predict(x_val_seg)
validation_accuracies.extend(task_metric(y_val_seg, y_pred))
cnt += len(y_val_seg)
validation_accuracy = sum(validation_accuracies) / cnt
logger.info("Validation accuracy: {}".format(validation_accuracy))
classifier.set_learning_phase(False)
# Evaluate ART classifier on test examples
logger.info(f"Loading testing dataset {config['dataset']['name']}...")
test_data_generator = load_dataset(
config["dataset"],
epochs=1,
split_type="test",
preprocessing_fn=preprocessing_fn,
)
logger.info("Running inference on benign test examples...")
cnt = 0
benign_accuracies = []
for x, y in tqdm(test_data_generator, desc="Benign"):
x_seg, y_seg = segment(x, y, n_tbins)
y_pred = classifier.predict(x_seg)
benign_accuracies.extend(task_metric(y_seg, y_pred))
cnt += len(y_seg)
benign_accuracy = sum(benign_accuracies) / cnt
logger.info(f"Accuracy on benign test examples: {benign_accuracy:.2%}")
# Evaluate the ART classifier on adversarial test examples
logger.info("Generating / testing adversarial examples...")
attack = load_attack(config["attack"], classifier)
test_data_generator = load_dataset(
config["dataset"],
epochs=1,
split_type="test",
preprocessing_fn=preprocessing_fn,
)
cnt = 0
adversarial_accuracies = []
for x, y in tqdm(test_data_generator, desc="Attack"):
x_seg, y_seg = segment(x, y, n_tbins)
x_adv = attack.generate(x=x_seg)
y_pred = classifier.predict(x_adv)
adversarial_accuracies.extend(task_metric(y_seg, y_pred))
cnt += len(y_seg)
adversarial_accuracy = sum(adversarial_accuracies) / cnt
logger.info(
f"Accuracy on adversarial test examples: {adversarial_accuracy:.2%}"
)
results = {
"mean_benign_accuracy": benign_accuracy,
"mean_adversarial_accuracy": adversarial_accuracy,
}
return results