7
7
import tempfile
8
8
import unittest
9
9
from collections import namedtuple
10
+ from itertools import product
10
11
11
12
import numpy as np
12
13
import tensorflow as tf
16
17
17
18
TMPPATH = tempfile .mkdtemp ()
18
19
20
+ # we can override BACKEND and OPSET from the command line, but that is to late
21
+ # to change the behavior of annotation. If need, pick the backend here.
22
+ OPSET = 7
23
+
19
24
BACKEND = "caffe2"
20
25
# BACKEND = "onnxmsrt"
21
26
# BACKEND = "onnxmsrtnext"
37
42
_OUTPUT = "output:0"
38
43
_OUTPUT1 = "output1:0"
39
44
40
- OPSET = 7
41
45
42
46
# pylint: disable=C0111
43
47
@@ -47,6 +51,50 @@ def make_xval(shape):
47
51
return x_val
48
52
49
53
54
+ def get_conv_getdata (kind = 1 ):
55
+ if kind == 0 :
56
+ # generate all combinations (costly)
57
+ dims = [
58
+ ("padding" , ["SAME" , "VALID" ]),
59
+ ("input_sizes" , [[32 , 35 , 35 , 288 ], [32 , 17 , 17 , 1248 ], [1 , 28 , 28 , 3 ], [32 , 8 , 8 , 2048 ]]),
60
+ ("filter_sizes" , [[1 , 3 , 3 , 1 ], [1 , 2 , 2 , 1 ], [1 , 5 , 5 , 1 ], [1 , 1 , 1 , 1 ], [1 , 5 , 2 , 1 ], [1 , 2 , 5 , 1 ]]),
61
+ ("strides" , [[1 , 2 , 2 , 1 ], [1 , 1 , 1 , 1 ]]),
62
+ ]
63
+ values = [key_values [1 ] for key_values in dims ]
64
+ for idx , v in enumerate (product (* values )):
65
+ if True or idx == 30 :
66
+ yield (idx ,) + v
67
+ elif kind == 1 :
68
+ # some combination to that give decent padding coverage
69
+ data = [
70
+ ('SAME' , [32 , 35 , 35 , 288 ], [1 , 3 , 3 , 1 ], [1 , 2 , 2 , 1 ]),
71
+ ('SAME' , [32 , 35 , 35 , 288 ], [1 , 2 , 2 , 1 ], [1 , 2 , 2 , 1 ]),
72
+ ('SAME' , [32 , 35 , 35 , 288 ], [1 , 2 , 2 , 1 ], [1 , 1 , 1 , 1 ]),
73
+ ('SAME' , [32 , 35 , 35 , 288 ], [1 , 5 , 5 , 1 ], [1 , 1 , 1 , 1 ]),
74
+ ('SAME' , [32 , 35 , 35 , 288 ], [1 , 1 , 1 , 1 ], [1 , 2 , 2 , 1 ]),
75
+ ('SAME' , [32 , 35 , 35 , 288 ], [1 , 1 , 1 , 1 ], [1 , 1 , 1 , 1 ]),
76
+ ('SAME' , [32 , 35 , 35 , 288 ], [1 , 5 , 2 , 1 ], [1 , 2 , 2 , 1 ]),
77
+ ('SAME' , [32 , 35 , 35 , 288 ], [1 , 2 , 5 , 1 ], [1 , 2 , 2 , 1 ]),
78
+ ('SAME' , [32 , 35 , 35 , 288 ], [1 , 2 , 5 , 1 ], [1 , 1 , 1 , 1 ]),
79
+ ('SAME' , [1 , 28 , 28 , 3 ], [1 , 3 , 3 , 1 ], [1 , 2 , 2 , 1 ]),
80
+ ('SAME' , [1 , 28 , 28 , 3 ], [1 , 3 , 3 , 1 ], [1 , 1 , 1 , 1 ]),
81
+ ('SAME' , [1 , 28 , 28 , 3 ], [1 , 2 , 2 , 1 ], [1 , 2 , 2 , 1 ]),
82
+ ('SAME' , [1 , 28 , 28 , 3 ], [1 , 2 , 2 , 1 ], [1 , 1 , 1 , 1 ]),
83
+ ('SAME' , [1 , 28 , 28 , 3 ], [1 , 5 , 5 , 1 ], [1 , 2 , 2 , 1 ]),
84
+ ('SAME' , [1 , 28 , 28 , 3 ], [1 , 5 , 5 , 1 ], [1 , 1 , 1 , 1 ]),
85
+ ('SAME' , [1 , 28 , 28 , 3 ], [1 , 5 , 2 , 1 ], [1 , 2 , 2 , 1 ]),
86
+ ('SAME' , [1 , 28 , 28 , 3 ], [1 , 2 , 5 , 1 ], [1 , 1 , 1 , 1 ]),
87
+ ('SAME' , [32 , 8 , 8 , 2048 ], [1 , 3 , 3 , 1 ], [1 , 2 , 2 , 1 ]),
88
+ ('SAME' , [32 , 8 , 8 , 2048 ], [1 , 3 , 3 , 1 ], [1 , 1 , 1 , 1 ]),
89
+ ('VALID' , [32 , 35 , 35 , 288 ], [1 , 3 , 3 , 1 ], [1 , 1 , 1 , 1 ]),
90
+ ('VALID' , [32 , 35 , 35 , 288 ], [1 , 2 , 2 , 1 ], [1 , 2 , 2 , 1 ]),
91
+ ]
92
+ for idx , v in enumerate (data ):
93
+ yield (idx ,) + v
94
+ else :
95
+ raise ValueError ("kind not known" )
96
+
97
+
50
98
class Tf2OnnxBackendTests (unittest .TestCase ):
51
99
def setUp (self ):
52
100
self .maxDiff = None
@@ -198,20 +246,26 @@ def test_multinomial1(self):
198
246
self .assertEqual (expected .shape , actual .shape )
199
247
200
248
def test_maxppol (self ):
201
- x_val = make_xval ((1 , 4 , 4 , 1 ))
202
- x = tf .placeholder (tf .float32 , shape = x_val .shape , name = _TFINPUT )
203
- mp = tf .nn .max_pool (x , [1 , 2 , 2 , 1 ], _STRIDE1x1 , padding = "VALID" )
204
- output = tf .identity (mp , name = _TFOUTPUT )
205
- actual , expected = self ._run (output , {x : x_val }, {_INPUT : x_val })
206
- self .assertAllClose (expected , actual )
249
+ for p in get_conv_getdata ():
250
+ idx , padding , x_shape , ksize , strides = p
251
+ tf .reset_default_graph ()
252
+ x_val = make_xval (x_shape )
253
+ x = tf .placeholder (tf .float32 , shape = x_val .shape , name = _TFINPUT )
254
+ mp = tf .nn .max_pool (x , ksize , strides , padding = padding )
255
+ output = tf .identity (mp , name = _TFOUTPUT )
256
+ actual , expected = self ._run (output , {x : x_val }, {_INPUT : x_val })
257
+ self .assertAllClose (expected , actual , err_msg = str (p ))
207
258
208
259
def test_avgppol (self ):
209
- x_val = make_xval ((1 , 4 , 4 , 1 ))
210
- x = tf .placeholder (tf .float32 , shape = x_val .shape , name = _TFINPUT )
211
- mp = tf .nn .avg_pool (x , [1 , 2 , 2 , 1 ], _STRIDE1x1 , padding = "VALID" )
212
- output = tf .identity (mp , name = _TFOUTPUT )
213
- actual , expected = self ._run (output , {x : x_val }, {_INPUT : x_val })
214
- self .assertAllClose (expected , actual )
260
+ for p in get_conv_getdata (kind = 0 ):
261
+ idx , padding , x_shape , ksize , strides = p
262
+ tf .reset_default_graph ()
263
+ x_val = make_xval (x_shape )
264
+ x = tf .placeholder (tf .float32 , shape = x_val .shape , name = _TFINPUT )
265
+ mp = tf .nn .avg_pool (x , ksize , strides , padding = padding )
266
+ output = tf .identity (mp , name = _TFOUTPUT )
267
+ actual , expected = self ._run (output , {x : x_val }, {_INPUT : x_val })
268
+ self .assertAllClose (expected , actual , err_msg = str (p ))
215
269
216
270
def _conv_test (self , x_val , w , strides = None , padding = "VALID" , dilations = None ):
217
271
if strides is None :
@@ -753,13 +807,20 @@ def test_pow_scalar(self):
753
807
754
808
@unittest .skipIf (BACKEND == "caffe2" , "not supported correctly in caffe2" )
755
809
def test_pad (self ):
756
- x_val = np .array ([[1.0 , 1.2 ], [2.3 , 3.4 ], [4.5 , 5.7 ]], dtype = np .float32 )
757
- x = tf .placeholder (tf .float32 , x_val .shape , name = _TFINPUT )
758
- paddings = tf .constant ([[0 , 0 , ], [2 , 0 ]])
759
- op = tf .pad (x , paddings , "CONSTANT" )
760
- output = tf .identity (op , name = _TFOUTPUT )
761
- actual , expected = self ._run (output , {x : x_val }, {_INPUT : x_val })
762
- self .assertAllClose (expected , actual )
810
+ params = [
811
+ ("CONSTANT" , [[1 , 1 ], [2 , 2 ]], [[1.0 , 1.2 ], [2.3 , 3.4 ], [4.5 , 5.7 ]]),
812
+ ("CONSTANT" , [[0 , 0 ], [3 , 3 ], [3 , 3 ], [0 , 0 ]], np .random .randn (1 , 3 , 4 , 5 ).astype (np .float32 )),
813
+ ]
814
+ for p in params :
815
+ tf .reset_default_graph ()
816
+ mode , pad , xv = p
817
+ x_val = np .array (xv , dtype = np .float32 )
818
+ x = tf .placeholder (tf .float32 , x_val .shape , name = _TFINPUT )
819
+ paddings = tf .constant (pad )
820
+ op = tf .pad (x , paddings , mode )
821
+ output = tf .identity (op , name = _TFOUTPUT )
822
+ actual , expected = self ._run (output , {x : x_val }, {_INPUT : x_val })
823
+ self .assertAllClose (expected , actual , err_msg = str (p ))
763
824
764
825
@unittest .skipIf (BACKEND in ["caffe2" , "onnxmsrt" ], "not supported correctly in caffe2" )
765
826
def test_randomuniform (self ):
0 commit comments