-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaffefileproc.py
More file actions
79 lines (57 loc) · 1.94 KB
/
caffefileproc.py
File metadata and controls
79 lines (57 loc) · 1.94 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
66
67
68
69
70
71
72
73
74
75
76
77
78
from datetime import datetime
from cnntools.utils import add_caffe_to_path
def freadlines(filepath, strip=True):
with open(filepath, 'r') as f:
if strip:
lines = [s.strip() for s in f.readlines()]
else:
lines = f.readlines()
return lines
def fwritelines(filepath, lines, endline=True):
with open(filepath, 'w') as f:
for l in lines:
if endline:
l = '{0}\n'.format(l)
f.write(l)
def parse_solver_file(filepath):
'''
Returns a protobuf object with the solver parameters
'''
lines = freadlines(filepath)
return parse_solver_file_content('\n'.join(lines))
def parse_solver_file_content(file_content):
'''
Returns a protobuf object with the solver parameters
'''
add_caffe_to_path()
from caffe.proto import caffe_pb2
from google import protobuf
solver_params = caffe_pb2.SolverParameter()
protobuf.text_format.Merge(file_content, solver_params)
return solver_params
def gen_protobuf_file_content(protobuf_params):
from google import protobuf
return protobuf.text_format.MessageToString(protobuf_params)
def save_protobuf_file(filepath, protobuf_params):
'''
Saves a protobuf file using the specified parameters
'''
with open(filepath, 'w') as f:
f.write('# GENERATED BY TRAINER.PY {0}\n'.format(datetime.now()))
f.write(gen_protobuf_file_content(protobuf_params))
def parse_model_definition_file(filepath):
'''
Parse protobuf file
'''
lines = freadlines(filepath)
return parse_model_definition_file_content('\n'.join(lines))
def parse_model_definition_file_content(file_content):
'''
Parse protobuf file content
'''
add_caffe_to_path()
from caffe.proto import caffe_pb2
from google import protobuf
model_params = caffe_pb2.NetParameter()
protobuf.text_format.Merge(file_content, model_params)
return model_params