forked from wookayin/tensorflow-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathops_test.py
More file actions
150 lines (113 loc) · 5 KB
/
Copy pathops_test.py
File metadata and controls
150 lines (113 loc) · 5 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
# -*- coding: utf-8 -*-
'''Unit Test for tfplot.ops'''
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import unittest
import types
import sys
import os
import hashlib
import six
import numpy as np
import scipy.misc
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # filter out INFO and WARN logs
import matplotlib
matplotlib.rcParams['figure.figsize'] = (2.5, 2.5)
import tfplot.figure
import tfplot.test_util as test_util
test_util.configure_tf_verbosity()
# some fixtures as in showcases.ipynb
def fake_attention():
import scipy.ndimage
attention = np.zeros([16, 16], dtype=np.float32)
attention[(12, 8)] = 1.0
attention[(10, 9)] = 1.0
attention = scipy.ndimage.filters.gaussian_filter(attention, sigma=1.5)
return attention
# the plot function can have additional kwargs for providing configuration points
def _overlay_attention(attention, image,
alpha=0.5, cmap='jet'):
fig = tfplot.Figure(figsize=(4, 4))
ax = fig.add_subplot(1, 1, 1)
ax.axis('off')
fig.subplots_adjust(0, 0, 1, 1) # get rid of margins
H, W = attention.shape
ax.imshow(image, extent=[0, H, 0, W])
ax.imshow(attention, cmap=cmap,
alpha=alpha, extent=[0, H, 0, W])
return fig
class TestOps(test_util.TestcaseBase):
'''
Tests tfplot.ops
'''
# ----------------------------------------------------------------------
def test_plot_basic(self):
'''1.1 A basic example'''
def test_figure():
fig, ax = tfplot.subplots(figsize=(4, 4))
ax.text(0.5, 0.5, "Hello World!", ha='center', va='center', size=24)
return fig
plot_op = tfplot.plot(test_figure, [])
r = self._execute_plot_op(plot_op, print_image=True)
def test_plot_with_arguments(self):
'''1.2 with Arguments that takes a tensor'''
def figure_attention(attention):
fig, ax = tfplot.subplots(figsize=(4, 3))
im = ax.imshow(attention, cmap='jet')
fig.colorbar(im)
return fig
attention_tensor = tf.constant(fake_attention())
plot_op = tfplot.plot(figure_attention, [attention_tensor])
r = self._execute_plot_op(plot_op, print_image=True)
# TODO: how to compare images?
def test_plot_with_kwargs(self):
'''1.3 with kwargs'''
attention_tensor = fake_attention()
image_tensor = tf.constant(scipy.misc.face())
# (a) default execution
plot_op = tfplot.plot(_overlay_attention, [attention_tensor, image_tensor])
r = self._execute_plot_op(plot_op, print_image=True)
self.assertEqual(test_util.hash_image(r), 'c2d64dedd4aa54218e6df95bfeb03bbc17bd17fa')
# (b) override cmap and alpha
plot_op = tfplot.plot(_overlay_attention, [attention_tensor, image_tensor],
cmap='gray', alpha=0.8)
r = self._execute_plot_op(plot_op, print_image=True)
self.assertEqual(test_util.hash_image(r), '31c8029aed7bbafe37bb8c451a3220d573d2d0e0')
# TODO: how to compare images?
def test_plot_with_unicode(self):
unicode_type = six.text_type
def fig_text_placeholder_scalar(text_scalar):
fig, ax = tfplot.subplots(figsize=(4, 1))
assert isinstance(text_scalar, unicode_type), str(type(text_scalar))
ax.text(0.5, 0.5, text_scalar, ha='center', va='center')
return fig
self._execute_plot_op(tfplot.plot(fig_text_placeholder_scalar,
[u"unicode should work here ↑↓★"]))
def fig_text_placeholder_tensor(text_tensor):
fig, ax = tfplot.subplots(figsize=(4, 1))
assert isinstance(text_tensor[0], unicode_type), str(type(text_tensor[0]))
assert isinstance(text_tensor[1], unicode_type), str(type(text_tensor[1]))
ax.text(0.5, 0.7, text_tensor[0], ha='center', va='center')
ax.text(0.5, 0.3, text_tensor[1], ha='center', va='center')
return fig
self._execute_plot_op(tfplot.plot(fig_text_placeholder_tensor, [
tf.convert_to_tensor(["ascii", u"unicode ★"])
]))
def test_plot_many(self):
'''1.4 plot_many'''
# make a fake batch
batch_size = 3
image_tensor = tf.constant(scipy.misc.face())
try:
attention_batch = tf.random.gamma([batch_size, 7, 7], alpha=0.3, seed=42)
except AttributeError: # legacy TF versions
attention_batch = tf.random_gamma([batch_size, 7, 7], alpha=0.3, seed=42)
image_batch = tf.tile(tf.expand_dims(image_tensor, 0),
[batch_size, 1, 1, 1], name='image_batch') # copy
plot_op = tfplot.plot_many(_overlay_attention, [attention_batch, image_batch])
r = self._execute_plot_op(plot_op, print_image=False)
#for i in range(3): imgcat(r[i])
self.assertEqual(r.shape, (3, 400, 400, 4))