forked from leiyu1980/caffe-darknet-convert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytorch2caffe.py
173 lines (150 loc) · 6.18 KB
/
pytorch2caffe.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
171
172
173
import sys
sys.path.append('/data/xiaohang/caffe/python')
import caffe
from collections import OrderedDict
import torch.nn as nn
import torch.nn.functional as F
import torch
from torch.autograd import Variable
from prototxt import *
layer_dict = {'ConvNdBackward' : 'Convolution',
'ThresholdBackward': 'ReLU',
'MaxPool2d' : 'Pooling',
'DropoutBackward' : 'Dropout',
'AddmmBackward' : 'InnerProduct',
'ViewBackward' : 'Reshape'}
layer_id = 0
def pytorch2caffe(input_var, output_var, protofile, caffemodel):
global layer_id
net_info = pytorch2prototxt(input_var, output_var)
print_prototxt(net_info)
save_prototxt(net_info, protofile)
net = caffe.Net(protofile, caffe.TEST)
params = net.params
layer_id = 1
def convert_layer(func):
global layer_id
parent_type = str(type(func).__name__)
parent_bottoms = []
if hasattr(func, 'next_functions'):
for u in func.next_functions:
if u[0] is not None:
child_type = str(type(u[0]).__name__)
child_name = child_type + str(layer_id)
if child_type != 'AccumulateGrad' and (parent_type != 'AddmmBackward' or child_type != 'TransposeBackward'):
convert_layer(u[0])
parent_name = parent_type+str(layer_id)
if parent_type == 'ConvNdBackward':
weights = func.next_functions[1][0].variable.data
biases = func.next_functions[2][0].variable.data
save_conv2caffe(weights, biases, params[parent_name])
elif parent_type == 'AddmmBackward':
biases = func.next_functions[0][0].variable.data
weights = func.next_functions[2][0].next_functions[0][0].variable.data
save_fc2caffe(weights, biases, params[parent_name])
if parent_type != 'ViewBackward':
layer_id = layer_id + 1
convert_layer(output_var.grad_fn)
print('save caffemodel to %s' % caffemodel)
net.save(caffemodel)
def save_conv2caffe(weights, biases, conv_param):
print(type(conv_param))
print(type(weights))
print(type(biases))
conv_param[1].data[...] = biases.numpy()
conv_param[0].data[...] = weights.numpy()
def save_fc2caffe(weights, biases, fc_param):
fc_param[1].data[...] = biases.numpy()
fc_param[0].data[...] = weights.numpy()
#def pytorch2prototxt(model, x, var):
def pytorch2prototxt(input_var, output_var):
global layer_id
net_info = OrderedDict()
props = OrderedDict()
props['name'] = 'pytorch'
props['input'] = 'data'
props['input_dim'] = input_var.size()
layers = []
layer_id = 1
def add_layer(func):
global layer_id
parent_type = str(type(func).__name__)
parent_bottoms = []
if hasattr(func, 'next_functions'):
for u in func.next_functions:
if u[0] is not None:
child_type = str(type(u[0]).__name__)
child_name = child_type + str(layer_id)
if child_type != 'AccumulateGrad' and (parent_type != 'AddmmBackward' or child_type != 'TransposeBackward'):
top_name = add_layer(u[0])
parent_bottoms.append(top_name)
parent_name = parent_type+str(layer_id)
layer = OrderedDict()
layer['name'] = parent_name
layer['type'] = layer_dict[parent_type]
parent_top = parent_name
if len(parent_bottoms) > 0:
layer['bottom'] = parent_bottoms
else:
layer['bottom'] = ['data']
layer['top'] = parent_top
if parent_type == 'ConvNdBackward':
weights = func.next_functions[1][0].variable
conv_param = OrderedDict()
conv_param['num_output'] = weights.size(0)
conv_param['pad'] = func.padding[0]
conv_param['kernel_size'] = weights.size(2)
conv_param['stride'] = func.stride[0]
layer['convolution_param'] = conv_param
#elif parent_type == 'ThresholdBackward':
elif parent_type == 'MaxPool2d':
pooling_param = OrderedDict()
pooling_param['pool'] = 'MAX'
pooling_param['kernel_size'] = func.kernel_size[0]
pooling_param['stride'] = func.stride[0]
layer['pooling_param'] = pooling_param
elif parent_type == 'DropoutBackward':
parent_top = parent_bottoms[0]
dropout_param = OrderedDict()
dropout_param['dropout_ratio'] = func.p
layer['dropout_param'] = dropout_param
elif parent_type == 'AddmmBackward':
inner_product_param = OrderedDict()
inner_product_param['num_output'] = func.next_functions[0][0].variable.size(0)
layer['inner_product_param'] = inner_product_param
elif parent_type == 'ViewBackward':
parent_top = parent_bottoms[0]
layer['top'] = parent_top
if parent_type != 'ViewBackward':
layers.append(layer)
layer_id = layer_id + 1
return parent_top
add_layer(output_var.grad_fn)
net_info['props'] = props
net_info['layers'] = layers
return net_info
if __name__ == '__main__':
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(320, 50)
self.fc2 = nn.Linear(50, 10)
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
x = x.view(-1, 320)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return F.log_softmax(x)
#m = Net()
#x = Variable(torch.rand(1,3,28,28))
import torchvision
m = torchvision.models.alexnet()
print(m)
input_var = Variable(torch.rand(1, 3, 227, 227))
output_var = m(input_var)
pytorch2caffe(input_var, output_var, 'out.prototxt', 'out.caffemodel')