@@ -15,33 +15,33 @@ def gen_data(placeholder):
15
15
return np .random .standard_normal (shape ).astype (placeholder .dtype .as_numpy_dtype ())
16
16
17
17
18
- def writeBlob (data , name ):
18
+ def writeBlob (data , name , nchw = False ):
19
19
try :
20
20
data = data .numpy ()
21
21
except :
22
22
pass
23
23
24
- if data .ndim == 4 :
24
+ if not nchw and data .ndim == 4 :
25
25
# NHWC->NCHW
26
26
data = data .transpose (0 , 3 , 1 , 2 )
27
- elif data .ndim == 5 :
27
+ elif not nchw and data .ndim == 5 :
28
28
# NDHWC->NCDHW
29
29
data = data .transpose (0 , 4 , 1 , 2 , 3 )
30
30
31
31
data = np .ascontiguousarray (data .astype (np .float32 ))
32
32
np .save (name + '.npy' , data )
33
33
34
34
35
- def save (model , name , ** kwargs ):
35
+ def save (model , name , nchw = False , ** kwargs ):
36
36
model .save (name )
37
37
38
38
assert (len (kwargs ) == 1 )
39
39
40
40
inputData = gen_data (next (iter (kwargs .values ())))
41
41
outputData = model (inputData )
42
42
43
- writeBlob (inputData , name + '_in' )
44
- writeBlob (outputData , name + '_out' )
43
+ writeBlob (inputData , name + '_in' , nchw )
44
+ writeBlob (outputData , name + '_out' , nchw )
45
45
46
46
# Freeze model
47
47
loaded = tf .saved_model .load (name )
@@ -78,6 +78,49 @@ def save(model, name, **kwargs):
78
78
])
79
79
save (model , 'tf2_permute_nhwc_ncwh' , average_pooling2d_input = tf .TensorSpec (shape = [None , 4 , 6 , 3 ], dtype = tf .float32 ))
80
80
################################################################################
81
+ # TF 2.5.0 + python 3.6.13
82
+ tf .keras .backend .set_image_data_format ('channels_first' )
83
+ x = tf .keras .layers .Input (batch_shape = (1 , 2 , 3 , 4 ), name = 'x' )
84
+ kernel = np .random .standard_normal ((3 , 3 , 2 , 3 )).astype (np .float32 )
85
+ y = tf .nn .conv2d (x , tf .constant (kernel , dtype = tf .float32 ), data_format = 'NCHW' , padding = [[0 , 0 ], [0 , 0 ], [2 , 1 ], [2 , 1 ]], strides = [1 , 1 , 3 , 2 ])
86
+ model = tf .keras .Model (x , y )
87
+ save (model , 'conv2d_asymmetric_pads_nchw' , True , x = tf .TensorSpec (shape = [1 , 2 , 3 , 4 ], dtype = tf .float32 ))
88
+ ################################################################################
89
+ # TF 2.5.0 + python 3.6.13
90
+ tf .keras .backend .set_image_data_format ('channels_last' )
91
+ x = tf .keras .layers .Input (batch_shape = (1 , 3 , 4 , 2 ), name = 'x' )
92
+ kernel = np .random .standard_normal ((3 , 3 , 2 , 3 )).astype (np .float32 )
93
+ y = tf .nn .conv2d (x , tf .constant (kernel , dtype = tf .float32 ), data_format = 'NHWC' , padding = [[0 , 0 ], [2 , 1 ], [2 , 1 ], [0 , 0 ]], strides = [1 , 3 , 2 , 1 ])
94
+ model = tf .keras .Model (x , y )
95
+ save (model , 'conv2d_asymmetric_pads_nhwc' , False , x = tf .TensorSpec (shape = [1 , 3 , 4 , 2 ], dtype = tf .float32 ))
96
+ ################################################################################
97
+ # TF 2.5.0 + python 3.6.13
98
+ tf .keras .backend .set_image_data_format ('channels_first' )
99
+ x = tf .keras .layers .Input (batch_shape = (1 , 1 , 2 , 3 ), name = 'x' )
100
+ y = tf .nn .max_pool (x , ksize = 2 , data_format = "NCHW" , padding = [[0 , 0 ], [0 , 0 ], [1 , 0 ], [1 , 1 ]], strides = [1 , 1 , 3 , 2 ])
101
+ model = tf .keras .Model (x , y )
102
+ save (model , 'max_pool2d_asymmetric_pads_nchw' , True , x = tf .TensorSpec (shape = (1 , 1 , 2 , 3 ), dtype = tf .float32 ))
103
+ ################################################################################
104
+ # TF 2.5.0 + python 3.6.13
105
+ tf .keras .backend .set_image_data_format ('channels_last' )
106
+ x = tf .keras .layers .Input (batch_shape = (1 , 2 , 3 , 1 ), name = 'x' )
107
+ y = tf .nn .max_pool (x , ksize = 2 , data_format = "NHWC" , padding = [[0 , 0 ], [1 , 0 ], [1 , 1 ], [0 , 0 ]], strides = [1 , 3 , 2 , 1 ])
108
+ model = tf .keras .Model (x , y )
109
+ save (model , 'max_pool2d_asymmetric_pads_nhwc' , False , x = tf .TensorSpec (shape = (1 , 2 , 3 , 1 ), dtype = tf .float32 ))
110
+ ################dd################################################################
111
+ tf .keras .backend .set_image_data_format ('channels_first' )
112
+ x = tf .keras .layers .Input (batch_shape = (1 , 3 , 2 , 3 ), name = 'x' )
113
+ kernel = np .random .standard_normal ((3 , 3 , 2 , 3 )).astype (np .float32 )
114
+ y = tf .compat .v1 .nn .conv2d_backprop_input (input_sizes = tf .constant ([1 , 2 , 3 , 4 ]), filter = kernel , out_backprop = x , data_format = "NCHW" , padding = [[0 , 0 ], [0 , 0 ], [2 , 1 ], [2 , 1 ]], strides = [1 , 1 , 3 , 2 ])
115
+ model = tf .keras .Model (x , y )
116
+ save (model , 'conv2d_backprop_input_asymmetric_pads_nchw' , True , x = tf .TensorSpec (shape = (1 , 3 , 2 , 3 ), dtype = tf .float32 ))
117
+ ################################################################################
118
+ tf .keras .backend .set_image_data_format ('channels_last' )
119
+ x = tf .keras .layers .Input (batch_shape = (1 , 2 , 3 , 3 ), name = 'x' )
120
+ kernel = np .random .standard_normal ((3 , 3 , 2 , 3 )).astype (np .float32 )
121
+ y = tf .compat .v1 .nn .conv2d_backprop_input (input_sizes = tf .constant ([1 , 3 , 4 , 2 ]), filter = kernel , out_backprop = x , data_format = "NHWC" , padding = [[0 , 0 ], [2 , 1 ], [2 , 1 ], [0 , 0 ]], strides = [1 , 3 , 2 , 1 ])
122
+ model = tf .keras .Model (x , y )
123
+ save (model , 'conv2d_backprop_input_asymmetric_pads_nhwc' , False , x = tf .TensorSpec (shape = (1 , 2 , 3 , 3 ), dtype = tf .float32 ))
81
124
82
125
# Uncomment to print the final graph.
83
126
# with tf.io.gfile.GFile('tf2_prelu_net.pb', 'rb') as f:
0 commit comments