-
Notifications
You must be signed in to change notification settings - Fork 7
/
visual.py
71 lines (50 loc) · 1.88 KB
/
visual.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 29 16:03:17 2019
@author: dhubel
"""
caffe_root='/home/dhubel/deeplab_v2/deeplab-public-ver2/' # Replace the caffe_root with your caffe path in your system
import sys
sys.path.insert(0,caffe_root + 'python')
import caffe
import numpy as np
from scipy import misc
import pylab as py
import scipy.io as scio
import matplotlib.pyplot as plt
import os
os.chdir('/home/dhubel/deeplab_v2/deeplab-public-ver2/') # Replace the caffe_root with your caffe path in your system
os.getcwd
caffe.set_device(0)
caffe.set_mode_gpu()
caffemodel_m='../models/LFnet_99/model/train_iter_160000.caffemodel'
net_test = caffe.Net('../models/LFnet_99/train_LF.prototxt',
caffemodel_m,
caffe.TEST)
def vis_square(data,name):
data = (data - data.min()) / (data.max() - data.min())
n = int(np.ceil(np.sqrt(data.shape[0])))
padding = (((0, n ** 2 - data.shape[0]),
(0, 1), (0, 1))
+ ((0, 0),) * (data.ndim - 3))
data = np.pad(data, padding, mode='constant', constant_values=1)
# Tile the filters into an image
data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])
plt.figure()
plt.imshow(data)
plt.axis('off')
plt.show()
misc.toimage(data,cmin=0.0,cmax=1).save(' '+name+'.jpg')
dataNew = ' /'+name+'.mat'
scio.savemat(dataNew, {'data':data})
net_test.forward()
## Visual feature map
name='conv3_1'
feat = net_test.blobs[name].data[0]
vis_square(feat,name)
### Visual convolution kernel
#name='LF_conv1_1'
#feat = net_test.params[name][0].data
#vis_square(feat.transpose(0, 2, 3, 1),name)