-
Notifications
You must be signed in to change notification settings - Fork 4
/
eval_final.py
88 lines (70 loc) · 2.86 KB
/
eval_final.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
### Keras Models
from keras.preprocessing import image
from keras.models import Sequential, load_model
from keras.layers import *
from keras import metrics, optimizers
from keras.models import model_from_json
import time
import numpy as np
import os
import csv
import keras
IMG_HEIGHT = 224
IMG_WIDTH = 224
BATCH_SIZE = 1
def MadMaxPool(team_name='madmaxpool', path_to_data_source='/om/user/nprasad/angel-pfizer/data/test/melspectrograms', path_to_model_weights='/om/user/nprasad/angel-pfizer/models/0/top_short/melspectrograms/vggnet.hdf5'):
# load json file and recreate model
model = load_model(path_to_model_weights)
# evaluate loaded model with test data
testing_data_dir = path_to_data_source + '/test'
testing_datagen = image.ImageDataGenerator(
rescale=1./255,
featurewise_center=True,
featurewise_std_normalization=True,
)
testing_generator = testing_datagen.flow_from_directory(
testing_data_dir,
target_size=(IMG_HEIGHT, IMG_WIDTH),
batch_size=BATCH_SIZE,
class_mode='binary'
)
keras.backend.set_learning_phase(0)
idx = 0
files_count = testing_generator.n
predictions = np.zeros((files_count, 2))
while testing_generator.total_batches_seen < files_count:
if idx % 200 == 0:
print(idx)
# calclate time to read data and obtain prediction
start_time = time.time()
x_test, y_test = testing_generator.next()
y_pred = model.predict(x_test, batch_size=1)
elapsed_time = time.time() - start_time
predictions[idx, 0] = np.argmax(y_pred) == np.argmax(y_test)
predictions[idx, 0] = np.rint(y_pred) == y_test
predictions[idx,1] = elapsed_time
idx += 1
test_acc = round(np.mean(predictions[:,0]), 8)
avg_time = round(np.mean(predictions[:,1]), 8)
std_time = round(np.std(predictions[:,1]), 8)
# path to csv file for performance recordings
path_to_csv = 'sickness_model_performances_3.csv'
colnames = ['team_name', 'model_name', 'test_acc', 'avg_time', 'std_time']
if not os.path.exists(path_to_csv):
with open(path_to_csv, 'a') as f:
writer = csv.writer(f)
writer.writerow(colnames)
model_name = 'final'
csv_dict = {'team_name': team_name,
'model_name': model_name,
'test_acc': test_acc,
'avg_time': avg_time,
'std_time': std_time}
print
print("%s obtained %.3f%% accuracy" % (team_name, 100*test_acc))
print("average prediction time: %.5f s" % (avg_time))
print("standard dev prediction time: %.5f s" % (std_time))
with open(path_to_csv, 'a') as f:
writer = csv.DictWriter(f, delimiter=',', fieldnames=colnames)
writer.writerow(csv_dict)
MadMaxPool()