-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
57 lines (36 loc) · 1.7 KB
/
predict.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
from argparse import ArgumentParser
from glob import glob
from importlib import import_module
import numpy as np
import tensorflow as tf
from common import load_labels, load_pickle_file
def run_prediction(args):
batch_size = args.batch_size
model_class = import_module("models.{}".format(args.model)).Model()
model = model_class.get_model()
model.compile(
optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()]
)
model.load_weights(args.checkpoint)
prediction_generator, _ = \
model_class.get_input_fn_and_steps_per_epoch('prediction', batch_size)
results = model.predict(prediction_generator, batch_size=None)
predicted_labels_id = np.argmax(results, axis=1)
id_to_labels, _ = load_labels()
predicted_labels = [id_to_labels[label_id] for label_id in predicted_labels_id]
test_filenames = list(sorted(list(load_pickle_file('test_filenames.pickle'))))
print("fname,label")
for filename, predicted_label in zip(test_filenames, predicted_labels):
print("{},{}".format(filename, predicted_label))
def main():
parser = ArgumentParser(description='DL-MAI project #2 (RNN) prediction script.')
available_models = [model_name.split("/")[1].split(".")[0] for model_name in glob("models/*.py")]
parser.add_argument('model', choices=available_models)
parser.add_argument('checkpoint', metavar="model.ckpt") # type=lambda x: is_valid_file(parser, x)
parser.add_argument('--batch-size', default=1024, type=int)
args = parser.parse_args()
run_prediction(args)
if __name__ == '__main__':
main()