forked from osmr/imgclsmob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_gl2pt_conv2d.py
91 lines (67 loc) · 2.3 KB
/
convert_gl2pt_conv2d.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
import numpy as np
import mxnet as mx
import torch
from torch.autograd import Variable
class GluonModel(mx.gluon.HybridBlock):
def __init__(self,
**kwargs):
super(GluonModel, self).__init__(**kwargs)
with self.name_scope():
self.conv = mx.gluon.nn.Conv2D(
channels=64,
kernel_size=7,
strides=2,
padding=3,
use_bias=True,
in_channels=3)
def hybrid_forward(self, F, x):
x = self.conv(x)
return x
class PytorchModel(torch.nn.Module):
def __init__(self):
super(PytorchModel, self).__init__()
self.conv = torch.nn.Conv2d(
in_channels=3,
out_channels=64,
kernel_size=7,
stride=2,
padding=3,
bias=True)
def forward(self, x):
x = self.conv(x)
return x
def main():
success = True
for i in range(10):
# w = np.random.randint(10, size=(64, 3, 7, 7)).astype(np.float32)
# x = np.random.randint(10, size=(1, 3, 224, 224)).astype(np.float32)
w = np.random.randn(64, 3, 7, 7).astype(np.float32)
b = np.random.randn(64, ).astype(np.float32)
x = np.random.randn(10, 3, 224, 224).astype(np.float32)
gl_model = GluonModel()
# ctx = mx.cpu()
ctx = mx.gpu(0)
gl_params = gl_model._collect_params_with_prefix()
gl_params['conv.weight']._load_init(mx.nd.array(w, ctx), ctx)
gl_params['conv.bias']._load_init(mx.nd.array(b, ctx), ctx)
gl_x = mx.nd.array(x, ctx)
gl_y = gl_model(gl_x).asnumpy()
pt_model = PytorchModel()
pt_model.eval()
pt_params = pt_model.state_dict()
pt_params['conv.weight'] = torch.from_numpy(w)
pt_params['conv.bias'] = torch.from_numpy(b)
pt_model.load_state_dict(pt_params)
pt_model = pt_model.cuda()
pt_x = Variable(torch.from_numpy(x)).cuda()
pt_y = pt_model(pt_x).detach().cpu().numpy()
dist = np.sum(np.abs(gl_y - pt_y))
if dist > 1e-5:
success = False
print("i={}, dist={}".format(i, dist))
# print(gl_y)
# print(tf_y)
if success:
print("All ok.")
if __name__ == '__main__':
main()