!16449 [assistant][ops] Add new audio operator BandBiquad

Merge pull request !16449 from StyleHang/BandBiquadOp
This commit is contained in:
i-robot 2021-07-30 03:35:07 +00:00 committed by Gitee
commit 6e84bf0f0f
23 changed files with 951 additions and 189 deletions

View File

@ -1,26 +1,43 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "minddata/dataset/include/dataset/audio.h"
#include "minddata/dataset/audio/ir/kernels/audio_ir.h"
namespace mindspore {
namespace dataset {
namespace audio {} // namespace audio
} // namespace dataset
} // namespace mindspore
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "minddata/dataset/include/dataset/audio.h"
#include "minddata/dataset/audio/ir/kernels/band_biquad_ir.h"
namespace mindspore {
namespace dataset {
namespace audio {
// BandBiquad Transform Operation.
struct BandBiquad::Data {
Data(int32_t sample_rate, float central_freq, float Q, bool noise)
: sample_rate_(sample_rate), central_freq_(central_freq), Q_(Q), noise_(noise) {}
int32_t sample_rate_;
float central_freq_;
float Q_;
bool noise_;
};
BandBiquad::BandBiquad(int32_t sample_rate, float central_freq, float Q, bool noise)
: data_(std::make_shared<Data>(sample_rate, central_freq, Q, noise)) {}
std::shared_ptr<TensorOperation> BandBiquad::Parse() {
return std::make_shared<BandBiquadOperation>(data_->sample_rate_, data_->central_freq_, data_->Q_, data_->noise_);
}
} // namespace audio
} // namespace dataset
} // namespace mindspore

View File

@ -1,24 +1,38 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "pybind11/pybind11.h"
#include "minddata/dataset/api/python/pybind_register.h"
#include "minddata/dataset/audio/ir/kernels/audio_ir.h"
namespace mindspore {
namespace dataset {} // namespace dataset
} // namespace mindspore
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "pybind11/pybind11.h"
#include "minddata/dataset/api/python/pybind_conversion.h"
#include "minddata/dataset/api/python/pybind_register.h"
#include "minddata/dataset/audio/ir/kernels/band_biquad_ir.h"
#include "minddata/dataset/include/dataset/transforms.h"
namespace mindspore {
namespace dataset {
PYBIND_REGISTER(
BandBiquadOperation, 1, ([](const py::module *m) {
(void)py::class_<audio::BandBiquadOperation, TensorOperation, std::shared_ptr<audio::BandBiquadOperation>>(
*m, "BandBiquadOperation")
.def(py::init([](int32_t sample_rate, float central_freq, float Q, bool noise) {
auto band_biquad = std::make_shared<audio::BandBiquadOperation>(sample_rate, central_freq, Q, noise);
THROW_IF_ERROR(band_biquad->ValidateParams());
return band_biquad;
}));
}));
} // namespace dataset
} // namespace mindspore

View File

@ -2,5 +2,5 @@ file(GLOB_RECURSE _CURRENT_SRC_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cc"
set_property(SOURCE ${_CURRENT_SRC_FILES} PROPERTY COMPILE_DEFINITIONS SUBMODULE_ID=mindspore::SubModuleId::SM_MD)
add_library(audio-ir-kernels OBJECT
audio_ir.cc
band_biquad_ir.cc
)

View File

@ -1,24 +0,0 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "minddata/dataset/audio/ir/kernels/audio_ir.h"
namespace mindspore {
namespace dataset {
namespace audio {} // namespace audio
} // namespace dataset
} // namespace mindspore

View File

@ -1,26 +0,0 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_IR_KERNELS_AUDIO_IR_H_
#define MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_IR_KERNELS_AUDIO_IR_H_
namespace mindspore {
namespace dataset {
namespace audio {} // namespace audio
} // namespace dataset
} // namespace mindspore
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_IR_KERNELS_AUDIO_IR_H_

View File

@ -0,0 +1,54 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "minddata/dataset/audio/ir/kernels/band_biquad_ir.h"
#include "minddata/dataset/audio/kernels/band_biquad_op.h"
#include "minddata/dataset/audio/ir/validators.h"
namespace mindspore {
namespace dataset {
namespace audio {
// BandBiquadOperation
BandBiquadOperation::BandBiquadOperation(int32_t sample_rate, float central_freq, float Q, bool noise)
: sample_rate_(sample_rate), central_freq_(central_freq), Q_(Q), noise_(noise) {}
Status BandBiquadOperation::ValidateParams() {
RETURN_IF_NOT_OK(ValidateScalar("BandBiquad", "Q", Q_, {0, 1.0}, true, false));
RETURN_IF_NOT_OK(CheckScalarNotZero("BandBIquad", "sample_rate", sample_rate_));
return Status::OK();
}
std::shared_ptr<TensorOp> BandBiquadOperation::Build() {
std::shared_ptr<BandBiquadOp> tensor_op = std::make_shared<BandBiquadOp>(sample_rate_, central_freq_, Q_, noise_);
return tensor_op;
}
Status BandBiquadOperation::to_json(nlohmann::json *out_json) {
nlohmann::json args;
args["sample_rate"] = sample_rate_;
args["central_freq"] = central_freq_;
args["Q"] = Q_;
args["noise"] = noise_;
*out_json = args;
return Status::OK();
}
} // namespace audio
} // namespace dataset
} // namespace mindspore

View File

@ -0,0 +1,60 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_IR_KERNELS_BAND_BIQUAD_IR_H_
#define MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_IR_KERNELS_BAND_BIQUAD_IR_H_
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "include/api/status.h"
#include "minddata/dataset/include/dataset/constants.h"
#include "minddata/dataset/include/dataset/transforms.h"
#include "minddata/dataset/kernels/ir/tensor_operation.h"
namespace mindspore {
namespace dataset {
namespace audio {
// Char arrays storing name of corresponding classes (in alphabetical order)
constexpr char kBandBiquadOperation[] = "BandBiquad";
class BandBiquadOperation : public TensorOperation {
public:
explicit BandBiquadOperation(int32_t sample_rate, float central_freq, float Q, bool noise);
~BandBiquadOperation() = default;
std::shared_ptr<TensorOp> Build() override;
Status ValidateParams() override;
std::string Name() const override { return kBandBiquadOperation; }
Status to_json(nlohmann::json *out_json) override;
private:
int32_t sample_rate_;
float central_freq_;
float Q_;
bool noise_;
};
} // namespace audio
} // namespace dataset
} // namespace mindspore
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_IR_KERNELS_BAND_BIQUAD_IR_H_

View File

@ -0,0 +1,39 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_IR_VALIDATORS_H_
#define MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_IR_VALIDATORS_H_
#include <string>
#include "minddata/dataset/kernels/ir/validators.h"
namespace mindspore {
namespace dataset {
template <typename T>
// Helper function to check scalar is not equal to zero
Status CheckScalarNotZero(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 0" + ", got: " + std::to_string(scalar);
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_

View File

@ -2,5 +2,6 @@ file(GLOB_RECURSE _CURRENT_SRC_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cc"
set_property(SOURCE ${_CURRENT_SRC_FILES} PROPERTY COMPILE_DEFINITIONS SUBMODULE_ID=mindspore::SubModuleId::SM_MD)
add_library(audio-kernels OBJECT
spectrogram_op.cc
band_biquad_op.cc
)

View File

@ -0,0 +1,143 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_KERNELS_AUDIO_UTILS_H_
#define MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_KERNELS_AUDIO_UTILS_H_
#include <cmath>
#include <memory>
#include <vector>
#include "minddata/dataset/core/tensor.h"
#include "minddata/dataset/kernels/tensor_op.h"
#include "minddata/dataset/util/status.h"
constexpr double PI = 3.141592653589793;
namespace mindspore {
namespace dataset {
/// \brief Perform a biquad filter of input tensor.
/// \param input/output: Tensor of shape <...,time>
/// \param a0: denominator coefficient of current output y[n], typically 1
/// \param a1: denominator coefficient of current output y[n-1]
/// \param a2: denominator coefficient of current output y[n-2]
/// \param b0: numerator coefficient of current input, x[n]
/// \param b1: numerator coefficient of input one time step ago x[n-1]
/// \param b2: numerator coefficient of input two time steps ago x[n-2]
/// \return Status code
template <typename T>
Status Biquad(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, T b0, T b1, T b2, T a0, T a1,
T a2) {
std::vector<T> a_coeffs;
std::vector<T> b_coeffs;
a_coeffs.push_back(a0);
a_coeffs.push_back(a1);
a_coeffs.push_back(a2);
b_coeffs.push_back(b0);
b_coeffs.push_back(b1);
b_coeffs.push_back(b2);
return LFilter(input, output, a_coeffs, b_coeffs, true);
}
/// \brief Perform an IIR filter by evaluating difference equation.
/// \param input/output: Tensor of shape <...,time>
/// \param a_coeffs: denominator coefficients of difference equation of dimension of (n_order + 1).
/// \param b_coeffs: numerator coefficients of difference equation of dimension of (n_order + 1).
/// \param clamp: If True, clamp the output signal to be in the range [-1, 1] (Default: True)
/// \return Status code
template <typename T>
Status LFilter(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, std::vector<T> a_coeffs,
std::vector<T> b_coeffs, bool clamp) {
// pack batch
TensorShape input_shape = input->shape();
TensorShape toShape({input->Size() / input_shape[-1], input_shape[-1]});
input->Reshape(toShape);
auto shape_0 = input->shape()[0];
auto shape_1 = input->shape()[1];
std::vector<T> signal;
std::shared_ptr<Tensor> out;
std::vector<T> out_vect(shape_0 * shape_1);
size_t x_idx = 0;
size_t channel_idx = 1;
size_t m_num_order = b_coeffs.size() - 1;
size_t m_den_order = a_coeffs.size() - 1;
// init A_coeffs and B_coeffs by div(a0)
for (size_t i = 1; i < a_coeffs.size(); i++) {
a_coeffs[i] /= a_coeffs[0];
}
for (size_t i = 0; i < b_coeffs.size(); i++) {
b_coeffs[i] /= a_coeffs[0];
}
// Sliding window
T *m_px = new T[m_num_order + 1];
T *m_py = new T[m_den_order + 1];
// Tensor -> vector
for (auto itr = input->begin<T>(); itr != input->end<T>();) {
while (x_idx < shape_1 * channel_idx) {
signal.push_back(*itr);
itr++;
x_idx++;
}
// Sliding window
for (size_t j = 0; j < m_den_order; j++) {
m_px[j] = static_cast<T>(0);
}
for (size_t j = 0; j <= m_den_order; j++) {
m_py[j] = static_cast<T>(0);
}
// Each channel is processed with the sliding window
for (size_t i = x_idx - shape_1; i < x_idx; i++) {
m_px[m_num_order] = signal[i];
for (size_t j = 0; j < m_num_order + 1; j++) {
m_py[m_num_order] += b_coeffs[j] * m_px[m_num_order - j];
}
for (size_t j = 1; j < m_den_order + 1; j++) {
m_py[m_num_order] -= a_coeffs[j] * m_py[m_num_order - j];
}
if (clamp) {
if (m_py[m_num_order] > static_cast<T>(1.))
out_vect[i] = static_cast<T>(1.);
else if (m_py[m_num_order] < static_cast<T>(-1.))
out_vect[i] = static_cast<T>(-1.);
else
out_vect[i] = m_py[m_num_order];
} else {
out_vect[i] = m_py[m_num_order];
}
if (i + 1 == x_idx) continue;
for (size_t j = 0; j < m_num_order; j++) {
m_px[j] = m_px[j + 1];
}
for (size_t j = 0; j < m_num_order; j++) {
m_py[j] = m_py[j + 1];
}
m_py[m_num_order] = static_cast<T>(0);
}
if (x_idx % shape_1 == 0) {
++channel_idx;
}
}
// unpack batch
Tensor::CreateFromVector(out_vect, input_shape, &out);
*output = out;
delete m_px;
delete m_py;
return Status::OK();
}
} // namespace dataset
} // namespace mindspore
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_KERNELS_AUDIO_UTILS_H_

View File

@ -0,0 +1,60 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "minddata/dataset/audio/kernels/band_biquad_op.h"
#include "minddata/dataset/audio/kernels/audio_utils.h"
#include "minddata/dataset/util/status.h"
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 dimension should be greater than 0.");
// 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 type should be float, but got " + input->type().ToString());
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.");
double b0 = sqrt(1 - a1 * a1 / (4 * a2)) * (1 - a2);
if (noise_) {
CHECK_FAIL_RETURN_UNEXPECTED(b0 != 0, "BandBiquad: ZeroDivisionError.");
double mutl = sqrt(((1 + a2) * (1 + a2) - a1 * a1) * (1 - a2) / (1 + a2)) / b0;
b0 *= mutl;
}
double b1 = 0.;
double b2 = 0.;
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));
else if (input->type() == DataType(DataType::DE_FLOAT64))
return Biquad(input, output, static_cast<double>(b0), static_cast<double>(b1), static_cast<double>(b2),
static_cast<double>(a0), static_cast<double>(a1), static_cast<double>(a2));
else
return Biquad(input, output, static_cast<float16>(b0), static_cast<float16>(b1), static_cast<float16>(b2),
static_cast<float16>(a0), static_cast<float16>(a1), static_cast<float16>(a2));
}
} // namespace dataset
} // namespace mindspore

View File

@ -0,0 +1,53 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_KERNELS_BAND_BIQUAD_OP_H_
#define MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_KERNELS_BAND_BIQUAD_OP_H_
#include <memory>
#include <vector>
#include <string>
#include "minddata/dataset/core/tensor.h"
#include "minddata/dataset/kernels/tensor_op.h"
#include "minddata/dataset/util/status.h"
namespace mindspore {
namespace dataset {
class BandBiquadOp : public TensorOp {
public:
BandBiquadOp(int32_t sample_rate, float central_freq, float Q, bool noise)
: sample_rate_(sample_rate), central_freq_(central_freq), Q_(Q), noise_(noise) {}
~BandBiquadOp() override = default;
void Print(std::ostream &out) const override {
out << Name() << ": sample_rate: " << sample_rate_ << ", central_freq: " << central_freq_ << ", Q: " << Q_
<< ", noise: " << noise_ << std::endl;
}
Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
std::string Name() const override { return kBandBiquadOp; }
private:
int32_t sample_rate_;
float central_freq_;
float Q_;
bool noise_;
};
} // namespace dataset
} // namespace mindspore
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_KERNELS_BAND_BIQUAD_OP_H_

View File

@ -1,21 +0,0 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "minddata/dataset/audio/kernels/spectrogram_op.h"
namespace mindspore {
namespace dataset {} // namespace dataset
} // namespace mindspore

View File

@ -1,23 +0,0 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_KERNELS_SPECTROGRAM_OP_H_
#define MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_KERNELS_SPECTROGRAM_OP_H_
namespace mindspore {
namespace dataset {} // namespace dataset
} // namespace mindspore
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_KERNELS_SPECTROGRAM_OP_H_

View File

@ -1,27 +1,63 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_DATASET_AUDIO_H_
#define MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_DATASET_AUDIO_H_
namespace mindspore {
namespace dataset {
namespace audio {} // namespace audio
} // namespace dataset
} // namespace mindspore
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_DATASET_AUDIO_H_
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_DATASET_AUDIO_H_
#define MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_DATASET_AUDIO_H_
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "include/api/dual_abi_helper.h"
#include "include/api/status.h"
#include "minddata/dataset/include/dataset/constants.h"
#include "minddata/dataset/include/dataset/transforms.h"
namespace mindspore {
namespace dataset {
class TensorOperation;
// Transform operations for performing computer audio.
namespace audio {
/// \brief Design two-pole band filter.
class BandBiquad final : public TensorTransform {
public:
/// \brief Constructor.
/// \param[in] sample_rate Sampling rate of the waveform, e.g. 44100 (Hz).
/// \param[in] central_freq Central frequency (in Hz).
/// \param[in] Q Quality factor, https://en.wikipedia.org/wiki/Q_factor (Default: 0.707).
/// \param[in] noise Choose alternate mode for un-pitched audio or mode oriented to pitched audio(Default: False).
explicit BandBiquad(int32_t sample_rate, float central_freq, float Q = 0.707, bool noise = false);
/// \brief Destructor.
~BandBiquad() = default;
protected:
/// \brief Function to convert TensorTransform object into a TensorOperation object.
/// \return Shared pointer to TensorOperation object.
std::shared_ptr<TensorOperation> Parse() override;
private:
struct Data;
std::shared_ptr<Data> data_;
};
} // namespace audio
} // namespace dataset
} // namespace mindspore
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_DATASET_AUDIO_H_

View File

@ -136,6 +136,9 @@ constexpr char kComposeOp[] = "Compose";
constexpr char kRandomSelectSubpolicyOp[] = "RandomSelectSubpolicyOp";
constexpr char kSentencepieceTokenizerOp[] = "SentencepieceTokenizerOp";
// audio
constexpr char kBandBiquadOp[] = "BandBiquadOp";
// data
constexpr char kConcatenateOp[] = "ConcatenateOp";
constexpr char kDuplicateOp[] = "DuplicateOp";

View File

@ -1,16 +1,71 @@
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
The module audio.transforms is inherited from _c_dataengine.
"""
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
The module audio.transforms is inherited from _c_dataengine.
and is implemented based on C++. It's a high performance module to
process audio. Users can apply suitable augmentations on audio data
to improve their training models.
"""
import mindspore._c_dataengine as cde
import numpy as np
from ..transforms.c_transforms import TensorOperation
from .validators import check_band_biquad
class AudioTensorOperation(TensorOperation):
"""
Base class of Audio Tensor Ops
"""
def __call__(self, *input_tensor_list):
for tensor in input_tensor_list:
if not isinstance(tensor, (np.ndarray,)):
raise TypeError(
"Input should be NumPy audio, got {}.".format(type(tensor)))
return super().__call__(*input_tensor_list)
def parse(self):
raise NotImplementedError(
"AudioTensorOperation has to implement parse() method.")
class BandBiquad(AudioTensorOperation):
"""
Design two-pole band filter for audio waveform of dimension of `(..., time)`
Args:
sample_rate (int): sampling rate of the waveform, e.g. 44100 (Hz), the value can't be zero.
central_freq (float): central frequency (in Hz),
Q(float, optional): Quality factor, https://en.wikipedia.org/wiki/Q_factor, Range: (0, 1] (Default=0.707).
noise (bool, optional) : If ``True``, uses the alternate mode for un-pitched audio (e.g. percussion).
If ``False``, uses mode oriented to pitched audio, i.e. voice, singing,
or instrumental music (Default: ``False``).
Examples:
>>> import mindspore.dataset.audio.transforms as audio
>>> import numpy as np
>>> waveform = np.array([[2.716064453125e-03, 6.34765625e-03],[9.246826171875e-03, 1.0894775390625e-02]])
>>> band_biquad_op = audio.BandBiquad(44100, 200.0)
>>> waveform_filtered = band_biquad_op(waveform)
"""
@check_band_biquad
def __init__(self, sample_rate, central_freq, Q=0.707, noise=False):
self.sample_rate = sample_rate
self.central_freq = central_freq
self.Q = Q
self.noise = noise
def parse(self):
return cde.BandBiquadOperation(self.sample_rate, self.central_freq, self.Q, self.noise)

View File

@ -0,0 +1,60 @@
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""
Validators for TensorOps.
"""
from functools import wraps
from mindspore.dataset.core.validator_helpers import check_not_zero, check_int32, check_float32, check_value_normalize_std, parse_user_args, type_check
def check_biquad_sample_rate(sample_rate):
"""Wrapper method to check the parameters of sample_rate."""
type_check(sample_rate, (int,), "sample_rate")
check_int32(sample_rate, "sample_rate")
check_not_zero(sample_rate, "sample_rate")
def check_biquad_central_freq(central_freq):
"""Wrapper method to check the parameters of central_freq."""
type_check(central_freq, (float, int), "central_freq")
check_float32(central_freq, "central_freq")
def check_biquad_Q(Q):
"""Wrapper method to check the parameters of Q."""
type_check(Q, (float, int), "Q")
check_value_normalize_std(Q, [0, 1], "Q")
def check_biquad_noise(noise):
"""Wrapper method to check the parameters of noise."""
type_check(noise, (bool,), "noise")
def check_band_biquad(method):
"""Wrapper method to check the parameters of BandBiquad."""
@wraps(method)
def new_method(self, *args, **kwargs):
[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_noise(noise)
return method(self, *args, **kwargs)
return new_method

View File

@ -185,6 +185,12 @@ def check_positive(value, arg_name=""):
raise ValueError("Input {0}must be greater than 0.".format(arg_name))
def check_not_zero(value, arg_name=""):
arg_name = pad_arg_name(arg_name)
if value == 0:
raise ValueError("Input {0}can not be 0.".format(arg_name))
def check_odd(value, arg_name=""):
arg_name = pad_arg_name(arg_name)
if value % 2 != 1:
@ -240,6 +246,11 @@ def check_pos_uint32(value, arg_name=""):
check_value(value, [POS_INT_MIN, UINT32_MAX])
def check_int32(value, arg_name=""):
type_check(value, (int,), arg_name)
check_value(value, [INT32_MIN, INT32_MAX], arg_name)
def check_pos_int32(value, arg_name=""):
"""
Validates the value of a variable is within the range of int32.

View File

@ -14,6 +14,7 @@ SET(DE_UT_SRCS
build_vocab_test.cc
c_api_cache_test.cc
c_api_dataset_album_test.cc
c_api_audio_a_to_q_test.cc
c_api_dataset_cifar_test.cc
c_api_dataset_clue_test.cc
c_api_dataset_coco_test.cc

View File

@ -0,0 +1,99 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common/common.h"
#include "include/api/types.h"
#include "utils/log_adapter.h"
#include "minddata/dataset/include/dataset/audio.h"
#include "minddata/dataset/include/dataset/datasets.h"
using namespace mindspore::dataset;
using mindspore::LogStream;
using mindspore::ExceptionType::NoExceptionType;
using mindspore::MsLogLevel::INFO;
using namespace std;
class MindDataTestPipeline : public UT::DatasetOpTesting {
protected:
};
TEST_F(MindDataTestPipeline, Level0_TestBandBiquad001) {
MS_LOG(INFO) << "Basic Function Test";
// Original waveform
std::shared_ptr<SchemaObj> schema = Schema();
ASSERT_OK(schema->add_column("inputData", mindspore::DataType::kNumberTypeFloat32, {2, 200}));
std::shared_ptr<Dataset> ds = RandomData(50, schema);
EXPECT_NE(ds, nullptr);
ds = ds->SetNumWorkers(4);
EXPECT_NE(ds, nullptr);
auto BandBiquadOp = audio::BandBiquad(44100, 200.0);
ds = ds->Map({BandBiquadOp});
EXPECT_NE(ds, nullptr);
// Filtered waveform by bandbiquad
std::shared_ptr<Iterator> iter = ds->CreateIterator();
EXPECT_NE(ds, nullptr);
std::unordered_map<std::string, mindspore::MSTensor> row;
ASSERT_OK(iter->GetNextRow(&row));
std::vector<int64_t> expected = {2, 200};
int i = 0;
while (row.size() != 0) {
auto col = row["inputData"];
ASSERT_EQ(col.Shape(), expected);
ASSERT_EQ(col.Shape().size(), 2);
ASSERT_EQ(col.DataType(), mindspore::DataType::kNumberTypeFloat32);
ASSERT_OK(iter->GetNextRow(&row));
i++;
}
EXPECT_EQ(i, 50);
iter->Stop();
}
TEST_F(MindDataTestPipeline, Level0_TestBandBiquad002) {
MS_LOG(INFO) << "Wrong Arg.";
std::shared_ptr<SchemaObj> schema = Schema();
// Original waveform
ASSERT_OK(schema->add_column("inputData", mindspore::DataType::kNumberTypeFloat32, {2, 2}));
std::shared_ptr<Dataset> ds = RandomData(50, schema);
std::shared_ptr<Dataset> ds01;
std::shared_ptr<Dataset> ds02;
EXPECT_NE(ds, nullptr);
// Check sample_rate
MS_LOG(INFO) << "sample_rate is zero.";
auto band_biquad_op_01 = audio::BandBiquad(0, 200);
ds01 = ds->Map({band_biquad_op_01});
EXPECT_NE(ds01, nullptr);
std::shared_ptr<Iterator> iter01 = ds01->CreateIterator();
EXPECT_EQ(iter01, nullptr);
// Check Q_
MS_LOG(INFO) << "Q_ is zero.";
auto band_biquad_op_02 = audio::BandBiquad(44100, 200, 0);
ds02 = ds->Map({band_biquad_op_02});
EXPECT_NE(ds02, nullptr);
std::shared_ptr<Iterator> iter02 = ds02->CreateIterator();
EXPECT_EQ(iter02, nullptr);
}

View File

@ -16,6 +16,7 @@
#include "common/common.h"
#include "include/api/types.h"
#include "minddata/dataset/core/de_tensor.h"
#include "minddata/dataset/include/dataset/audio.h"
#include "minddata/dataset/include/dataset/execute.h"
#include "minddata/dataset/include/dataset/transforms.h"
#include "minddata/dataset/include/dataset/vision.h"
@ -295,3 +296,41 @@ TEST_F(MindDataTestExecute, TestResizeWithBBox) {
Status rc = transform(image, &image);
EXPECT_FALSE(rc.IsOk());
}
TEST_F(MindDataTestExecute, TestBandBiquadWithEager) {
MS_LOG(INFO) << "Basic Function Test With Eager.";
// Original waveform
std::vector<float> labels = {
2.716064453125000000e-03, 6.347656250000000000e-03, 9.246826171875000000e-03, 1.089477539062500000e-02,
1.138305664062500000e-02, 1.156616210937500000e-02, 1.394653320312500000e-02, 1.550292968750000000e-02,
1.614379882812500000e-02, 1.840209960937500000e-02, 1.718139648437500000e-02, 1.599121093750000000e-02,
1.647949218750000000e-02, 1.510620117187500000e-02, 1.385498046875000000e-02, 1.345825195312500000e-02,
1.419067382812500000e-02, 1.284790039062500000e-02, 1.052856445312500000e-02, 9.368896484375000000e-03};
std::shared_ptr<Tensor> input;
ASSERT_OK(Tensor::CreateFromVector(labels, TensorShape({2, 10}), &input));
auto input_02 = mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(input));
std::shared_ptr<TensorTransform> band_biquad_01 = std::make_shared<audio::BandBiquad>(44100, 200);
mindspore::dataset::Execute Transform01({band_biquad_01});
// Filtered waveform by bandbiquad
Status s01 = Transform01(input_02, &input_02);
EXPECT_TRUE(s01.IsOk());
}
TEST_F(MindDataTestExecute, TestBandBiquadWithWrongArg) {
MS_LOG(INFO) << "Wrong Arg.";
std::vector<double> labels = {
2.716064453125000000e-03, 6.347656250000000000e-03, 9.246826171875000000e-03, 1.089477539062500000e-02,
1.138305664062500000e-02, 1.156616210937500000e-02, 1.394653320312500000e-02, 1.550292968750000000e-02,
1.614379882812500000e-02, 1.840209960937500000e-02, 1.718139648437500000e-02, 1.599121093750000000e-02,
1.647949218750000000e-02, 1.510620117187500000e-02, 1.385498046875000000e-02, 1.345825195312500000e-02,
1.419067382812500000e-02, 1.284790039062500000e-02, 1.052856445312500000e-02, 9.368896484375000000e-03};
std::shared_ptr<Tensor> input;
ASSERT_OK(Tensor::CreateFromVector(labels, TensorShape({2, 10}), &input));
auto input_02 = mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(input));
// Check Q
MS_LOG(INFO) << "Q is zero.";
std::shared_ptr<TensorTransform> band_biquad_op = std::make_shared<audio::BandBiquad>(44100, 200, 0);
mindspore::dataset::Execute Transform01({band_biquad_op});
Status s01 = Transform01(input_02, &input_02);
EXPECT_FALSE(s01.IsOk());
}

View File

@ -0,0 +1,111 @@
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import numpy as np
import pytest
import mindspore.dataset as ds
import mindspore.dataset.audio.transforms as audio
from mindspore import log as logger
def _count_unequal_element(data_expected, data_me, rtol, atol):
assert data_expected.shape == data_me.shape
total_count = len(data_expected.flatten())
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])
def test_func_band_biquad_eager():
""" mindspore eager mode normal testcase:band_biquad op"""
# Original waveform
waveform = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
# Expect waveform
expect_waveform = np.array([[0.00137832, 0.00545664, 0.01350014],
[0.00551329, 0.01769161, 0.03763063]], dtype=np.float64)
band_biquad_op = audio.BandBiquad(44100, 200.0, 0.707, False)
# Filtered waveform by bandbiquad
output = band_biquad_op(waveform)
_count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
def test_func_band_biquad_pipeline():
""" mindspore pipeline mode normal testcase:band_biquad op"""
# Original waveform
waveform = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
# Expect waveform
expect_waveform = np.array([[0.00137832, 0.00545664, 0.01350014],
[0.00551329, 0.01769161, 0.03763063]], dtype=np.float64)
label = np.random.sample((2, 1))
data = (waveform, label)
dataset = ds.NumpySlicesDataset(data, ["channel", "sample"], shuffle=False)
band_biquad_op = audio.BandBiquad(44100, 200.0)
# Filtered waveform by bandbiquad
dataset = dataset.map(
input_columns=["channel"], operations=band_biquad_op, num_parallel_workers=8)
i = 0
for _ in dataset.create_dict_iterator(output_numpy=True):
_count_unequal_element(expect_waveform[i, :],
_['channel'], 0.0001, 0.0001)
i += 1
def test_band_biquad_invalid_input():
def test_invalid_input(test_name, sample_rate, central_freq, Q, noise, error, error_msg):
logger.info("Test BandBiquad with bad input: {0}".format(test_name))
with pytest.raises(error) as error_info:
audio.BandBiquad(sample_rate, central_freq, Q, noise)
assert error_msg in str(error_info.value)
test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, 200, 0.707, True, TypeError,
"Argument sample_rate with value 44100.5 is not of type [<class 'int'>],"
" but got <class 'float'>.")
test_invalid_input("invalid sample_rate parameter type as a String", "44100", 200, 0.707, True, TypeError,
"Argument sample_rate with value 44100 is not of type [<class 'int'>], but got <class 'str'>.")
test_invalid_input("invalid contral_freq parameter type as a String", 44100, "200", 0.707, True, TypeError,
"Argument central_freq with value 200 is not of type [<class 'float'>, <class 'int'>],"
" but got <class 'str'>.")
test_invalid_input("invalid sample_rate parameter value", 0, 200, 0.707, True, ValueError,
"Input sample_rate can not be 0.")
test_invalid_input("invalid contral_freq parameter value", 44100, 32434324324234321, 0.707, True, ValueError,
"Input central_freq is not within the required interval of [-16777216, 16777216].")
test_invalid_input("invalid Q parameter type as a String", 44100, 200, "0.707", True, TypeError,
"Argument Q with value 0.707 is not of type [<class 'float'>, <class 'int'>],"
" but got <class 'str'>.")
test_invalid_input("invalid Q parameter value", 44100, 200, 1.707, True, ValueError,
"Input Q is not within the required interval of (0, 1].")
test_invalid_input("invalid Q parameter value", 44100, 200, 0, True, ValueError,
"Input Q is not within the required interval of (0, 1].")
test_invalid_input("invalid sample_rate parameter value", 441324343243242342345300, 200, 0.707, True, ValueError,
"Input sample_rate is not within the required interval of [-2147483648, 2147483647].")
test_invalid_input("invalid sample_rate parameter value", None, 200, 0.707, True, TypeError,
"Argument sample_rate with value None is not of type [<class 'int'>],"
" but got <class 'NoneType'>.")
test_invalid_input("invalid central_rate parameter value", 44100, None, 0.707, True, TypeError,
"Argument central_freq with value None is not of type [<class 'float'>, <class 'int'>],"
" but got <class 'NoneType'>.")
test_invalid_input("invalid noise parameter type as a String", 44100, 200, 0.707, "False", TypeError,
"Argument noise with value False is not of type [<class 'bool'>], but got <class 'str'>.")
if __name__ == "__main__":
test_func_band_biquad_eager()
test_func_band_biquad_pipeline()
test_band_biquad_invalid_input()