This is an automated email from the ASF dual-hosted git repository.

echuraev pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new 90b5acca59 [Bugfix][Relay][Keras] Fix the wrong implementation logic 
about cropping2D (#15053)
90b5acca59 is described below

commit 90b5acca59421d67494955c4fc943a5ddbbc7373
Author: Qingchao Shen <[email protected]>
AuthorDate: Thu Jun 15 17:46:37 2023 +0800

    [Bugfix][Relay][Keras] Fix the wrong implementation logic about cropping2D 
(#15053)
    
    * fix the wrong calculation logic of cropping2d
    
    The implementation of cropping2D is wrong. This pr fix it.
    
    * add a test case to caputure the bug
    
    * Update test_forward.py
    
    * Update test_forward.py
    
    * correct the patch
    
    * Update keras.py
    
    * Update test_forward.py
    
    * Update test_forward.py
    
    * Update test_forward.py
---
 python/tvm/relay/frontend/keras.py          | 10 ++++++++--
 tests/python/frontend/keras/test_forward.py | 10 +++++++++-
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/python/tvm/relay/frontend/keras.py 
b/python/tvm/relay/frontend/keras.py
index d963a5d160..8d437027e5 100644
--- a/python/tvm/relay/frontend/keras.py
+++ b/python/tvm/relay/frontend/keras.py
@@ -816,10 +816,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, 0, crop_t, crop_l],
-        end=[int32_max, int32_max, in_h - crop_b, in_w - crop_r],
+        begin=begin,
+        end=end,
     )
 
 
diff --git a/tests/python/frontend/keras/test_forward.py 
b/tests/python/frontend/keras/test_forward.py
index 45935f87f4..cc6421614e 100644
--- a/tests/python/frontend/keras/test_forward.py
+++ b/tests/python/frontend/keras/test_forward.py
@@ -449,7 +449,15 @@ class TestKeras:
         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))

Reply via email to