-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.py
228 lines (165 loc) · 12 KB
/
main.py
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import sys
from pathlib import Path
from socket import gethostname
from typing import List, Tuple
from speechless import configuration, german_corpus
from speechless.configuration import Configuration, LoggedRun
from speechless.german_corpus import german_frequent_characters
from speechless.net import ExpectationsVsPredictionsInGroupedBatches
from speechless.tools import log, distinct
def restrict_gpu_memory(per_process_gpu_memory_fraction: float = 0.9):
import os
import tensorflow as tf
import keras
thread_count = os.environ.get('OMP_NUM_THREADS')
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=per_process_gpu_memory_fraction)
config = tf.ConfigProto(gpu_options=gpu_options,
allow_soft_placement=True,
intra_op_parallelism_threads=thread_count) \
if thread_count else tf.ConfigProto(gpu_options=gpu_options, allow_soft_placement=True)
keras.backend.tensorflow_backend.set_session(tf.Session(config=config))
if __name__ == '__main__':
class SubmissionRuns:
freeze0day4hour7 = ("20170420-001258-adam-small-learning-rate-transfer-to-German-freeze-0", 2066)
german_from_beginning = ("20170415-001150-adam-small-learning-rate-complete-training-German", 443)
english_baseline = ("20170314-134351-adam-small-learning-rate-complete-95", 1689)
english_correct_test_split = ("20170414-113509-adam-small-learning-rate-complete-training", 733)
english_baseline_in_one_run = ("20170316-180957-adam-small-learning-rate-complete-95", 1192)
freeze0 = ("20170420-001258-adam-small-learning-rate-transfer-to-German-freeze-0", 1704)
freeze6 = ("20170419-212024-adam-small-learning-rate-transfer-to-German-freeze-6", 1708)
freeze8 = ("20170418-120145-adam-small-learning-rate-transfer-to-German-freeze-8", 1759)
freeze9 = ("20170419-235043-adam-small-learning-rate-transfer-to-German-freeze-9", 1789)
freeze10 = ("20170415-092748-adam-small-learning-rate-transfer-to-German-freeze-10", 1778)
freeze8reinitialize = (
"20170418-140152-adam-small-learning-rate-transfer-to-German-freeze-8-reinitialize", 1755)
freeze8small = ("20170420-174046-adam-small-learning-rate-transfer-to-German-freeze-8-50000examples", 1809)
freeze8small_15hours = (
"20170420-174046-adam-small-learning-rate-transfer-to-German-freeze-8-50000examples", 1727)
freeze8small_20hours = (
"20170420-174046-adam-small-learning-rate-transfer-to-German-freeze-8-50000examples", 1767)
freeze8small_40hours = (
"20170420-174046-adam-small-learning-rate-transfer-to-German-freeze-8-50000examples", 1939)
freeze8small_50hours = (
"20170420-174046-adam-small-learning-rate-transfer-to-German-freeze-8-50000examples", 2021)
freeze8tiny = ("20170424-231220-adam-small-learning-rate-transfer-to-German-freeze-8-10000examples", 1844)
freeze8tiny_1742 = ("20170424-231220-adam-small-learning-rate-transfer-to-German-freeze-8-10000examples", 1742)
freeze8tiny_1716 = ("20170424-231220-adam-small-learning-rate-transfer-to-German-freeze-8-10000examples", 1716)
german_small_from_beginning_day2hour15 = \
("20170424-232706-adam-small-learning-rate-complete-training-German-50000examples", 237)
freeze8small_day2hour15 = \
("20170420-174046-adam-small-learning-rate-transfer-to-German-freeze-8-50000examples", 2121)
german_model_names_with_epochs = [freeze0day4hour7, german_from_beginning, freeze0, freeze6, freeze8, freeze9,
freeze10, freeze8reinitialize,
freeze8small, freeze8small_15hours, freeze8small_20hours,
freeze8small_day2hour15, freeze8small_40hours, freeze8small_50hours,
freeze8tiny, freeze8tiny_1742, freeze8tiny_1716,
german_small_from_beginning_day2hour15]
class EluRuns:
english_elu_500 = ("20170509-140404-adam-small-learning-rate-complete-training-English-elu", 500)
english_elu_750 = ("20170509-140404-adam-small-learning-rate-complete-training-English-elu", 750)
english_elu_1000 = ("20170509-140404-adam-small-learning-rate-complete-training-English-elu", 1000)
english_elu_1250 = ("20170509-140404-adam-small-learning-rate-complete-training-English-elu", 1250)
english_elu_1500 = ("20170509-140404-adam-small-learning-rate-complete-training-English-elu", 1500)
english_elu_2000 = ("20170509-140404-adam-small-learning-rate-complete-training-English-elu", 2000)
english_elu_3000 = ("20170509-140404-adam-small-learning-rate-complete-training-English-elu", 3000)
class ValidationRuns:
freeze8 = ("20170525-181412-adam-small-learning-rate-transfer-to-German-freeze-8", 1924)
freeze8_100h = ("20170525-181449-adam-small-learning-rate-transfer-to-German-freeze-8-50000examples", 1966)
freeze8_20h = ("20170525-181524-adam-small-learning-rate-transfer-to-German-freeze-8-10000examples", 2033)
if gethostname() == "ketos":
ketos_spectrogram_cache_base_directory = configuration.default_data_directories.data_directory / "ketos-spectrogram-cache"
ketos_kenlm_base_directory = configuration.default_data_directories.data_directory / "ketos-kenlm"
log("Running on ketos, using spectrogram cache base directory {} and kenlm base directory {}".format(
ketos_spectrogram_cache_base_directory, ketos_kenlm_base_directory))
configuration.default_data_directories.spectrogram_cache_base_directory = ketos_spectrogram_cache_base_directory
configuration.default_data_directories.kenlm_base_directory = ketos_kenlm_base_directory
else:
restrict_gpu_memory()
# Configuration.german().train_from_beginning()
# Configuration.german().train_transfer_from_best_english_model(frozen_layer_count=8, reinitialize_trainable_loaded_layers=True)
# Configuration.german().train_transfer_from_best_english_model(frozen_layer_count=0)
# Configuration.german().train_transfer_from_best_english_model(frozen_layer_count=6)
# Configuration.german().train_transfer_from_best_english_model(frozen_layer_count=9)
# Configuration.german().train_transfer_from_best_english_model(frozen_layer_count=10)
# Configuration.german().train_transfer_from_best_english_model(frozen_layer_count=8)
# Configuration.german(sampled_training_example_count_when_loading_from_cached=50000).train_transfer_from_best_english_model(frozen_layer_count=8)
# Configuration.german(sampled_training_example_count_when_loading_from_cached=10000).train_transfer_from_best_english_model(frozen_layer_count=8)
# Configuration.german(from_cached=False).summarize_and_save_corpus()
# Configuration.german().fill_cache(repair_incorrect=True)
# Configuration.german().test_best_model()
# Configuration.english().summarize_and_save_corpus()
# Configuration.german(sampled_training_example_count_when_loading_from_cached=50000).train_from_beginning()
# net = Configuration.english().load_best_english_model().predictive_net
# Configuration.english().save_corpus()
# Configuration.mixed_german_english().train_from_beginning()
# Configuration.english().train_from_beginning()
def summarize_and_save_small():
Configuration(name="German",
allowed_characters=german_frequent_characters,
corpus_from_directory=german_corpus.sc10).summarize_and_save_corpus()
def positional():
german = Configuration.german()
wav2letter = german.load_best_german_model()
example = german.corpus.examples[0]
for section in example.sections():
print(wav2letter.test_and_predict(section))
def run(use_kenlm=False, language_model_name_extension="",
index: int = int(sys.argv[1] if len(sys.argv) == 2 else 0)):
kenlm_extension = ("kenlm" + language_model_name_extension) if use_kenlm else "greedy"
def logged_german_run(model_name: str, epoch: int) -> LoggedRun:
return LoggedRun(lambda: Configuration.german().test_german_model(
model_name, epoch, use_ken_lm=use_kenlm,
language_model_name_extension=language_model_name_extension),
"{}-{}-{}.txt".format(model_name, epoch, kenlm_extension))
def english_on_english_and_german(model_name: str, epoch: int) -> List[LoggedRun]:
def test_english_baseline():
english = Configuration.english()
# german_frequent_characters, as this model was accidentally trained with these
# german extra characters will be ignored
model = english.load_model(model_name, epoch,
use_kenlm=use_kenlm,
language_model_name_extension=language_model_name_extension)
english.test_model_grouped_by_loaded_corpus_name(model)
return [LoggedRun(test_english_baseline,
"{}-{}-{}-on-English.txt".format(model_name, epoch,
kenlm_extension)),
LoggedRun(lambda: Configuration.german().test_best_english_model(use_kenlm=use_kenlm),
"{}-{}-{}.txt".format(model_name, epoch, kenlm_extension))]
logged_runs = english_on_english_and_german(*Configuration.english_baseline) + [
logged_german_run(model_name, epoch) for model_name, epoch in
SubmissionRuns.german_model_names_with_epochs]
logged_runs[index]()
run(use_kenlm=True) # language_model_name_extension="-incl-trans")
def validate_to_csv(model_name: str, last_epoch: int,
configuration: Configuration = Configuration.german(),
step_count=10, first_epoch: int = 0,
csv_directory: Path = configuration.default_data_directories.test_results_directory) -> List[
Tuple[int, ExpectationsVsPredictionsInGroupedBatches]]:
step_size = (last_epoch - first_epoch) / (step_count - 1)
epochs = distinct(list(int(first_epoch + index * step_size) for index in range(step_count)))
log("Testing model {} on epochs {}.".format(model_name, epochs))
model = configuration.load_model(model_name, last_epoch,
allowed_characters_for_loaded_model=configuration.allowed_characters,
use_kenlm=True,
language_model_name_extension="-incl-trans")
def get_result(epoch: int) -> ExpectationsVsPredictionsInGroupedBatches:
log("Testing epoch {}.".format(epoch))
model.load_weights(
allowed_characters_for_loaded_model=configuration.allowed_characters,
load_model_from_directory=configuration.directories.nets_base_directory / model_name, load_epoch=epoch)
return configuration.test_model_grouped_by_loaded_corpus_name(model)
results_with_epochs = []
csv_file = csv_directory / "{}.csv".format(model_name + "-incl-trans")
import csv
with csv_file.open('w', encoding='utf8') as opened_csv:
writer = csv.writer(opened_csv, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for epoch in epochs:
result = get_result(epoch)
writer.writerow((epoch, result.average_loss, result.average_letter_error_rate,
result.average_word_error_rate, result.average_letter_error_count,
result.average_word_error_count))
return results_with_epochs
model, max_epoch = ValidationRuns.freeze8_20h
first_epoch = 1689
# results = validate_to_csv(model, max_epoch, first_epoch=first_epoch, step_count=10)
# print("Result: {}".format(results))