Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,19 @@ def func(x):
# rtol is a bit high, 2 values have a bit high error. Maybe use different input data.
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val}, rtol=0.01)

def test_depthwiseconv_shared_kernel(self):
x_shape = [1, 3, 4, 3]
kernel_shape = [3, 3, 3, 3]
x_val = np.arange(1, 1 + np.prod(x_shape)).astype("float32").reshape(x_shape)
kernel_val = np.arange(1, 1 + np.prod(kernel_shape)).astype("float32").reshape(kernel_shape)
def func(x, y):
kernel = tf.constant(kernel_val, dtype=tf.float32, name='k')
conv1 = tf.nn.depthwise_conv2d(x, kernel, strides=[1, 1, 1, 1], padding='VALID')
conv2 = tf.nn.depthwise_conv2d(y, kernel, strides=[1, 1, 1, 1], padding='VALID')
conv = tf.add(conv1, conv2)
return tf.identity(conv, name=_TFOUTPUT)
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val, _INPUT1: x_val}, rtol=0.08)

@check_tf_min_version("1.14", "tf depthwise_conv2d dilations")
@check_opset_min_version(11, "non-const pads")
def test_depthwiseconv_dilations(self):
Expand Down
32 changes: 13 additions & 19 deletions tf2onnx/onnx_opset/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,22 @@ def conv_convert_inputs(ctx, node, with_kernel=False, new_kernel_shape=None,
if with_kernel:
# Some ONNX convolution ops require to reshape the kernel (ie. depthwise_conv2d).
if new_kernel_shape:
if node.inputs[1].is_const():
input_node = node.inputs[1]
val = input_node.get_tensor_value(as_list=False)
val = np.reshape(val, new_kernel_shape)
input_node.set_tensor_value(val)
kernel_name = node.input[1]
if ctx.opset < 5:
# Old reshape takes new shape as attribute.
reshape = ctx.insert_new_node_on_input(node, "Reshape", kernel_name)
reshape.set_attr("shape", new_kernel_shape)
reshape.skip_conversion = True
else:
kernel_name = node.input[1]
if ctx.opset < 5:
# Old reshape takes new shape as attribute.
reshape = ctx.insert_new_node_on_input(node, "Reshape", kernel_name)
reshape.set_attr("shape", new_kernel_shape)
reshape.skip_conversion = True
else:
# New reshape takes new shape as input[1].
shape_name = utils.make_name(node.name)
ctx.make_const(shape_name, np.array(new_kernel_shape, dtype=np.int64))
# New reshape takes new shape as input[1].
shape_name = utils.make_name(node.name)
ctx.make_const(shape_name, np.array(new_kernel_shape, dtype=np.int64))

reshape = ctx.make_node("Reshape", [kernel_name, shape_name])
ctx.replace_input(node, kernel_name, reshape.output[0], 1)
reshape = ctx.make_node("Reshape", [kernel_name, shape_name])
ctx.replace_input(node, kernel_name, reshape.output[0], 1)

reshape.skip_conversion = True
ctx.set_shape(reshape.output[0], new_kernel_shape)
reshape.skip_conversion = True
ctx.set_shape(reshape.output[0], new_kernel_shape)

# Get kernel (may have be changed to a reshape above).
kernel_node = node.inputs[1]
Expand Down