From ce84aa805bf7e06143be172b8e04e2a5a6733bfd Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Wed, 7 Jun 2023 21:59:51 +0800 Subject: [PATCH 1/9] fix the wrong calculation logic of cropping2d The implementation of cropping2D is wrong. This pr fix it. --- python/tvm/relay/frontend/keras.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/tvm/relay/frontend/keras.py b/python/tvm/relay/frontend/keras.py index 86131a6216bc..567f058beea1 100644 --- a/python/tvm/relay/frontend/keras.py +++ b/python/tvm/relay/frontend/keras.py @@ -816,8 +816,8 @@ def _convert_cropping( int32_max = np.iinfo(np.int32).max return _op.strided_slice( inexpr, - begin=[0, 0, crop_t, crop_l], - end=[int32_max, int32_max, in_h - crop_b, in_w - crop_r], + begin=[0, crop_t, crop_l, 0], + end=[int32_max, in_h - crop_b, in_w - crop_r, int32_max], ) From 2a3747132a3dbd6d5566c9edc22080abef7216ee Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Wed, 7 Jun 2023 22:03:40 +0800 Subject: [PATCH 2/9] add a test case to caputure the bug --- tests/python/frontend/keras/test_forward.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 4a3b0108f888..529873df14cf 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -439,6 +439,8 @@ def test_forward_crop(self, keras_mod): x = keras_mod.layers.Cropping2D(cropping=((0, 1), (1, 0)))(x) x = keras_mod.layers.Cropping2D(cropping=(1, 0))(x) x = keras_mod.layers.Cropping2D(cropping=0)(x) + x = keras_mod.layers.Cropping2D(cropping=(2, 1))(x) + x = keras_mod.layers.Cropping2D(cropping=(4, 1))(x) x = keras_mod.layers.Add()([x, x]) keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) From ccaf32fc4db43964264ebdd415c4c0b9a804d4c3 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Thu, 8 Jun 2023 10:46:35 +0800 Subject: [PATCH 3/9] Update test_forward.py --- tests/python/frontend/keras/test_forward.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 529873df14cf..24a24332d01a 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -440,7 +440,6 @@ def test_forward_crop(self, keras_mod): x = keras_mod.layers.Cropping2D(cropping=(1, 0))(x) x = keras_mod.layers.Cropping2D(cropping=0)(x) x = keras_mod.layers.Cropping2D(cropping=(2, 1))(x) - x = keras_mod.layers.Cropping2D(cropping=(4, 1))(x) x = keras_mod.layers.Add()([x, x]) keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) From 5d364f3d8e93742acd9ef37be1bec49d46d07147 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Thu, 8 Jun 2023 18:49:42 +0800 Subject: [PATCH 4/9] Update test_forward.py --- tests/python/frontend/keras/test_forward.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 24a24332d01a..4e0b6919b2b3 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -439,11 +439,15 @@ def test_forward_crop(self, keras_mod): x = keras_mod.layers.Cropping2D(cropping=((0, 1), (1, 0)))(x) x = keras_mod.layers.Cropping2D(cropping=(1, 0))(x) x = keras_mod.layers.Cropping2D(cropping=0)(x) - x = keras_mod.layers.Cropping2D(cropping=(2, 1))(x) x = keras_mod.layers.Add()([x, x]) keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) + x = keras_mod.layers.Cropping2D(cropping=(2, 1)) + x = keras_mod.layers.Cropping2D(cropping=(1, 2))(x) + keras_model = keras_mod.models.Model(data, x) + verify_keras_frontend(keras_model) + def test_forward_multi_inputs(self, keras_mod): data1 = keras_mod.layers.Input(shape=(32, 32, 3)) data2 = keras_mod.layers.Input(shape=(32, 32, 3)) From 577240abf2b010309d6d90aa8ee2557c1473ebf2 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Tue, 13 Jun 2023 11:28:23 +0800 Subject: [PATCH 5/9] correct the patch --- python/tvm/relay/frontend/keras.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/python/tvm/relay/frontend/keras.py b/python/tvm/relay/frontend/keras.py index 567f058beea1..1efe064407bf 100644 --- a/python/tvm/relay/frontend/keras.py +++ b/python/tvm/relay/frontend/keras.py @@ -814,10 +814,16 @@ def _convert_cropping( f"Operator {crop_type} is not supported for frontend Keras." ) int32_max = np.iinfo(np.int32).max + if data_layout == 'NHWC': + begin = [0, crop_t, crop_l, 0] + end = [int32_max, in_h - crop_b, in_w - crop_r, int32_max] + else: + begin = [0, 0, crop_t, crop_l] + end = [int32_max, int32_max, in_h - crop_b, in_w - crop_r] return _op.strided_slice( inexpr, - begin=[0, crop_t, crop_l, 0], - end=[int32_max, in_h - crop_b, in_w - crop_r, int32_max], + begin=begin, + end=end, ) From 81cc21526945791a0ad51ff625bbd6920d6f2f3a Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Tue, 13 Jun 2023 12:37:28 +0800 Subject: [PATCH 6/9] Update keras.py --- python/tvm/relay/frontend/keras.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/keras.py b/python/tvm/relay/frontend/keras.py index 1efe064407bf..3f45aee4fc4c 100644 --- a/python/tvm/relay/frontend/keras.py +++ b/python/tvm/relay/frontend/keras.py @@ -814,7 +814,7 @@ def _convert_cropping( f"Operator {crop_type} is not supported for frontend Keras." ) int32_max = np.iinfo(np.int32).max - if data_layout == 'NHWC': + if data_layout == "NHWC": begin = [0, crop_t, crop_l, 0] end = [int32_max, in_h - crop_b, in_w - crop_r, int32_max] else: From ddce339399aadd77fbcb472a5a0695a964c42263 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Tue, 13 Jun 2023 15:56:54 +0800 Subject: [PATCH 7/9] Update test_forward.py --- tests/python/frontend/keras/test_forward.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 4e0b6919b2b3..66175118099c 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -443,10 +443,10 @@ def test_forward_crop(self, keras_mod): keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) - x = keras_mod.layers.Cropping2D(cropping=(2, 1)) + x = keras_mod.layers.Cropping2D(cropping=(2, 1))(data) x = keras_mod.layers.Cropping2D(cropping=(1, 2))(x) keras_model = keras_mod.models.Model(data, x) - verify_keras_frontend(keras_model) + verify_keras_frontend(keras_model, layout="NHWC") def test_forward_multi_inputs(self, keras_mod): data1 = keras_mod.layers.Input(shape=(32, 32, 3)) From c9cb062e5c4e4e20d563a36728ce434cbd30374c Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Tue, 13 Jun 2023 15:59:17 +0800 Subject: [PATCH 8/9] Update test_forward.py --- tests/python/frontend/keras/test_forward.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index 66175118099c..c5c68dfe913e 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -443,6 +443,7 @@ def test_forward_crop(self, keras_mod): keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model) + data = keras_mod.layers.Input(shape=(32, 32, 3)) x = keras_mod.layers.Cropping2D(cropping=(2, 1))(data) x = keras_mod.layers.Cropping2D(cropping=(1, 2))(x) keras_model = keras_mod.models.Model(data, x) From 9e3f8858d602b430a37df2c912d797b0aea2454a Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Tue, 13 Jun 2023 16:04:39 +0800 Subject: [PATCH 9/9] Update test_forward.py --- tests/python/frontend/keras/test_forward.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/python/frontend/keras/test_forward.py b/tests/python/frontend/keras/test_forward.py index c5c68dfe913e..beaf378fc7b5 100644 --- a/tests/python/frontend/keras/test_forward.py +++ b/tests/python/frontend/keras/test_forward.py @@ -441,13 +441,15 @@ def test_forward_crop(self, keras_mod): x = keras_mod.layers.Cropping2D(cropping=0)(x) x = keras_mod.layers.Add()([x, x]) keras_model = keras_mod.models.Model(data, x) - verify_keras_frontend(keras_model) + verify_keras_frontend(keras_model, layout="NHWC") + verify_keras_frontend(keras_model, layout="NHWC") data = keras_mod.layers.Input(shape=(32, 32, 3)) x = keras_mod.layers.Cropping2D(cropping=(2, 1))(data) x = keras_mod.layers.Cropping2D(cropping=(1, 2))(x) keras_model = keras_mod.models.Model(data, x) verify_keras_frontend(keras_model, layout="NHWC") + verify_keras_frontend(keras_model, layout="NCHW") def test_forward_multi_inputs(self, keras_mod): data1 = keras_mod.layers.Input(shape=(32, 32, 3))