From 5f294fb90d14b5d42413e2f55cc72be0a6752079 Mon Sep 17 00:00:00 2001
From: jonyguo <guozhijian@huawei.com>
Date: Tue, 18 Aug 2020 21:11:10 +0800
Subject: [PATCH] fix: random affine parameter check

---
 .../dataset/transforms/vision/c_transforms.py |  4 ++-
 .../dataset/transforms/vision/validators.py   |  7 +++++
 tests/ut/python/dataset/test_random_affine.py | 26 +++++++++++++++++++
 3 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/mindspore/dataset/transforms/vision/c_transforms.py b/mindspore/dataset/transforms/vision/c_transforms.py
index 66b055ab9f..4c4b1f7eef 100644
--- a/mindspore/dataset/transforms/vision/c_transforms.py
+++ b/mindspore/dataset/transforms/vision/c_transforms.py
@@ -262,7 +262,8 @@ class RandomAffine(cde.RandomAffineOp):
             - Inter.BICUBIC, means resample method is bicubic interpolation.
 
         fill_value (tuple or int, optional): Optional fill_value to fill the area outside the transform
-            in the output image. Used only in Pillow versions > 5.0.0 (default=0, filling is performed).
+            in the output image. There must be three elements in tuple and the value of single element is [0, 255].
+            Used only in Pillow versions > 5.0.0 (default=0, filling is performed).
 
     Raises:
         ValueError: If degrees is negative.
@@ -274,6 +275,7 @@ class RandomAffine(cde.RandomAffineOp):
         TypeError: If translate is specified but is not list or a tuple of length 2.
         TypeError: If scale is not a list or tuple of length 2.''
         TypeError: If shear is not a list or tuple of length 2 or 4.
+        TypeError: If fill_value is not a single integer or a 3-tuple.
 
     Examples:
         >>> c_transform.RandomAffine(degrees=15, translate=(0.1, 0.1), scale=(0.9, 1.1))
diff --git a/mindspore/dataset/transforms/vision/validators.py b/mindspore/dataset/transforms/vision/validators.py
index cc95c93162..e2772a8f94 100644
--- a/mindspore/dataset/transforms/vision/validators.py
+++ b/mindspore/dataset/transforms/vision/validators.py
@@ -509,6 +509,8 @@ def check_random_affine(method):
             if len(scale) == 2:
                 for i, s in enumerate(scale):
                     check_positive(s, "scale[{}]".format(i))
+                if scale[0] > scale[1]:
+                    raise ValueError("Input scale[1] must be equal to or greater than scale[0].")
             else:
                 raise TypeError("scale should be a list or tuple of length 2.")
 
@@ -519,6 +521,11 @@ def check_random_affine(method):
             else:
                 if len(shear) not in (2, 4):
                     raise TypeError("shear must be of length 2 or 4.")
+                if len(shear) == 2 and shear[0] > shear[1]:
+                    raise ValueError("Input shear[1] must be equal to or greater than shear[0]")
+                if len(shear) == 4 and (shear[0] > shear[1] or shear[2] > shear[3]):
+                    raise ValueError("Input shear[1] must be equal to or greater than shear[0] and "
+                                     "shear[3] must be equal to or greater than shear[2].")
 
         type_check(resample, (Inter,), "resample")
 
diff --git a/tests/ut/python/dataset/test_random_affine.py b/tests/ut/python/dataset/test_random_affine.py
index 68267030a7..87ee26c5b0 100644
--- a/tests/ut/python/dataset/test_random_affine.py
+++ b/tests/ut/python/dataset/test_random_affine.py
@@ -190,6 +190,12 @@ def test_random_affine_exception_scale_value():
         logger.info("Got an exception in DE: {}".format(str(e)))
         assert str(e) == "Input scale[0] must be greater than 0."
 
+    try:
+        _ = py_vision.RandomAffine(degrees=15, scale=(2.0, 1.1))
+    except ValueError as e:
+        logger.info("Got an exception in DE: {}".format(str(e)))
+        assert str(e) == "Input scale[1] must be equal to or greater than scale[0]."
+
 
 def test_random_affine_exception_shear_value():
     """
@@ -202,6 +208,26 @@ def test_random_affine_exception_shear_value():
         logger.info("Got an exception in DE: {}".format(str(e)))
         assert str(e) == "Input shear must be greater than 0."
 
+    try:
+        _ = py_vision.RandomAffine(degrees=15, shear=(5, 1))
+    except ValueError as e:
+        logger.info("Got an exception in DE: {}".format(str(e)))
+        assert str(e) == "Input shear[1] must be equal to or greater than shear[0]"
+
+    try:
+        _ = py_vision.RandomAffine(degrees=15, shear=(5, 1, 2, 8))
+    except ValueError as e:
+        logger.info("Got an exception in DE: {}".format(str(e)))
+        assert str(e) == "Input shear[1] must be equal to or greater than shear[0] and " \
+                         "shear[3] must be equal to or greater than shear[2]."
+
+    try:
+        _ = py_vision.RandomAffine(degrees=15, shear=(5, 9, 2, 1))
+    except ValueError as e:
+        logger.info("Got an exception in DE: {}".format(str(e)))
+        assert str(e) == "Input shear[1] must be equal to or greater than shear[0] and " \
+                         "shear[3] must be equal to or greater than shear[2]."
+
 
 def test_random_affine_exception_degrees_size():
     """