-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpredict_from_model.py
187 lines (158 loc) · 5.68 KB
/
predict_from_model.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
import os
from tqdm import tqdm
from experiment import _make_dataset
from pytorch_lightning import Trainer
from ctc_model import CTCGlossingModel
from experiment import _make_test_path
from morpheme_model import MorphemeGlossingModel
language_code_mapping = {
"Arapaho": "arp",
"Gitksan": "git",
"Lezgi": "lez",
"Natugu": "ntu",
"Nyangbo": "nyb",
"Tsez": "ddo",
"Uspanteko": "usp",
}
code_language_mapping = {
code: language for language, code in language_code_mapping.items()
}
def decode_predictions(predictions, source_tokenizer, target_tokenizer):
sentence_predictions = []
sentence_segmentations = []
for batch_prediction in predictions:
batch_prediction, batch_segmentation = batch_prediction
sentence_predictions.extend(batch_prediction)
if batch_segmentation is not None:
sentence_segmentations.extend(batch_segmentation)
else:
sentence_segmentations.extend([None for _ in batch_prediction])
decoded_predictions = []
for sentence_prediction, sentence_segmentation in zip(
sentence_predictions, sentence_segmentations
):
decoded_sentence_prediction = [
target_tokenizer.lookup_tokens(word_predictions)
for word_predictions in sentence_prediction
]
decoded_sentence_prediction = [
"-".join(word_predictions)
for word_predictions in decoded_sentence_prediction
]
decoded_sentence_prediction = " ".join(decoded_sentence_prediction)
if sentence_segmentation is not None:
decoded_sentence_segmentation = [
[
"".join(source_tokenizer.lookup_tokens(morpheme_indices))
for morpheme_indices in token_indices
]
for token_indices in sentence_segmentation
]
decoded_sentence_segmentation = [
"-".join(morphemes) for morphemes in decoded_sentence_segmentation
]
decoded_sentence_segmentation = " ".join(decoded_sentence_segmentation)
else:
decoded_sentence_segmentation = None
decoded_predictions.append(
(decoded_sentence_prediction, decoded_sentence_segmentation)
)
return decoded_predictions
def write_predictions(
predictions,
language_code: str,
track: int,
model_type: str,
trial: int = 1,
base_path: str = "./predictions",
data_path: str = "./data",
):
os.makedirs(base_path, exist_ok=True)
predictions_iterator = iter(predictions)
prediction_file_name = os.path.join(
"./predictions",
f"{language_code}_track{track}_{model_type}_trial{trial}.prediction",
)
test_file_name = _make_test_path(
language=code_language_mapping[language_code], track=track, data_path=data_path
)
with open(prediction_file_name, "w") as pf:
with open(test_file_name) as tf:
for line in tf:
if not line.startswith("\\g"):
pf.write(line)
else:
prediction, segmentation = next(predictions_iterator)
# if segmentation is not None:
# pf.write("\\m " + segmentation + "\n")
pf.write("\\g " + prediction + "\n")
def get_predictions(
path_to_model: str,
language: str,
track: int,
model_type: str,
data_path: str = "./data",
verbose: bool = False,
):
# Load Data
dm = _make_dataset(language, track, data_path, 16)
dm.prepare_data()
dm.setup(stage="fit")
dm.setup(stage="test")
# Load Model
if model_type == "ctc":
model = CTCGlossingModel.load_from_checkpoint(checkpoint_path=path_to_model)
elif model_type == "morph":
model = MorphemeGlossingModel.load_from_checkpoint(
checkpoint_path=path_to_model
)
else:
raise ValueError(f"Unknown model: {model_type}")
# Create Trainer
# Train Model
trainer = Trainer(
accelerator="cpu",
devices=1,
enable_progress_bar=True,
enable_model_summary=verbose,
logger=False,
)
# Get predictions
predictions = trainer.predict(model=model, dataloaders=dm.test_dataloader())
predictions = decode_predictions(
predictions, dm.source_tokenizer, dm.target_tokenizer
)
return predictions
def parse_model_name(model_name: str):
entries = model_name.split("-")
language_code = entries[0]
track = int(entries[1][-1])
model = entries[2].split("=")[1]
trial = int(entries[3].split("=")[1])
return language_code, track, model, trial
def get_predictions_from_retrained_models(base_path: str = "./retrain_results"):
for retrained_model in os.listdir(base_path):
language_code, track, model_type, trial = parse_model_name(retrained_model)
path_to_saved_models = os.path.join(base_path, retrained_model, "saved_models")
best_checkpoint = [
checkpoint
for checkpoint in os.listdir(path_to_saved_models)
if checkpoint != "last.ckpt"
]
best_checkpoint = best_checkpoint[0]
path_to_best_checkpoint = os.path.join(path_to_saved_models, best_checkpoint)
predictions = get_predictions(
path_to_model=path_to_best_checkpoint,
language=code_language_mapping[language_code],
track=track,
model_type=model_type,
)
write_predictions(
predictions=predictions,
language_code=language_code,
track=track,
model_type=model_type,
trial=trial,
)
if __name__ == "__main__":
get_predictions_from_retrained_models()