-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathdraw_ponder_maps.py
74 lines (58 loc) · 2.49 KB
/
draw_ponder_maps.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
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Draws example ponder cost maps"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import h5py
import matplotlib
matplotlib.use('agg') # disables drawing to X
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('input_file', None,
'An HDF5 file produced by imagenet_export.')
tf.app.flags.DEFINE_string('output_dir', None,
'The directory to output the plotted ponder maps to.')
def main(_):
f = h5py.File(FLAGS.input_file, 'r')
num_images = f['images'].shape[0]
ponder_cost = np.array(f['ponder_cost_map'])
min_ponder = np.percentile(ponder_cost.ravel(), 0.1)
max_ponder = np.percentile(ponder_cost.ravel(), 99.9)
print('0.1st percentile of ponder cost {:.2f} '.format(min_ponder))
print('99.9th percentile of ponder cost {:.2f} '.format(max_ponder))
fig = plt.figure(figsize=(0.2, 2))
ax = fig.add_axes([0.0, 0.0, 1.0, 1.0])
cb = matplotlib.colorbar.ColorbarBase(
ax, cmap='viridis',
norm=matplotlib.colors.Normalize(vmin=min_ponder, vmax=max_ponder))
ax.tick_params(labelsize=12)
filename = os.path.join(FLAGS.output_dir, 'colorbar.pdf')
plt.savefig(filename, bbox_inches='tight')
for i in range(num_images):
current_map = np.squeeze(f['ponder_cost_map'][i])
mean_ponder = np.mean(current_map)
filename = '{}/{:.2f}_{}_ponder.png'.format(FLAGS.output_dir, mean_ponder, i)
matplotlib.image.imsave(
filename, current_map, cmap='viridis', vmin=min_ponder, vmax=max_ponder)
im = f['images'][i]
im = (im + 1.0) / 2.0
filename = '{}/{:.2f}_{}_im.jpg'.format(FLAGS.output_dir, mean_ponder, i)
matplotlib.image.imsave(filename, im)
if __name__ == '__main__':
tf.app.run()