-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply_model.py
104 lines (80 loc) · 3.28 KB
/
apply_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
"""
Applies the U-net based on the testing data
"""
# -----------------------------------------------------------------------------
# IMPORTS
# -----------------------------------------------------------------------------
import numpy as np
import tensorflow as tf
import argparse
import time
import os
from keras import models
from utils.configfiles import read_json_config
from utils.data_processing import get_normalized_data
# -----------------------------------------------------------------------------
# FUNCTION DEFINITIONS
# -----------------------------------------------------------------------------
def get_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(description='Tests and saves the results of the U-net.')
parser.add_argument('--testing_config',
default='testing.json',
type=str,
help='Name the JSON file the program uses to '
'evaluate the model, Default: testing.json')
parser.add_argument('--save-predictions',
action='store_true',
default=True,
help='Choose to save the model\'s predictions of the '
'testing data as numpy objects, Default: True')
arguments = parser.parse_args()
return arguments
if __name__ == '__main__':
# -------------------------------------------------------------------------
# PRELIMINARIES
# -------------------------------------------------------------------------
print('')
print('TESTING & EVALUATING MODEL', flush=True)
print('')
# Start stopwatch
script_start = time.time()
# Get command line arguments
args = get_arguments()
# Get JSON config file
config_path = f'config_files/{args.testing_config}'
config = read_json_config(config_path)
# Set seeds
seed = config['random_seed']
np.random.seed(seed)
tf.random.set_seed(seed)
# -------------------------------------------------------------------------
# ACQUIRING THE MODEL & TESTING DATA
# -------------------------------------------------------------------------
# Get model
model_name = config['model_name']
model_path = f'outputs/models/{model_name}'
model = models.load_model(model_path)
hdf_file_name = config['testing_hdf_file_name']
X_test, y_test = get_normalized_data(hdf_file_name)
# Save predictions
if args.save_predictions:
print('Predicting and saving predictions: ', flush=True)
batch_size = int(config['batch_size'])
preds = model.predict(X_test, batch_size=batch_size)
preds_name = f'{model_name[:-3]}_predictions.npy'
preds_path = f'outputs/predictions/{preds_name}'
try:
np.save(preds_path, preds)
except FileNotFoundError:
os.mkdir('outputs/predictions')
np.save(preds_path, preds)
print('Done!', flush=True)
print('')
# Evaluating model against accuracy metrics
print('Evaluating model: ', flush=True)
model.evaluate(X_test, y_test, batch_size=config['batch_size'])
print('Done!', flush=True)
# Print total runtime
print('')
print('Total runtime: {:.1f}s'.format(time.time()-script_start))
print('')