-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_visualize.py
263 lines (200 loc) · 9.06 KB
/
eval_visualize.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
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
"""
File for detecting parts on images without ground truth.
"""
import argparse
import json
from matplotlib import pyplot as plt
import numpy as np
import os
import pprint
from scipy import interpolate
from scipy.misc import imresize
import sys
import tensorflow as tf
from tensorflow.contrib import slim
import time
from config import parse_config_file
import eval_inputs as inputs
import model
def visualize(tfrecords, checkpoint_path, cfg):
tf.logging.set_verbosity(tf.logging.DEBUG)
graph = tf.Graph()
num_parts = cfg.PARTS.NUM_PARTS
with graph.as_default():
batched_images, batched_bboxes, batched_parts, batched_part_visibilities, batched_image_ids, batched_image_height_widths, batched_crop_bboxes = inputs.input_nodes(
tfrecords=tfrecords,
num_parts = num_parts,
num_epochs=1,
batch_size=cfg.BATCH_SIZE,
num_threads=cfg.NUM_INPUT_THREADS,
capacity = cfg.QUEUE_CAPACITY,
shuffle_batch=True,
cfg=cfg
)
batch_norm_params = {
'decay': cfg.BATCHNORM_MOVING_AVERAGE_DECAY,
'epsilon': 0.001,
'variables_collections' : [tf.GraphKeys.MOVING_AVERAGE_VARIABLES],
'is_training' : False
}
# Set activation_fn and parameters for batch_norm.
with slim.arg_scope([slim.conv2d], activation_fn=tf.nn.relu,
normalizer_fn=slim.batch_norm,
normalizer_params=batch_norm_params,
weights_regularizer=slim.l2_regularizer(0.00004),
biases_regularizer=slim.l2_regularizer(0.00004)) as scope:
predicted_heatmaps = model.build(
input = batched_images,
num_parts = cfg.PARTS.NUM_PARTS
)
ema = tf.train.ExponentialMovingAverage(
decay=cfg.MOVING_AVERAGE_DECAY
)
shadow_vars = {
ema.average_name(var) : var
for var in slim.get_model_variables()
}
saver = tf.train.Saver(shadow_vars, reshape=True)
fetches = [predicted_heatmaps[-1], batched_bboxes, batched_parts, batched_part_visibilities, batched_image_ids, batched_image_height_widths, batched_crop_bboxes, batched_images]
# Now create a training coordinator that will control the different threads
coord = tf.train.Coordinator()
sess_config = tf.ConfigProto(
log_device_placement=False,
#device_filters = device_filters,
allow_soft_placement = True,
gpu_options = tf.GPUOptions(
per_process_gpu_memory_fraction=cfg.SESSION_CONFIG.PER_PROCESS_GPU_MEMORY_FRACTION
)
)
session = tf.Session(graph=graph, config=sess_config)
with session.as_default():
# make sure to initialize all of the variables
tf.initialize_all_variables().run()
tf.initialize_local_variables().run()
# launch the queue runner threads
threads = tf.train.start_queue_runners(sess=session, coord=coord)
try:
if tf.gfile.IsDirectory(checkpoint_path):
checkpoint_path = tf.train.latest_checkpoint(checkpoint_path)
if checkpoint_path is None:
print "ERROR: No checkpoint file found."
return
# Restores from checkpoint
saver.restore(session, checkpoint_path)
# Assuming model_checkpoint_path looks something like:
# /my-favorite-path/cifar10_train/model.ckpt-0,
# extract global_step from it.
global_step = int(checkpoint_path.split('/')[-1].split('-')[-1])
print "Found model for global step: %d" % (global_step,)
# we will store results into a tfrecord file
output_writer_iteration = 0
#output_path = os.path.join(save_dir, 'heatmap_results-%d-%d.tfrecords' % (global_step, output_writer_iteration))
#output_writer = tf.python_io.TFRecordWriter(output_path)
plt.ion()
image_to_convert = tf.placeholder(tf.float32)
convert_to_uint8 = tf.image.convert_image_dtype(tf.add(tf.div(image_to_convert, 2.0), 0.5), tf.uint8)
image_to_resize = tf.placeholder(tf.float32)
resize_to_input_size = tf.image.resize_bilinear(image_to_resize, size=[cfg.INPUT_SIZE, cfg.INPUT_SIZE])
num_part_cols = 3
num_part_rows = int(np.ceil(cfg.PARTS.NUM_PARTS / (num_part_cols * 1.)))
done = False
step = 0
print_str = ', '.join([
'Step: %d',
'Time/image network (ms): %.1f',
'Time/image post proc (ms): %.1f'
])
while not coord.should_stop() and not done:
outputs = session.run(fetches)
for b in range(cfg.BATCH_SIZE):
fig = plt.figure("heat maps")
plt.clf()
heatmaps = outputs[0][b]
bbox = outputs[1][b]
parts = outputs[2][b]
part_visibilities = outputs[3][b]
image_id = outputs[4][b]
image_height_widths = outputs[5][b]
crop_bbox = outputs[6][b]
image = outputs[7][b]
int8_image = session.run(convert_to_uint8, {image_to_convert : image})
heatmaps = np.clip(heatmaps, 0., 1.)
heatmaps = np.expand_dims(heatmaps, 0)
resized_heatmaps = session.run(resize_to_input_size, {image_to_resize : heatmaps})
resized_heatmaps = np.squeeze(resized_heatmaps)
image_height, image_width = image_height_widths
crop_x1, crop_y1, crop_x2, crop_y2 = crop_bbox
crop_w, crop_h = np.array([crop_x2 - crop_x1, crop_y2 - crop_y1]) * np.array([image_width, image_height], dtype=np.float32)
if crop_h > crop_w:
new_height = cfg.INPUT_SIZE
width_factor = new_height / float(crop_h)
im_scale = width_factor
else:
new_width = cfg.INPUT_SIZE
height_factor = new_width / float(crop_w)
im_scale = height_factor
for j in range(cfg.PARTS.NUM_PARTS):
heatmap = resized_heatmaps[:,:,j]
fig.add_subplot(num_part_rows, num_part_cols, j+1)
plt.imshow(int8_image)
# rescale the values of the heatmap
f = interpolate.interp1d([0., 1.], [0, 255])
int_heatmap = f(heatmap).astype(np.uint8)
# Add the heatmap as an alpha channel over the image
blank_image = np.zeros(image.shape, np.uint8)
blank_image[:] = [255, 0, 0]
heat_map_alpha = np.dstack((blank_image, int_heatmap))
plt.imshow(heat_map_alpha)
plt.axis('off')
plt.title(cfg.PARTS.NAMES[j])
# Render the argmax point
x, y = np.array(np.unravel_index(np.argmax(heatmap), heatmap.shape)[::-1])
plt.plot(x, y, color=cfg.PARTS.COLORS[j], marker=cfg.PARTS.SYMBOLS[j], label=cfg.PARTS.NAMES[j])
# Render the ground truth part location
part_v = part_visibilities[j]
if part_v:
indx = j*2
part_x, part_y = parts[indx:indx+2]
# We need to transform the ground truth annotations to the crop space
part_x = (part_x - crop_x1) * image_width * im_scale
part_y = (part_y - crop_y1) * image_height * im_scale
plt.plot(part_x, part_y, color=cfg.PARTS.COLORS[j], marker='*', label=cfg.PARTS.NAMES[j])
else:
print "Part not visible"
print "%s : max %0.3f, min %0.3f" % (cfg.PARTS.NAMES[j], np.max(heatmap), np.min(heatmap))
plt.show()
#plt.pause(0.0001)
r = raw_input("Push button")
if r != "":
done = True
break
except Exception as e:
# Report exceptions to the coordinator.
coord.request_stop(e)
# When done, ask the threads to stop. It is innocuous to request stop twice.
coord.request_stop()
# And wait for them to actually do it.
coord.join(threads)
def parse_args():
parser = argparse.ArgumentParser(description='Test an Inception V3 network')
parser.add_argument('--tfrecords', dest='tfrecords',
help='paths to tfrecords files', type=str,
nargs='+', required=True)
parser.add_argument('--checkpoint_path', dest='checkpoint_path',
help='path to directory where the checkpoint files are stored. The latest model will be tested against.', type=str,
required=False, default=None)
parser.add_argument('--config', dest='config_file',
help='Path to the configuration file',
required=True, type=str)
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
cfg = parse_config_file(args.config_file)
print "Configurations:"
print pprint.pprint(cfg)
visualize(
tfrecords=args.tfrecords,
checkpoint_path=args.checkpoint_path,
cfg=cfg
)