-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathvisualize.py
More file actions
65 lines (54 loc) · 2.11 KB
/
visualize.py
File metadata and controls
65 lines (54 loc) · 2.11 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
import sys
import os
import numpy as np
import os.path as osp
import scipy.io as sio
from copy import copy
from PIL import Image
import random
import matplotlib.pyplot as plt
plt.switch_backend('agg')
def sortbyvar(data):
sortdata = np.zeros(data.shape)
sumdata = np.sum(data, axis=3)
flat = sumdata.reshape(sumdata.shape[0], sumdata.shape[1]*sumdata.shape[2])
std = np.std(flat, axis=1)
order = np.argsort(std)
for i in range(order.shape[0]):
sortdata[i,:,:,:] = data[order[i],:,:,:]
return sortdata
def vis_square(data, fname):
"""Take an array of shape (n, height, width) or (n, height, width, 3)
and visualize each (height, width) thing in a grid of size approx. sqrt(n) by sqrt(n)"""
data = np.squeeze(data)
# normalize data for display
data = (data - data.min()) / (data.max() - data.min())
data = sortbyvar(data)
# force the number of filters to be square
n = int(np.ceil(np.sqrt(data.shape[0])))
padding = (((0, 96 - data.shape[0]),
(0, 1), (0, 1)) # add some space between filters
+ ((0, 0),) * (data.ndim - 3)) # don't pad the last dimension (if there is one)
data = np.pad(data, padding, mode='constant', constant_values=1) # pad with ones (white)
# tile the filters into an image
data = data.reshape((6, 16) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
data = data.reshape((6 * data.shape[1], 16 * data.shape[3]) + data.shape[4:])
plt.imshow(data); plt.axis('off')
plt.savefig(fname,bbox_inches='tight')
if __name__ == '__main__':
"""
usage: visualize.py model output_fig
"""
caffe_root = '/home/hylee/Unsupervised/caffe_s2/'
sys.path.append(caffe_root + 'python')
import caffe
gpu_id = 0
caffe.set_mode_gpu()
caffe.set_device(gpu_id)
caffemodel = sys.argv[1]
fname = sys.argv[2]
prototxt = 'prototxt/deploy_RGB.prototxt'
conv1 = 'conv1'
net = caffe.Net(prototxt, caffemodel, caffe.TEST)
filters = net.params[conv1][0].data
vis_square(filters.transpose(0, 2, 3, 1), fname)