Skip to content

Commit c57f5fc

Browse files
committed
add tests for asymmetric padding in conv2d, max_pool and conv2d_backprop_input
1 parent 4a0c11f commit c57f5fc

19 files changed

+49
-6
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

testdata/dnn/tensorflow/generate_tf2_models.py

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,33 @@ def gen_data(placeholder):
1515
return np.random.standard_normal(shape).astype(placeholder.dtype.as_numpy_dtype())
1616

1717

18-
def writeBlob(data, name):
18+
def writeBlob(data, name, nchw = False):
1919
try:
2020
data = data.numpy()
2121
except:
2222
pass
2323

24-
if data.ndim == 4:
24+
if not nchw and data.ndim == 4:
2525
# NHWC->NCHW
2626
data = data.transpose(0, 3, 1, 2)
27-
elif data.ndim == 5:
27+
elif not nchw and data.ndim == 5:
2828
# NDHWC->NCDHW
2929
data = data.transpose(0, 4, 1, 2, 3)
3030

3131
data = np.ascontiguousarray(data.astype(np.float32))
3232
np.save(name + '.npy', data)
3333

3434

35-
def save(model, name, **kwargs):
35+
def save(model, name, nchw = False, **kwargs):
3636
model.save(name)
3737

3838
assert(len(kwargs) == 1)
3939

4040
inputData = gen_data(next(iter(kwargs.values())))
4141
outputData = model(inputData)
4242

43-
writeBlob(inputData, name + '_in')
44-
writeBlob(outputData, name + '_out')
43+
writeBlob(inputData, name + '_in', nchw)
44+
writeBlob(outputData, name + '_out', nchw)
4545

4646
# Freeze model
4747
loaded = tf.saved_model.load(name)
@@ -78,6 +78,49 @@ def save(model, name, **kwargs):
7878
])
7979
save(model, 'tf2_permute_nhwc_ncwh', average_pooling2d_input=tf.TensorSpec(shape=[None, 4, 6, 3], dtype=tf.float32))
8080
################################################################################
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))
81124

82125
# Uncomment to print the final graph.
83126
# with tf.io.gfile.GFile('tf2_prelu_net.pb', 'rb') as f:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)