Merge pull request !26896 from xiaotianci/enhance_audio_err_messagetags/v1.6.0
| @@ -41,7 +41,7 @@ Status ComputeDeltasOperation::ValidateParams() { | |||
| RETURN_IF_NOT_OK(ValidateScalar("ComputeDeltas", "win_length", win_length_, {3}, false)); | |||
| if (pad_mode_ != BorderType::kConstant && pad_mode_ != BorderType::kEdge && pad_mode_ != BorderType::kReflect && | |||
| pad_mode_ != BorderType::kSymmetric) { | |||
| std::string err_msg = "ComputeDeltas: invalid BorderType, please check input value of enum."; | |||
| std::string err_msg = "ComputeDeltas: invalid pad_mode value, check the optional value of BorderType."; | |||
| LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg); | |||
| } | |||
| return Status::OK(); | |||
| @@ -16,7 +16,6 @@ | |||
| #include "minddata/dataset/audio/ir/kernels/deemph_biquad_ir.h" | |||
| #include "minddata/dataset/audio/ir/validators.h" | |||
| #include "minddata/dataset/audio/kernels/deemph_biquad_op.h" | |||
| namespace mindspore { | |||
| @@ -28,7 +27,7 @@ DeemphBiquadOperation::DeemphBiquadOperation(int32_t sample_rate) : sample_rate_ | |||
| Status DeemphBiquadOperation::ValidateParams() { | |||
| if ((sample_rate_ != 44100 && sample_rate_ != 48000)) { | |||
| std::string err_msg = | |||
| "DeemphBiquad: sample_rate should be 44100 (hz) or 48000 (hz), but got: " + std::to_string(sample_rate_); | |||
| "DeemphBiquad: sample_rate can only be 44100 or 48000, but got: " + std::to_string(sample_rate_); | |||
| MS_LOG(ERROR) << err_msg; | |||
| return Status(StatusCode::kMDSyntaxError, __LINE__, __FILE__, err_msg); | |||
| } | |||
| @@ -39,7 +39,7 @@ Status MagphaseOperation::to_json(nlohmann::json *out_json) { | |||
| } | |||
| Status MagphaseOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) { | |||
| CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("power") != op_params.end(), "Fail to find power"); | |||
| RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "power", kMagphaseOperation)); | |||
| float power = op_params["power"]; | |||
| *operation = std::make_shared<audio::MagphaseOperation>(power); | |||
| return Status::OK(); | |||
| @@ -21,14 +21,5 @@ Status ValidateIntScalarNonNegative(const std::string &op_name, const std::strin | |||
| RETURN_IF_NOT_OK(ValidateScalar(op_name, scalar_name, scalar, {0}, false)); | |||
| return Status::OK(); | |||
| } | |||
| Status ValidateFloatScalarNotNan(const std::string &op_name, const std::string &scalar_name, float scalar) { | |||
| if (std::isnan(scalar)) { | |||
| std::string err_msg = op_name + ": " + scalar_name + " should be specified, got: Nan"; | |||
| MS_LOG(ERROR) << err_msg; | |||
| return Status(StatusCode::kMDSyntaxError, __LINE__, __FILE__, err_msg); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| } // namespace dataset | |||
| } // namespace mindspore | |||
| @@ -24,16 +24,13 @@ | |||
| #include "minddata/dataset/kernels/ir/tensor_operation.h" | |||
| #include "minddata/dataset/kernels/ir/validators.h" | |||
| #include "minddata/dataset/util/status.h" | |||
| #include "minddata/dataset/util/validators.h" | |||
| namespace mindspore { | |||
| namespace dataset { | |||
| // Helper function to positive int scalar | |||
| Status ValidateIntScalarNonNegative(const std::string &op_name, const std::string &scalar_name, int32_t scalar); | |||
| // Helper function to non-nan float scalar | |||
| Status ValidateFloatScalarNotNan(const std::string &op_name, const std::string &scalar_name, float scalar); | |||
| // Helper function to validate scalar value | |||
| template <typename T> | |||
| Status ValidateScalarValue(const std::string &op_name, const std::string &scalar_name, T scalar, | |||
| @@ -59,7 +56,8 @@ Status ValidateScalarValue(const std::string &op_name, const std::string &scalar | |||
| template <typename T> | |||
| Status ValidateScalarNotZero(const std::string &op_name, const std::string &scalar_name, const T scalar) { | |||
| if (scalar == 0) { | |||
| std::string err_msg = op_name + ": " + scalar_name + " can't be zero, got: " + std::to_string(scalar); | |||
| std::string err_msg = | |||
| op_name + ": " + scalar_name + " can not be equal to zero, but got: " + std::to_string(scalar); | |||
| MS_LOG(ERROR) << err_msg; | |||
| return Status(StatusCode::kMDSyntaxError, __LINE__, __FILE__, err_msg); | |||
| } | |||
| @@ -70,7 +68,7 @@ Status ValidateScalarNotZero(const std::string &op_name, const std::string &scal | |||
| template <typename T> | |||
| Status ValidateVectorNotEmpty(const std::string &op_name, const std::string &vec_name, const std::vector<T> &vec) { | |||
| if (vec.empty()) { | |||
| std::string err_msg = op_name + ": " + vec_name + " can't be empty."; | |||
| std::string err_msg = op_name + ": " + vec_name + " can not be an empty vector."; | |||
| MS_LOG(ERROR) << err_msg; | |||
| return Status(StatusCode::kMDSyntaxError, __LINE__, __FILE__, err_msg); | |||
| } | |||
| @@ -79,16 +77,17 @@ Status ValidateVectorNotEmpty(const std::string &op_name, const std::string &vec | |||
| // Helper function to check two vector size equal | |||
| template <typename T> | |||
| Status ValidateVectorSameSize(const std::string &op_name, const std::string &vec1_name, const std::vector<T> &vec1, | |||
| const std::string &vec2_name, const std::vector<T> &vec2) { | |||
| if (vec1.size() != vec2.size()) { | |||
| std::string err_msg = op_name + ": the size of " + vec1_name + " should be the same as that of " + vec2_name; | |||
| Status ValidateVectorSameSize(const std::string &op_name, const std::string &vec_name, const std::vector<T> &vec, | |||
| const std::string &other_vec_name, const std::vector<T> &other_vec) { | |||
| if (vec.size() != other_vec.size()) { | |||
| std::string err_msg = op_name + ": the size of '" + vec_name + "' should be the same as that of '" + | |||
| other_vec_name + "', but got: '" + vec_name + "' size " + std::to_string(vec.size()) + | |||
| " and '" + other_vec_name + "' size " + std::to_string(other_vec.size()) + "."; | |||
| MS_LOG(ERROR) << err_msg; | |||
| return Status(StatusCode::kMDSyntaxError, __LINE__, __FILE__, err_msg); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| } // namespace dataset | |||
| } // namespace mindspore | |||
| #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_ADUIO_IR_VALIDATORS_H_ | |||
| @@ -22,12 +22,8 @@ namespace mindspore { | |||
| namespace dataset { | |||
| Status AllpassBiquadOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "AllpassBiquad: input tensor is not in shape of <..., time>."); | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type() == DataType(DataType::DE_FLOAT32) || input->type() == DataType(DataType::DE_FLOAT16) || | |||
| input->type() == DataType(DataType::DE_FLOAT64), | |||
| "AllpassBiquad: input tensor type should be float, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("AllpassBiquad", input, kMinAudioDim, "<..., time>")); | |||
| RETURN_IF_NOT_OK(ValidateTensorFloat("AllpassBiquad", input)); | |||
| double w0 = 2 * PI * central_freq_ / sample_rate_; | |||
| double alpha = sin(w0) / 2 / Q_; | |||
| double b0 = 1 - alpha; | |||
| @@ -23,10 +23,7 @@ namespace mindspore { | |||
| namespace dataset { | |||
| Status AmplitudeToDBOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| if (input->shape().Rank() < 2) { | |||
| std::string err_msg = "AmplitudeToDB: input tensor is not in shape of <..., freq, time>."; | |||
| LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg); | |||
| } | |||
| RETURN_IF_NOT_OK(ValidateLowRank("AmplitudeToDB", input, kDefaultAudioDim, "<..., freq, time>")); | |||
| std::shared_ptr<Tensor> input_tensor; | |||
| @@ -35,13 +32,10 @@ Status AmplitudeToDBOp::Compute(const std::shared_ptr<Tensor> &input, std::share | |||
| const float amin = 1e-10; | |||
| float db_multiplier = std::log10(std::max(amin_, ref_value_)); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("AmplitudeToDB", input)); | |||
| // typecast | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->type() != DataType::DE_STRING, | |||
| "AmplitudeToDB: input tensor type should be float, but got: string."); | |||
| if (input->type() != DataType::DE_FLOAT64) { | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| TypeCast(input, &input_tensor, DataType(DataType::DE_FLOAT32)), | |||
| "AmplitudeToDB: input tensor type should be float, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(TypeCast(input, &input_tensor, DataType(DataType::DE_FLOAT32))); | |||
| return AmplitudeToDB<float>(input_tensor, output, multiplier, amin, db_multiplier, top_db); | |||
| } else { | |||
| input_tensor = input; | |||
| @@ -13,9 +13,8 @@ | |||
| * See the License for the specific language governing permissions and | |||
| * limitations under the License. | |||
| */ | |||
| #include <cmath> | |||
| #include "minddata/dataset/audio/kernels/angle_op.h" | |||
| #include "minddata/dataset/audio/kernels/audio_utils.h" | |||
| #include "minddata/dataset/kernels/data/data_utils.h" | |||
| @@ -24,10 +23,8 @@ namespace dataset { | |||
| Status AngleOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| // if If the last dimension is not 2, then it's not a complex number | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->shape()[-1] == 2, "Angle: input tensor is not in shape of <..., complex=2>."); | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type().IsNumeric(), | |||
| "Angle: input tensor type should be int, float or double, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorShape("Angle", input->IsComplex(), "<..., complex=2>")); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("Angle", input)); | |||
| if (input->type() == DataType(DataType::DE_FLOAT64)) { | |||
| return Angle<double>(input, output); | |||
| } else { | |||
| @@ -46,7 +43,7 @@ Status AngleOp::OutputShape(const std::vector<TensorShape> &inputs, std::vector< | |||
| TensorShape out = TensorShape{shape}; | |||
| outputs.emplace_back(out); | |||
| if (!outputs.empty()) return Status::OK(); | |||
| return Status(StatusCode::kMDUnexpectedError, "Angle: invalid input wrong shape."); | |||
| return Status(StatusCode::kMDUnexpectedError, "Angle: invalid shape of input tensor."); | |||
| } | |||
| Status AngleOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| @@ -21,7 +21,6 @@ | |||
| #include "mindspore/core/base/float16.h" | |||
| #include "minddata/dataset/core/type_id.h" | |||
| #include "minddata/dataset/kernels/data/data_utils.h" | |||
| #include "minddata/dataset/util/random.h" | |||
| #include "utils/file_utils.h" | |||
| @@ -35,10 +34,7 @@ namespace dataset { | |||
| /// \return Status return code. | |||
| template <typename T> | |||
| Status Linspace(std::shared_ptr<Tensor> *output, T start, T end, int n) { | |||
| if (start > end) { | |||
| std::string err = "Linspace: input param end must be greater than start."; | |||
| RETURN_STATUS_UNEXPECTED(err); | |||
| } | |||
| RETURN_IF_NOT_OK(ValidateNoGreaterThan("Linspace", "start", start, "end", end)); | |||
| n = std::isnan(n) ? 100 : n; | |||
| TensorShape out_shape({n}); | |||
| std::vector<T> linear_vect(n); | |||
| @@ -61,10 +57,7 @@ Status Linspace(std::shared_ptr<Tensor> *output, T start, T end, int n) { | |||
| template <typename T> | |||
| Status ComplexAngle(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| // check complex | |||
| if (!input->IsComplex()) { | |||
| std::string err_msg = "ComplexAngle: input tensor is not in shape of <..., 2>."; | |||
| LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg); | |||
| } | |||
| RETURN_IF_NOT_OK(ValidateTensorShape("ComplexAngle", input->IsComplex(), "<..., complex=2>")); | |||
| TensorShape input_shape = input->shape(); | |||
| TensorShape out_shape({input_shape[0], input_shape[1], input_shape[2]}); | |||
| std::vector<T> phase(input_shape[0] * input_shape[1] * input_shape[2]); | |||
| @@ -92,10 +85,7 @@ Status ComplexAngle(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor | |||
| template <typename T> | |||
| Status ComplexAbs(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| // check complex | |||
| if (!input->IsComplex()) { | |||
| std::string err_msg = "ComplexAngle: input tensor is not in shape of <..., 2>."; | |||
| LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg); | |||
| } | |||
| RETURN_IF_NOT_OK(ValidateTensorShape("ComplexAngle", input->IsComplex(), "<..., complex=2>")); | |||
| TensorShape input_shape = input->shape(); | |||
| TensorShape out_shape({input_shape[0], input_shape[1], input_shape[2]}); | |||
| std::vector<T> abs(input_shape[0] * input_shape[1] * input_shape[2]); | |||
| @@ -123,7 +113,8 @@ Status Polar(const std::shared_ptr<Tensor> &abs, const std::shared_ptr<Tensor> & | |||
| std::shared_ptr<Tensor> *output) { | |||
| // check shape | |||
| if (abs->shape() != angle->shape()) { | |||
| std::string err_msg = "Polar: input tensor shape of abs and angle must be the same."; | |||
| std::string err_msg = "Polar: the shape of input tensor abs and angle should be the same, but got: abs " + | |||
| abs->shape().ToString() + " and angle " + angle->shape().ToString(); | |||
| LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg); | |||
| } | |||
| @@ -340,8 +331,7 @@ Status TimeStretch(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> | |||
| RETURN_IF_NOT_OK(TimeStretch<double>(input, output, rate, phase_advance)); | |||
| break; | |||
| default: | |||
| RETURN_STATUS_UNEXPECTED("TimeStretch: input tensor type should be float or double, but got: " + | |||
| input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorFloat("TimeStretch", input)); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| @@ -389,7 +379,8 @@ Status RandomMaskAlongAxis(const std::shared_ptr<Tensor> &input, std::shared_ptr | |||
| Status MaskAlongAxis(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, int32_t mask_width, | |||
| int32_t mask_start, float mask_value, int32_t axis) { | |||
| if (axis != 2 && axis != 1) { | |||
| RETURN_STATUS_UNEXPECTED("MaskAlongAxis: only support Time and Frequency masking, axis should be 1 or 2."); | |||
| LOG_AND_RETURN_STATUS_SYNTAX_ERROR( | |||
| "MaskAlongAxis: invalid parameter, 'axis' can only be 1 for Frequency Masking or 2 for Time Masking."); | |||
| } | |||
| TensorShape input_shape = input->shape(); | |||
| // squeeze input | |||
| @@ -397,10 +388,17 @@ Status MaskAlongAxis(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tenso | |||
| (void)input->Reshape(squeeze_shape); | |||
| int check_dim_ind = (axis == 1) ? -2 : -1; | |||
| CHECK_FAIL_RETURN_UNEXPECTED(0 <= mask_start && mask_start <= input_shape[check_dim_ind], | |||
| "MaskAlongAxis: mask_start should be less than the length of chosen dimension."); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(mask_start + mask_width <= input_shape[check_dim_ind], | |||
| "MaskAlongAxis: the sum of mask_start and mask_width is out of bounds."); | |||
| CHECK_FAIL_RETURN_SYNTAX_ERROR(mask_start >= 0 && mask_start <= input_shape[check_dim_ind], | |||
| "MaskAlongAxis: invalid parameter, 'mask_start' should be less than the length of the " | |||
| "masked dimension, but got: 'mask_start' " + | |||
| std::to_string(mask_start) + " and length " + | |||
| std::to_string(input_shape[check_dim_ind])); | |||
| CHECK_FAIL_RETURN_SYNTAX_ERROR( | |||
| mask_start + mask_width <= input_shape[check_dim_ind], | |||
| "MaskAlongAxis: invalid parameter, the sum of 'mask_start' and 'mask_width' should be no more " | |||
| "than the length of the masked dimension, but got: 'mask_start' " + | |||
| std::to_string(mask_start) + ", 'mask_width' " + std::to_string(mask_width) + " and length " + | |||
| std::to_string(input_shape[check_dim_ind])); | |||
| int32_t cell_size = input->type().SizeInBytes(); | |||
| @@ -451,8 +449,8 @@ Status Norm(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *outpu | |||
| // calculate the output dimension | |||
| auto input_size = input->shape().AsVector(); | |||
| int32_t dim_back = static_cast<int32_t>(input_size.back()); | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| dim_back == 2, "ComplexNorm: expect complex input of shape <..., 2>, but got: " + std::to_string(dim_back)); | |||
| RETURN_IF_NOT_OK( | |||
| ValidateTensorShape("ComplexNorm", input->IsComplex(), "<..., complex=2>", std::to_string(dim_back))); | |||
| input_size.pop_back(); | |||
| TensorShape out_shape = TensorShape(input_size); | |||
| RETURN_IF_NOT_OK(Tensor::CreateEmpty(out_shape, input->type(), output)); | |||
| @@ -474,25 +472,20 @@ Status Norm(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *outpu | |||
| } | |||
| Status ComplexNorm(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, float power) { | |||
| try { | |||
| if (input->type().value() >= DataType::DE_INT8 && input->type().value() <= DataType::DE_FLOAT16) { | |||
| // convert the data type to float | |||
| std::shared_ptr<Tensor> input_tensor; | |||
| RETURN_IF_NOT_OK(TypeCast(input, &input_tensor, DataType(DataType::DE_FLOAT32))); | |||
| RETURN_IF_NOT_OK(Norm<float>(input_tensor, output, power)); | |||
| } else if (input->type().value() == DataType::DE_FLOAT32) { | |||
| RETURN_IF_NOT_OK(Norm<float>(input, output, power)); | |||
| } else if (input->type().value() == DataType::DE_FLOAT64) { | |||
| RETURN_IF_NOT_OK(Norm<double>(input, output, power)); | |||
| } else { | |||
| RETURN_STATUS_UNEXPECTED("ComplexNorm: input tensor type should be int, float or double, but got: " + | |||
| input->type().ToString()); | |||
| } | |||
| return Status::OK(); | |||
| } catch (std::runtime_error &e) { | |||
| RETURN_STATUS_UNEXPECTED("ComplexNorm: " + std::string(e.what())); | |||
| if (input->type().value() >= DataType::DE_INT8 && input->type().value() <= DataType::DE_FLOAT16) { | |||
| // convert the data type to float | |||
| std::shared_ptr<Tensor> input_tensor; | |||
| RETURN_IF_NOT_OK(TypeCast(input, &input_tensor, DataType(DataType::DE_FLOAT32))); | |||
| RETURN_IF_NOT_OK(Norm<float>(input_tensor, output, power)); | |||
| } else if (input->type().value() == DataType::DE_FLOAT32) { | |||
| RETURN_IF_NOT_OK(Norm<float>(input, output, power)); | |||
| } else if (input->type().value() == DataType::DE_FLOAT64) { | |||
| RETURN_IF_NOT_OK(Norm<double>(input, output, power)); | |||
| } else { | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("ComplexNorm", input)); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| template <typename T> | |||
| @@ -509,7 +502,7 @@ Status Decoding(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *o | |||
| while (itr != end) { | |||
| auto x_mu = *itr; | |||
| CHECK_FAIL_RETURN_SYNTAX_ERROR(mu != 0, "mu can not be zero."); | |||
| CHECK_FAIL_RETURN_SYNTAX_ERROR(mu != 0, "Decoding: invalid parameter, 'mu' can not be zero."); | |||
| x_mu = ((x_mu) / mu) * 2 - 1.0; | |||
| x_mu = sgn(x_mu) * expm1(fabs(x_mu) * log1p(mu)) / mu; | |||
| *itr_out = x_mu; | |||
| @@ -535,8 +528,7 @@ Status MuLawDecoding(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tenso | |||
| RETURN_IF_NOT_OK(Decoding<double>(input, output, f_mu)); | |||
| } else { | |||
| RETURN_STATUS_UNEXPECTED("MuLawDecoding: input tensor type should be int, float or double, but got: " + | |||
| input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("MuLawDecoding", input)); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| @@ -578,8 +570,7 @@ Status MuLawEncoding(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tenso | |||
| RETURN_IF_NOT_OK(Encoding<double>(input, output, f_mu)); | |||
| } else { | |||
| RETURN_STATUS_UNEXPECTED("MuLawEncoding: input tensor type should be int, float or double, but got: " + | |||
| input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("MuLawEncoding", input)); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| @@ -652,8 +643,8 @@ Status Fade(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *outpu | |||
| RETURN_IF_NOT_OK(Tensor::CreateFromTensor(input, output)); | |||
| const TensorShape input_shape = input->shape(); | |||
| int32_t waveform_length = static_cast<int32_t>(input_shape[-1]); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(fade_in_len <= waveform_length, "Fade: fade_in_len exceeds waveform length."); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(fade_out_len <= waveform_length, "Fade: fade_out_len exceeds waveform length."); | |||
| RETURN_IF_NOT_OK(ValidateNoGreaterThan("Fade", "fade_in_len", fade_in_len, "length of waveform", waveform_length)); | |||
| RETURN_IF_NOT_OK(ValidateNoGreaterThan("Fade", "fade_out_len", fade_out_len, "length of waveform", waveform_length)); | |||
| int32_t num_waveform = static_cast<int32_t>(input->Size() / waveform_length); | |||
| TensorShape toShape = TensorShape({num_waveform, waveform_length}); | |||
| RETURN_IF_NOT_OK((*output)->Reshape(toShape)); | |||
| @@ -699,8 +690,7 @@ Status Fade(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *outpu | |||
| } else if (input->type().value() == DataType::DE_FLOAT64) { | |||
| RETURN_IF_NOT_OK(Fade<double>(input, output, fade_in_len, fade_out_len, fade_shape)); | |||
| } else { | |||
| RETURN_STATUS_UNEXPECTED("Fade: input tensor type should be int, float or double, but got: " + | |||
| input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("Fade", input)); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| @@ -857,31 +847,31 @@ Status ReadWaveFile(const std::string &wav_file_dir, std::vector<float> *wavefor | |||
| RETURN_UNEXPECTED_IF_NULL(sample_rate); | |||
| auto wav_realpath = FileUtils::GetRealPath(wav_file_dir.data()); | |||
| if (!wav_realpath.has_value()) { | |||
| MS_LOG(ERROR) << "Invalid file, get real path failed, path=" << wav_file_dir; | |||
| RETURN_STATUS_UNEXPECTED("Invalid file, get real path failed, path=" + wav_file_dir); | |||
| LOG_AND_RETURN_STATUS_SYNTAX_ERROR("Invalid file path, get real path failed: " + wav_file_dir); | |||
| } | |||
| const float kMaxVal = 32767.0; | |||
| Path file_path(wav_realpath.value()); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(file_path.Exists() && !file_path.IsDirectory(), | |||
| "Invalid file, failed to find metadata file:" + file_path.ToString()); | |||
| "Invalid file path, failed to find waveform file: " + file_path.ToString()); | |||
| std::ifstream in(file_path.ToString(), std::ios::in | std::ios::binary); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(in.is_open(), "Invalid file, failed to open metadata file:" + file_path.ToString() + | |||
| CHECK_FAIL_RETURN_UNEXPECTED(in.is_open(), "Invalid file, failed to open waveform file: " + file_path.ToString() + | |||
| ", make sure the file not damaged or permission denied."); | |||
| WavHeader *header = new WavHeader(); | |||
| in.read(reinterpret_cast<char *>(header), sizeof(WavHeader)); | |||
| *sample_rate = header->sampleRate; | |||
| float bytesPerSample = header->bitsPerSample / 8; | |||
| if (bytesPerSample == 0) { | |||
| *sample_rate = header->sample_rate; | |||
| float bytes_per_sample = header->bits_per_sample / 8; | |||
| if (bytes_per_sample == 0) { | |||
| in.close(); | |||
| delete header; | |||
| return Status(StatusCode::kMDUnexpectedError, __LINE__, __FILE__, "ReadWaveFile: divide zero error."); | |||
| return Status(StatusCode::kMDUnexpectedError, __LINE__, __FILE__, | |||
| "ReadWaveFile: zero division error, bits per sample of the audio can not be zero."); | |||
| } | |||
| int numSamples = header->subChunk2Size / bytesPerSample; | |||
| std::unique_ptr<int16_t[]> data = std::make_unique<int16_t[]>(numSamples); | |||
| in.read(reinterpret_cast<char *>(data.get()), sizeof(int16_t) * numSamples); | |||
| waveform_vec->resize(numSamples); | |||
| for (int i = 0; i < numSamples; i++) { | |||
| int num_samples = header->sub_chunk2_size / bytes_per_sample; | |||
| std::unique_ptr<int16_t[]> data = std::make_unique<int16_t[]>(num_samples); | |||
| in.read(reinterpret_cast<char *>(data.get()), sizeof(int16_t) * num_samples); | |||
| waveform_vec->resize(num_samples); | |||
| for (int i = 0; i < num_samples; i++) { | |||
| (*waveform_vec)[i] = data[i] / kMaxVal; | |||
| } | |||
| in.close(); | |||
| @@ -893,10 +883,8 @@ Status ComputeCmnStartAndEnd(int32_t cmn_window, int32_t min_cmn_window, bool ce | |||
| int32_t *cmn_window_start_p, int32_t *cmn_window_end_p) { | |||
| RETURN_UNEXPECTED_IF_NULL(cmn_window_start_p); | |||
| RETURN_UNEXPECTED_IF_NULL(cmn_window_end_p); | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| cmn_window >= 0, "SlidingWindowCmn: cmn_window must be non negative, but got: " + std::to_string(cmn_window)); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(min_cmn_window >= 0, "SlidingWindowCmn: min_cmn_window must be non negative, but got: " + | |||
| std::to_string(min_cmn_window)); | |||
| RETURN_IF_NOT_OK(ValidateNonNegative("SlidingWindowCmn", "cmn_window", cmn_window)); | |||
| RETURN_IF_NOT_OK(ValidateNonNegative("SlidingWindowCmn", "min_cmn_window", min_cmn_window)); | |||
| int32_t cmn_window_start = 0, cmn_window_end = 0; | |||
| constexpr int window_center = 2; | |||
| if (center) { | |||
| @@ -1046,9 +1034,7 @@ Status SlidingWindowCmnHelper(const std::shared_ptr<Tensor> &input, std::shared_ | |||
| Status SlidingWindowCmn(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, int32_t cmn_window, | |||
| int32_t min_cmn_window, bool center, bool norm_vars) { | |||
| TensorShape input_shape = input->shape(); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() >= kMinAudioRank, | |||
| "SlidingWindowCmn: input tensor is not in shape of <..., freq, time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("SlidingWindowCmn", input, kDefaultAudioDim, "<..., freq, time>")); | |||
| if (input->type().IsNumeric() && input->type().value() != DataType::DE_FLOAT64) { | |||
| std::shared_ptr<Tensor> temp; | |||
| @@ -1057,8 +1043,7 @@ Status SlidingWindowCmn(const std::shared_ptr<Tensor> &input, std::shared_ptr<Te | |||
| } else if (input->type().value() == DataType::DE_FLOAT64) { | |||
| RETURN_IF_NOT_OK(SlidingWindowCmnHelper<double>(input, output, cmn_window, min_cmn_window, center, norm_vars)); | |||
| } else { | |||
| RETURN_STATUS_UNEXPECTED("SlidingWindowCmn: input tensor type should be int, float or double, but got: " + | |||
| input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("SlidingWindowCmn", input)); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| @@ -1066,13 +1051,10 @@ Status SlidingWindowCmn(const std::shared_ptr<Tensor> &input, std::shared_ptr<Te | |||
| template <typename T> | |||
| Status Pad(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, int32_t pad_left, int32_t pad_right, | |||
| BorderType padding_mode, T value = 0) { | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->shape().Size() >= 2, "Pad: input tensor is not in shape of <..., time>."); | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type().IsNumeric(), | |||
| "Pad: input tensor type should be int, float or double, but got: " + input->type().ToString()); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(pad_left >= 0 && pad_right >= 0, | |||
| "Pad: left and right padding values must be non negative, but got pad_left: " + | |||
| std::to_string(pad_left) + " and pad_right: " + std::to_string(pad_right)); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("Pad", input, kMinAudioDim, "<..., time>")); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("Pad", input)); | |||
| RETURN_IF_NOT_OK(ValidateNonNegative("Pad", "pad_left", pad_left)); | |||
| RETURN_IF_NOT_OK(ValidateNonNegative("Pad", "pad_right", pad_right)); | |||
| TensorShape input_shape = input->shape(); | |||
| int32_t wave_length = input_shape[-1]; | |||
| int32_t num_wavs = static_cast<int32_t>(input->Size() / wave_length); | |||
| @@ -1145,7 +1127,7 @@ Status Pad(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output | |||
| } | |||
| output_map.block(0, 0, num_wavs, pad_left) = output_map.block(0, pad_left, num_wavs, pad_left).rowwise().reverse(); | |||
| } else { | |||
| RETURN_STATUS_UNEXPECTED("Pad: unsupported border type."); | |||
| LOG_AND_RETURN_STATUS_SYNTAX_ERROR("Pad: invalid padding_mode value, check the optional value of BorderType."); | |||
| } | |||
| std::vector<dsize_t> shape_vec = input_shape.AsVector(); | |||
| shape_vec[shape_vec.size() - 1] = static_cast<dsize_t>(pad_length); | |||
| @@ -1176,15 +1158,11 @@ Status ComputeDeltasImpl(const std::shared_ptr<Tensor> &input, std::shared_ptr<T | |||
| Status ComputeDeltas(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, int32_t win_length, | |||
| const BorderType &mode) { | |||
| constexpr int min_shape_dim = 2; | |||
| auto raw_shape = input->shape(); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(raw_shape.Size() >= min_shape_dim, | |||
| "ComputeDeltas: input tensor is not in shape of <..., freq, time>."); | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type().IsNumeric(), | |||
| "ComputeDeltas: input tensor type should be int, float or double, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("ComputeDeltas", input, kDefaultAudioDim, "<..., freq, time>")); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("ComputeDeltas", input)); | |||
| // reshape Tensor from <..., freq, time> to <-1, time> | |||
| auto raw_shape = input->shape(); | |||
| int32_t n_frames = raw_shape[-1]; | |||
| int32_t all_freqs = raw_shape.NumOfElements() / n_frames; | |||
| RETURN_IF_NOT_OK(input->Reshape(TensorShape{all_freqs, n_frames})); | |||
| @@ -29,9 +29,11 @@ | |||
| #include "minddata/dataset/kernels/data/data_utils.h" | |||
| #include "minddata/dataset/kernels/tensor_op.h" | |||
| #include "minddata/dataset/util/status.h" | |||
| #include "minddata/dataset/util/validators.h" | |||
| constexpr double PI = 3.141592653589793; | |||
| constexpr int kMinAudioRank = 2; | |||
| constexpr int kMinAudioDim = 1; | |||
| constexpr int kDefaultAudioDim = 2; | |||
| namespace mindspore { | |||
| namespace dataset { | |||
| @@ -997,19 +999,19 @@ Status Flanger(const std::shared_ptr<Tensor> input, std::shared_ptr<Tensor> *out | |||
| // A brief structure of wave file header. | |||
| struct WavHeader { | |||
| int8_t chunkID[4] = {0}; | |||
| int32_t chunkSize = 0; | |||
| int8_t chunk_id[4] = {0}; | |||
| int32_t chunk_size = 0; | |||
| int8_t format[4] = {0}; | |||
| int8_t subChunk1ID[4] = {0}; | |||
| int32_t subChunk1Size = 0; | |||
| int16_t audioFormat = 0; | |||
| int16_t numChannels = 0; | |||
| int32_t sampleRate = 0; | |||
| int32_t byteRate = 0; | |||
| int16_t byteAlign = 0; | |||
| int16_t bitsPerSample = 0; | |||
| int8_t subChunk2ID[4] = {0}; | |||
| int32_t subChunk2Size = 0; | |||
| int8_t sub_chunk1_id[4] = {0}; | |||
| int32_t sub_chunk1_size = 0; | |||
| int16_t audio_format = 0; | |||
| int16_t num_channels = 0; | |||
| int32_t sample_rate = 0; | |||
| int32_t byte_rate = 0; | |||
| int16_t byte_align = 0; | |||
| int16_t bits_per_sample = 0; | |||
| int8_t sub_chunk2_id[4] = {0}; | |||
| int32_t sub_chunk2_size = 0; | |||
| WavHeader() {} | |||
| }; | |||
| @@ -22,23 +22,20 @@ namespace mindspore { | |||
| namespace dataset { | |||
| Status BandBiquadOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| // check input tensor dimension, it should be greater than 0. | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "BandBiquad: input tensor is not in shape of <..., time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("BandBiquad", input, kMinAudioDim, "<..., time>")); | |||
| // check input type, it should be DE_FLOAT32 or DE_FLOAT16 or DE_FLOAT64 | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->type() == DataType(DataType::DE_FLOAT32) || | |||
| input->type() == DataType(DataType::DE_FLOAT16) || | |||
| input->type() == DataType(DataType::DE_FLOAT64), | |||
| "BandBiquad: input tensor type should be float, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorFloat("BandBiquad", input)); | |||
| double w0 = 2 * PI * central_freq_ / sample_rate_; | |||
| double bw_Hz = central_freq_ / Q_; | |||
| double a0 = 1.; | |||
| double a2 = exp(-2 * PI * bw_Hz / sample_rate_); | |||
| double a1 = -4 * a2 / (1 + a2) * cos(w0); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(a2 != 0, "BandBiquad: ZeroDivisionError."); | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| a2 != 0, "BandBiquad: zero division error, 'central_freq / Q / sample_rate' got a big negative value."); | |||
| double b0 = sqrt(1 - a1 * a1 / (4 * a2)) * (1 - a2); | |||
| if (noise_) { | |||
| CHECK_FAIL_RETURN_UNEXPECTED(b0 != 0, "BandBiquad: ZeroDivisionError."); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(b0 != 0, "BandBiquad: zero division error, 'b0' can not be zero."); | |||
| double mutl = sqrt(((1 + a2) * (1 + a2) - a1 * a1) * (1 - a2) / (1 + a2)) / b0; | |||
| b0 *= mutl; | |||
| } | |||
| @@ -22,13 +22,9 @@ namespace mindspore { | |||
| namespace dataset { | |||
| Status BandpassBiquadOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "BandpassBiquad: input tensor is not in shape of <..., time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("BandpassBiquad", input, kMinAudioDim, "<..., time>")); | |||
| // check input type, it should be DE_FLOAT32 or DE_FLOAT16 or DE_FLOAT64 | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type() == DataType(DataType::DE_FLOAT32) || input->type() == DataType(DataType::DE_FLOAT16) || | |||
| input->type() == DataType(DataType::DE_FLOAT64), | |||
| "BandpassBiquad: input tensor type should be float, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorFloat("BandpassBiquad", input)); | |||
| float w0 = 2 * PI * central_freq_ / sample_rate_; | |||
| float alpha = sin(w0) / 2 / Q_; | |||
| float temp; | |||
| @@ -23,13 +23,8 @@ namespace dataset { | |||
| Status BandrejectBiquadOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| // check input type and input shape | |||
| TensorShape input_shape = input->shape(); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, | |||
| "BandrejectBiquad: input tensor is not in shape of <..., time>."); | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type() == DataType(DataType::DE_FLOAT32) || input->type() == DataType(DataType::DE_FLOAT16) || | |||
| input->type() == DataType(DataType::DE_FLOAT64), | |||
| "BandrejectBiquad: input tensor type should be float, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("BandrejectBiquad", input, kMinAudioDim, "<..., time>")); | |||
| RETURN_IF_NOT_OK(ValidateTensorFloat("BandrejectBiquad", input)); | |||
| double w0 = 2 * PI * central_freq_ / sample_rate_; | |||
| double alpha = sin(w0) / 2 / Q_; | |||
| double b0 = 1; | |||
| @@ -22,14 +22,9 @@ namespace mindspore { | |||
| namespace dataset { | |||
| Status BassBiquadOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "BassBiquad: input tensor is not in shape of <..., time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("BassBiquad", input, kMinAudioDim, "<..., time>")); | |||
| // check input type, it should be DE_FLOAT32 or DE_FLOAT16 or DE_FLOAT64 | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->type() == DataType(DataType::DE_FLOAT32) || | |||
| input->type() == DataType(DataType::DE_FLOAT16) || | |||
| input->type() == DataType(DataType::DE_FLOAT64), | |||
| "BassBiquad: input tensor type should be float, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorFloat("BassBiquad", input)); | |||
| double w0 = 2 * PI * central_freq_ / sample_rate_; | |||
| double alpha = sin(w0) / 2 / Q_; | |||
| double A = exp(gain_ / 40 * log(10)); | |||
| @@ -22,14 +22,10 @@ namespace mindspore { | |||
| namespace dataset { | |||
| Status BiquadOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| // check input tensor dimension, it should be greater than 0. | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "Biquad: input tensor is not in shape of <..., time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("Biquad", input, kMinAudioDim, "<..., time>")); | |||
| // check input type, it should be DE_FLOAT32 or DE_FLOAT16 or DE_FLOAT64 | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type() == DataType(DataType::DE_FLOAT32) || input->type() == DataType(DataType::DE_FLOAT16) || | |||
| input->type() == DataType(DataType::DE_FLOAT64), | |||
| "Biquad: input tensor type should be float or double, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorFloat("Biquad", input)); | |||
| if (input->type() == DataType(DataType::DE_FLOAT32)) { | |||
| return Biquad(input, output, static_cast<float>(b0_), static_cast<float>(b1_), static_cast<float>(b2_), | |||
| static_cast<float>(a0_), static_cast<float>(a1_), static_cast<float>(a2_)); | |||
| @@ -25,8 +25,7 @@ ComplexNormOp::ComplexNormOp(float power) : power_(power) {} | |||
| // main function | |||
| Status ComplexNormOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->Rank() >= 2, "ComplexNorm: input tensor is not in shape of <..., 2>."); | |||
| RETURN_IF_NOT_OK(ValidateTensorShape("ComplexNorm", input->IsComplex(), "<..., complex=2>")); | |||
| return ComplexNorm(input, output, power_); | |||
| } | |||
| @@ -38,11 +37,13 @@ Status ComplexNormOp::OutputShape(const std::vector<TensorShape> &inputs, std::v | |||
| TensorShape out = TensorShape(input_size); | |||
| outputs.emplace_back(out); | |||
| if (!outputs.empty()) return Status::OK(); | |||
| return Status(StatusCode::kMDUnexpectedError, "ComplexNorm: invalid input shape."); | |||
| return Status(StatusCode::kMDUnexpectedError, "ComplexNorm: invalid shape of input tensor."); | |||
| } | |||
| Status ComplexNormOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs)); | |||
| RETURN_IF_NOT_OK( | |||
| ValidateTensorType("ComplexNorm", inputs[0].IsNumeric(), "[int, float, double]", inputs[0].ToString())); | |||
| if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| outputs[0] = DataType(DataType::DE_FLOAT64); | |||
| } else { | |||
| @@ -30,10 +30,8 @@ Status ComputeDeltasOp::Compute(const std::shared_ptr<Tensor> &input, std::share | |||
| Status ComputeDeltasOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs)); | |||
| if (!inputs[0].IsNumeric()) { | |||
| RETURN_STATUS_UNEXPECTED("ComputeDeltas: input tensor type should be int, float or double, but got: " + | |||
| inputs[0].ToString()); | |||
| } else if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| RETURN_IF_NOT_OK(ValidateTensorType("Biquad", inputs[0].IsNumeric(), "[int, float, double]", inputs[0].ToString())); | |||
| if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| outputs[0] = DataType(DataType::DE_FLOAT64); | |||
| } else { | |||
| outputs[0] = DataType(DataType::DE_FLOAT32); | |||
| @@ -24,13 +24,10 @@ namespace mindspore { | |||
| namespace dataset { | |||
| Status ContrastOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| // check input tensor dimension, it should be greater than 0. | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "Contrast: input tensor is not in shape of <..., time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("Contrast", input, kMinAudioDim, "<..., time>")); | |||
| // check input type, it should be DE_FLOAT | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type().IsNumeric(), | |||
| "Contrast: input tensor type should be int, float or double, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("Contrast", input)); | |||
| if (input->type() == DataType(DataType::DE_FLOAT64)) { | |||
| return Contrast(input, output, static_cast<double>(enhancement_amount_)); | |||
| @@ -43,13 +40,11 @@ Status ContrastOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr | |||
| Status ContrastOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs)); | |||
| if (inputs[0] >= DataType::DE_INT8 && inputs[0] <= DataType::DE_FLOAT32) { | |||
| outputs[0] = DataType(DataType::DE_FLOAT32); | |||
| } else if (inputs[0] == DataType::DE_FLOAT64) { | |||
| RETURN_IF_NOT_OK(ValidateTensorType("Contrast", inputs[0].IsNumeric(), "[int, float, double]", inputs[0].ToString())); | |||
| if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| outputs[0] = DataType(DataType::DE_FLOAT64); | |||
| } else { | |||
| RETURN_STATUS_UNEXPECTED("Contrast: input tensor type should be int, float or double, but got: " + | |||
| inputs[0].ToString()); | |||
| outputs[0] = DataType(DataType::DE_FLOAT32); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| @@ -23,12 +23,9 @@ namespace mindspore { | |||
| namespace dataset { | |||
| Status DBToAmplitudeOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| // check input tensor dimension, it should be greater than 0. | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "DBToAmplitude: input tensor is not in shape of <..., time>."); | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type().IsNumeric(), | |||
| "DBToAmplitude: input tensor type should be int, float or double, but got " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("DBToAmplitude", input, kMinAudioDim, "<..., time>")); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("DBToAmplitude", input)); | |||
| std::shared_ptr<Tensor> input_tensor; | |||
| if (input->type() != DataType(DataType::DE_FLOAT64)) { | |||
| @@ -23,11 +23,9 @@ namespace dataset { | |||
| Status DCShiftOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| // input <..., time>. | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->Rank() > 0, "ComplexNorm: input tensor is not in shape of <..., time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("DCShift", input, kMinAudioDim, "<..., time>")); | |||
| // If datatype is not a numeric type, then we cannot deal with the data. | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type().IsNumeric(), | |||
| "DCShift: input tensor type should be int, float or double, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("DCShift", input)); | |||
| if (input->type() == DataType(DataType::DE_FLOAT64)) { | |||
| return DCShift<double>(input, output, shift_, limiter_gain_); | |||
| } else { | |||
| @@ -39,9 +37,7 @@ Status DCShiftOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr< | |||
| Status DCShiftOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs)); | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| inputs[0].IsNumeric(), | |||
| "DCShift: input tensor type should be int, float or double, but got: " + inputs[0].ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorType("DCShift", inputs[0].IsNumeric(), "[int, float, double]", inputs[0].ToString())); | |||
| if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| outputs[0] = DataType(DataType::DE_FLOAT64); | |||
| } else { | |||
| @@ -22,24 +22,21 @@ namespace mindspore { | |||
| namespace dataset { | |||
| Status DeemphBiquadOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "DeemphBiquad: input tensor is not in shape of <..., time>."); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->type() == DataType(DataType::DE_FLOAT32) || | |||
| input->type() == DataType(DataType::DE_FLOAT16) || | |||
| input->type() == DataType(DataType::DE_FLOAT64), | |||
| "DeemphBiquad: input tensor type should be float, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("DeemphBiquad", input, kMinAudioDim, "<..., time>")); | |||
| RETURN_IF_NOT_OK(ValidateTensorFloat("DeemphBiquad", input)); | |||
| const int32_t kSampleRate44100 = 44100; | |||
| const int32_t kSampleRate48000 = 48000; | |||
| int32_t central_freq = 0; | |||
| double width_slope = 0.0; | |||
| double width_slope = 1; | |||
| double gain = 0.0; | |||
| // central_freq, width_slope and gain value reference sox values | |||
| if (sample_rate_ == 44100) { | |||
| central_freq = 5283; | |||
| width_slope = 0.4845; | |||
| gain = -9.477; | |||
| } else if (sample_rate_ == 48000) { | |||
| central_freq = 5356; | |||
| width_slope = 0.479; | |||
| gain = -9.62; | |||
| if (sample_rate_ == kSampleRate44100) { | |||
| central_freq = 5283; // central_freq value from SoX | |||
| width_slope = 0.4845; // width_slope value from SoX | |||
| gain = -9.477; // gain value from SoX | |||
| } else if (sample_rate_ == kSampleRate48000) { | |||
| central_freq = 5356; // central_freq value from SoX | |||
| width_slope = 0.479; // width_slope value from SoX | |||
| gain = -9.62; // gain value from SoX | |||
| } | |||
| double w0 = 2 * PI * central_freq / sample_rate_; | |||
| @@ -22,15 +22,10 @@ namespace mindspore { | |||
| namespace dataset { | |||
| Status DetectPitchFrequencyOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| // check input tensor dimension, it should be greater than 0. | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, | |||
| "DetectPitchFrequency: input tensor is not in shape of <..., time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("DetectPitchFrequency", input, kMinAudioDim, "<..., time>")); | |||
| // check input type, it should be DE_FLOAT16, DE_FLOAT32 or DE_FLOAT64 | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type() == DataType(DataType::DE_FLOAT32) || input->type() == DataType(DataType::DE_FLOAT64) || | |||
| input->type() == DataType(DataType::DE_FLOAT16), | |||
| "DetectPitchFrequency: input tensor type should be float or double, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorFloat("DetectPitchFrequency", input)); | |||
| return DetectPitchFrequency(input, output, sample_rate_, frame_time_, win_length_, freq_low_, freq_high_); | |||
| } | |||
| } // namespace dataset | |||
| @@ -25,13 +25,9 @@ constexpr float_t EqualizerBiquadOp::kQ = 0.707; | |||
| Status EqualizerBiquadOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| // check input tensor dimension, it should be greater than 0. | |||
| TensorShape input_shape = input->shape(); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "EqualizerBiquad: input tensor in not in shape of <..., time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("EqualizerBiquad", input, kMinAudioDim, "<..., time>")); | |||
| // check input tensor type, it should be DE_FLOAT32 or DE_FLOAT16 or DE_FLOAT64 | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type() == DataType(DataType::DE_FLOAT32) || input->type() == DataType(DataType::DE_FLOAT16) || | |||
| input->type() == DataType(DataType::DE_FLOAT64), | |||
| "EqualizerBiquad: input tensor type should be float, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorFloat("EqualizerBiquad", input)); | |||
| double w0 = 2.0 * PI * center_freq_ / sample_rate_; | |||
| double alpha = sin(w0) / 2.0 / Q_; | |||
| @@ -27,10 +27,8 @@ constexpr FadeShape FadeOp::kFadeShape = FadeShape::kLinear; | |||
| Status FadeOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->shape().Size() >= 2, "Fade: input tensor is not in shape of <..., time>."); | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| DataType::DE_INT8 <= input->type().value() && input->type().value() <= DataType::DE_FLOAT64, | |||
| "Fade: input tensor type should be int, float or double, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("Fade", input, kMinAudioDim, "<..., time>")); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("Fade", input)); | |||
| if (fade_in_len_ == 0 && fade_out_len_ == 0) { | |||
| *output = input; | |||
| } else { | |||
| @@ -41,13 +39,11 @@ Status FadeOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Ten | |||
| Status FadeOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs)); | |||
| if (inputs[0] >= DataType::DE_INT8 && inputs[0] <= DataType::DE_FLOAT32) { | |||
| outputs[0] == DataType(DataType::DE_FLOAT32); | |||
| } else if (inputs[0] == DataType::DE_FLOAT64) { | |||
| outputs[0] == DataType(DataType::DE_FLOAT64); | |||
| RETURN_IF_NOT_OK(ValidateTensorType("Fade", inputs[0].IsNumeric(), "[int, float, double]", inputs[0].ToString())); | |||
| if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| outputs[0] = DataType(DataType::DE_FLOAT64); | |||
| } else { | |||
| RETURN_STATUS_UNEXPECTED("Fade: input tensor type should be int, float or double, but got: " + | |||
| inputs[0].ToString()); | |||
| outputs[0] = DataType(DataType::DE_FLOAT32); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| @@ -23,18 +23,18 @@ namespace dataset { | |||
| Status FlangerOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| // check input dimensions, it should be 2 dimensions or more | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->shape().Rank() >= 2, | |||
| "Flanger: input tensor is not in shape of <..., channel, time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("Flanger", input, kDefaultAudioDim, "<..., channel, time>")); | |||
| // check input channel, it should be less than or equal to 4 | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->shape()[-2] <= 4, | |||
| "Flanger: the channel of input tensor must be less than or equal to 4, but got: " + | |||
| std::to_string(input->shape()[-2])); | |||
| const int32_t kChannelIndex = -2; | |||
| const int32_t kChannelLimit = 4; | |||
| CHECK_FAIL_RETURN_SYNTAX_ERROR(input->shape()[kChannelIndex] <= kChannelLimit, | |||
| "Flanger: the channel of input tensor dose not match the requirement of operator. " | |||
| "Expecting tensor with channel less than or equal to 4. But got channel: " + | |||
| std::to_string(input->shape()[kChannelIndex])); | |||
| // check input type, it should be [int, float, double] | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type().IsNumeric(), | |||
| "Flanger: input tensor type should be int, float or double, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("Flanger", input)); | |||
| if (input->type() == DataType(DataType::DE_FLOAT64)) { | |||
| return Flanger<double>(input, output, sample_rate_, delay_, depth_, regen_, width_, speed_, phase_, Modulation_, | |||
| @@ -47,9 +47,7 @@ Status FlangerOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr< | |||
| Status FlangerOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs)); | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| inputs[0].IsNumeric(), | |||
| "Flanger: input tensor type should be int, float or double, but got: " + inputs[0].ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorType("Flanger", inputs[0].IsNumeric(), "[int, float, double]", inputs[0].ToString())); | |||
| outputs[0] = inputs[0]; | |||
| return Status::OK(); | |||
| } | |||
| @@ -36,17 +36,17 @@ FrequencyMaskingOp::FrequencyMaskingOp(bool iid_masks, int32_t frequency_mask_pa | |||
| Status FrequencyMaskingOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| // input <..., freq, time> | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->Rank() >= 2, | |||
| "FrequencyMasking: input tensor is not in shape of <..., freq, time>."); | |||
| TensorShape input_shape = input->shape(); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("FrequencyMasking", input, kDefaultAudioDim, "<..., freq, time>")); | |||
| const int32_t kFreqIndex = -2; | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input_shape[-2] >= frequency_mask_param_, | |||
| "FrequencyMasking: frequency_mask_param should be less than or equal to the length of frequency dimension."); | |||
| input->shape()[kFreqIndex] >= frequency_mask_param_, | |||
| "FrequencyMasking: invalid parameter, 'frequency_mask_param' should be less than or equal to " | |||
| "the length of frequency dimension, but got: 'frequency_mask_param' " + | |||
| std::to_string(frequency_mask_param_) + " and length " + std::to_string(input->shape()[kFreqIndex])); | |||
| std::shared_ptr<Tensor> input_tensor; | |||
| // typecast | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->type() != DataType::DE_STRING, | |||
| "FrequencyMasking: input tensor type should be float, but got: string."); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("FrequencyMasking", input)); | |||
| if (input->type() != DataType::DE_FLOAT64) { | |||
| RETURN_IF_NOT_OK(TypeCast(input, &input_tensor, DataType(DataType::DE_FLOAT32))); | |||
| } else { | |||
| @@ -59,5 +59,17 @@ Status FrequencyMaskingOp::Compute(const std::shared_ptr<Tensor> &input, std::sh | |||
| return RandomMaskAlongAxis(input_tensor, output, frequency_mask_param_, mask_value_, 1, rnd_); | |||
| } | |||
| } | |||
| Status FrequencyMaskingOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs)); | |||
| RETURN_IF_NOT_OK( | |||
| ValidateTensorType("FrequencyMasking", inputs[0].IsNumeric(), "[int, float, double]", inputs[0].ToString())); | |||
| if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| outputs[0] = DataType(DataType::DE_FLOAT64); | |||
| } else { | |||
| outputs[0] = DataType(DataType::DE_FLOAT32); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| } // namespace dataset | |||
| } // namespace mindspore | |||
| @@ -37,6 +37,8 @@ class FrequencyMaskingOp : public TensorOp { | |||
| Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override; | |||
| Status OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) override; | |||
| std::string Name() const override { return kFrequencyMaskingOp; } | |||
| private: | |||
| @@ -22,14 +22,10 @@ namespace mindspore { | |||
| namespace dataset { | |||
| Status HighpassBiquadOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| // check input tensor dimension, it should be greater than 0. | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "HighpassBiquad: input tensor in not in shape of <..., time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("HighpassBiquad", input, kMinAudioDim, "<..., time>")); | |||
| // check input tensor type, it should be DE_FLOAT32 or DE_FLOAT16 or DE_FLOAT64 | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type() == DataType(DataType::DE_FLOAT32) || input->type() == DataType(DataType::DE_FLOAT16) || | |||
| input->type() == DataType(DataType::DE_FLOAT64), | |||
| "HighpassBiquad: input tensor type should be float, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorFloat("HighpassBiquad", input)); | |||
| double w0 = 2 * PI * cutoff_freq_ / sample_rate_; | |||
| double alpha = sin(w0) / 2 / Q_; | |||
| @@ -22,12 +22,8 @@ namespace mindspore { | |||
| namespace dataset { | |||
| Status LFilterOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "LFilter: input tensor is not in shape of <..., time>."); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->type() == DataType(DataType::DE_FLOAT32) || | |||
| input->type() == DataType(DataType::DE_FLOAT16) || | |||
| input->type() == DataType(DataType::DE_FLOAT64), | |||
| "LFilter: input tensor type should be float, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("LFilter", input, kMinAudioDim, "<..., time>")); | |||
| RETURN_IF_NOT_OK(ValidateTensorFloat("LFilter", input)); | |||
| if (input->type() == DataType(DataType::DE_FLOAT32)) { | |||
| return LFilter(input, output, a_coeffs_, b_coeffs_, clamp_); | |||
| } else if (input->type() == DataType(DataType::DE_FLOAT64)) { | |||
| @@ -26,14 +26,10 @@ const float LowpassBiquadOp::kQ = 0.707; | |||
| Status LowpassBiquadOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| // check input tensor dimension, it should be greater than 0. | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "LowpassBiquad: input tensor is not in shape of <..., time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("LowpassBiquad", input, kMinAudioDim, "<..., time>")); | |||
| // check input type, it should be DE_FLOAT32 or DE_FLOAT16 or DE_FLOAT64 | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type() == DataType(DataType::DE_FLOAT32) || input->type() == DataType(DataType::DE_FLOAT16) || | |||
| input->type() == DataType(DataType::DE_FLOAT64), | |||
| "LowpassBiquad: input tensor type should be float, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorFloat("LowpassBiquad", input)); | |||
| double w0 = 2 * PI * cutoff_freq_ / sample_rate_; | |||
| double alpha = sin(w0) / 2 / Q_; | |||
| @@ -24,11 +24,8 @@ constexpr float MagphaseOp::kPower = 1.0; | |||
| Status MagphaseOp::Compute(const TensorRow &input, TensorRow *output) { | |||
| IO_CHECK_VECTOR(input, output); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input[0]->shape().Size() >= 2 && input[0]->shape()[-1] == 2, | |||
| "Magphase: input tensor is not in shape of <..., 2>."); | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input[0]->type().IsNumeric(), | |||
| "Magphase: input tensor type should be int, float or double, but got: " + input[0]->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorShape("Magphase", input[0]->IsComplex(), "<..., complex=2>")); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("Magphase", input[0])); | |||
| RETURN_IF_NOT_OK(Magphase(input, output, power_)); | |||
| return Status::OK(); | |||
| } | |||
| @@ -43,11 +40,12 @@ Status MagphaseOp::OutputShape(const std::vector<TensorShape> &inputs, std::vect | |||
| if (!outputs.empty()) { | |||
| return Status::OK(); | |||
| } | |||
| return Status(StatusCode::kMDUnexpectedError, "Magphase: invalid input wrong shape."); | |||
| return Status(StatusCode::kMDUnexpectedError, "Magphase: invalid shape of input tensor."); | |||
| } | |||
| Status MagphaseOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs)); | |||
| RETURN_IF_NOT_OK(ValidateTensorType("Magphase", inputs[0].IsNumeric(), "[int, float, double]", inputs[0].ToString())); | |||
| if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| outputs[0] = DataType(DataType::DE_FLOAT64); | |||
| } else { | |||
| @@ -25,27 +25,19 @@ MuLawDecodingOp::MuLawDecodingOp(int32_t quantization_channels) : quantization_c | |||
| // main function | |||
| Status MuLawDecodingOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->Rank() >= 1, "MuLawDecoding: input tensor is not in shape of <..., time>."); | |||
| if (input->type().IsNumeric()) { | |||
| return MuLawDecoding(input, output, quantization_channels_); | |||
| } else { | |||
| RETURN_STATUS_UNEXPECTED("MuLawDecoding: input tensor type should be int, float or double, but got: " + | |||
| input->type().ToString()); | |||
| } | |||
| RETURN_IF_NOT_OK(ValidateLowRank("MuLawDecoding", input, kMinAudioDim, "<..., time>")); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("MuLawDecoding", input)); | |||
| return MuLawDecoding(input, output, quantization_channels_); | |||
| } | |||
| Status MuLawDecodingOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs)); | |||
| RETURN_IF_NOT_OK( | |||
| ValidateTensorType("MuLawDecoding", inputs[0].IsNumeric(), "[int, float, double]", inputs[0].ToString())); | |||
| if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| outputs[0] = DataType(DataType::DE_FLOAT64); | |||
| } else if (inputs[0].IsInt() || inputs[0] == DataType(DataType::DE_FLOAT16) || | |||
| inputs[0] == DataType(DataType::DE_FLOAT32)) { | |||
| outputs[0] = DataType(DataType::DE_FLOAT32); | |||
| } else { | |||
| RETURN_STATUS_UNEXPECTED("MuLawDecoding: input tensor type should be int, float or double, but got: " + | |||
| inputs[0].ToString()); | |||
| outputs[0] = DataType(DataType::DE_FLOAT32); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| @@ -26,19 +26,15 @@ MuLawEncodingOp::MuLawEncodingOp(int32_t quantization_channels) : quantization_c | |||
| // main function | |||
| Status MuLawEncodingOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->Rank() >= 1, "MuLawEncoding: input tensor is not in shape of <..., time>."); | |||
| if (input->type().IsNumeric()) { | |||
| return MuLawEncoding(input, output, quantization_channels_); | |||
| } else { | |||
| RETURN_STATUS_UNEXPECTED("MuLawEncoding: input tensor type should be int, float or double, but got: " + | |||
| input->type().ToString()); | |||
| } | |||
| RETURN_IF_NOT_OK(ValidateLowRank("MuLawEncoding", input, kMinAudioDim, "<..., time>")); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("MuLawEncoding", input)); | |||
| return MuLawEncoding(input, output, quantization_channels_); | |||
| } | |||
| Status MuLawEncodingOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs)); | |||
| RETURN_IF_NOT_OK( | |||
| ValidateTensorType("MuLawEncoding", inputs[0].IsNumeric(), "[int, float, double]", inputs[0].ToString())); | |||
| outputs[0] = DataType(DataType::DE_INT32); | |||
| return Status::OK(); | |||
| } | |||
| @@ -25,13 +25,10 @@ OverdriveOp::OverdriveOp(float gain, float color) : gain_(gain), color_(color) { | |||
| Status OverdriveOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| // check input tensor dimension, it should be greater than 0. | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "Overdrive: input tensor is not in shape of <..., time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("Overdrive", input, kMinAudioDim, "<..., time>")); | |||
| // check input type, it should be numeric. | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type().IsNumeric(), | |||
| "Overdrive: input tensor type should be int, float or double, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("Overdrive", input)); | |||
| std::shared_ptr<Tensor> input_tensor; | |||
| if (input->type() != DataType::DE_FLOAT64) { | |||
| RETURN_IF_NOT_OK(TypeCast(input, &input_tensor, DataType(DataType::DE_FLOAT32))); | |||
| @@ -44,10 +41,9 @@ Status OverdriveOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_pt | |||
| Status OverdriveOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs)); | |||
| if (!inputs[0].IsNumeric()) { | |||
| RETURN_STATUS_UNEXPECTED("Overdrive: input tensor type should be int, float or double, but got: " + | |||
| inputs[0].ToString()); | |||
| } else if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| RETURN_IF_NOT_OK( | |||
| ValidateTensorType("Overdrive", inputs[0].IsNumeric(), "[int, float, double]", inputs[0].ToString())); | |||
| if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| outputs[0] = DataType(DataType::DE_FLOAT64); | |||
| } else { | |||
| outputs[0] = DataType(DataType::DE_FLOAT32); | |||
| @@ -32,13 +32,10 @@ PhaserOp::PhaserOp(int32_t sample_rate, float gain_in, float gain_out, float del | |||
| Status PhaserOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| // check input tensor dimension, it should be greater than 0. | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "Phaser: input tensor is not in shape of <..., time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("Phaser", input, kMinAudioDim, "<..., time>")); | |||
| // check input type, it should be DE_FLOAT | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type().IsNumeric(), | |||
| "Phaser: input tensor type should be int, float or double, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("Phaser", input)); | |||
| std::shared_ptr<Tensor> input_tensor; | |||
| if (input->type() != DataType::DE_FLOAT64) { | |||
| RETURN_IF_NOT_OK(TypeCast(input, &input_tensor, DataType(DataType::DE_FLOAT32))); | |||
| @@ -53,10 +50,8 @@ Status PhaserOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<T | |||
| Status PhaserOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs)); | |||
| if (!inputs[0].IsNumeric()) { | |||
| RETURN_STATUS_UNEXPECTED("Phaser: input tensor type should be int, float or double, but got: " + | |||
| inputs[0].ToString()); | |||
| } else if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| RETURN_IF_NOT_OK(ValidateTensorType("Phaser", inputs[0].IsNumeric(), "[int, float, double]", inputs[0].ToString())); | |||
| if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| outputs[0] = DataType(DataType::DE_FLOAT64); | |||
| } else { | |||
| outputs[0] = DataType(DataType::DE_FLOAT32); | |||
| @@ -26,14 +26,10 @@ RiaaBiquadOp::RiaaBiquadOp(int32_t sample_rate) : sample_rate_(sample_rate) {} | |||
| Status RiaaBiquadOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| // check input tensor dimension, it should be greater than 0. | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "RiaaBiquad: input tensor is not in shape of <..., time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("RiaaBiquad", input, kMinAudioDim, "<..., time>")); | |||
| // check input type, it should be DE_FLOAT32 or DE_FLOAT16 or DE_FLOAT64. | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->type() == DataType(DataType::DE_FLOAT32) || | |||
| input->type() == DataType(DataType::DE_FLOAT16) || | |||
| input->type() == DataType(DataType::DE_FLOAT64), | |||
| "RiaaBiquad: input tensor type should be float, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorFloat("RiaaBiquad", input)); | |||
| // indicate array zeros and poles. | |||
| const std::map<int32_t, std::vector<float>> kZeros = { | |||
| {44100, {-0.2014898, 0.9233820}}, | |||
| @@ -32,16 +32,17 @@ TimeMaskingOp::TimeMaskingOp(bool iid_masks, int32_t time_mask_param, int32_t ma | |||
| Status TimeMaskingOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| // input <..., freq, time> | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->Rank() >= 2, "TimeMasking: input tensor is not in shape of <..., freq, time>."); | |||
| TensorShape input_shape = input->shape(); | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input_shape[-1] >= time_mask_param_, | |||
| "TimeMasking: time_mask_param should be less than or equal to the length of time dimension."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("TimeMasking", input, kDefaultAudioDim, "<..., freq, time>")); | |||
| const int32_t kTimeIndex = -1; | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->shape()[kTimeIndex] >= time_mask_param_, | |||
| "TimeMasking: invalid parameter, 'time_mask_param' should be less than or equal to " | |||
| "the length of time dimension, but got: 'frequency_mask_param' " + | |||
| std::to_string(time_mask_param_) + " and length " + | |||
| std::to_string(input->shape()[kTimeIndex])); | |||
| std::shared_ptr<Tensor> input_tensor; | |||
| // typecast | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->type() != DataType::DE_STRING, | |||
| "TimeMasking: input tensor type should be float, but got: string."); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("TimeMasking", input)); | |||
| if (input->type() != DataType::DE_FLOAT64) { | |||
| RETURN_IF_NOT_OK(TypeCast(input, &input_tensor, DataType(DataType::DE_FLOAT32))); | |||
| } else { | |||
| @@ -55,5 +56,17 @@ Status TimeMaskingOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ | |||
| return RandomMaskAlongAxis(input_tensor, output, time_mask_param_, mask_value_, 2, rnd_); | |||
| } | |||
| } | |||
| Status TimeMaskingOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs)); | |||
| RETURN_IF_NOT_OK( | |||
| ValidateTensorType("TimeMasking", inputs[0].IsNumeric(), "[int, float, double]", inputs[0].ToString())); | |||
| if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| outputs[0] = DataType(DataType::DE_FLOAT64); | |||
| } else { | |||
| outputs[0] = DataType(DataType::DE_FLOAT32); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| } // namespace dataset | |||
| } // namespace mindspore | |||
| @@ -37,6 +37,8 @@ class TimeMaskingOp : public TensorOp { | |||
| Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override; | |||
| Status OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) override; | |||
| std::string Name() const override { return kTimeMaskingOp; } | |||
| private: | |||
| @@ -32,17 +32,14 @@ Status TimeStretchOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ | |||
| IO_CHECK(input, output); | |||
| // check shape | |||
| if (input->shape().Rank() < 3 || !input->IsComplex()) { | |||
| std::string err_msg = "TimeStretch: input tensor is not in shape of <..., freq, num_frame, complex=2>."; | |||
| LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg); | |||
| } | |||
| RETURN_IF_NOT_OK(ValidateTensorShape("TimeStretch", input->shape().Size() > kDefaultAudioDim && input->IsComplex(), | |||
| "<..., freq, num_frame, complex=2>")); | |||
| std::shared_ptr<Tensor> input_tensor; | |||
| float hop_length = std::isnan(hop_length_) ? (n_freq_ - 1) : hop_length_; | |||
| float fixed_rate = std::isnan(fixed_rate_) ? 1 : fixed_rate_; | |||
| // typecast | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->type() != DataType::DE_STRING, | |||
| "TimeStretch: input tensor type should be int, float or double, but got: string."); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("TimeStretch", input)); | |||
| if (input->type() != DataType::DE_FLOAT64) { | |||
| RETURN_IF_NOT_OK(TypeCast(input, &input_tensor, DataType(DataType::DE_FLOAT32))); | |||
| } else { | |||
| @@ -52,6 +49,18 @@ Status TimeStretchOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ | |||
| return TimeStretch(input_tensor, output, fixed_rate, hop_length, n_freq_); | |||
| } | |||
| Status TimeStretchOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs)); | |||
| RETURN_IF_NOT_OK( | |||
| ValidateTensorType("TimeStretch", inputs[0].IsNumeric(), "[int, float, double]", inputs[0].ToString())); | |||
| if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| outputs[0] = DataType(DataType::DE_FLOAT64); | |||
| } else { | |||
| outputs[0] = DataType(DataType::DE_FLOAT32); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| Status TimeStretchOp::OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputShape(inputs, outputs)); | |||
| outputs.clear(); | |||
| @@ -44,6 +44,8 @@ class TimeStretchOp : public TensorOp { | |||
| std::string Name() const override { return kTimeStretchOp; } | |||
| Status OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) override; | |||
| Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override; | |||
| private: | |||
| @@ -26,14 +26,10 @@ TrebleBiquadOp::TrebleBiquadOp(int32_t sample_rate, float gain, float central_fr | |||
| Status TrebleBiquadOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| TensorShape input_shape = input->shape(); | |||
| // check input tensor dimension, it should be greater than 0. | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "TrebleBiquad: input tensor is not in shape of <..., time>."); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("TrebleBiquad", input, kMinAudioDim, "<..., time>")); | |||
| // check input type, it should be DE_FLOAT32 or DE_FLOAT16 or DE_FLOAT64 | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input->type() == DataType(DataType::DE_FLOAT32) || | |||
| input->type() == DataType(DataType::DE_FLOAT16) || | |||
| input->type() == DataType(DataType::DE_FLOAT64), | |||
| "TrebleBiquad: input tensor type should be float, but got: " + input->type().ToString()); | |||
| RETURN_IF_NOT_OK(ValidateTensorFloat("TrebleBiquad", input)); | |||
| // computer a0, a1, a2, b0, b1, b2 | |||
| float w0 = 2 * PI * central_freq_ / sample_rate_; | |||
| float alpha = sin(w0) / 2 / Q_; | |||
| @@ -63,6 +59,5 @@ Status TrebleBiquadOp::Compute(const std::shared_ptr<Tensor> &input, std::shared | |||
| static_cast<float16>(a0), static_cast<float16>(a1), static_cast<float16>(a2)); | |||
| } | |||
| } | |||
| } // namespace dataset | |||
| } // namespace mindspore | |||
| @@ -23,12 +23,9 @@ namespace mindspore { | |||
| namespace dataset { | |||
| Status VolOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) { | |||
| IO_CHECK(input, output); | |||
| RETURN_IF_NOT_OK(ValidateLowRank("Vol", input, kMinAudioDim, "<..., time>")); | |||
| RETURN_IF_NOT_OK(ValidateTensorNumeric("Vol", input)); | |||
| std::shared_ptr<Tensor> input_tensor; | |||
| TensorShape input_shape = input->shape(); | |||
| CHECK_FAIL_RETURN_UNEXPECTED(input_shape.Size() > 0, "Vol: input tensor is not in shape of <..., time>."); | |||
| CHECK_FAIL_RETURN_UNEXPECTED( | |||
| input->type().IsNumeric(), | |||
| "Vol: input tensor type should be int, float or double, but got: " + input->type().ToString()); | |||
| if (input->type() != DataType::DE_FLOAT64) { | |||
| RETURN_IF_NOT_OK(TypeCast(input, &input_tensor, DataType(DataType::DE_FLOAT32))); | |||
| return Vol(input_tensor, output, gain_, gain_type_); | |||
| @@ -40,9 +37,8 @@ Status VolOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tens | |||
| Status VolOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) { | |||
| RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs)); | |||
| if (!inputs[0].IsNumeric()) { | |||
| RETURN_STATUS_UNEXPECTED("Vol: input tensor type should be int, float or double, but got: " + inputs[0].ToString()); | |||
| } else if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| RETURN_IF_NOT_OK(ValidateTensorType("Vol", inputs[0].IsNumeric(), "[int, float, double]", inputs[0].ToString())); | |||
| if (inputs[0] == DataType(DataType::DE_FLOAT64)) { | |||
| outputs[0] = DataType(DataType::DE_FLOAT64); | |||
| } else { | |||
| outputs[0] = DataType(DataType::DE_FLOAT32); | |||
| @@ -317,6 +317,9 @@ class Tensor { | |||
| /// Check if tensor is complex | |||
| /// \return bool - true if tensor is complex | |||
| bool IsComplex() const { | |||
| if (shape_.empty()) { | |||
| return false; | |||
| } | |||
| // check the last dim all be 2 | |||
| return shape_[-1] == 2; | |||
| } | |||
| @@ -3543,7 +3543,7 @@ inline std::shared_ptr<SBUDataset> MS_API SBU(const std::string &dataset_dir, bo | |||
| /// \class SpeechCommandsDataset. | |||
| /// \brief A source dataset that reads and parses SpeechCommands dataset. | |||
| class SpeechCommandsDataset : public Dataset { | |||
| class MS_API SpeechCommandsDataset : public Dataset { | |||
| public: | |||
| /// \brief Constructor of SpeechCommandsDataset. | |||
| /// \param[in] dataset_dir Path to the root directory that contains the dataset. | |||
| @@ -3581,10 +3581,10 @@ class SpeechCommandsDataset : public Dataset { | |||
| /// given, a `RandomSampler` will be used to randomly iterate the entire dataset (default = RandomSampler()). | |||
| /// \param[in] cache Tensor cache to use (default=nullptr, which means no cache is used). | |||
| /// \return Shared pointer to the SpeechCommandsDataset. | |||
| inline std::shared_ptr<SpeechCommandsDataset> SpeechCommands( | |||
| const std::string &dataset_dir, const std::string &usage = "all", | |||
| const std::shared_ptr<Sampler> &sampler = std::make_shared<RandomSampler>(), | |||
| const std::shared_ptr<DatasetCache> &cache = nullptr) { | |||
| inline std::shared_ptr<SpeechCommandsDataset> MS_API | |||
| SpeechCommands(const std::string &dataset_dir, const std::string &usage = "all", | |||
| const std::shared_ptr<Sampler> &sampler = std::make_shared<RandomSampler>(), | |||
| const std::shared_ptr<DatasetCache> &cache = nullptr) { | |||
| return std::make_shared<SpeechCommandsDataset>(StringToChar(dataset_dir), StringToChar(usage), sampler, cache); | |||
| } | |||
| @@ -3595,9 +3595,9 @@ inline std::shared_ptr<SpeechCommandsDataset> SpeechCommands( | |||
| /// \param[in] sampler Raw pointer to a sampler object used to choose samples from the dataset. | |||
| /// \param[in] cache Tensor cache to use (default=nullptr, which means no cache is used). | |||
| /// \return Shared pointer to the SpeechCommandsDataset. | |||
| inline std::shared_ptr<SpeechCommandsDataset> SpeechCommands(const std::string &dataset_dir, const std::string &usage, | |||
| const Sampler *sampler, | |||
| const std::shared_ptr<DatasetCache> &cache = nullptr) { | |||
| inline std::shared_ptr<SpeechCommandsDataset> MS_API | |||
| SpeechCommands(const std::string &dataset_dir, const std::string &usage, const Sampler *sampler, | |||
| const std::shared_ptr<DatasetCache> &cache = nullptr) { | |||
| return std::make_shared<SpeechCommandsDataset>(StringToChar(dataset_dir), StringToChar(usage), sampler, cache); | |||
| } | |||
| @@ -3608,9 +3608,9 @@ inline std::shared_ptr<SpeechCommandsDataset> SpeechCommands(const std::string & | |||
| /// \param[in] sampler Sampler object used to choose samples from the dataset. | |||
| /// \param[in] cache Tensor cache to use (default=nullptr, which means no cache is used). | |||
| /// \return Shared pointer to the SpeechCommandsDataset. | |||
| inline std::shared_ptr<SpeechCommandsDataset> SpeechCommands(const std::string &dataset_dir, const std::string &usage, | |||
| const std::reference_wrapper<Sampler> sampler, | |||
| const std::shared_ptr<DatasetCache> &cache = nullptr) { | |||
| inline std::shared_ptr<SpeechCommandsDataset> MS_API | |||
| SpeechCommands(const std::string &dataset_dir, const std::string &usage, const std::reference_wrapper<Sampler> sampler, | |||
| const std::shared_ptr<DatasetCache> &cache = nullptr) { | |||
| return std::make_shared<SpeechCommandsDataset>(StringToChar(dataset_dir), StringToChar(usage), sampler, cache); | |||
| } | |||
| @@ -17,9 +17,13 @@ | |||
| #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_VALIDATORS_H_ | |||
| #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_VALIDATORS_H_ | |||
| #include <nlohmann/json.hpp> | |||
| #include <limits> | |||
| #include <memory> | |||
| #include <string> | |||
| #include <nlohmann/json.hpp> | |||
| #include "minddata/dataset/core/tensor.h" | |||
| #include "minddata/dataset/util/status.h" | |||
| namespace mindspore { | |||
| @@ -34,6 +38,162 @@ inline Status ValidateParamInJson(const nlohmann::json &json_obj, const std::str | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| inline Status ValidateTensorShape(const std::string &op_name, bool cond, const std::string &expected_shape = "", | |||
| const std::string &actual_dim = "") { | |||
| if (!cond) { | |||
| std::string err_msg = op_name + ": the shape of input tensor does not match the requirement of operator."; | |||
| if (expected_shape != "") { | |||
| err_msg += " Expecting tensor in shape of " + expected_shape + "."; | |||
| } | |||
| if (actual_dim != "") { | |||
| err_msg += " But got tensor with dimension " + actual_dim + "."; | |||
| } | |||
| RETURN_STATUS_UNEXPECTED(err_msg); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| inline Status ValidateLowRank(const std::string &op_name, const std::shared_ptr<Tensor> &input, dsize_t threshold = 0, | |||
| const std::string &expected_shape = "") { | |||
| dsize_t dim = input->shape().Size(); | |||
| return ValidateTensorShape(op_name, dim >= threshold, expected_shape, std::to_string(dim)); | |||
| } | |||
| inline Status ValidateTensorType(const std::string &op_name, bool cond, const std::string &expected_type = "", | |||
| const std::string &actual_type = "") { | |||
| if (!cond) { | |||
| std::string err_msg = op_name + ": the data type of input tensor does not match the requirement of operator."; | |||
| if (expected_type != "") { | |||
| err_msg += " Expecting tensor in type of " + expected_type + "."; | |||
| } | |||
| if (actual_type != "") { | |||
| err_msg += " But got type " + actual_type + "."; | |||
| } | |||
| RETURN_STATUS_UNEXPECTED(err_msg); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| inline Status ValidateTensorNumeric(const std::string &op_name, const std::shared_ptr<Tensor> &input) { | |||
| return ValidateTensorType(op_name, input->type().IsNumeric(), "[int, float, double]", input->type().ToString()); | |||
| } | |||
| inline Status ValidateTensorFloat(const std::string &op_name, const std::shared_ptr<Tensor> &input) { | |||
| return ValidateTensorType(op_name, input->type().IsFloat(), "[float, double]", input->type().ToString()); | |||
| } | |||
| template <typename T> | |||
| inline Status ValidateEqual(const std::string &op_name, const std::string ¶m_name, T param_value, | |||
| const std::string &other_name, T other_value) { | |||
| if (param_value != other_value) { | |||
| std::string err_msg = op_name + ": invalid parameter, '" + param_name + "' should be equal to '" + other_name + | |||
| "', but got: " + param_name + " " + std::to_string(param_value) + " while " + other_name + | |||
| " " + std::to_string(other_value) + "."; | |||
| RETURN_STATUS_UNEXPECTED(err_msg); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| template <typename T> | |||
| inline Status ValidateNotEqual(const std::string &op_name, const std::string ¶m_name, T param_value, | |||
| const std::string &other_name, T other_value) { | |||
| if (param_value == other_value) { | |||
| std::string err_msg = op_name + ": invalid parameter, '" + param_name + "' can not be equal to '" + other_name + | |||
| "', but got: " + param_name + " " + std::to_string(param_value) + " while " + other_name + | |||
| " " + std::to_string(other_value) + "."; | |||
| RETURN_STATUS_UNEXPECTED(err_msg); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| template <typename T> | |||
| inline Status ValidateGreaterThan(const std::string &op_name, const std::string ¶m_name, T param_value, | |||
| const std::string &other_name, T other_value) { | |||
| if (param_value <= other_value) { | |||
| std::string err_msg = op_name + ": invalid parameter, '" + param_name + "' should be greater than '" + other_name + | |||
| "', but got: " + param_name + " " + std::to_string(param_value) + " while " + other_name + | |||
| " " + std::to_string(other_value) + "."; | |||
| RETURN_STATUS_UNEXPECTED(err_msg); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| template <typename T> | |||
| inline Status ValidateLessThan(const std::string &op_name, const std::string ¶m_name, T param_value, | |||
| const std::string &other_name, T other_value) { | |||
| if (param_value >= other_value) { | |||
| std::string err_msg = op_name + ": invalid parameter, '" + param_name + "' should be less than '" + other_name + | |||
| "', but got: " + param_name + " " + std::to_string(param_value) + " while " + other_name + | |||
| " " + std::to_string(other_value) + "."; | |||
| RETURN_STATUS_UNEXPECTED(err_msg); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| template <typename T> | |||
| inline Status ValidateNoGreaterThan(const std::string &op_name, const std::string ¶m_name, T param_value, | |||
| const std::string &other_name, T other_value) { | |||
| if (param_value > other_value) { | |||
| std::string err_msg = op_name + ": invalid parameter, '" + param_name + "' should be no greater than '" + | |||
| other_name + "', but got: " + param_name + " " + std::to_string(param_value) + " while " + | |||
| other_name + " " + std::to_string(other_value) + "."; | |||
| RETURN_STATUS_UNEXPECTED(err_msg); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| template <typename T> | |||
| inline Status ValidateNoLessThan(const std::string &op_name, const std::string ¶m_name, T param_value, | |||
| const std::string &other_name, T other_value) { | |||
| if (param_value < other_value) { | |||
| std::string err_msg = op_name + ": invalid parameter, '" + param_name + "' should be no less than '" + other_name + | |||
| "', but got: " + param_name + " " + std::to_string(param_value) + " while " + other_name + | |||
| " " + std::to_string(other_value) + "."; | |||
| RETURN_STATUS_UNEXPECTED(err_msg); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| template <typename T> | |||
| inline Status ValidatePositive(const std::string &op_name, const std::string ¶m_name, T param_value) { | |||
| if (param_value <= 0) { | |||
| std::string err_msg = op_name + ": invalid parameter, '" + param_name + | |||
| "' should be positive, but got: " + std::to_string(param_value) + "."; | |||
| RETURN_STATUS_UNEXPECTED(err_msg); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| template <typename T> | |||
| inline Status ValidateNegative(const std::string &op_name, const std::string ¶m_name, T param_value) { | |||
| if (param_value >= 0) { | |||
| std::string err_msg = op_name + ": invalid parameter, '" + param_name + | |||
| "' should be negative, but got: " + std::to_string(param_value) + "."; | |||
| RETURN_STATUS_UNEXPECTED(err_msg); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| template <typename T> | |||
| inline Status ValidateNonPositive(const std::string &op_name, const std::string ¶m_name, T param_value) { | |||
| if (param_value > 0) { | |||
| std::string err_msg = op_name + ": invalid parameter, '" + param_name + | |||
| "' should be non positive, but got: " + std::to_string(param_value) + "."; | |||
| RETURN_STATUS_UNEXPECTED(err_msg); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| template <typename T> | |||
| inline Status ValidateNonNegative(const std::string &op_name, const std::string ¶m_name, T param_value) { | |||
| if (param_value < 0) { | |||
| std::string err_msg = op_name + ": invalid parameter, '" + param_name + | |||
| "' should be non negative, but got: " + std::to_string(param_value) + "."; | |||
| RETURN_STATUS_UNEXPECTED(err_msg); | |||
| } | |||
| return Status::OK(); | |||
| } | |||
| } // namespace dataset | |||
| } // namespace mindspore | |||
| #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_VALIDATORS_H_ | |||
| @@ -319,7 +319,7 @@ class ComputeDeltas(AudioTensorOperation): | |||
| Args: | |||
| win_length (int): The window length used for computing delta, must be no less than 3 (default=5). | |||
| mode (BorderType): Mode parameter passed to padding (default=BorderType.EDGE).It can be any of | |||
| pad_mode (BorderType): Mode parameter passed to padding (default=BorderType.EDGE).It can be any of | |||
| [BorderType.CONSTANT, BorderType.EDGE, BorderType.REFLECT, BordBorderTypeer.SYMMETRIC]. | |||
| - BorderType.CONSTANT, means it fills the border with constant values. | |||
| @@ -15,8 +15,8 @@ | |||
| """ | |||
| Enum for audio ops. | |||
| """ | |||
| from enum import Enum | |||
| import mindspore._c_dataengine as cde | |||
| @@ -160,4 +160,3 @@ class BorderType(str, Enum): | |||
| EDGE: str = "edge" | |||
| REFLECT: str = "reflect" | |||
| SYMMETRIC: str = "symmetric" | |||
| @@ -18,7 +18,7 @@ Validators for TensorOps. | |||
| from functools import wraps | |||
| from mindspore.dataset.core.validator_helpers import check_float32, check_float32_not_zero, check_int32,\ | |||
| from mindspore.dataset.core.validator_helpers import check_float32, check_float32_not_zero, check_int32, \ | |||
| check_int32_not_zero, check_list_same_size, check_non_negative_float32, check_non_negative_int32, \ | |||
| check_pos_float32, check_pos_int32, check_value, INT32_MAX, parse_user_args, type_check | |||
| from .utils import BorderType, FadeShape, GainType, Interpolation, Modulation, ScaleType | |||
| @@ -62,10 +62,10 @@ def check_biquad_central_freq(central_freq): | |||
| check_float32(central_freq, "central_freq") | |||
| def check_biquad_Q(Q): | |||
| def check_biquad_q(q): | |||
| """Wrapper method to check the parameters of Q.""" | |||
| type_check(Q, (float, int), "Q") | |||
| check_value(Q, [0, 1], "Q", True) | |||
| type_check(q, (float, int), "Q") | |||
| check_value(q, [0, 1], "Q", True) | |||
| def check_biquad_noise(noise): | |||
| @@ -89,11 +89,11 @@ def check_band_biquad(method): | |||
| @wraps(method) | |||
| def new_method(self, *args, **kwargs): | |||
| [sample_rate, central_freq, Q, noise], _ = parse_user_args( | |||
| [sample_rate, central_freq, q, noise], _ = parse_user_args( | |||
| method, *args, **kwargs) | |||
| check_biquad_sample_rate(sample_rate) | |||
| check_biquad_central_freq(central_freq) | |||
| check_biquad_Q(Q) | |||
| check_biquad_q(q) | |||
| check_biquad_noise(noise) | |||
| return method(self, *args, **kwargs) | |||
| @@ -111,10 +111,10 @@ def check_highpass_biquad(method): | |||
| @wraps(method) | |||
| def new_method(self, *args, **kwargs): | |||
| [sample_rate, cutoff_freq, Q], _ = parse_user_args(method, *args, **kwargs) | |||
| [sample_rate, cutoff_freq, q], _ = parse_user_args(method, *args, **kwargs) | |||
| check_biquad_sample_rate(sample_rate) | |||
| check_biquad_cutoff_freq(cutoff_freq) | |||
| check_biquad_Q(Q) | |||
| check_biquad_q(q) | |||
| return method(self, *args, **kwargs) | |||
| return new_method | |||
| @@ -125,11 +125,11 @@ def check_allpass_biquad(method): | |||
| @wraps(method) | |||
| def new_method(self, *args, **kwargs): | |||
| [sample_rate, central_freq, Q], _ = parse_user_args( | |||
| [sample_rate, central_freq, q], _ = parse_user_args( | |||
| method, *args, **kwargs) | |||
| check_biquad_sample_rate(sample_rate) | |||
| check_biquad_central_freq(central_freq) | |||
| check_biquad_Q(Q) | |||
| check_biquad_q(q) | |||
| return method(self, *args, **kwargs) | |||
| return new_method | |||
| @@ -140,11 +140,11 @@ def check_bandpass_biquad(method): | |||
| @wraps(method) | |||
| def new_method(self, *args, **kwargs): | |||
| [sample_rate, central_freq, Q, const_skirt_gain], _ = parse_user_args( | |||
| [sample_rate, central_freq, q, const_skirt_gain], _ = parse_user_args( | |||
| method, *args, **kwargs) | |||
| check_biquad_sample_rate(sample_rate) | |||
| check_biquad_central_freq(central_freq) | |||
| check_biquad_Q(Q) | |||
| check_biquad_q(q) | |||
| check_biquad_const_skirt_gain(const_skirt_gain) | |||
| return method(self, *args, **kwargs) | |||
| @@ -156,11 +156,11 @@ def check_bandreject_biquad(method): | |||
| @wraps(method) | |||
| def new_method(self, *args, **kwargs): | |||
| [sample_rate, central_freq, Q], _ = parse_user_args( | |||
| [sample_rate, central_freq, q], _ = parse_user_args( | |||
| method, *args, **kwargs) | |||
| check_biquad_sample_rate(sample_rate) | |||
| check_biquad_central_freq(central_freq) | |||
| check_biquad_Q(Q) | |||
| check_biquad_q(q) | |||
| return method(self, *args, **kwargs) | |||
| return new_method | |||
| @@ -171,12 +171,12 @@ def check_bass_biquad(method): | |||
| @wraps(method) | |||
| def new_method(self, *args, **kwargs): | |||
| [sample_rate, gain, central_freq, Q], _ = parse_user_args( | |||
| [sample_rate, gain, central_freq, q], _ = parse_user_args( | |||
| method, *args, **kwargs) | |||
| check_biquad_sample_rate(sample_rate) | |||
| check_biquad_gain(gain) | |||
| check_biquad_central_freq(central_freq) | |||
| check_biquad_Q(Q) | |||
| check_biquad_q(q) | |||
| return method(self, *args, **kwargs) | |||
| return new_method | |||
| @@ -221,6 +221,7 @@ def check_dc_shift(method): | |||
| if limiter_gain is not None: | |||
| type_check(limiter_gain, (float, int), "limiter_gain") | |||
| return method(self, *args, **kwargs) | |||
| return new_method | |||
| @@ -232,7 +233,7 @@ def check_deemph_biquad(method): | |||
| [sample_rate], _ = parse_user_args(method, *args, **kwargs) | |||
| type_check(sample_rate, (int,), "sample_rate") | |||
| if sample_rate not in (44100, 48000): | |||
| raise ValueError("Input sample_rate should be 44100 or 48000, but got {0}.".format(sample_rate)) | |||
| raise ValueError("Argument sample_rate should be 44100 or 48000, but got {0}.".format(sample_rate)) | |||
| return method(self, *args, **kwargs) | |||
| return new_method | |||
| @@ -243,11 +244,11 @@ def check_equalizer_biquad(method): | |||
| @wraps(method) | |||
| def new_method(self, *args, **kwargs): | |||
| [sample_rate, center_freq, gain, Q], _ = parse_user_args(method, *args, **kwargs) | |||
| [sample_rate, center_freq, gain, q], _ = parse_user_args(method, *args, **kwargs) | |||
| check_biquad_sample_rate(sample_rate) | |||
| check_biquad_central_freq(center_freq) | |||
| check_biquad_gain(gain) | |||
| check_biquad_Q(Q) | |||
| check_biquad_q(q) | |||
| return method(self, *args, **kwargs) | |||
| return new_method | |||
| @@ -279,10 +280,10 @@ def check_lowpass_biquad(method): | |||
| @wraps(method) | |||
| def new_method(self, *args, **kwargs): | |||
| [sample_rate, cutoff_freq, Q], _ = parse_user_args(method, *args, **kwargs) | |||
| [sample_rate, cutoff_freq, q], _ = parse_user_args(method, *args, **kwargs) | |||
| check_biquad_sample_rate(sample_rate) | |||
| check_biquad_cutoff_freq(cutoff_freq) | |||
| check_biquad_Q(Q) | |||
| check_biquad_q(q) | |||
| return method(self, *args, **kwargs) | |||
| return new_method | |||
| @@ -382,12 +383,12 @@ def check_treble_biquad(method): | |||
| @wraps(method) | |||
| def new_method(self, *args, **kwargs): | |||
| [sample_rate, gain, central_freq, Q], _ = parse_user_args( | |||
| [sample_rate, gain, central_freq, q], _ = parse_user_args( | |||
| method, *args, **kwargs) | |||
| check_biquad_sample_rate(sample_rate) | |||
| check_biquad_gain(gain) | |||
| check_biquad_central_freq(central_freq) | |||
| check_biquad_Q(Q) | |||
| check_biquad_q(q) | |||
| return method(self, *args, **kwargs) | |||
| return new_method | |||
| @@ -420,6 +421,7 @@ def check_power(power): | |||
| def check_complex_norm(method): | |||
| """Wrapper method to check the parameters of ComplexNorm.""" | |||
| @wraps(method) | |||
| def new_method(self, *args, **kwargs): | |||
| [power], _ = parse_user_args(method, *args, **kwargs) | |||
| @@ -580,6 +582,7 @@ def check_sliding_window_cmn(method): | |||
| def check_compute_deltas(method): | |||
| """Wrapper method to check the parameter of ComputeDeltas.""" | |||
| @wraps(method) | |||
| def new_method(self, *args, **kwargs): | |||
| [win_length, pad_mode], _ = parse_user_args(method, *args, **kwargs) | |||
| @@ -114,7 +114,8 @@ class DSCallback: | |||
| at_least_one = True | |||
| if not at_least_one: | |||
| raise AttributeError("Provided Callback class did not override any of the 6 callback methods.") | |||
| raise AttributeError( | |||
| "Inheriting Callback class without overriding any methods, check the usage of user defined Callback.") | |||
| return c_cb | |||
| @@ -241,7 +242,8 @@ class WaitedDSCallback(Callback, DSCallback): | |||
| at_least_one = True | |||
| if not at_least_one: | |||
| raise AttributeError("Provided Callback class did not override any of the 2 callback methods.") | |||
| raise AttributeError( | |||
| "Inheriting Callback class without overriding any methods, check the usage of user defined Callback.") | |||
| return c_cb | |||
| @@ -16,7 +16,6 @@ | |||
| """ | |||
| Built-in validators. | |||
| """ | |||
| from functools import wraps | |||
| from ..core.validator_helpers import parse_user_args, check_pos_int32 | |||
| @@ -18,25 +18,13 @@ import os | |||
| import sys | |||
| import importlib | |||
| import numpy as np | |||
| from mindspore import log as logger | |||
| def imshow_det_bbox(image, | |||
| bboxes, | |||
| labels, | |||
| segm=None, | |||
| class_names=None, | |||
| score_threshold=0, | |||
| bbox_color=(0, 255, 0), | |||
| text_color=(203, 192, 255), | |||
| mask_color=(128, 0, 128), | |||
| thickness=2, | |||
| font_size=0.8, | |||
| show=True, | |||
| win_name="win", | |||
| wait_time=2000, | |||
| out_file=None | |||
| ): | |||
| def imshow_det_bbox(image, bboxes, labels, segm=None, class_names=None, score_threshold=0, bbox_color=(0, 255, 0), | |||
| text_color=(203, 192, 255), mask_color=(128, 0, 128), thickness=2, font_size=0.8, show=True, | |||
| win_name="win", wait_time=2000, out_file=None): | |||
| """Draw an image with given bboxes and class labels (with scores). | |||
| Args: | |||
| @@ -69,16 +57,17 @@ def imshow_det_bbox(image, | |||
| try: | |||
| cv2 = importlib.import_module("cv2") | |||
| except ModuleNotFoundError: | |||
| raise ImportError("import cv2 failed, seems you have to run `pip install opencv-python`.") | |||
| raise ImportError("Importing cv2 failed, try to install it by running `pip install opencv-python`.") | |||
| # validation | |||
| assert isinstance(image, np.ndarray) and image.ndim == 3 and (image.shape[0] == 3 or image.shape[2] == 3),\ | |||
| assert isinstance(image, np.ndarray) and image.ndim == 3 and (image.shape[0] == 3 or image.shape[2] == 3), \ | |||
| "image must be a ndarray in (H, W, C) or (C, H, W) format." | |||
| if bboxes is not None: | |||
| assert isinstance(bboxes, np.ndarray) and bboxes.ndim == 2 and (bboxes.shape[1] == 4 or bboxes.shape[1] == 5), \ | |||
| "bboxes must be a ndarray in (N, 4) or (N, 5) format." | |||
| assert isinstance(labels, np.ndarray) and labels.ndim == 2 and labels.shape[1] == 1 and \ | |||
| labels.shape[0] == bboxes.shape[0], "labels must be a ndarray in (N, 1) format and has same N with bboxes." | |||
| labels.shape[0] == bboxes.shape[ | |||
| 0], "labels must be a ndarray in (N, 1) format and has same N with bboxes." | |||
| if segm is not None: | |||
| assert isinstance(segm, np.ndarray) and segm.ndim == 3, "segm must be a ndarray in (M, H, W) format." | |||
| H, W = (image.shape[0], image.shape[1]) if image.shape[2] == 3 else (image.shape[1], image.shape[2]) | |||
| @@ -122,7 +111,7 @@ def imshow_det_bbox(image, | |||
| continue | |||
| # bbox | |||
| x1, y1 = int(draw_bbox[0]), int(draw_bbox[1]) | |||
| x2, y2 = int(draw_bbox[0]+draw_bbox[2]), int(draw_bbox[1]+draw_bbox[3]) | |||
| x2, y2 = int(draw_bbox[0] + draw_bbox[2]), int(draw_bbox[1] + draw_bbox[3]) | |||
| cv2.rectangle(draw_image, (x1, y1), (x2, y2), bbox_color, thickness) | |||
| # label | |||
| try: | |||
| @@ -73,7 +73,7 @@ def test_func_angle_003(): | |||
| angle_op = a_c_trans.Angle() | |||
| dataset = dataset.map(operations=angle_op, input_columns=["col1"]) | |||
| num_itr = 0 | |||
| with pytest.raises(RuntimeError, match="input tensor type should be int, float or double"): | |||
| with pytest.raises(RuntimeError, match="the data type of input tensor does not match the requirement of operator"): | |||
| for _ in dataset.create_dict_iterator(num_epochs=1, output_numpy=True): | |||
| num_itr += 1 | |||
| @@ -393,7 +393,7 @@ def test_callbacks_validations(): | |||
| data = data.map(operations=(lambda x: x), callbacks=my_cb) | |||
| for _ in data: | |||
| pass | |||
| assert "Provided Callback class did not override any of the 6 callback methods." in str(err.value) | |||
| assert "Inheriting Callback class without overriding any methods" in str(err.value) | |||
| def test_callbacks_sink_simulation(): | |||
| @@ -26,9 +26,8 @@ def count_unequal_element(data_expected, data_me, rtol, atol): | |||
| error = np.abs(data_expected - data_me) | |||
| greater = np.greater(error, atol + np.abs(data_expected) * rtol) | |||
| loss_count = np.count_nonzero(greater) | |||
| assert (loss_count / total_count) < rtol, \ | |||
| "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}". \ | |||
| format(data_expected[greater], data_me[greater], error[greater]) | |||
| assert (loss_count / total_count) < rtol, "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}".format( | |||
| data_expected[greater], data_me[greater], error[greater]) | |||
| def test_func_deemph_biquad_eager(): | |||
| @@ -63,6 +62,7 @@ def test_func_deemph_biquad_pipeline(): | |||
| def test_invalid_input_all(): | |||
| waveform = np.random.rand(2, 1000) | |||
| def test_invalid_input(test_name, sample_rate, error, error_msg): | |||
| logger.info("Test DeemphBiquad with bad input: {0}".format(test_name)) | |||
| with pytest.raises(error) as error_info: | |||
| @@ -76,7 +76,7 @@ def test_invalid_input_all(): | |||
| "Argument sample_rate with value 44100 is not of type [<class 'int'>]," | |||
| + " but got <class 'str'>.") | |||
| test_invalid_input("invalid sample_rate parameter value", 45000, ValueError, | |||
| "Input sample_rate should be 44100 or 48000, but got 45000.") | |||
| "Argument sample_rate should be 44100 or 48000, but got 45000.") | |||
| if __name__ == '__main__': | |||
| @@ -117,10 +117,9 @@ def test_frequency_masking_invalid_input(): | |||
| "Argument iid_masks with value True is not of type [<class 'bool'>], but got <class 'str'>.") | |||
| test_invalid_input("invalid mask_start", False, 2, 100, RuntimeError, | |||
| "MaskAlongAxis: mask_start should be less than the length of chosen dimension.") | |||
| "'mask_start' should be less than the length of the masked dimension") | |||
| test_invalid_input("invalid mask_width", False, 200, 2, RuntimeError, | |||
| "FrequencyMasking: frequency_mask_param should be less than or equal to the length of " + | |||
| "frequency dimension.") | |||
| "'frequency_mask_param' should be less than or equal to the length of frequency dimension") | |||
| if __name__ == "__main__": | |||
| @@ -72,21 +72,21 @@ def test_magphase_exception(): | |||
| _ = magphase_window(input_number) | |||
| except RuntimeError as error: | |||
| logger.info("Got an exception in Magphase: {}".format(str(error))) | |||
| assert "Magphase: input tensor is not in shape of <..., 2>." in str(error) | |||
| assert "the shape of input tensor does not match the requirement of operator" in str(error) | |||
| try: | |||
| input_number = np.array([1, 2, 3, 4]).reshape(1, 4).astype("double") | |||
| magphase_window = audio.Magphase(power=2.0) | |||
| _ = magphase_window(input_number) | |||
| except RuntimeError as error: | |||
| logger.info("Got an exception in Magphase: {}".format(str(error))) | |||
| assert "Magphase: input tensor is not in shape of <..., 2>." in str(error) | |||
| assert "the shape of input tensor does not match the requirement of operator" in str(error) | |||
| try: | |||
| input_number = np.array(['test', 'test']).reshape(1, 2) | |||
| magphase_window = audio.Magphase(power=2.0) | |||
| _ = magphase_window(input_number) | |||
| except RuntimeError as error: | |||
| logger.info("Got an exception in Magphase: {}".format(str(error))) | |||
| assert "Magphase: input tensor type should be int, float or double" in str(error) | |||
| assert "the data type of input tensor does not match the requirement of operator" in str(error) | |||
| try: | |||
| input_number = np.array([1, 2, 3, 4]).reshape(2, 2).astype("double") | |||
| magphase_window = audio.Magphase(power=-1.0) | |||
| @@ -117,9 +117,9 @@ def test_time_masking_invalid_input(): | |||
| "Argument iid_masks with value True is not of type [<class 'bool'>], but got <class 'str'>.") | |||
| test_invalid_input("invalid mask_start", False, 2, 100, RuntimeError, | |||
| "MaskAlongAxis: mask_start should be less than the length of chosen dimension.") | |||
| "'mask_start' should be less than the length of the masked dimension") | |||
| test_invalid_input("invalid mask_width", False, 200, 2, RuntimeError, | |||
| "TimeMasking: time_mask_param should be less than or equal to the length of time dimension.") | |||
| "'time_mask_param' should be less than or equal to the length of time dimension") | |||
| if __name__ == "__main__": | |||