-
Notifications
You must be signed in to change notification settings - Fork 23
/
vis_layers.py
executable file
·43 lines (33 loc) · 1.07 KB
/
vis_layers.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
#!/usr/bin/env python3
from models import *
from utils import *
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import sys
def vis_layer(activ_map):
plt.ion()
plt.imshow(activ_map[:,:,0], cmap='gray')
if __name__ == '__main__':
if len(sys.argv) < 2:
print('Usage: '+sys.argv[0]+' img_file')
sys.exit(0)
img_filename = sys.argv[1]
n_classes = 1000 # using ImageNet pretrained weights
vgg16_c = VGG16_conv(n_classes)
img = np.asarray(Image.open(img_filename).resize((224,224)))
img_var = torch.autograd.Variable(torch.FloatTensor(img.transpose(2,0,1)[np.newaxis,:,:,:].astype(float)))
conv_out = vgg16_c(img_var)
print('VGG16 model:')
print(vgg16_c)
done = False
while not done:
layer = input('Layer to view: ')
try:
layer = int(layer)
except ValueError:
continue
if layer < 0:
sys.exit(0)
activ_map = vgg16_c.feature_outputs[layer].data.numpy()
vis_layer(vis_grid(activ_map.transpose(1,2,3,0)))