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 Vol op in DE 17""" 18import numpy as np 19import pytest 20 21import mindspore.dataset as ds 22import mindspore.dataset.audio.transforms as c_audio 23from mindspore import log as logger 24from mindspore.dataset.audio import utils 25 26 27def count_unequal_element(data_expected, data_me, rtol, atol): 28 """ Precision calculation func """ 29 assert data_expected.shape == data_me.shape 30 total_count = len(data_expected.flatten()) 31 error = np.abs(data_expected - data_me) 32 greater = np.greater(error, atol + np.abs(data_expected) * rtol) 33 loss_count = np.count_nonzero(greater) 34 assert (loss_count / total_count) < rtol, \ 35 "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}". \ 36 format(data_expected[greater], data_me[greater], error[greater]) 37 38 39def allclose_nparray(data_expected, data_me, rtol, atol, equal_nan=True): 40 """ Precision calculation formula """ 41 if np.any(np.isnan(data_expected)): 42 assert np.allclose(data_me, data_expected, rtol, atol, equal_nan=equal_nan) 43 elif not np.allclose(data_me, data_expected, rtol, atol, equal_nan=equal_nan): 44 count_unequal_element(data_expected, data_me, rtol, atol) 45 46 47def test_func_vol_eager(): 48 """ mindspore eager mode normal testcase:vol op""" 49 50 logger.info("check vol op output") 51 ndarr_in = np.array([[0.3667, 0.5295, 0.2949, 0.4508, 0.6457, 0.3625, 0.4377, 0.3568], 52 [0.6488, 0.6525, 0.5140, 0.6725, 0.9261, 0.0609, 0.3910, 0.4608], 53 [0.0454, 0.0487, 0.6990, 0.1637, 0.5763, 0.1086, 0.5343, 0.4699], 54 [0.9993, 0.0776, 0.3498, 0.0429, 0.1588, 0.3061, 0.1166, 0.3716], 55 [0.7625, 0.2410, 0.8888, 0.5027, 0.0913, 0.2520, 0.5625, 0.9873]]).astype(np.float32) 56 # cal from benchmark 57 out_expect = np.array([[0.0733, 0.1059, 0.0590, 0.0902, 0.1291, 0.0725, 0.0875, 0.0714], 58 [0.1298, 0.1305, 0.1028, 0.1345, 0.1852, 0.0122, 0.0782, 0.0922], 59 [0.0091, 0.0097, 0.1398, 0.0327, 0.1153, 0.0217, 0.1069, 0.0940], 60 [0.1999, 0.0155, 0.0700, 0.0086, 0.0318, 0.0612, 0.0233, 0.0743], 61 [0.1525, 0.0482, 0.1778, 0.1005, 0.0183, 0.0504, 0.1125, 0.1975]]) 62 op = c_audio.Vol(gain=0.2, gain_type=utils.GainType.AMPLITUDE) 63 out_mindspore = op(ndarr_in) 64 allclose_nparray(out_mindspore, out_expect, 0.0001, 0.0001) 65 66 ndarr_in = np.array([[[-0.5794799327850342, 0.19526369869709015], 67 [-0.5935744047164917, 0.2948109209537506], 68 [-0.42077431082725525, 0.04923877865076065]], 69 [[0.5497273802757263, -0.22815021872520447], 70 [-0.05891447141766548, -0.16206198930740356], 71 [-1.4782767295837402, -1.3815662860870361]]]).astype(np.float32) 72 # cal from benchmark 73 out_expect = np.array([[[-0.5761537551879883, 0.1941428929567337], 74 [-0.5901673436164856, 0.2931187152862549], 75 [-0.41835910081863403, 0.04895615205168724]], 76 [[0.5465719699859619, -0.22684065997600555], 77 [-0.0585763081908226, -0.16113176941871643], 78 [-1.0, -1.0]]]) 79 op = c_audio.Vol(gain=-0.05, gain_type=utils.GainType.DB) 80 out_mindspore = op(ndarr_in) 81 allclose_nparray(out_mindspore, out_expect, 0.0001, 0.0001) 82 83 ndarr_in = np.array([[[0.09491927176713943, 0.11639882624149323, -0.1725238710641861, -0.18556903302669525], 84 [-0.7140364646911621, 1.6223102807998657, 1.6710518598556519, 0.6019048094749451]], 85 [[-0.8635917901992798, -0.31538113951683044, -0.2209240198135376, 1.3067045211791992], 86 [-2.0922982692718506, 0.6822009682655334, 0.20066820085048676, 0.006392406765371561]]]) 87 # cal from benchmark 88 out_expect = np.array([[[0.042449187487363815, 0.05205513536930084, -0.07715501636266708, -0.08298899233341217], 89 [-0.31932681798934937, 0.7255191802978516, 0.7473170757293701, 0.2691799998283386]], 90 [[-0.38620999455451965, -0.14104272425174713, -0.09880022704601288, 0.5843760371208191], 91 [-0.935704231262207, 0.30508953332901, 0.0897415429353714, 0.0028587712440639734]]]) 92 op = c_audio.Vol(gain=0.2, gain_type=utils.GainType.POWER) 93 out_mindspore = op(ndarr_in) 94 allclose_nparray(out_mindspore, out_expect, 0.0001, 0.0001) 95 96 97def test_func_vol_pipeline(): 98 """ mindspore pipeline mode normal testcase:vol op""" 99 100 logger.info("test vol op with gain_type='power'") 101 data = np.array([[[0.7012, 0.2500, 0.0108], 102 [0.3617, 0.6367, 0.6096]]]).astype(np.float32) 103 out_expect = np.array([[1.0000, 0.7906, 0.0342], 104 [1.0000, 1.0000, 1.0000]]) 105 data1 = ds.NumpySlicesDataset(data, column_names=["multi_dimensional_data"]) 106 transforms = [c_audio.Vol(gain=10, gain_type=utils.GainType.POWER)] 107 data1 = data1.map(operations=transforms, input_columns=["multi_dimensional_data"]) 108 for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True): 109 out_put = item["multi_dimensional_data"] 110 allclose_nparray(out_put, out_expect, 0.0001, 0.0001) 111 112 logger.info("test vol op with gain_type='amplitude' and datatype='float64'") 113 data = np.array([[[0.9342139979247938, 0.613965955965896, 0.5356328030249583, 0.589909976354571], 114 [0.7301220295167696, 0.31194499547960186, 0.3982210622160919, 0.20984374897512215], 115 [0.18619300588033616, 0.9443723899839336, 0.7395507950492876, 0.4904588086175671]]]) 116 data = data.astype(np.float64) 117 out_expect = np.array([[0.18684279918670654, 0.12279318571090699, 0.10712655782699586, 0.1179819941520691], 118 [0.1460244059562683, 0.062388998270034794, 0.07964421510696412, 0.04196875095367432], 119 [0.03723860085010529, 0.1888744831085205, 0.14791015386581421, 0.09809176325798036]]) 120 data1 = ds.NumpySlicesDataset(data, column_names=["multi_dimensional_data"]) 121 transforms = [c_audio.Vol(gain=0.2, gain_type=utils.GainType.AMPLITUDE)] 122 data1 = data1.map(operations=transforms, input_columns=["multi_dimensional_data"]) 123 for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True): 124 out_put = item["multi_dimensional_data"] 125 allclose_nparray(out_put, out_expect, 0.0001, 0.0001) 126 127 logger.info("test vol op with gain_type='db'") 128 data = np.array([[[0.1302, 0.5908, 0.1225, 0.7044], 129 [0.6405, 0.6540, 0.9908, 0.8605], 130 [0.7023, 0.0115, 0.8790, 0.5806]]]).astype(np.float32) 131 out_expect = np.array([[0.1096, 0.4971, 0.1031, 0.5927], 132 [0.5389, 0.5503, 0.8336, 0.7240], 133 [0.5909, 0.0097, 0.7396, 0.4885]]) 134 data1 = ds.NumpySlicesDataset(data, column_names=["multi_dimensional_data"]) 135 transforms = [c_audio.Vol(gain=-1.5, gain_type=utils.GainType.DB)] 136 data1 = data1.map(operations=transforms, input_columns=["multi_dimensional_data"]) 137 for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True): 138 out_put = item["multi_dimensional_data"] 139 allclose_nparray(out_put, out_expect, 0.0001, 0.0001) 140 141 142def test_vol_invalid_input(): 143 def test_invalid_input(test_name, gain, gain_type, error, error_msg): 144 logger.info("Test Vol with invalid input: {0}".format(test_name)) 145 with pytest.raises(error) as error_info: 146 c_audio.Vol(gain, gain_type) 147 assert error_msg in str(error_info.value) 148 149 test_invalid_input("invalid gain value when gain_type equals 'power'", -1.5, utils.GainType.POWER, ValueError, 150 "Input gain is not within the required interval of (0, 16777216].") 151 test_invalid_input("invalid gain value when gain_type equals 'amplitude'", -1.5, utils.GainType.AMPLITUDE, 152 ValueError, "Input gain is not within the required interval of [0, 16777216].") 153 test_invalid_input("invalid gain value when gain_type equals 'amplitude'", 1.5, "TEST", TypeError, 154 "Argument gain_type with value TEST is not of type [<enum 'GainType'>], but got <class 'str'>.") 155 156 157if __name__ == "__main__": 158 test_func_vol_eager() 159 test_func_vol_pipeline() 160 test_vol_invalid_input() 161