-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_test_reports.py
66 lines (54 loc) · 1.84 KB
/
generate_test_reports.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
import os
import sys
import numpy as np
import tensorflow as tf
from tqdm import tqdm
import csv
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from simple_cnn_model import SimpleModel
from deep_cnn_model import DeepModel
from configs import SimpleConfig, DeepConfig
from utils.prepare_data import init_data, init_test_data
if (len(sys.argv) != 2):
print(
"""
Please specify which model to train
\"python generate_test_reports.py DEEP\" or
\"python generate_test_reports.py SIMPLE\"
""")
exit()
DEPTH = sys.argv[1]
print("Testing with " + DEPTH + " CNN Model")
TEST_DIR = 'data/test'
# Process test data and create batches in memory
graph = tf.Graph()
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.9)
sess_config = tf.ConfigProto(
allow_soft_placement=True, log_device_placement=True, gpu_options=gpu_options)
sess_config.gpu_options.allow_growth = True
sess = tf.Session(config=sess_config)
if DEPTH == 'DEEP':
config = DeepConfig()
model = DeepModel(config, sess, graph)
else:
config = SimpleConfig()
model = SimpleModel(config, sess, graph)
model.restore()
print(DEPTH + " CNN Model Restored")
test_batches = init_test_data(model.config)
print(len(test_batches))
# Get predictions and Write them in CSV for submission
with open('results/' + DEPTH + '.csv', 'wb') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['id', 'label'])
for test_batch in test_batches:
images, labels = map(list, zip(*test_batch))
labels = np.array(labels).reshape(-1, 1)
pred = np.array(model.test_batch(images, labels))
print(pred.flatten().shape)
print(labels.flatten().shape)
for id, label in zip(labels.flatten(), pred.flatten()):
print(int(id), label)
writer.writerow([int(id), label])