-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpatchmonitor.py
191 lines (160 loc) · 8.2 KB
/
patchmonitor.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
# these two lines ensure matplotlib doesn't try to use X11.
import matplotlib
matplotlib.use('Agg')
import os
import math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.patches
from blocks.extensions import SimpleExtension
class PatchMonitoring(SimpleExtension):
def __init__(self, data_stream, extractor, map_to_input_space, save_to=".", **kwargs):
if not os.path.isdir(save_to):
os.makedirs(save_to)
self.data_stream = data_stream
self.save_to = save_to
self.extractor = extractor
self.map_to_input_space = map_to_input_space
self.colors = dict()
super(PatchMonitoring, self).__init__(**kwargs)
def do(self, which_callback, *args):
current_dir = os.getcwd()
os.chdir(self.save_to)
self.save_patches("patches_iteration_%i.png" % self.main_loop.status['iterations_done'])
os.chdir(current_dir)
def save_patches(self, filename):
batch = self.data_stream.get_epoch_iterator(as_dict=True).next()
images, image_shapes = batch['features'], batch['shapes']
locationss, scaless, patchess = self.extractor(images, image_shapes)
batch_size = images.shape[0]
npatches = patchess.shape[1]
patch_shape = patchess.shape[-2:]
# move channel axis to the end because pyplot wants this
images = np.rollaxis(images, 1, images.ndim)
patchess = np.rollaxis(patchess, 2, patchess.ndim)
outer_grid = gridspec.GridSpec(batch_size, 2,
width_ratios=[1, npatches])
for i, (image, image_shape, patches, locations, scales) in enumerate(
zip(images, image_shapes, patchess, locationss, scaless)):
image = image[tuple(map(slice, image_shape))]
# images are not in any predictable range, renormalize but make sure
# the patches are normalized in the same way.
vmin, vmax = image.min(), image.max()
image_ax = plt.subplot(outer_grid[i, 0])
self.imshow(image, axes=image_ax, vmin=vmin, vmax=vmax)
image_ax.axis("off")
inner_grid = gridspec.GridSpecFromSubplotSpec(1, npatches,
subplot_spec=outer_grid[i, 1],
wspace=0.1, hspace=0.1)
for j, (patch, location, scale) in enumerate(zip(patches, locations, scales)):
true_location, true_scale = self.map_to_input_space(
location, scale,
np.array(patch_shape, dtype='float32'),
np.array(image_shape, dtype='float32'))
patch_ax = plt.subplot(inner_grid[0, j])
self.imshow(patch, axes=patch_ax, vmin=vmin, vmax=vmax)
patch_ax.set_title("l (%3.2f, %3.2f)\ns (%3.2f, %3.2f)" %
(location[0], location[1], true_scale[0], true_scale[1]))
patch_ax.axis("off")
patch_hw = patch_shape / true_scale
image_yx = true_location - patch_hw/2.0
image_ax.add_patch(matplotlib.patches.Rectangle((image_yx[1], image_yx[0]),
patch_hw[1], patch_hw[0],
edgecolor="red",
facecolor="none"))
fig = plt.gcf()
fig.set_size_inches((16, 9))
plt.tight_layout()
fig.savefig(filename, bbox_inches="tight", facecolor="gray")
plt.close()
def imshow(self, image, *args, **kwargs):
kwargs.setdefault("cmap", "gray")
kwargs.setdefault("aspect", "equal")
kwargs.setdefault("interpolation", "none")
kwargs.setdefault("vmin", 0.0)
kwargs.setdefault("vmax", 1.0)
kwargs.setdefault("shape", image.shape)
plt.imshow(image, *args, **kwargs)
class VideoPatchMonitoring(SimpleExtension):
def __init__(self, data_stream, extractor, map_to_input_space, save_to=".", **kwargs):
if not os.path.isdir(save_to):
os.makedirs(save_to)
self.data_stream = data_stream
self.save_to = save_to
self.extractor = extractor
self.map_to_input_space = map_to_input_space
super(VideoPatchMonitoring, self).__init__(**kwargs)
def do(self, which_callback, *args):
current_dir = os.getcwd()
os.chdir(self.save_to)
self.save_patches("patches_iteration_%i" % self.main_loop.status['iterations_done'])
os.chdir(current_dir)
def save_patches(self, filename_stem):
batch = self.data_stream.get_epoch_iterator(as_dict=True).next()
videos, video_shapes = batch['features'], batch['shapes']
locationss, scaless, patchess = self.extractor(videos, video_shapes)
patch_shape = patchess.shape[-3:]
# move channel axis to the end
videos = np.rollaxis(videos, 1, videos.ndim)
patchess = np.rollaxis(patchess, 2, patchess.ndim)
outer_grid = gridspec.GridSpec(2, 1)
for i, (video, video_shape, patches, locations, scales) in enumerate(
zip(videos, video_shapes, patchess, locationss, scaless)):
video = video[tuple(map(slice, video_shape))]
vmin, vmax = video.min(), video.max()
video_ax = plt.subplot(outer_grid[0, 0])
video_image = (video
.transpose(1, 0, 2, 3)
.reshape((video.shape[1],
video.shape[0] * video.shape[2],
video.shape[3])))
self.imshow(video_image, axes=video_ax, vmin=vmin, vmax=vmax)
video_ax.axis("off")
true_locations, true_scales = self.map_to_input_space(
locations, scales,
np.array(patch_shape, dtype='float32'),
np.array(video_shape, dtype='float32'))
patch_ax = plt.subplot(outer_grid[1, 0])
patch_image = (patches
.transpose(0, 2, 1, 3, 4)
.reshape((patches.shape[0]*patches.shape[2],
patches.shape[1]*patches.shape[3],
patches.shape[4])))
self.imshow(patch_image, axes=patch_ax, vmin=vmin, vmax=vmax)
patch_ax.axis("off")
patch_ax.set_title("\n".join(map(str, (true_locations.T, true_scales.T))),
family="monospace")
# draw rectangles in video_ax to show patch support
for true_location, true_scale in zip(true_locations, true_scales):
# duration, height, width
patch_dhw = patch_shape / true_scale
# first-frame, top, left
patch_tyx = true_location - patch_dhw/2.0
# for each frame covered by the patch
for patch_t in range(int(math.floor(patch_tyx[0])),
int(math.ceil(patch_tyx[0] + patch_dhw[0]))):
frame_x = patch_t * video_shape[2]
video_ax.add_patch(matplotlib.patches.Rectangle(
(frame_x + patch_tyx[2], patch_tyx[1]),
patch_dhw[2], patch_dhw[1],
edgecolor="red",
facecolor="none"))
fig = plt.gcf()
fig.set_size_inches((20, 20))
plt.tight_layout()
filename = "%s_example_%i.png" % (filename_stem, i)
fig.savefig(filename, bbox_inches="tight", facecolor="gray")
plt.close()
def imshow(self, image, *args, **kwargs):
# remove degenerate channel axis
if image.ndim == 3 and image.shape[-1] == 1:
image = np.squeeze(image, axis=image.ndim - 1)
if image.ndim == 2:
kwargs.setdefault("cmap", "gray")
kwargs.setdefault("aspect", "equal")
kwargs.setdefault("interpolation", "none")
kwargs.setdefault("vmin", 0.0)
kwargs.setdefault("vmax", 1.0)
kwargs.setdefault("shape", image.shape)
plt.imshow(image, *args, **kwargs)