!16447 [assistant][AngleOp]

Merge pull request !16447 from StyleHang/AngleOp
This commit is contained in:
i-robot 2021-08-05 08:29:12 +00:00 committed by Gitee
commit 447f3e5746
15 changed files with 437 additions and 1 deletions

View File

@ -17,6 +17,7 @@
#include "minddata/dataset/include/dataset/audio.h"
#include "minddata/dataset/audio/ir/kernels/allpass_biquad_ir.h"
#include "minddata/dataset/audio/ir/kernels/angle_ir.h"
#include "minddata/dataset/audio/ir/kernels/band_biquad_ir.h"
#include "minddata/dataset/audio/ir/kernels/bandpass_biquad_ir.h"
#include "minddata/dataset/audio/ir/kernels/bandreject_biquad_ir.h"
@ -42,6 +43,11 @@ std::shared_ptr<TensorOperation> AllpassBiquad::Parse() {
return std::make_shared<AllpassBiquadOperation>(data_->sample_rate_, data_->central_freq_, data_->Q_);
}
// Angle Transform Operation.
Angle::Angle() {}
std::shared_ptr<TensorOperation> Angle::Parse() { return std::make_shared<AngleOperation>(); }
// BandBiquad Transform Operation.
struct BandBiquad::Data {
Data(int32_t sample_rate, float central_freq, float Q, bool noise)

View File

@ -18,6 +18,7 @@
#include "minddata/dataset/api/python/pybind_conversion.h"
#include "minddata/dataset/api/python/pybind_register.h"
#include "minddata/dataset/audio/ir/kernels/allpass_biquad_ir.h"
#include "minddata/dataset/audio/ir/kernels/angle_ir.h"
#include "minddata/dataset/audio/ir/kernels/band_biquad_ir.h"
#include "minddata/dataset/audio/ir/kernels/bandpass_biquad_ir.h"
#include "minddata/dataset/audio/ir/kernels/bandreject_biquad_ir.h"
@ -37,6 +38,17 @@ PYBIND_REGISTER(
return allpass_biquad;
}));
}));
PYBIND_REGISTER(AngleOperation, 1, ([](const py::module *m) {
(void)py::class_<audio::AngleOperation, TensorOperation, std::shared_ptr<audio::AngleOperation>>(
*m, "AngleOperation")
.def(py::init([]() {
auto angle = std::make_shared<audio::AngleOperation>();
THROW_IF_ERROR(angle->ValidateParams());
return angle;
}));
}));
PYBIND_REGISTER(
BandBiquadOperation, 1, ([](const py::module *m) {
(void)py::class_<audio::BandBiquadOperation, TensorOperation, std::shared_ptr<audio::BandBiquadOperation>>(
@ -47,6 +59,7 @@ PYBIND_REGISTER(
return band_biquad;
}));
}));
PYBIND_REGISTER(
BandpassBiquadOperation, 1, ([](const py::module *m) {
(void)py::class_<audio::BandpassBiquadOperation, TensorOperation, std::shared_ptr<audio::BandpassBiquadOperation>>(

View File

@ -3,6 +3,7 @@ set_property(SOURCE ${_CURRENT_SRC_FILES} PROPERTY COMPILE_DEFINITIONS SUBMODULE
add_library(audio-ir-kernels OBJECT
allpass_biquad_ir.cc
angle_ir.cc
band_biquad_ir.cc
bandpass_biquad_ir.cc
bandreject_biquad_ir.cc

View File

@ -0,0 +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 "minddata/dataset/audio/ir/kernels/angle_ir.h"
// Kernel Audio headers
#include "minddata/dataset/audio/kernels/angle_op.h"
namespace mindspore {
namespace dataset {
namespace audio {
// AngleOperation
AngleOperation::AngleOperation() {}
Status AngleOperation::ValidateParams() { return Status::OK(); }
std::shared_ptr<TensorOp> AngleOperation::Build() {
std::shared_ptr<AngleOp> tensor_op = std::make_shared<AngleOp>();
return tensor_op;
}
} // namespace audio
} // namespace dataset
} // namespace mindspore

View File

@ -0,0 +1,52 @@
/**
* 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_ANGLE_IR_H_
#define MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_IR_KERNELS_ANGLE_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
constexpr char kAngleOperation[] = "Angle";
class AngleOperation : public TensorOperation {
public:
AngleOperation();
~AngleOperation() = default;
std::shared_ptr<TensorOp> Build() override;
Status ValidateParams() override;
std::string Name() const override { return kAngleOperation; }
};
} // namespace audio
} // namespace dataset
} // namespace mindspore
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_IR_KERNELS_ANGLE_IR_H_

View File

@ -3,6 +3,7 @@ set_property(SOURCE ${_CURRENT_SRC_FILES} PROPERTY COMPILE_DEFINITIONS SUBMODULE
add_library(audio-kernels OBJECT
allpass_biquad_op.cc
angle_op.cc
band_biquad_op.cc
bandpass_biquad_op.cc
bandreject_biquad_op.cc

View File

@ -0,0 +1,62 @@
/**
* 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 <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"
namespace mindspore {
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: The input is not several legal complex numbers");
CHECK_FAIL_RETURN_UNEXPECTED(input->type().IsNumeric(), "Angle: The input type should be numbers");
if (input->type() == DataType(DataType::DE_FLOAT64)) {
return Angle<double>(input, output);
} else {
std::shared_ptr<Tensor> tmp;
TypeCast(input, &tmp, DataType(DataType::DE_FLOAT32));
return Angle<float>(tmp, output);
}
}
Status AngleOp::OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) {
RETURN_IF_NOT_OK(TensorOp::OutputShape(inputs, outputs));
outputs.clear();
std::vector shape = inputs[0].AsVector();
shape.pop_back();
TensorShape out = TensorShape{shape};
outputs.emplace_back(out);
if (!outputs.empty()) return Status::OK();
return Status(StatusCode::kMDUnexpectedError, "Angle: invalid input wrong shape.");
}
Status AngleOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) {
RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs));
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

View File

@ -0,0 +1,47 @@
/**
* 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_ANGLE_OP_H_
#define MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_KERNELS_ANGLE_OP_H_
#include <memory>
#include <string>
#include <vector>
#include "minddata/dataset/core/tensor.h"
#include "minddata/dataset/kernels/tensor_op.h"
#include "minddata/dataset/util/status.h"
namespace mindspore {
namespace dataset {
class AngleOp : public TensorOp {
public:
// Convert complex numbers to angles
AngleOp() = default;
~AngleOp() override = default;
Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
std::string Name() const override { return kAngleOp; }
Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override;
Status OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) override;
};
} // namespace dataset
} // namespace mindspore
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_AUDIO_KERNELS_ANGLE_OP_H_

View File

@ -28,6 +28,31 @@
constexpr double PI = 3.141592653589793;
namespace mindspore {
namespace dataset {
/// \brief Calculate the angles of the complex numbers
/// \param input/output: Tensor of shape <...,time>
template <typename T>
Status Angle(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
TensorShape shape = input->shape();
std::vector output_shape = shape.AsVector();
output_shape.pop_back();
std::shared_ptr<Tensor> output_tensor;
std::vector<T> out;
T o;
T x;
T y;
for (auto itr = input->begin<T>(); itr != input->end<T>(); itr++) {
x = static_cast<T>(*itr);
itr++;
y = static_cast<T>(*itr);
o = std::atan2(y, x);
out.emplace_back(o);
}
// Generate multidimensional results corresponding to input
Tensor::CreateFromVector(out, TensorShape{output_shape}, &output_tensor);
*output = output_tensor;
return Status::OK();
}
/// \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

View File

@ -34,6 +34,20 @@ class TensorOperation;
// Transform operations for performing computer audio.
namespace audio {
/// \brief Compute the angle of complex tensor input.
class Angle final : public TensorTransform {
public:
/// \brief Constructor.
Angle();
/// \brief Destructor.
~Angle() = default;
protected:
/// \brief Function to convert TensorTransform object into a TensorOperation object.
/// \return Shared pointer to TensorOperation object.
std::shared_ptr<TensorOperation> Parse() override;
};
/// \brief Design two-pole band filter.
class BandBiquad final : public TensorTransform {
public:

View File

@ -139,6 +139,7 @@ constexpr char kSentencepieceTokenizerOp[] = "SentencepieceTokenizerOp";
// audio
constexpr char kAllpassBiquadOp[] = "AllpassBiquadOp";
constexpr char kAngleOp[] = "AngleOp";
constexpr char kBandBiquadOp[] = "BandBiquadOp";
constexpr char kBandpassBiquadOp[] = "BandpassBiquadOp";
constexpr char kBandrejectBiquadOp[] = "BandrejectBiquadOp";

View File

@ -74,6 +74,25 @@ class AllpassBiquad(AudioTensorOperation):
return cde.AllpassBiquadOperation(self.sample_rate, self.central_freq, self.Q)
class Angle(AudioTensorOperation):
"""
Calculate the angle of the complex number sequence of shape (..., 2).
The first dimension represents the real part while the second represents the imaginary.
Args:
Examples:
>>> import mindspore.dataset.audio.transforms as audio
>>> import numpy as np
>>> input_complex = np.array([[1.43, 5.434], [23.54, 89.38]])
>>> angle_op = audio.Angle()
>>> angles = angle_op(input_complex)
"""
def parse(self):
return cde.AngleOperation()
class BandBiquad(AudioTensorOperation):
"""
Design two-pole band filter for audio waveform of dimension of `(..., time)`

View File

@ -368,4 +368,63 @@ TEST_F(MindDataTestPipeline, Level0_TestBassBiquad002) {
std::shared_ptr<Iterator> iter02 = ds02->CreateIterator();
EXPECT_EQ(iter02, nullptr);
}
}
TEST_F(MindDataTestPipeline, TestAnglePipeline) {
MS_LOG(INFO) << "Doing MindDataTestPipeline-TestAnglePipeline";
std::shared_ptr<SchemaObj> schema = Schema();
ASSERT_OK(schema->add_column("complex", mindspore::DataType::kNumberTypeFloat32, {2, 2}));
std::shared_ptr<Dataset> ds = RandomData(50, schema);
EXPECT_NE(ds, nullptr);
ds = ds->SetNumWorkers(4);
EXPECT_NE(ds, nullptr);
auto angle_op = audio::Angle();
ds = ds->Map({angle_op});
EXPECT_NE(ds, nullptr);
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};
int i = 0;
while (row.size() != 0) {
auto col = row["complex"];
ASSERT_EQ(col.Shape(), expected);
ASSERT_EQ(col.Shape().size(), 1);
ASSERT_EQ(col.DataType(), mindspore::DataType::kNumberTypeFloat32);
ASSERT_OK(iter->GetNextRow(&row));
i++;
}
EXPECT_EQ(i, 50);
iter->Stop();
}
TEST_F(MindDataTestPipeline, TestAnglePipelineError) {
MS_LOG(INFO) << "Doing MindDataTestPipeline-TestAnglePipelineError";
std::shared_ptr<SchemaObj> schema = Schema();
ASSERT_OK(schema->add_column("complex", mindspore::DataType::kNumberTypeFloat32, {3, 2, 1}));
std::shared_ptr<Dataset> ds = RandomData(4, schema);
EXPECT_NE(ds, nullptr);
ds = ds->SetNumWorkers(4);
EXPECT_NE(ds, nullptr);
auto angle_op = audio::Angle();
ds = ds->Map({angle_op});
EXPECT_NE(ds, nullptr);
std::shared_ptr<Iterator> iter = ds->CreateIterator();
std::unordered_map<std::string, mindspore::MSTensor> row;
EXPECT_ERROR(iter->GetNextRow(&row));
}

View File

@ -477,3 +477,18 @@ TEST_F(MindDataTestExecute, TestBandrejectBiquadWithWrongArg) {
Status s01 = Transform01(input_02, &input_02);
EXPECT_FALSE(s01.IsOk());
}
TEST_F(MindDataTestExecute, TestAngleEager) {
MS_LOG(INFO) << "Doing MindDataTestExecute-TestAngleEager";
std::vector<double> origin = {1.143, 1.3123, 2.632, 2.554, -1.213, 1.3, 0.456, 3.563};
TensorShape input_shape({4, 2});
std::shared_ptr<Tensor> de_tensor;
Tensor::CreateFromVector(origin, input_shape, &de_tensor);
std::shared_ptr<TensorTransform> angle = std::make_shared<audio::Angle>();
auto input = mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_tensor));
mindspore::dataset::Execute Transform({angle});
Status s = Transform(input, &input);
ASSERT_TRUE(s.IsOk());
}

View File

@ -0,0 +1,83 @@
# 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 a_c_trans
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_angle_001():
"""
Eager Test
"""
arr = np.array([[73.04, -13.00], [57.49, 13.20], [-57.64, 6.51], [-52.25, 30.67], [-30.11, -18.34], \
[-63.32, 99.33], [95.82, -24.76]], dtype=np.double)
expected = np.array([-0.17614017, 0.22569334, 3.02912684, 2.6107975, -2.59450886, 2.13831337, -0.25286988], \
dtype=np.double)
angle_op = a_c_trans.Angle()
output = angle_op(arr)
_count_unequal_element(expected, output, 0.0001, 0.0001)
def test_func_angle_002():
"""
Pipeline Test
"""
np.random.seed(6)
arr = np.array([[[84.25, -85.92], [-92.23, 23.06], [-7.33, -44.17], [-62.95, -14.73]], \
[[93.09, 38.18], [-81.94, 71.34], [71.33, -39.00], [95.25, -32.94]]], dtype=np.double)
expected = np.array([[-0.79521156, 2.89658848, -1.73524737, -2.91173309], \
[0.3892177, 2.42523905, -0.50034807, -0.33295219]], dtype=np.double)
label = np.random.sample((2, 4, 1))
data = (arr, label)
dataset = ds.NumpySlicesDataset(data, column_names=["col1", "col2"], shuffle=False)
angle_op = a_c_trans.Angle()
dataset = dataset.map(operations=angle_op, input_columns=["col1"])
for item1, item2 in zip(dataset.create_dict_iterator(output_numpy=True), expected):
_count_unequal_element(item2, item1['col1'], 0.0001, 0.0001)
def test_func_angle_003():
"""
Pipeline Error Test
"""
np.random.seed(78)
arr = np.array([["11", "22"], ["33", "44"], ["55", "66"], ["77", "88"]])
label = np.random.sample((4, 1))
data = (arr, label)
dataset = ds.NumpySlicesDataset(data, column_names=["col1", 'col2'], shuffle=False)
angle_op = a_c_trans.Angle()
dataset = dataset.map(operations=angle_op, input_columns=["col1"])
num_itr = 0
with pytest.raises(RuntimeError, match="The input type should be numbers"):
for _ in dataset.create_dict_iterator(output_numpy=True):
num_itr += 1
if __name__ == "__main__":
test_func_angle_001()
test_func_angle_002()
test_func_angle_003()