• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""
16Testing GaussianBlur Python API
17"""
18import cv2
19
20import mindspore.dataset as ds
21import mindspore.dataset.vision.c_transforms as c_vision
22
23from mindspore import log as logger
24from util import visualize_image, diff_mse
25
26DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
27SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
28IMAGE_FILE = "../data/dataset/apple.jpg"
29
30
31def test_gaussian_blur_pipeline(plot=False):
32    """
33    Test GaussianBlur of c_transforms
34    """
35    logger.info("test_gaussian_blur_pipeline")
36
37    # First dataset
38    dataset1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
39    decode_op = c_vision.Decode()
40    gaussian_blur_op = c_vision.GaussianBlur(3, 3)
41    dataset1 = dataset1.map(operations=decode_op, input_columns=["image"])
42    dataset1 = dataset1.map(operations=gaussian_blur_op, input_columns=["image"])
43
44    # Second dataset
45    dataset2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
46    dataset2 = dataset2.map(operations=decode_op, input_columns=["image"])
47
48    num_iter = 0
49    for data1, data2 in zip(dataset1.create_dict_iterator(num_epochs=1, output_numpy=True),
50                            dataset2.create_dict_iterator(num_epochs=1, output_numpy=True)):
51        if num_iter > 0:
52            break
53        gaussian_blur_ms = data1["image"]
54        original = data2["image"]
55        gaussian_blur_cv = cv2.GaussianBlur(original, (3, 3), 3)
56        mse = diff_mse(gaussian_blur_ms, gaussian_blur_cv)
57        logger.info("gaussian_blur_{}, mse: {}".format(num_iter + 1, mse))
58        assert mse == 0
59        num_iter += 1
60        if plot:
61            visualize_image(original, gaussian_blur_ms, mse, gaussian_blur_cv)
62
63
64def test_gaussian_blur_eager():
65    """
66    Test GaussianBlur with eager mode
67    """
68    logger.info("test_gaussian_blur_eager")
69    img = cv2.imread(IMAGE_FILE)
70
71    img_ms = c_vision.GaussianBlur((3, 5), (3.5, 3.5))(img)
72    img_cv = cv2.GaussianBlur(img, (3, 5), 3.5, 3.5)
73    mse = diff_mse(img_ms, img_cv)
74    assert mse == 0
75
76
77def test_gaussian_blur_exception():
78    """
79    Test GaussianBlur with invalid parameters
80    """
81    logger.info("test_gaussian_blur_exception")
82    try:
83        _ = c_vision.GaussianBlur([2, 2])
84    except ValueError as e:
85        logger.info("Got an exception in GaussianBlur: {}".format(str(e)))
86        assert "not an odd value" in str(e)
87    try:
88        _ = c_vision.GaussianBlur(3.0, [3, 3])
89    except TypeError as e:
90        logger.info("Got an exception in GaussianBlur: {}".format(str(e)))
91        assert "not of type [<class 'int'>, <class 'list'>, <class 'tuple'>]" in str(e)
92    try:
93        _ = c_vision.GaussianBlur(3, -3)
94    except ValueError as e:
95        logger.info("Got an exception in GaussianBlur: {}".format(str(e)))
96        assert "not within the required interval" in str(e)
97    try:
98        _ = c_vision.GaussianBlur(3, [3, 3, 3])
99    except TypeError as e:
100        logger.info("Got an exception in GaussianBlur: {}".format(str(e)))
101        assert "should be a single number or a list/tuple of length 2" in str(e)
102
103
104if __name__ == "__main__":
105    test_gaussian_blur_pipeline(plot=False)
106    test_gaussian_blur_eager()
107    test_gaussian_blur_exception()
108