Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better ASR Labels #863

Merged
merged 2 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion armory/utils/config_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ def load_label_targeter(config):
return labels.ManualTargeter(values, repeat)
elif scheme == "identity":
return labels.IdentityTargeter()
elif scheme == "matched length":
transcripts = config.get("transcripts")
return labels.MatchedTranscriptLengthTargeter(transcripts)
else:
raise ValueError(
f'scheme {scheme} not in ("fixed", "random", "round-robin", "manual", "identity")'
f'scheme {scheme} not in ("fixed", "random", "round-robin", "manual", "identity", "matched length")'
)
44 changes: 41 additions & 3 deletions armory/utils/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ def generate(self, y):

class ManualTargeter:
def __init__(self, values, repeat=False):
if not isinstance(values, list) or not all(isinstance(x, int) for x in values):
raise ValueError(f'"values" {values} must be a list of ints')
elif not values:
if not values:
raise ValueError('"values" cannot be an empty list')
self.values = values
self.repeat = bool(repeat)
Expand All @@ -84,3 +82,43 @@ def generate(self, y):
class IdentityTargeter:
def generate(self, y):
return y.copy().astype(int)


class MatchedTranscriptLengthTargeter:
"""
Targets labels of a length close to the true label

If two labels are tied in length, then it pseudorandomly picks one.
"""

def __init__(self, transcripts):
if not transcripts:
raise ValueError('"transcripts" cannot be None or an empty list')
for t in transcripts:
if type(t) not in (bytes, str):
raise ValueError(f"transcript type {type(t)} not in (bytes, str)")
self.transcripts = transcripts
self.count = 0

def _generate(self, y):
distances = [
(np.abs(len(y) - len(t)), i) for (i, t) in enumerate(self.transcripts)
]
distances.sort()
min_dist, i = distances[0]
pool = [i]
for dist, i in distances[1:]:
if dist == min_dist:
pool.append(i)

chosen_index = pool[self.count % len(pool)]
y_target = self.transcripts[chosen_index]
self.count += 1

return y_target

def generate(self, y):
y_target = [self._generate(y_i) for y_i in y]
if type(y) != list:
y_target = np.array(y_target)
return y_target
8 changes: 6 additions & 2 deletions scenario_configs/asr_deepspeech_baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
"name": "ImperceptibleASRPytorch",
"targeted": true,
"targeted_labels": {
"scheme": "string",
"value": "TEST STRING"
"scheme": "matched length",
"transcripts": [
"REALLY SHORT TEST STRING",
"THE TEST STRING HAS A LENGTH EQUAL TO THE MEDIAN OF THE CLEAN TEST TRANSCRIPT LENGTHS",
"THIS IS AN EXCEEDINGLY LONG TEST STRING BUT NOT REALLY AS THE LONGEST STRING HAS OVER FIVE HUNDRED CHARACTERS IN ITS TRANSCRIPT AND INCLUDES A LIST OF PEOPLE AND SPEASKS OF A SENATOR FROM NEW JERSEY"
]
},
"use_label": false
},
Expand Down