-
Notifications
You must be signed in to change notification settings - Fork 13
/
plot_filter_and_output.py
58 lines (46 loc) · 2.15 KB
/
plot_filter_and_output.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
import logging
import mxnet as mx
from mxboard import SummaryWriter
logging.basicConfig(level=logging.INFO)
def rescale(x, x_min=None, x_max=None):
if x_min is None:
x_min = x.min().asscalar()
if x_max is None:
x_max = x.max().asscalar()
return (x - x_min) / (x_max - x_min)
def rescale_per_image(x):
assert x.ndim == 4
x = x.copy()
for i in range(x.shape[0]):
min_val = x[i].min().asscalar()
max_val = x[i].max().asscalar()
x[i] = rescale(x[i], min_val, max_val)
return x
sw = SummaryWriter(logdir='./logs')
swan = mx.nd.load('./data/imagenet_swan.ndarray')[0]
swan = swan.reshape((1,) + swan.shape).astype('float32')
sw.add_image(tag='swan', image=swan.astype('uint8'))
# plot conv filter and output of inception-bn
weight = mx.nd.load('./data/inception_bn_conv_1_weight.param')[0]
bias = mx.nd.load('./data/inception_bn_conv_1_bias.param')[0]
mean_rgb = mx.nd.array([123.68, 116.779, 103.939])
mean_rgb = mean_rgb.reshape((1, 3, 1, 1))
out = mx.nd.Convolution(swan-mean_rgb, weight=weight, bias=bias, kernel=weight.shape[2:], num_filter=weight.shape[0])
out = out.transpose((1, 0, 2, 3))
tag = 'test_weight'
sw.add_image(tag='inception_bn_conv_1_weight', image=rescale_per_image(weight))
sw.add_image(tag='inception_bn_conv_1_output', image=rescale_per_image(out))
# plot conv filter and output of resnet-152
weight = mx.nd.load('./data/resnet_152_conv0_weight.param')[0]
out = mx.nd.Convolution(swan, weight=weight, kernel=weight.shape[2:], num_filter=weight.shape[0], no_bias=True)
out = out.transpose((1, 0, 2, 3))
sw.add_image(tag='resnet_152_conv0_weight', image=rescale_per_image(weight))
sw.add_image(tag='resnet_152_conv0_output', image=rescale_per_image(out))
# plot conv filter and output of vgg16
weight = mx.nd.load('./data/vgg16_conv1_1_weight.param')[0]
bias = mx.nd.load('./data/vgg16_conv1_1_bias.param')[0]
out = mx.nd.Convolution(swan, weight=weight, bias=bias, kernel=weight.shape[2:], num_filter=weight.shape[0])
out = out.transpose((1, 0, 2, 3))
sw.add_image(tag='vgg16_conv1_1_weight', image=rescale_per_image(weight))
sw.add_image(tag='vgg16_conv1_1_output', image=rescale_per_image(out))
sw.close()