-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest_op.py
192 lines (170 loc) · 12.1 KB
/
test_op.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# MIT License
# Copyright (c) 2018 Changan Wang
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import shutil
import uuid
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
import math
LIB_NAME = 'ps_roi_align'
def load_op_module(lib_name):
"""
Load TensorFlow operator library.
"""
# use absolute path so that ops.py can be called from other directory
lib_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'build/lib{0}.so'.format(lib_name))
# duplicate library with a random new name so that
# a running program will not be interrupted when the original library is updated
lib_copy_path = '/tmp/lib{0}_{1}.so'.format(str(uuid.uuid4())[:8], LIB_NAME)
shutil.copyfile(lib_path, lib_copy_path)
oplib = tf.load_op_library(lib_copy_path)
#print(_)
return oplib
op_module = load_op_module(LIB_NAME)
#print("----",op_module.OP_LIST)
# map_to_pool = [[[[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.]], [[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.]], [[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.]], [[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.]]]]
map_to_pool = [[
[[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.]],
[[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.]],
[[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.]],
[[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.]],
[[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.]],
[[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.]],
[[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.]],
[[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.]],
[[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.]],
[[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.]],
[[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.]],
[[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.]],
[[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.]],
[[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.]],
[[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.]],
[[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.]]
]]
pool_method = 'max'
class PSROIAlignTest(tf.test.TestCase):
def testPSROIAlign(self):
with tf.device('/gpu:1'):
# map C++ operators to python objects
ps_roi_align = op_module.ps_roi_align
result = ps_roi_align(map_to_pool, [[[0.2, 0.2, 0.7, 0.7], [0.5, 0.5, 0.9, 0.9], [0.9, 0.9, 1., 1.]]], 2, 2, pool_method)
with self.test_session() as sess:
print('ps_roi_align in gpu:', sess.run(result))
with tf.device('/cpu:0'):
# map C++ operators to python objects
ps_roi_align = op_module.ps_roi_align
result = ps_roi_align(map_to_pool, [[[0.2, 0.2, 0.7, 0.7], [0.5, 0.5, 0.9, 0.9], [0.9, 0.9, 1., 1.]]], 2, 2, pool_method)
with self.test_session() as sess:
print('ps_roi_align in cpu:', sess.run(result))
# expect [3.18034267 0.39960092 0.00709875 2.96500921]
#self.assertAllEqual(result.eval(), [5, 0, 0, 0, 0])
@ops.RegisterGradient("PsRoiAlign")
def _ps_roi_align_grad(op, grad, _):
'''The gradients for `PsRoiAlign`.
'''
inputs_features = op.inputs[0]
rois = op.inputs[1]
pooled_features_grad = op.outputs[0]
pooled_index = op.outputs[1]
grid_dim_width = op.get_attr('grid_dim_width')
grid_dim_height = op.get_attr('grid_dim_height')
return [op_module.ps_roi_align_grad(inputs_features, rois, grad, pooled_index, grid_dim_width, grid_dim_height, pool_method), None]
class PSROIAlignGradTest(tf.test.TestCase):
def testPSROIAlignGrad(self):
with tf.device('/cpu:0'):
ps_roi_align = op_module.ps_roi_align
inputs_features = tf.constant(map_to_pool, dtype=tf.float32)
pool_result = ps_roi_align(inputs_features, [[[0.2, 0.2, 0.7, 0.7], [0.5, 0.5, 0.9, 0.9], [0.9, 0.9, 1., 1.]]], 2, 2, pool_method)
with tf.Session() as sess:
#print(sess.run(tf.gradients(pool_result[0], [inputs_features])))
print(tf.test.compute_gradient_error(inputs_features, [1, 16, 5, 5], pool_result[0], [1, 3, 4, 4], delta=0.0001, x_init_value=np.array(map_to_pool)))
# _, jaccobian = tf.test.compute_gradient(inputs_features, [1, 16, 5, 5], pool_result[0], [1, 3, 4, 4], delta=0.0001, x_init_value=np.array(map_to_pool))
# y = sess.run(pool_result[0])
# print(jaccobian.shape)
# print(np.reshape(np.matmul(jaccobian, np.ones_like(y.flatten())), np.array(map_to_pool).shape))
print(tf.test.compute_gradient(inputs_features, [1, 16, 5, 5], pool_result[0], [1, 3, 4, 4], delta=0.0001, x_init_value=np.array(map_to_pool)))
with tf.device('/gpu:0'):
ps_roi_align = op_module.ps_roi_align
inputs_features = tf.constant(map_to_pool, dtype=tf.float32)
pool_result = ps_roi_align(inputs_features, [[[0.2, 0.2, 0.7, 0.7], [0.5, 0.5, 0.9, 0.9], [0.9, 0.9, 1., 1.]]], 2, 2, pool_method)
with tf.Session() as sess:
#print(sess.run(tf.gradients(pool_result[0], [inputs_features])))
print(tf.test.compute_gradient_error(inputs_features, [1, 16, 5, 5], pool_result[0], [1, 3, 4, 4], delta=0.0001, x_init_value=np.array(map_to_pool)))
# _, jaccobian = tf.test.compute_gradient(inputs_features, [1, 16, 5, 5], pool_result[0], [1, 3, 4, 4], delta=0.0001, x_init_value=np.array(map_to_pool))
# y = sess.run(pool_result[0])
# print(jaccobian.shape)
# print(np.reshape(np.matmul(jaccobian, np.ones_like(y.flatten())), np.array(map_to_pool).shape))
print(tf.test.compute_gradient(inputs_features, [1, 16, 5, 5], pool_result[0], [1, 3, 4, 4], delta=0.0001, x_init_value=np.array(map_to_pool)))
class RotatedPSROIAlignTest(tf.test.TestCase):
def testRotatedPSROIAlign(self):
with tf.device('/gpu:1'):
# map C++ operators to python objects
rotated_ps_roi_align = op_module.rotated_ps_roi_align
result = rotated_ps_roi_align(map_to_pool, [[[0.1, 0.1, 0.2, 0.3, 0.5, 0.5, 0.3, 0.2], [0.5, 0.5, 0.6, 0.7, 0.9, 0.9, 0.7, 0.6], [0.6, 0.7, 0.9, 0.9, 0.7, 0.6, 0.2, 0.2]]], [[1, -1, 3]], 2, 2, pool_method)
with self.test_session() as sess:
print('rotated_ps_roi_align in gpu:', sess.run(result))
with tf.device('/cpu:0'):
# map C++ operators to python objects
rotated_ps_roi_align = op_module.rotated_ps_roi_align
result = rotated_ps_roi_align(map_to_pool, [[[0.1, 0.1, 0.2, 0.3, 0.5, 0.5, 0.3, 0.2], [0.5, 0.5, 0.6, 0.7, 0.9, 0.9, 0.7, 0.6], [0.6, 0.7, 0.9, 0.9, 0.7, 0.6, 0.2, 0.2]]], [[1, -1, 3]], 2, 2, pool_method)
with self.test_session() as sess:
print('rotated_ps_roi_align in cpu:', sess.run(result))
# expect [3.18034267 0.39960092 0.00709875 2.96500921]
#self.assertAllEqual(result.eval(), [5, 0, 0, 0, 0])
@ops.RegisterGradient("RotatedPsRoiAlign")
def _rotated_ps_roi_align_grad(op, grad, _):
'''The gradients for `RotatedPsRoiAlign`.
'''
inputs_features = op.inputs[0]
rois = op.inputs[1]
orders = op.inputs[2]
pooled_features_grad = op.outputs[0]
pooled_index = op.outputs[1]
grid_dim_width = op.get_attr('grid_dim_width')
grid_dim_height = op.get_attr('grid_dim_height')
return [op_module.rotated_ps_roi_align_grad(inputs_features, rois, orders, grad, pooled_index, grid_dim_width, grid_dim_height, pool_method), None, None]
class RotatedPSROIAlignGradTest(tf.test.TestCase):
def testRotatedPSROIAlignGrad(self):
with tf.device('/cpu:0'):
rotated_ps_roi_align = op_module.rotated_ps_roi_align
inputs_features = tf.constant(map_to_pool, dtype=tf.float32)
pool_result = rotated_ps_roi_align(inputs_features, [[[0.1, 0.1, 0.2, 0.3, 0.5, 0.5, 0.3, 0.2], [0.5, 0.5, 0.6, 0.7, 0.9, 0.9, 0.7, 0.6], [0.6, 0.7, 0.9, 0.9, 0.7, 0.6, 0.2, 0.2]]], [[1, -1, 3]], 2, 2, pool_method)
with tf.Session() as sess:
#print(sess.run(tf.gradients(pool_result[0], [inputs_features])))
print(tf.test.compute_gradient_error(inputs_features, [1, 16, 5, 5], pool_result[0], [1, 3, 4, 4], delta=0.0001, x_init_value=np.array(map_to_pool)))
# _, jaccobian = tf.test.compute_gradient(inputs_features, [1, 16, 5, 5], pool_result[0], [1, 3, 4, 4], delta=0.0001, x_init_value=np.array(map_to_pool))
# y = sess.run(pool_result[0])
# print(jaccobian.shape)
# print(np.reshape(np.matmul(jaccobian, np.ones_like(y.flatten())), np.array(map_to_pool).shape))
print(tf.test.compute_gradient(inputs_features, [1, 16, 5, 5], pool_result[0], [1, 3, 4, 4], delta=0.0001, x_init_value=np.array(map_to_pool)))
with tf.device('/gpu:0'):
rotated_ps_roi_align = op_module.rotated_ps_roi_align
inputs_features = tf.constant(map_to_pool, dtype=tf.float32)
pool_result = rotated_ps_roi_align(inputs_features, [[[0.1, 0.1, 0.2, 0.3, 0.5, 0.5, 0.3, 0.2], [0.5, 0.5, 0.6, 0.7, 0.9, 0.9, 0.7, 0.6], [0.6, 0.7, 0.9, 0.9, 0.7, 0.6, 0.2, 0.2]]], [[1, -1, 3]], 2, 2, pool_method)
with tf.Session() as sess:
#print(sess.run(tf.gradients(pool_result[0], [inputs_features])))
print(tf.test.compute_gradient_error(inputs_features, [1, 16, 5, 5], pool_result[0], [1, 3, 4, 4], delta=0.0001, x_init_value=np.array(map_to_pool)))
# _, jaccobian = tf.test.compute_gradient(inputs_features, [1, 16, 5, 5], pool_result[0], [1, 3, 4, 4], delta=0.0001, x_init_value=np.array(map_to_pool))
# y = sess.run(pool_result[0])
# print(jaccobian.shape)
# print(np.reshape(np.matmul(jaccobian, np.ones_like(y.flatten())), np.array(map_to_pool).shape))
print(tf.test.compute_gradient(inputs_features, [1, 16, 5, 5], pool_result[0], [1, 3, 4, 4], delta=0.0001, x_init_value=np.array(map_to_pool)))
if __name__ == "__main__":
tf.test.main()