-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathextract.py
36 lines (31 loc) · 1.02 KB
/
extract.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
import caffe
import numpy as np
import argparse
import os
def extract_caffe_model(model, weights, output_path):
"""extract caffe model's parameters to numpy array, and write them to files
Args:
model: path of '.prototxt'
weights: path of '.caffemodel'
output_path: output path of numpy params
Returns:
None
"""
net = caffe.Net(model, caffe.TEST)
net.copy_from(weights)
if not os.path.exists(output_path):
os.makedirs(output_path)
for item in net.params.items():
name, layer = item
print('convert layer: ' + name)
num = 0
for p in net.params[name]:
np.save(output_path + '/' + str(name) + '_' + str(num), p.data)
num += 1
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--model", help="model prototxt path .prototxt")
parser.add_argument("--weights", help="caffe model weights path .caffemodel")
parser.add_argument("--output", help="output path")
args = parser.parse_args()
extract_caffe_model(args.model, args.weights, args.output)