-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_vis.py
280 lines (262 loc) · 13.2 KB
/
feature_vis.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import os
import pathlib
import imageio
import warnings
from utils import *
from feature_vis_utils import *
def log_conv_features(model=None, layer_nums=None, preprocess_func=None,
directory="./models/", filter_indices = np.arange(16),
iterations=200, step_size=1, resizes=10, resize_factor=1.2,
clip=True, scale_early_layers=True, train_step=None, entropy=True,
save_to_disk=True, tensorboard_log=True, show_plots=False):
"""
Save visualizations and entropy of convolutional layer features.
Features are visualized by computing an image that maximizes the mean
activation of a filter.
Args:
model: A Tensorflow `model` with accessible convolutional layers. You can use
custom models, or (pretrained) `Tf.keras.application models`,
but Tensorflow Model Garden `models` will not work, because their
layers are repacked into a single layer.
layer_nums: A `list' of `integers` specifying the conv layers to visualize.
use `get_conv_layers(model)` or `show_conv_layers(model)` to get the conv
layer numbers. E.g. layer_nums=[0,1,2,7,9,15,16] would visualize the
0th, 1st, 2nd, 7th, 9th, 15th, and 16th, convolutional layers that
appear in the model. These layers probably will not be the 0th, 1st, ...16th
layers in the model, since it may contain normalizing, and activation layers.
preprocess_func: EITHER `None` if the model accepts inputs in the [0,1] range,
OR 'default' if the model accepts inputs in the [-1,1] range,
OR '255' if the model accpets inputs in the [0,255] range (as floats),
OR a preprocessing function/layer in line with the specifications of
a tf.keras.applications.model_name.preprocess_input function.
See: https://www.tensorflow.org/api_docs/python/tf/keras/applications/mobilenet/preprocess_input.
directory: a pathlib.Path of the save directory for the logs.
E.g. directory=pathlib.Path('./models/VGG16/')
filter_indices: a `list` or `np.array` of indices of the filters to visualize.
(default: [0,...15]).
iterations: An `int` specifying the total number of gradient ascent iterations
to use when producing the feature visualization. Typical ranges are 50-200.
step size: An `int` controlling the step size of gradient ascent. Generally
it is not necessary to modify this. Values in the range of 1-5 are reasonable.
resizes: An `int` specifying the number of times to resize upwards, crop,
then add noise when generating the filter feature visualizations.
(This can help eliminate high-frequency noise, and improve image quality.
However, to many resizes can effect entropy calculations and image
quality as well).
resize_factor: A 'float' specifying how much to resize the image by during
resizing.
sigma: A `float` givng the standard deviation of the gaussian bluring during
resizing.
clip: A `Boolean` controlling whether to 'clip' pixel values in the lower
1/8 of the range to 0 (or -1 for images in [-1,1]). This can reduce noise
and improve the quality of feature visualizations. (default: `True`).
train_step: (Optional) An `int` specifying the training iteration step.
entropy: A `Boolean` controlling whether the entropy of the visualized
features is computed and saved.
Returns:
Nothing
Example Usage:
>>> model = tf.keras.applications.VGG16()
>>> show_conv_layers(model=my_model)
conv layer #, layer name, layer index in model
0 block1_conv1 1
1 block1_conv2 2
2 block2_conv1 4
.
.
.
10 block5_conv1 15
11 block5_conv2 16
12 block5_conv3 17
>>> log_conv_features(model, layer_nums=[0,1,6,5,11,12], preprocess_func=None,
directory=pathlib.Path('./feature_logs/'), filter_indices=np.arange(16),
iterations=200, step_size=1, resizes=10, resize_factor=1.2, entropy=True)
"""
if not os.path.exists(directory):
os.mkdir(directory)
conv_layers = get_conv_layers(model)
iterations_base = iterations
resizes_base = resizes
if layer_nums is None:
warnings.warn("If you do not pass a list of ints specifying which conv layers \
you want logged, then every conv layer will be logged.\
For non-trivial models, this can be extremely time consuming.")
layer_nums = list(range(len(conv_layers)))
for conv_layer_index in layer_nums:
layer_name = conv_layers[conv_layer_index][1]
if scale_early_layers and (conv_layer_index < 4):
iterations = iterations_base
resizes = resizes_base // 4
else:
iterations = iterations_base
resizes = resizes_base
try:
save_features(model=model, layer_name=layer_name, preprocess_func=preprocess_func,
save_directory=directory, filter_indices=filter_indices,
iterations=iterations, step_size=step_size, resizes=resizes,
resize_factor=resize_factor, clip=clip, step=train_step, entropy=entropy,
save_to_disk=save_to_disk, tensorboard_log=tensorboard_log,
show_plots=show_plots)
except ValueError as e:
print(e)
#%% log_conv_features_callback
class log_conv_features_callback(tf.keras.callbacks.Callback):
"""
Tensorflow callback for `log_conv_features` function.
Save visualizations and entropy of convolutional layer features.
Features are visualized by computing an image that maximizes the mean
activation of a filter.
Args:
log_dir: A `string` path of the save directory for the logs.
update_freq: One of the`strings` 'epoch' or 'batch' declaring the frequency
of log_conv_feature updates.
update_freq_val: An `int` specifying frequency value for the updates.
overwite: (default: False) A `boolean` of whether logs should be overwritten
every update by writing to the same folder. If entropy is true, overwrite
will write to one folder, but won't actually overwrite the old images.
layer_nums: A `list' of `integers` specifying the conv layers to visualize.
use `get_conv_layers(model)` or `show_conv_layers(model)` to get the conv
layer numbers. E.g. layer_nums=[0,1,2,7,9,15,16] would visualize the
0th, 1st, 2nd, 7th, 9th, 15th, and 16th, convolutional layers that
appear in the model. These layers probably will not be the 0th, 1st, ...16th
layers in the model, since it may contain normalizing, and activation layers.
preprocess_func: EITHER `None` if the model accepts inputs in the [0,1] range,
OR 'default' if the model accepts inputs in the [-1,1] range,
OR '255' if the model accpets inputs in the [0,255] range (as floats),
OR a preprocessing function/layer in line with the specifications of
a tf.keras.applications.model_name.preprocess_input function.
See: https://www.tensorflow.org/api_docs/python/tf/keras/applications/mobilenet/preprocess_input.
filter_indices: a `list` or `np.array` of indices of the filters to visualize.
(default: [0,...15]).
iterations: An `int` specifying the total number of gradient ascent iterations
to use when producing the feature visualization. Typical ranges are 50-200.
step size: An `int` controlling the step size of gradient ascent. Generally
it is not necessary to modify this. Values in the range of 1-5 are reasonable.
resizes: An `int` specifying the number of times to resize upwards, crop,
then add noise when generating the filter feature visualizations.
(This can help eliminate high-frequency noise, and improve image quality.
However, to many resizes can effect entropy calculations and image
quality as well).
resize_factor: A 'float' specifying how much to resize the image by during
resizing.
clip: A `Boolean` controlling whether to 'clip' pixel values in the lower
1/8 of the range to 0 (or -1 for images in [-1,1]). This can reduce noise
and improve the quality of feature visualizations. (default: `True`).
train_step: (Optional) An `int` specifying the training iteration step.
entropy: A `Boolean` controlling whether the entropy of the visualized
features is computed and saved.
Returns:
Nothing
Example Usage:
>>> model = tf.keras.applications.VGG16()
>>> show_conv_layers(model=model)
conv layer #, layer name, layer index in model
0 block1_conv1 1
1 block1_conv2 2
2 block2_conv1 4
.
.
.
10 block5_conv1 15
11 block5_conv2 16
12 block5_conv3 17
>>> feature_callback = log_conv_features_callback(
log_dir=pathlib.Path('./feature_logs/'),
layer_nums=[0,1,2,10,11,12],
preprocess_func=tf.keras.applications.vgg16.preprocess_input,
clip=True, entropy=True)
>>> history = model.fit(train_dataset, epochs=20, validation_data=val_dataset,
callbacks=[feature_callback])
"""
def __init__(self,
log_dir='feature_logs',
update_freq='epoch',
update_freq_val=1,
overwrite=False,
layer_nums=[0,1,2,3],
preprocess_func=None,
filter_indices=np.arange(16),
iterations=200,
step_size=1,
resizes=10,
resize_factor=1.2,
clip=True,
scale_early_layers=True,
train_step=None,
entropy=True,
save_to_disk=True,
tensorboard_log=True,
show_plots=False):
super(log_conv_features_callback, self).__init__()
self.log_dir = pathlib.Path(log_dir)
self.update_freq = update_freq
self.update_freq_val = update_freq_val
self.overwrite = overwrite
self.layer_nums = layer_nums
self.preprocess_func = preprocess_func
self.filter_indices = filter_indices
self.iterations = iterations
self.step_size = step_size
self.resizes = resizes
self.resize_factor = resize_factor
self.clip = clip
self.scale_early_layers = scale_early_layers
self.train_step = train_step
self.entropy = entropy
self.save_to_disk = save_to_disk
self.tensorboard_log = tensorboard_log
self.show_plots = show_plots
self.file_idx = 0
def on_epoch_end(self, epoch, logs=None):
if self.update_freq != 'epoch':
return
if (epoch == 0) or (epoch % self.update_freq_val != 0):
return
if (not self.overwrite) and self.save_to_disk:
if epoch == 1:
log_dir_temp = pathlib.Path(self.log_dir / str(epoch))
if not os.path.exists(log_dir_temp): os.mkdir(log_dir_temp)
self.log_dir = pathlib.Path(log_dir_temp)
else:
log_dir_temp = pathlib.Path(self.log_dir.parent / str(epoch))
if not os.path.exists(log_dir_temp): os.mkdir(log_dir_temp)
self.log_dir = pathlib.Path(log_dir_temp)
log_conv_features(model=self.model, layer_nums=self.layer_nums,
preprocess_func=self.preprocess_func, directory=self.log_dir,
filter_indices=self.filter_indices, iterations=self.iterations,
step_size=self.step_size, resizes=self.resizes,
resize_factor=self.resize_factor, clip=self.clip,
scale_early_layers = self.scale_early_layers,
train_step=self.train_step, entropy=self.entropy,
save_to_disk=self.save_to_disk, tensorboard_log=self.tensorboard_log,
show_plots=self.show_plots
)
def on_batch_end(self, batch, logs=None):
if self.update_freq != 'batch':
return
if (batch == 1) or (batch % self.update_freq_val != 0):
return
if (not self.overwrite) and self.save_to_disk:
if self.file_idx == 0:
log_dir_temp = self.log_dir / str(self.file_idx)
if not os.path.exists(log_dir_temp): os.mkdir(log_dir_temp)
self.log_dir = pathlib.Path(log_dir_temp)
self.file_idx += 1
else:
log_dir_temp = self.log_dir.parent / str(self.file_idx)
if not os.path.exists(log_dir_temp): os.mkdir(log_dir_temp)
self.log_dir = pathlib.Path(log_dir_temp)
self.file_idx += 1
log_conv_features(model=self.model, layer_nums=self.layer_nums,
preprocess_func=self.preprocess_func, directory=self.log_dir,
filter_indices=self.filter_indices, iterations=self.iterations,
step_size=self.step_size, resizes=self.resizes,
resize_factor=self.resize_factor, clip=self.clip,
scale_early_layers = self.scale_early_layers,
train_step=self.train_step, entropy=self.entropy,
save_to_disk=self.save_to_disk, tensorboard_log=self.tensorboard_log,
show_plots=self.show_plots
)