-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
70 lines (59 loc) · 2.29 KB
/
test.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
from tf_records import DataLoad
import tensorflow as tf
from sklearn.metrics import confusion_matrix
import seaborn as sns
from sklearn.metrics import classification_report
import numpy as np
import matplotlib.pyplot as plt
def classification_metrics(actual, pred, msg):
cm = confusion_matrix(actual, pred)
plt.figure()
ax= plt.subplot()
sns.heatmap(cm, annot = True, fmt = 'g')
# labels, title and ticks
ax.set_xlabel('Predicted labels')
ax.set_ylabel('True labels')
ax.set_title('Confusion Matrix')
ax.xaxis.set_ticklabels(['0', '1', '2', '3', '4', '5', '6'])
ax.yaxis.set_ticklabels(['0', '1', '2', '3', '4', '5', '6'])
plt.show()
print(classification_report(actual, pred))
def array_argmax(value):
args = []
for i in range(len(value)):
index_max = np.argmax(value[i])
args.append(index_max)
return args
dataset_test = DataLoad('./tfrecord_files/data_test.tfrecords', 1, 1, 32).return_dataset()
iterator_test = dataset_test.make_one_shot_iterator()
test = iterator_test.get_next()
i = 0
with tf.Session() as sess:
import_path = "./savedmodel/augmented_batch_64_lr_1e_resnetv2_100/epoch_21_6360"
signature_key = tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY
input_key = 'x_input'
output_key = 'y_output'
meta_graph_def = tf.saved_model.loader.load(
sess,
[tf.saved_model.tag_constants.SERVING],
import_path)
signature = meta_graph_def.signature_def
x_tensor_name = signature[signature_key].inputs[input_key].name
y_tensor_name = signature[signature_key].outputs[output_key].name
x = sess.graph.get_tensor_by_name(x_tensor_name)
y = sess.graph.get_tensor_by_name(y_tensor_name)
while(True):
try:
value_ = sess.run(test)
predicted_ = sess.run(y, {x: value_[0]})
actual_ = value_[1]
if i == 0:
actual = actual_
predicted = predicted_
else:
actual = np.concatenate((actual, actual_))
predicted = np.concatenate((predicted, predicted_))
i = 1
except tf.errors.OutOfRangeError:
break
classification_metrics(array_argmax(actual), array_argmax(predicted), msg = '')