|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- # Copyright 2022 Huawei Technologies Co., Ltd
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
-
- """Example for natural robustness methods."""
-
- import pytest
- import numpy as np
- from mindspore import context
-
- from mindarmour.natural_robustness.transform.image import Translate, Curve, Perspective, Scale, Shear, Rotate, \
- SaltAndPepperNoise, NaturalNoise, GaussianNoise, UniformNoise, MotionBlur, GaussianBlur, GradientBlur, Contrast,\
- GradientLuminance
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_perspective():
- """
- Feature: Test image perspective.
- Description: Image will be transform for given perspective projection.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
- image = np.random.random((32, 32, 3))
- ori_pos = [[0, 0], [0, 800], [800, 0], [800, 800]]
- dst_pos = [[50, 0], [0, 800], [780, 0], [800, 800]]
- trans = Perspective(ori_pos, dst_pos)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_uniform_noise():
- """
- Feature: Test image uniform noise.
- Description: Add uniform image in image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
- image = np.random.random((32, 32, 3))
- trans = UniformNoise(factor=0.1)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_gaussian_noise():
- """
- Feature: Test image gaussian noise.
- Description: Add gaussian image in image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
- image = np.random.random((32, 32, 3))
- trans = GaussianNoise(factor=0.1)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_contrast():
- """
- Feature: Test image contrast.
- Description: Adjust image contrast.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
- image = np.random.random((32, 32, 3))
- trans = Contrast(alpha=0.3, beta=0)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_gaussian_blur():
- """
- Feature: Test image gaussian blur.
- Description: Add gaussian blur to image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
- image = np.random.random((32, 32, 3))
- trans = GaussianBlur(ksize=5)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_salt_and_pepper_noise():
- """
- Feature: Test image salt and pepper noise.
- Description: Add salt and pepper to image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
- image = np.random.random((32, 32, 3))
- trans = SaltAndPepperNoise(factor=0.01)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_translate():
- """
- Feature: Test image translate.
- Description: Translate an image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
- image = np.random.random((32, 32, 3))
- trans = Translate(x_bias=0.1, y_bias=0.1)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_scale():
- """
- Feature: Test image scale.
- Description: Scale an image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
- image = np.random.random((32, 32, 3))
- trans = Scale(factor_x=0.7, factor_y=0.7)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_shear():
- """
- Feature: Test image shear.
- Description: Shear an image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
- image = np.random.random((32, 32, 3))
- trans = Shear(factor=0.2)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_rotate():
- """
- Feature: Test image rotate.
- Description: Rotate an image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
- image = np.random.random((32, 32, 3))
- trans = Rotate(angle=20)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_curve():
- """
- Feature: Test image curve.
- Description: Transform an image with curve.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
- image = np.random.random((32, 32, 3))
- trans = Curve(curves=1.5, depth=1.5, mode='horizontal')
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_natural_noise():
- """
- Feature: Test natural noise.
- Description: Add natural noise to an.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
- image = np.random.random((32, 32, 3))
- trans = NaturalNoise(ratio=0.0001, k_x_range=(1, 30), k_y_range=(1, 10), auto_param=True)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_gradient_luminance():
- """
- Feature: Test gradient luminance.
- Description: Adjust image luminance.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
- image = np.random.random((32, 32, 3))
- height, width = image.shape[:2]
- point = (height // 4, width // 2)
- start = (255, 255, 255)
- end = (0, 0, 0)
- scope = 0.3
- bright_rate = 0.4
- trans = GradientLuminance(start, end, start_point=point, scope=scope, pattern='dark', bright_rate=bright_rate,
- mode='horizontal')
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_motion_blur():
- """
- Feature: Test motion blur.
- Description: Add motion blur to an image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
- image = np.random.random((32, 32, 3))
- angle = -10.5
- i = 3
- trans = MotionBlur(degree=i, angle=angle)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_gradient_blur():
- """
- Feature: Test gradient blur.
- Description: Add gradient blur to an image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
- image = np.random.random((32, 32, 3))
- number = 10
- h, w = image.shape[:2]
- point = (int(h / 5), int(w / 5))
- center = False
- trans = GradientBlur(point, number, center)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_perspective_ascend():
- """
- Feature: Test image perspective.
- Description: Image will be transform for given perspective projection.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- image = np.random.random((32, 32, 3))
- ori_pos = [[0, 0], [0, 800], [800, 0], [800, 800]]
- dst_pos = [[50, 0], [0, 800], [780, 0], [800, 800]]
- trans = Perspective(ori_pos, dst_pos)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_uniform_noise_ascend():
- """
- Feature: Test image uniform noise.
- Description: Add uniform image in image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- image = np.random.random((32, 32, 3))
- trans = UniformNoise(factor=0.1)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_gaussian_noise_ascend():
- """
- Feature: Test image gaussian noise.
- Description: Add gaussian image in image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- image = np.random.random((32, 32, 3))
- trans = GaussianNoise(factor=0.1)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_contrast_ascend():
- """
- Feature: Test image contrast.
- Description: Adjust image contrast.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- image = np.random.random((32, 32, 3))
- trans = Contrast(alpha=0.3, beta=0)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_gaussian_blur_ascend():
- """
- Feature: Test image gaussian blur.
- Description: Add gaussian blur to image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- image = np.random.random((32, 32, 3))
- trans = GaussianBlur(ksize=5)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_salt_and_pepper_noise_ascend():
- """
- Feature: Test image salt and pepper noise.
- Description: Add salt and pepper to image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- image = np.random.random((32, 32, 3))
- trans = SaltAndPepperNoise(factor=0.01)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_translate_ascend():
- """
- Feature: Test image translate.
- Description: Translate an image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- image = np.random.random((32, 32, 3))
- trans = Translate(x_bias=0.1, y_bias=0.1)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_card
- @pytest.mark.component_ascend_mindarmour
- def test_scale_ascend():
- """
- Feature: Test image scale.
- Description: Scale an image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- image = np.random.random((32, 32, 3))
- trans = Scale(factor_x=0.7, factor_y=0.7)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_shear_ascend():
- """
- Feature: Test image shear.
- Description: Shear an image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- image = np.random.random((32, 32, 3))
- trans = Shear(factor=0.2)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_rotate_ascend():
- """
- Feature: Test image rotate.
- Description: Rotate an image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- image = np.random.random((32, 32, 3))
- trans = Rotate(angle=20)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_curve_ascend():
- """
- Feature: Test image curve.
- Description: Transform an image with curve.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- image = np.random.random((32, 32, 3))
- trans = Curve(curves=1.5, depth=1.5, mode='horizontal')
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_natural_noise_ascend():
- """
- Feature: Test natural noise.
- Description: Add natural noise to an.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- image = np.random.random((32, 32, 3))
- trans = NaturalNoise(ratio=0.0001, k_x_range=(1, 30), k_y_range=(1, 10), auto_param=True)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_gradient_luminance_ascend():
- """
- Feature: Test gradient luminance.
- Description: Adjust image luminance.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- image = np.random.random((32, 32, 3))
- height, width = image.shape[:2]
- point = (height // 4, width // 2)
- start = (255, 255, 255)
- end = (0, 0, 0)
- scope = 0.3
- bright_rate = 0.4
- trans = GradientLuminance(start, end, start_point=point, scope=scope, pattern='dark', bright_rate=bright_rate,
- mode='horizontal')
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_motion_blur_ascend():
- """
- Feature: Test motion blur.
- Description: Add motion blur to an image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- image = np.random.random((32, 32, 3))
- angle = -10.5
- i = 3
- trans = MotionBlur(degree=i, angle=angle)
- dst = trans(image)
- print(dst)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_card
- @pytest.mark.component_mindarmour
- def test_gradient_blur_ascend():
- """
- Feature: Test gradient blur.
- Description: Add gradient blur to an image.
- Expectation: success.
- """
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- image = np.random.random((32, 32, 3))
- number = 10
- h, w = image.shape[:2]
- point = (int(h / 5), int(w / 5))
- center = False
- trans = GradientBlur(point, number, center)
- dst = trans(image)
- print(dst)
|