-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTopology.py
170 lines (155 loc) · 6.3 KB
/
Topology.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
import os
import io
import numpy as np
import math
import matplotlib.pyplot as plt
import pylab as P
try:
CAFFE_ROOT = os.environ['CAFFE_ROOT']
CAFFE_PYTHON_LIB = CAFFE_ROOT+'/python'
sys.path.insert(0, CAFFE_PYTHON_LIB)
except KeyError:
print("Warning: CAFFE_ROOT environment variable not set")
os.environ['GLOG_minloglevel'] = '2' # Supresses Display on console
import caffe;
def FormatedPrint(layer_name,W,H,C,V,U,N,K,s,p):
if "layer" in layer_name:
x = layer_name + "\t"
else:
x = layer_name + "\t\t"
print (x +
str(W) + "\t" +
str(H) + "\t" +
str(C) + "\t" +
str(V) + "\t" +
str(U) + "\t" +
str(N) + "\t" +
str(K) + "\t" +
str(s) + "\t" +
str(p) + ""
);
def Topology(protoFile, modelFile):
cnn = caffe.Net(protoFile,1,weights=modelFile)
params = cnn.params
blobs = cnn.blobs
for l in cnn._layer_names:
layerId = list(cnn._layer_names).index(l)
layerType = cnn.layers[layerId].type
if (layerType == 'Convolution'):
non_lin = ""
N = params[l][0].data.shape[0]
C = params[l][0].data.shape[1]
J = params[l][0].data.shape[2]
K = params[l][0].data.shape[3]
U = blobs[l].data.shape[2]
V = blobs[l].data.shape[3]
for i in range (1,6):
try:
cnn.layers[layerId+i]
if (cnn.layers[layerId+i].type == 'Convolution'):
break
if (cnn.layers[layerId+i].type == 'ReLU'):
non_lin = non_lin + "+" + "ReLU"
if (cnn.layers[layerId+i].type == 'Scale'):
non_lin = non_lin + "+" + "Scale"
if (cnn.layers[layerId+i].type == 'TanH'):
non_lin = non_lin + "+" + "TanH"
if (cnn.layers[layerId+i].type == 'Sigmoid'):
non_lin = non_lin + "+" + "Sigmoid"
if (cnn.layers[layerId+i].type == 'Pooling'):
non_lin = non_lin + "+" + "Pool"
if (cnn.layers[layerId+i].type == 'LRN' or cnn.layers[layerId+i].type == "BatchNorm"):
non_lin = non_lin + "+" + "BN"
except IndexError:
pass
print((l + "\t" +
str(C) + "\t" +
str(N) + "\t" +
str(J) + "x" +
str(K) + "\t" +
str(U) + "x" +
str(V) + "\t" +
"" + non_lin + ""));
if (layerType == 'InnerProduct'):
non_lin = ""
N = params[l][0].data.shape[0]
C = params[l][0].data.shape[1]
for i in range (4):
try:
cnn.layers[layerId+i]
if (cnn.layers[layerId+i].type == 'TanH'):
non_lin = non_lin + "+" + "TanH"
if (cnn.layers[layerId+i].type == 'ReLU'):
non_lin = non_lin + "+" + "ReLU"
if (cnn.layers[layerId+i].type == 'Softmax'):
non_lin = non_lin + "+" + "Softmax"
except IndexError:
pass
print((l + "\t" +
str(C) + "\t" +
str(N) + "\t" + "\t" + "\t" +
"" + non_lin + ""));
def CompleteTopology(protoFile, modelFile):
from caffe.proto import caffe_pb2
from google.protobuf import text_format
parsible_net = caffe_pb2.NetParameter()
text_format.Merge(open(protoFile).read(), parsible_net)
cnn = caffe.Net(protoFile,1,weights=modelFile)
params = cnn.params
blobs = cnn.blobs
print("----------------------------------------------------------------------------------")
print("layer\t\tW\tH\tC\tV\tU\tN\tK\ts\tp")
print("----------------------------------------------------------------------------------")
for layer in parsible_net.layer:
if (layer.type == 'Input'):
l = layer.name
H = blobs[l].data.shape[2]
W = blobs[l].data.shape[3]
if (layer.type == 'Convolution'):
l = layer.name
N = layer.convolution_param.num_output if len(layer.convolution_param.kernel_size) else 1
C = params[l][0].data.shape[1]
K = layer.convolution_param.kernel_size[0] if len(layer.convolution_param.kernel_size) else 1
p = layer.convolution_param.pad[0] if len(layer.convolution_param.pad) else 0
s = layer.convolution_param.stride[0] if len(layer.convolution_param.stride) else 1
U = blobs[l].data.shape[2]
V = blobs[l].data.shape[3]
FormatedPrint(l,W,H,C,V,U,N,K,s,p)
H = blobs[l].data.shape[2]
W = blobs[l].data.shape[3]
if (layer.type == 'Pooling'):
l = layer.name
N = blobs[l].data.shape[1]
C = blobs[l].data.shape[1]
K = layer.pooling_param.kernel_size
p = layer.pooling_param.pad
s = layer.pooling_param.stride
U = blobs[l].data.shape[2]
V = blobs[l].data.shape[3]
FormatedPrint(l,W,H,C,V,U,N,K,s,p)
H = blobs[l].data.shape[2]
W = blobs[l].data.shape[3]
print("----------------------------------------------------------------------------------")
if (layer.type == "ReLU"):
print(layer.name)
if (layer.type == "LRN"):
print(layer.name)
if (layer.type == "BatchNorm"):
print(layer.name)
if (layer.type == "Scale"):
print(layer.name)
if __name__ == '__main__':
if (len(sys.argv) == 3):
protoFile = sys.argv[1]
modelFile = sys.argv[2]
## Display
print("----------------------------------------------------------------------------------")
print(("Model: "+ modelFile))
CompleteTopology(protoFile, modelFile)
print("----------------------------------------------------------------------------------")
else:
print("Not enought arguments")
print("python Workload.py <path_to_proto> <path_to_caffemodel>")