-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
170 lines (147 loc) · 4.93 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import tensorflow as tf
import argparse
import os
import csv
import numpy as np
import dataloader
import model
def manage_arguments():
parser = argparse.ArgumentParser('TT-Training-Predict')
parser.add_argument(
'--chkpt_dir', type=str,
help='directory where to load the model under evaluation'
)
parser.add_argument(
'--ouput_file', type=str, default='./prediction.csv',
help='path of the file where to output the prediction result'
)
parser.add_argument(
'--data_dir', type=str,
default='/network/tmp1/fansitca/TT-Training/',
help='Directory where to find the data'
)
parser.add_argument(
'--batch_size', type=int, default=32,
help='batch size for training'
)
parser.add_argument(
'--kernel_size', type=int, default=3,
help='the kernel for convolution layers'
)
parser.add_argument(
'--strides', type=int, default=1,
help='the strides for convolution layers'
)
parser.add_argument(
'--pool_size', type=int, default=2,
help='the pool size for max_pooling layers'
)
parser.add_argument(
'--num_fc', type=int, default=2,
help='the number of dense layers before output predictions'
)
parser.add_argument(
'--fc_size', type=int, default=1000,
help='the output size of the dense layers before output predictions'
)
parser.add_argument(
'--dropout_rate', type=float, default=0.5,
help='the dropout rate applied within the model'
)
args = parser.parse_args()
return args
def main_process():
tf.logging.set_verbosity(tf.logging.INFO)
args = manage_arguments()
# prepare the dataset if necessary
if not os.path.isfile(args.data_dir + '/Test_Images.TFRecord'):
tf.gfile.MakeDirs(args.data_dir)
dataloader.prepare_datasets(args.data_dir)
real_labels = {}
with open(args.data_dir + '/label_info.csv', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for i, row in enumerate(reader):
if row[0].strip().startswith('#'): # header
continue
index = int(row[0])
label = row[1]
real_labels[index] = label
tf.enable_eager_execution()
# define the model
conv_list = [(2, 64), (2, 128), (3, 256)]
dense_list = [args.fc_size] * args.num_fc
dense_list.append(17)
network_model = model.create_model(
input_shape=(224, 224, 3),
conv_list=conv_list,
dense_list=dense_list,
kernel_size=args.kernel_size,
strides=args.strides,
pool_size=args.pool_size,
dropout_rate=args.dropout_rate,
output_activation='sigmoid',
layer_activation='relu'
)
network_model.summary()
ckpt = tf.train.Checkpoint(
model=network_model
)
status = ckpt.restore(tf.train.latest_checkpoint(args.chkpt_dir))
status.assert_existing_objects_matched()
# define the train/valid dataloaders
ds_test_x = dataloader.input_dataset_fn(
batch_size=args.batch_size,
image_file=args.data_dir + 'Test_Images.TFRecord',
label_file=None,
repeat=False, shuffle=False, drop_remainder=False,
data_augmentation=False,
)
predictions = network_model.predict(
ds_test_x,
)
num_elements = len(predictions)
ds_test_y = dataloader.input_dataset_fn(
batch_size=num_elements,
image_file=None,
label_file=args.data_dir + 'Test_Labels.TFRecord',
repeat=False, shuffle=False, drop_remainder=False,
data_augmentation=False,
)
ground_truth = next(iter(ds_test_y))
# define the loss function, metrics, and optimizer
loss_fn = tf.keras.losses.BinaryCrossentropy()
metrics = [
tf.keras.metrics.BinaryAccuracy(),
tf.keras.metrics.Precision(),
tf.keras.metrics.Recall()
]
loss_test = loss_fn(ground_truth, predictions)
for i in range(len(metrics)):
metrics[i].update_state(ground_truth, predictions)
metric_values = dict(
('test_' + m.name, m.result().numpy()) for m in metrics
)
metric_values['loss_test'] = loss_test.numpy()
test_precision = metric_values.get('test_precision', 0.0)
test_recall = metric_values.get('test_recall', 0.0)
denom = test_precision + test_recall
if denom <= 0:
denom = 1e-5
test_f1 = 2 * (test_precision * test_recall) / denom
metric_values['test_f1'] = test_f1
predicted_classes = np.argwhere(predictions > 0.5)
item_classes = {}
for i in range(len(predicted_classes)):
index, a_class = predicted_classes[i]
if not (index in item_classes):
item_classes[index] = []
item_classes[index].append(
real_labels[a_class]
)
print("\n\n")
print(metric_values)
print("\n\n")
def main(_):
main_process()
if __name__ == '__main__':
tf.app.run(main=main)