forked from osmr/imgclsmob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_gl2tf_avgpool2d.py
133 lines (112 loc) · 3.31 KB
/
convert_gl2tf_avgpool2d.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
# import math
import numpy as np
import mxnet as mx
import tensorflow as tf
class GluonModel(mx.gluon.HybridBlock):
def __init__(self,
**kwargs):
super(GluonModel, self).__init__(**kwargs)
with self.name_scope():
self.pool = mx.gluon.nn.AvgPool2D(
pool_size=2,
strides=2,
padding=0)
def hybrid_forward(self, F, x):
x = self.pool(x)
return x
# def avgpool2d(x,
# pool_size,
# strides,
# padding=0,
# ceil_mode=False,
# name=None):
# """
# Average pooling operation for two dimensional (spatial) data.
#
# Parameters:
# ----------
# x : Tensor
# Input tensor.
# pool_size : int or tuple/list of 2 int
# Size of the max pooling windows.
# strides : int or tuple/list of 2 int
# Strides of the pooling.
# padding : int or tuple/list of 2 int, default 0
# Padding value for convolution layer.
# ceil_mode : bool, default False
# When `True`, will use ceil instead of floor to compute the output shape.
# name : str, default 'conv2d'
# Layer name.
#
# Returns
# -------
# Tensor
# Resulted tensor.
# """
# if isinstance(padding, int):
# padding = (padding, padding)
#
# if ceil_mode:
# height = x.shape[2]
# out_height = float(height + 2 * padding[0] - pool_size[0]) / strides[0] + 1.0
# if math.ceil(out_height) > math.floor(out_height):
# padding[0] += 1
# width = x.shape[3]
# out_width = float(width + 2 * padding[1] - pool_size[1]) / strides[1] + 1.0
# if math.ceil(out_width) > math.floor(out_width):
# padding[1] += 1
#
# if (padding[0] > 0) or (padding[1] > 0):
# x = tf.pad(x, [[0, 0], [0, 0], list(padding), list(padding)], mode="REFLECT")
#
# x = tf.layers.average_pooling2d(
# inputs=x,
# pool_size=pool_size,
# strides=strides,
# padding='valid',
# data_format='channels_first',
# name=name)
# return x
def tensorflow_model(x):
x = tf.layers.average_pooling2d(
inputs=x,
pool_size=2,
strides=2,
padding='valid',
data_format='channels_first',
name="pool")
# x = avgpool2d(
# x=x,
# pool_size=2,
# strides=2,
# padding=1,
# ceil_mode=False,
# name="pool")
return x
def main():
success = True
for i in range(10):
x = np.random.randn(10, 10, 224, 224).astype(np.float32)
gl_model = GluonModel()
# ctx = mx.cpu()
ctx = mx.gpu(0)
gl_x = mx.nd.array(x, ctx)
gl_y = gl_model(gl_x).asnumpy()
xx = tf.placeholder(
dtype=tf.float32,
shape=(None, 10, 224, 224),
name='xx')
tf_model = tensorflow_model(xx)
with tf.Session() as sess:
tf_y = sess.run(tf_model, feed_dict={xx: x})
tf.reset_default_graph()
dist = np.sum(np.abs(gl_y - tf_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()