Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix][Relay][Keras] Fix SeparableConv2D conversion in dilation_rate attribute #15122

Merged
merged 3 commits into from
Jun 21, 2023

Conversation

jikechao
Copy link
Contributor

@jikechao jikechao commented Jun 19, 2023

This PR fixed a bug about dilation_rate in SeparableConv2D.

The attribute dilation_rate about SeparableConv2D layer has the following constraints in Keras documentation:

dilation_rate: An integer or tuple/list of 2 integers, specifying the dilation rate to use for dilated convolution.

TVM always considered the dilation_rate as a default value. This will lead to wrong calculation results.

This PR fix this bug and added the bug-triggering test cases.

Steps to reproduce

import tvm
import tvm.relay as relay
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers, models
input_shape = (2, 32,32,3)
input_data = np.random.random(size=input_shape)
x = layers.Input(shape=input_shape[1:], dtype='float32')

kwargs={'filters':2,'kernel_size':[3, 3],'dilation_rate':2,} 
layer = keras.layers.SeparableConv2D(**kwargs)
layer.set_weights(layer.get_weights())

y = layer(x)
model = models.Model(x, y)
model.summary()
res_keras = model(input_data)

shape_dict = {'input_1': input_shape}
mod, params = relay.frontend.from_keras(model, shape_dict,layout='NHWC')
with tvm.transform.PassContext(opt_level=3):
    model = relay.build_module.create_executor("graph", mod, tvm.cpu(0), 'llvm', params).evaluate()

test_x_tvm = input_data
res_tvm = model(tvm.nd.array(test_x_tvm.astype('float32'))).numpy()

np.testing.assert_allclose(res_keras, res_tvm, atol=1e-3, rtol=1e-3)

image

cc @Hzfengsy @echuraev @leandron

jikechao added 2 commits June 20, 2023 00:24
fix the _convert_separable_convolution. The dilation_rate always be the default value (e.g., [1,1]).
@tvm-bot
Copy link
Collaborator

tvm-bot commented Jun 19, 2023

Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.

Generated by tvm-bot

Copy link
Contributor

@echuraev echuraev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your PR! LGTM!

@masahi masahi merged commit 54b9741 into apache:main Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants