-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevaluate.py
More file actions
286 lines (191 loc) · 9.46 KB
/
evaluate.py
File metadata and controls
286 lines (191 loc) · 9.46 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# coding=utf-8
from __future__ import print_function
from __future__ import division
import tensorflow as tf
import numpy as np
import os
from PIL import Image
import cv2
import datetime
import matplotlib.pyplot as plt
import deeplab_model
import input_data
import utils.utils as Utils
SCALES = input_data.SCALES
FLIPPED = True
PRETRAINED_MODEL_PATH = deeplab_model.PRETRAINED_MODEL_PATH
BATCH_SIZE = 1
CLASSES = deeplab_model.CLASSES
saved_ckpt_path = './checkpoint/'
saved_prediction_val_color = './pred/val_color'
saved_prediction_val_gray = './pred/val_gray'
saved_prediction_test_color = './pred/test_color'
saved_prediction_test_gray = './pred/test_gray'
VAL_LIST = input_data.VAL_LIST
ANNOTATION_PATH = input_data.ANNOTATION_PATH
val_num = 1449
test_num = 1456
if not os.path.exists('./pred'):
os.mkdir('./pred')
val_data = input_data.read_val_data()
test_data = input_data.read_test_data()
with tf.name_scope("input"):
x = tf.placeholder(tf.float32, [BATCH_SIZE, None, None, 3], name='x_input')
y = tf.placeholder(tf.int32, [BATCH_SIZE, None, None], name='ground_truth')
logits = deeplab_model.deeplab_v3_plus(x, is_training=False, output_stride=8, pre_trained_model=PRETRAINED_MODEL_PATH)
with tf.name_scope('prediction_and_miou'):
prediction = tf.argmax(logits, axis=-1, name='predictions')
def get_val_predictions():
with tf.Session() as sess:
sess.run(tf.local_variables_initializer())
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.restore(sess, './checkpoint/deeplabv3plus.model-55000')
#ckpt = tf.train.get_checkpoint_state(saved_ckpt_path)
#if ckpt and ckpt.model_checkpoint_path:
# saver.restore(sess, ckpt.model_checkpoint_path)
# print("Model restored...")
print("predicting on val set...")
for i in range(val_num):
b_image_0, b_image, b_anno, b_filename = val_data.next_batch(BATCH_SIZE, is_training=False, Shuffle=False)
pred = sess.run(prediction, feed_dict={x: b_image})
basename = b_filename.split('.')[0]
if i % 100 == 0:
print(i, pred.shape)
print(basename)
# save raw image, annotation, and prediction
pred = pred.astype(np.uint8)
b_anno = b_anno.astype(np.uint8)
pred_color = Utils.color_gray(pred[0, :, :])
b_anno_color = Utils.color_gray(b_anno[0, :, :])
b_image_0 = b_image_0.astype(np.uint8)
pred_gray = Image.fromarray(pred[0])
img = Image.fromarray(b_image_0[0])
anno = Image.fromarray(b_anno_color)
pred = Image.fromarray(pred_color)
if not os.path.exists(saved_prediction_val_gray):
os.mkdir(saved_prediction_val_gray)
pred_gray.save(os.path.join(saved_prediction_val_gray, basename + '.png'))
if not os.path.exists(saved_prediction_val_color):
os.mkdir(saved_prediction_val_color)
img.save(os.path.join(saved_prediction_val_color, basename + '_raw.png'))
anno.save(os.path.join(saved_prediction_val_color, basename + '_anno.png'))
pred.save(os.path.join(saved_prediction_val_color, basename + '_pred.png'))
print("predicting on val set finished")
def get_test_predictions():
with tf.Session() as sess:
sess.run(tf.local_variables_initializer())
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.restore(sess, './checkpoint/deeplabv3plus.model-55000')
#ckpt = tf.train.get_checkpoint_state(saved_ckpt_path)
#if ckpt and ckpt.model_checkpoint_path:
# saver.restore(sess, ckpt.model_checkpoint_path)
# print("Model restored...")
print("predicting on test set...")
for i in range(test_num):
b_image_0, b_image, b_anno, b_filename = test_data.next_batch(BATCH_SIZE, is_training=False, Shuffle=False)
pred = sess.run(prediction, feed_dict={x: b_image})
basename = b_filename.split('.')[0]
if i % 100 == 0:
print(i, pred.shape)
print(basename)
# save raw image, annotation, and prediction
pred = pred.astype(np.uint8)
pred_color = Utils.color_gray(pred[0, :, :])
b_anno_color = Utils.color_gray(b_anno[0, :, :])
b_image_0 = b_image_0.astype(np.uint8)
img = Image.fromarray(b_image_0[0])
pred = Image.fromarray(pred_color)
pred_gray = Image.fromarray(pred[0, :, :])
if not os.path.exists(saved_prediction_test_gray):
os.mkdir(saved_prediction_test_gray)
pred_gray.save(os.path.join(saved_prediction_test_gray, basename + '.png'))
if not os.path.exists(saved_prediction_test_color):
os.mkdir(saved_prediction_test_color)
img.save(os.path.join(saved_prediction_test_color, basename + '_raw.png'))
pred.save(os.path.join(saved_prediction_test_color, basename + '_pred.png'))
print("predicting on test set finished")
def get_ms_flip_val_predictions():
with tf.Session() as sess:
sess.run(tf.local_variables_initializer())
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
# saver.restore(sess, './checkpoint/deeplabv3plus.model-5000')
ckpt = tf.train.get_checkpoint_state(saved_ckpt_path)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
print("Model restored...")
print("multi predicting on val set...")
for i in range(val_num):
b_image_0, b_image, b_anno, b_filename = val_data.next_batch(BATCH_SIZE, is_training=False, Shuffle=False)
pred = []
for scale in SCALES:
image = np.squeeze(b_image)
height, width, _ = image.shape
scale_image = cv2.resize(image, (int(scale * width), int(scale * height)), interpolation=cv2.INTER_LINEAR)
mutli_prediction = tf.image.resize_bilinear(logits, [height, width], align_corners=True)
reversed_multi_prediction = tf.image.flip_left_right(mutli_prediction)
multi_pred = sess.run(mutli_prediction, feed_dict={x: np.expand_dims(scale_image, 0)})
pred.append(np.squeeze(multi_pred))
if FLIPPED:
flipped_image = cv2.flip(scale_image, 1)
mutli_prediction = tf.image.resize_bilinear(logits, [height, width], align_corners=True)
multi_pred = sess.run(reversed_multi_prediction, feed_dict={x: np.expand_dims(flipped_image, 0)})
pred.append(np.squeeze(multi_pred))
pred = np.array(pred)
pred = np.squeeze(np.mean(pred, 0))
pred = np.squeeze(np.argmax(pred, -1))
basename = b_filename.split('.')[0]
if i % 100 == 0:
print(i, pred.shape)
print(basename)
# save raw image, annotation, and prediction
pred = pred.astype(np.uint8)
b_anno = b_anno.astype(np.uint8)
pred_color = Utils.color_gray(pred[:, :])
b_anno_color = Utils.color_gray(b_anno[0, :, :])
b_image_0 = b_image_0.astype(np.uint8)
pred_gray = Image.fromarray(pred)
img = Image.fromarray(b_image_0[0])
anno = Image.fromarray(b_anno_color)
pred = Image.fromarray(pred_color)
if not os.path.exists(saved_prediction_val_gray):
os.mkdir(saved_prediction_val_gray)
pred_gray.save(os.path.join(saved_prediction_val_gray, basename + '.png'))
if not os.path.exists(saved_prediction_val_color):
os.mkdir(saved_prediction_val_color)
img.save(os.path.join(saved_prediction_val_color, basename + '_raw.png'))
anno.save(os.path.join(saved_prediction_val_color, basename + '_anno.png'))
pred.save(os.path.join(saved_prediction_val_color, basename + '_pred.png'))
print("multi predicting on val set finished")
def get_val_mIoU():
print("Start to get mIoU on val set...")
f = open(VAL_LIST)
lines = f.readlines()
annotation_files = [os.path.join(ANNOTATION_PATH, line.strip() + '.png') for line in lines]
prediction_files = [os.path.join(saved_prediction_val_gray, line.strip() + '.png') for line in lines]
for i, annotation_file in enumerate(annotation_files):
annotation_data = cv2.imread(annotation_file, cv2.IMREAD_GRAYSCALE)
annotation_data = annotation_data.reshape(-1)
if i == 0:
annotations_data = annotation_data
else:
annotations_data = np.concatenate((annotations_data, annotation_data))
print(annotations_data.shape)
for i, prediction_file in enumerate(prediction_files):
prediction_data = cv2.imread(prediction_file, cv2.IMREAD_GRAYSCALE)
prediction_data = prediction_data.reshape(-1)
if i == 0:
predictions_data = prediction_data
else:
predictions_data = np.concatenate((predictions_data, prediction_data))
print(predictions_data.shape)
mIoU, IoU = Utils.cal_batch_mIoU(predictions_data, annotations_data, CLASSES)
print(mIoU)
print(IoU)
if __name__ == '__main__':
get_val_predictions()
#get_ms_flip_val_predictions()
get_val_mIoU()
get_test_predictions()