forked from intel/caffe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
executable file
·116 lines (104 loc) · 3.54 KB
/
utils.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
import os
import sys
sys.path.insert(0,"python")
import caffe
from caffe.proto import caffe_pb2
import google.protobuf.text_format as txtf
def readFile(filePath):
lines = []
file = open(filePath, 'r')
for line in file.readlines():
lines.append(line)
file.close()
return lines
def writeFile(filePath, lines):
file = open(filePath, 'w+')
file.write(lines)
file.close()
def parseLog(log):
lines = readFile(log)
model_start = False
time_start = False
model_lines = []
time_lines = []
for line in lines:
trim_line = line.strip()
if trim_line.endswith("Initializing net from parameters:"):
model_start = True
continue
if model_start:
if trim_line.find("Creating layer") <> -1:
model_start = False
continue
model_lines.append(line)
if trim_line.endswith("Average time per layer:"):
time_start = True
continue
if time_start:
if trim_line.find("Average Forward pass") <> -1:
time_start = False
break
time_lines.append(line)
model_lines = model_lines[1:]
model_str = ""
for line in model_lines:
model_str = model_str + line
return (model_str, time_lines)
def parseTimeLines(timeLines):
layer_map = {}
for line in timeLines:
trim_line = line.strip()
items = trim_line.split("\t")
layer_items = items[0].split(" ")
layer_name = layer_items[-1]
time_items = items[1].split(" ")
if layer_name not in layer_map.keys():
layer_map[layer_name] = (float)(time_items[1])
else:
layer_map[layer_name] = layer_map[layer_name] + (float)(time_items[1])
return layer_map
def parseModelStr(modelStr):
net = caffe_pb2.NetParameter()
txtf.Merge(modelStr, net)
layer_model_map = {}
global_engine = "CAFFE"
if net.engine != "":
global_engine = net.engine
for index in range(0, len(net.layer)):
engine = global_engine
l = net.layer[index]
if l.engine != "":
engine = l.engine
param_engine = -1
if l.type == "Convolution" or l.type == "Deconvolution":
if l.convolution_param.engine != "":
param_engine = l.convolution_param.engine
elif l.type == "BatchNorm":
if l.batch_norm_param.engine != "":
param_engine = l.batch_norm_param.engine
elif l.type == "Concat":
if l.concat_param.engine != "":
param_engine = l.concat_param.engine
elif l.type == "Eltwise":
if l.eltwise_param.engine != "":
param_engine = l.eltwise_param.engine
elif l.type == "InnerProduct":
if l.inner_product_param.engine != "":
param_engine = l.inner_product_param.engine
elif l.type == "LRN":
if l.lrn_param.engine != "":
param_engine = l.lrn_param.engine
elif l.type == "Pooling":
if l.pooling_param.engine != "":
param_engine = l.pooling_param.engine
elif l.type == "ReLU":
if l.relu_param.engine != "":
param_engine = l.relu_param.engine
if param_engine == 0 or param_engine == 1:
engine = "CAFFE"
elif param_engine == 3:
engine = "MKL2017"
elif param_engine == 4:
engine = "MKLDNN"
layer_model_map[l.name] = (index, engine, l)
return (net, layer_model_map)