commit
3e7ba14e19
@ -0,0 +1,198 @@
|
||||
/**
|
||||
* Copyright 2020 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/de_tensor.h"
|
||||
#include "minddata/dataset/core/constants.h"
|
||||
#include "minddata/dataset/core/data_type.h"
|
||||
#include "mindspore/core/ir/dtype/type_id.h"
|
||||
#include "utils/hashing.h"
|
||||
#include "mindspore/lite/src/ir/tensor.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace tensor {
|
||||
dataset::DataType MSTypeToDEType(TypeId data_type) {
|
||||
switch (data_type) {
|
||||
case kNumberTypeBool:
|
||||
return dataset::DataType(dataset::DataType::DE_BOOL);
|
||||
case kNumberTypeInt8:
|
||||
return dataset::DataType(dataset::DataType::DE_INT8);
|
||||
case kNumberTypeUInt8:
|
||||
return dataset::DataType(dataset::DataType::DE_UINT8);
|
||||
case kNumberTypeInt16:
|
||||
return dataset::DataType(dataset::DataType::DE_INT16);
|
||||
case kNumberTypeUInt16:
|
||||
return dataset::DataType(dataset::DataType::DE_UINT16);
|
||||
case kNumberTypeInt32:
|
||||
return dataset::DataType(dataset::DataType::DE_INT32);
|
||||
case kNumberTypeUInt32:
|
||||
return dataset::DataType(dataset::DataType::DE_UINT32);
|
||||
case kNumberTypeInt64:
|
||||
return dataset::DataType(dataset::DataType::DE_INT64);
|
||||
case kNumberTypeUInt64:
|
||||
return dataset::DataType(dataset::DataType::DE_UINT64);
|
||||
case kNumberTypeFloat16:
|
||||
return dataset::DataType(dataset::DataType::DE_FLOAT16);
|
||||
case kNumberTypeFloat32:
|
||||
return dataset::DataType(dataset::DataType::DE_FLOAT32);
|
||||
case kNumberTypeFloat64:
|
||||
return dataset::DataType(dataset::DataType::DE_FLOAT64);
|
||||
default:
|
||||
return dataset::DataType(dataset::DataType::DE_UNKNOWN);
|
||||
}
|
||||
}
|
||||
|
||||
TypeId DETypeToMSType(dataset::DataType data_type) {
|
||||
switch (data_type.value()) {
|
||||
case dataset::DataType::DE_BOOL:
|
||||
return mindspore::TypeId::kNumberTypeBool;
|
||||
case dataset::DataType::DE_INT8:
|
||||
return mindspore::TypeId::kNumberTypeInt8;
|
||||
case dataset::DataType::DE_UINT8:
|
||||
return mindspore::TypeId::kNumberTypeUInt8;
|
||||
case dataset::DataType::DE_INT16:
|
||||
return mindspore::TypeId::kNumberTypeInt16;
|
||||
case dataset::DataType::DE_UINT16:
|
||||
return mindspore::TypeId::kNumberTypeUInt16;
|
||||
case dataset::DataType::DE_INT32:
|
||||
return mindspore::TypeId::kNumberTypeInt32;
|
||||
case dataset::DataType::DE_UINT32:
|
||||
return mindspore::TypeId::kNumberTypeUInt32;
|
||||
case dataset::DataType::DE_INT64:
|
||||
return mindspore::TypeId::kNumberTypeInt64;
|
||||
case dataset::DataType::DE_UINT64:
|
||||
return mindspore::TypeId::kNumberTypeUInt64;
|
||||
case dataset::DataType::DE_FLOAT16:
|
||||
return mindspore::TypeId::kNumberTypeFloat16;
|
||||
case dataset::DataType::DE_FLOAT32:
|
||||
return mindspore::TypeId::kNumberTypeFloat32;
|
||||
case dataset::DataType::DE_FLOAT64:
|
||||
return mindspore::TypeId::kNumberTypeFloat64;
|
||||
default:
|
||||
return kTypeUnknown;
|
||||
}
|
||||
}
|
||||
|
||||
MSTensor *DETensor::CreateTensor(TypeId data_type, const std::vector<int> &shape) {
|
||||
return new DETensor(data_type, shape);
|
||||
}
|
||||
|
||||
MSTensor *DETensor::CreateTensor(const std::string &path) {
|
||||
std::shared_ptr<dataset::Tensor> t;
|
||||
(void)dataset::Tensor::CreateFromFile(path, &t);
|
||||
return new DETensor(std::move(t));
|
||||
}
|
||||
|
||||
DETensor::DETensor(TypeId data_type, const std::vector<int> &shape) {
|
||||
std::vector<dataset::dsize_t> t_shape;
|
||||
t_shape.reserve(shape.size());
|
||||
std::transform(shape.begin(), shape.end(), std::back_inserter(t_shape),
|
||||
[](int s) -> dataset::dsize_t { return static_cast<dataset::dsize_t>(s); });
|
||||
dataset::Tensor::CreateEmpty(dataset::TensorShape(t_shape), MSTypeToDEType(data_type), &this->tensor_impl_);
|
||||
}
|
||||
|
||||
DETensor::DETensor(std::shared_ptr<dataset::Tensor> tensor_ptr) { this->tensor_impl_ = std::move(tensor_ptr); }
|
||||
|
||||
MSTensor *DETensor::ConvertToLiteTensor() {
|
||||
// static MSTensor::CreateTensor is only for the LiteTensor
|
||||
MSTensor *tensor = MSTensor::CreateTensor(this->data_type(), this->shape());
|
||||
MS_ASSERT(tensor->Size() == this->Size());
|
||||
memcpy_s(tensor->MutableData(), tensor->Size(), this->MutableData(), this->Size());
|
||||
return tensor;
|
||||
}
|
||||
|
||||
std::shared_ptr<dataset::Tensor> DETensor::tensor() const {
|
||||
MS_ASSERT(this->tensor_impl_ != nullptr);
|
||||
return this->tensor_impl_;
|
||||
}
|
||||
|
||||
TypeId DETensor::data_type() const {
|
||||
MS_ASSERT(this->tensor_impl_ != nullptr);
|
||||
return DETypeToMSType(this->tensor_impl_->type());
|
||||
}
|
||||
|
||||
TypeId DETensor::set_data_type(TypeId data_type) {
|
||||
MS_ASSERT(this->tensor_impl_ != nullptr);
|
||||
if (data_type != this->data_type()) {
|
||||
std::shared_ptr<dataset::Tensor> temp;
|
||||
dataset::Tensor::CreateFromMemory(this->tensor_impl_->shape(), MSTypeToDEType(data_type),
|
||||
this->tensor_impl_->GetBuffer(), &temp);
|
||||
this->tensor_impl_ = temp;
|
||||
}
|
||||
return data_type;
|
||||
}
|
||||
|
||||
std::vector<int> DETensor::shape() const {
|
||||
MS_ASSERT(this->tensor_impl_ != nullptr);
|
||||
std::vector<dataset::dsize_t> t_shape = this->tensor_impl_->shape().AsVector();
|
||||
std::vector<int> shape;
|
||||
shape.reserve(t_shape.size());
|
||||
std::transform(t_shape.begin(), t_shape.end(), std::back_inserter(shape),
|
||||
[](dataset::dsize_t s) -> int { return static_cast<int>(s); });
|
||||
return shape;
|
||||
}
|
||||
|
||||
size_t DETensor::set_shape(const std::vector<int> &shape) {
|
||||
MS_ASSERT(this->tensor_impl_ != nullptr);
|
||||
std::vector<dataset::dsize_t> t_shape;
|
||||
t_shape.reserve(shape.size());
|
||||
std::transform(shape.begin(), shape.end(), std::back_inserter(t_shape),
|
||||
[](int s) -> dataset::dsize_t { return static_cast<dataset::dsize_t>(s); });
|
||||
dataset::Status rc = this->tensor_impl_->Reshape(dataset::TensorShape(t_shape));
|
||||
return shape.size();
|
||||
}
|
||||
|
||||
int DETensor::DimensionSize(size_t index) const {
|
||||
MS_ASSERT(this->tensor_impl_ != nullptr);
|
||||
int dim_size = -1;
|
||||
auto shape = this->shape();
|
||||
if (index < shape.size()) {
|
||||
dim_size = shape[index];
|
||||
} else {
|
||||
MS_LOG(ERROR) << "Dimension index is wrong: " << index;
|
||||
}
|
||||
return dim_size;
|
||||
}
|
||||
|
||||
int DETensor::ElementsNum() const {
|
||||
MS_ASSERT(this->tensor_impl_ != nullptr);
|
||||
return this->tensor_impl_->Size();
|
||||
}
|
||||
|
||||
std::size_t DETensor::hash() const {
|
||||
MS_ASSERT(this->tensor_impl_ != nullptr);
|
||||
auto shape = this->shape();
|
||||
std::size_t hash_value = std::hash<int>{}(SizeToInt(this->data_type()));
|
||||
hash_value = hash_combine(hash_value, std::hash<size_t>{}(shape.size()));
|
||||
// hash all elements may costly, so only take at most 4 elements into account based on
|
||||
// some experiments.
|
||||
for (size_t i = 0; (i < shape.size()) && (i < 4); ++i) {
|
||||
hash_value = hash_combine(hash_value, (std::hash<int>{}(shape[i])));
|
||||
}
|
||||
return hash_value;
|
||||
}
|
||||
|
||||
size_t DETensor::Size() const {
|
||||
MS_ASSERT(this->tensor_impl_ != nullptr);
|
||||
return this->tensor_impl_->SizeInBytes();
|
||||
}
|
||||
|
||||
void *DETensor::MutableData() const {
|
||||
MS_ASSERT(this->tensor_impl_ != nullptr);
|
||||
return this->tensor_impl_->GetMutableBuffer();
|
||||
}
|
||||
|
||||
} // namespace tensor
|
||||
} // namespace mindspore
|
@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Copyright 2020 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/execute.h"
|
||||
#include "minddata/dataset/include/de_tensor.h"
|
||||
#include "minddata/dataset/include/tensor.h"
|
||||
#include "minddata/dataset/kernels/tensor_op.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
namespace api {
|
||||
|
||||
Execute::Execute(std::shared_ptr<TensorOperation> op) : op_(std::move(op)) {}
|
||||
|
||||
std::shared_ptr<tensor::MSTensor> Execute::operator()(std::shared_ptr<tensor::MSTensor> input) {
|
||||
// Build the op
|
||||
if (op_ == nullptr) {
|
||||
MS_LOG(ERROR) << "Input TensorOperation is not valid";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<Tensor> de_input = std::dynamic_pointer_cast<tensor::DETensor>(input)->tensor();
|
||||
if (de_input == nullptr) {
|
||||
MS_LOG(ERROR) << "Input Tensor is not valid";
|
||||
return nullptr;
|
||||
}
|
||||
std::shared_ptr<TensorOp> transform = op_->Build();
|
||||
std::shared_ptr<Tensor> de_output;
|
||||
Status rc = transform->Compute(de_input, &de_output);
|
||||
|
||||
if (rc.IsError()) {
|
||||
// execution failed
|
||||
MS_LOG(ERROR) << "Operation execution failed : " << rc.ToString();
|
||||
return nullptr;
|
||||
}
|
||||
return std::make_shared<tensor::DETensor>(std::move(de_output));
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Copyright 2020 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_API_DETENSOR_H_
|
||||
#define MINDSPORE_CCSRC_MINDDATA_DATASET_API_DETENSOR_H_
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include "include/ms_tensor.h"
|
||||
#include "minddata/dataset/include/tensor.h"
|
||||
#include "minddata/dataset/util/status.h"
|
||||
namespace mindspore {
|
||||
namespace tensor {
|
||||
class DETensor : public MSTensor {
|
||||
public:
|
||||
/// \brief Create a MSTensor pointer.
|
||||
/// \param[data_type] DataTypeId of tensor to be created.
|
||||
/// \param[shape] Shape of tensor to be created.
|
||||
/// \return - MSTensor pointer.
|
||||
static MSTensor *CreateTensor(TypeId data_type, const std::vector<int> &shape);
|
||||
|
||||
/// \brief Create a MSTensor pointer.
|
||||
/// \param[path] Path file to be read.
|
||||
/// \return - MSTensor pointer.
|
||||
static MSTensor *CreateTensor(const std::string &path);
|
||||
|
||||
DETensor(TypeId data_type, const std::vector<int> &shape);
|
||||
|
||||
explicit DETensor(std::shared_ptr<dataset::Tensor> tensor_ptr);
|
||||
|
||||
~DETensor() = default;
|
||||
|
||||
/// \brief Create a duplicate instance, convert the DETensor to the LiteTensor.
|
||||
/// \return - MSTensor pointer.
|
||||
MSTensor *ConvertToLiteTensor();
|
||||
|
||||
std::shared_ptr<dataset::Tensor> tensor() const;
|
||||
|
||||
TypeId data_type() const override;
|
||||
|
||||
TypeId set_data_type(const TypeId data_type) override;
|
||||
|
||||
std::vector<int> shape() const override;
|
||||
|
||||
size_t set_shape(const std::vector<int> &shape) override;
|
||||
|
||||
int DimensionSize(size_t index) const override;
|
||||
|
||||
int ElementsNum() const override;
|
||||
|
||||
std::size_t hash() const override;
|
||||
|
||||
size_t Size() const override;
|
||||
|
||||
void *MutableData() const override;
|
||||
|
||||
protected:
|
||||
std::shared_ptr<dataset::Tensor> tensor_impl_;
|
||||
};
|
||||
} // namespace tensor
|
||||
} // namespace mindspore
|
||||
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_API_DETENSOR_H_
|
@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Copyright 2020 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 DATASET_API_EXECUTE_H_
|
||||
#define DATASET_API_EXECUTE_H_
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include "minddata/dataset/core/constants.h"
|
||||
#include "minddata/dataset/include/de_tensor.h"
|
||||
#include "minddata/dataset/include/transforms.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
|
||||
class TensorOp;
|
||||
|
||||
namespace api {
|
||||
|
||||
// class to run tensor operations in eager mode
|
||||
class Execute {
|
||||
public:
|
||||
/// \brief Constructor
|
||||
explicit Execute(std::shared_ptr<TensorOperation> op);
|
||||
|
||||
/// \brief callable function to execute the TensorOperation in eager mode
|
||||
/// \param[inout] input - the tensor to be transformed
|
||||
/// \return - the output tensor, nullptr if Compute fails
|
||||
std::shared_ptr<tensor::MSTensor> operator()(std::shared_ptr<tensor::MSTensor> input);
|
||||
|
||||
private:
|
||||
std::shared_ptr<TensorOperation> op_;
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
||||
#endif // DATASET_API_EXECUTE_H_
|
@ -0,0 +1,47 @@
|
||||
set(MINDDATA_DIR ${CCSRC_DIR}/minddata/dataset)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Wno-deprecated-declarations")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -g2 -ggdb")
|
||||
if (CMAKE_BUILD_TYPE EQUAL "DEBUG")
|
||||
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s")
|
||||
endif()
|
||||
|
||||
AUX_SOURCE_DIRECTORY(${MINDDATA_DIR}/core MINDDATA_CORE_SRC_FILES)
|
||||
list(REMOVE_ITEM MINDDATA_CORE_SRC_FILES "${MINDDATA_DIR}/core/client.cc")
|
||||
|
||||
AUX_SOURCE_DIRECTORY(${MINDDATA_DIR}/kernels MINDDATA_KERNELS_SRC_FILES)
|
||||
list(REMOVE_ITEM MINDDATA_KERNELS_SRC_FILES "${MINDDATA_DIR}/kernels/py_func_op.cc")
|
||||
|
||||
AUX_SOURCE_DIRECTORY(${MINDDATA_DIR}/kernels/image MINDDATA_KERNELS_IMAGE_SRC_FILES)
|
||||
|
||||
AUX_SOURCE_DIRECTORY(${MINDDATA_DIR}/kernels/data MINDDATA_KERNELS_DATA_SRC_FILES)
|
||||
|
||||
add_library(minddata-eager OBJECT
|
||||
${MINDDATA_DIR}/api/de_tensor.cc
|
||||
${MINDDATA_DIR}/api/execute.cc
|
||||
)
|
||||
|
||||
add_library(minddata-lite SHARED
|
||||
${MINDDATA_CORE_SRC_FILES}
|
||||
${MINDDATA_KERNELS_SRC_FILES}
|
||||
${MINDDATA_KERNELS_IMAGE_SRC_FILES}
|
||||
${MINDDATA_KERNELS_DATA_SRC_FILES}
|
||||
${MINDDATA_DIR}/util/status.cc
|
||||
${MINDDATA_DIR}/util/memory_pool.cc
|
||||
${MINDDATA_DIR}/util/path.cc
|
||||
${MINDDATA_DIR}/api/transforms.cc
|
||||
${CORE_DIR}/utils/log_adapter.cc
|
||||
${CCSRC_DIR}/gvar/logging_level.cc
|
||||
)
|
||||
|
||||
target_link_libraries(minddata-lite
|
||||
securec
|
||||
jpeg-turbo
|
||||
jpeg
|
||||
opencv_core
|
||||
opencv_imgcodecs
|
||||
opencv_imgproc
|
||||
mindspore::json
|
||||
)
|
@ -0,0 +1,98 @@
|
||||
/**
|
||||
* Copyright 2020 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 <memory>
|
||||
#include <string>
|
||||
#include "common/common_test.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "./securec.h"
|
||||
#include "dataset/core/tensor.h"
|
||||
#include "dataset/core/cv_tensor.h"
|
||||
#include "dataset/core/data_type.h"
|
||||
#include "mindspore/lite/src/ir/tensor.h"
|
||||
|
||||
using MSTensor = mindspore::tensor::MSTensor;
|
||||
using DETensor = mindspore::tensor::DETensor;
|
||||
using LiteTensor = mindspore::lite::tensor::LiteTensor;
|
||||
using Tensor = mindspore::dataset::Tensor;
|
||||
using DataType = mindspore::dataset::DataType;
|
||||
using TensorShape = mindspore::dataset::TensorShape;
|
||||
|
||||
class MindDataTestTensorDE : public mindspore::Common {
|
||||
public:
|
||||
MindDataTestTensorDE() {}
|
||||
};
|
||||
|
||||
TEST_F(MindDataTestTensorDE, MSTensorBasic) {
|
||||
std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 3}), DataType(DataType::DE_FLOAT32));
|
||||
auto ms_tensor = std::shared_ptr<MSTensor>(new DETensor(t));
|
||||
ASSERT_EQ(t == std::dynamic_pointer_cast<DETensor>(ms_tensor)->tensor(), true);
|
||||
}
|
||||
|
||||
TEST_F(MindDataTestTensorDE, MSTensorConvertToLiteTensor) {
|
||||
std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 3}), DataType(DataType::DE_FLOAT32));
|
||||
auto ms_tensor = std::shared_ptr<DETensor>(new DETensor(t));
|
||||
std::shared_ptr<MSTensor> lite_ms_tensor = std::shared_ptr<MSTensor>(
|
||||
std::dynamic_pointer_cast<DETensor>(ms_tensor)->ConvertToLiteTensor());
|
||||
// check if the lite_ms_tensor is the derived LiteTensor
|
||||
LiteTensor * lite_tensor = static_cast<LiteTensor *>(lite_ms_tensor.get());
|
||||
ASSERT_EQ(lite_tensor != nullptr, true);
|
||||
}
|
||||
|
||||
TEST_F(MindDataTestTensorDE, MSTensorShape) {
|
||||
std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 3}), DataType(DataType::DE_FLOAT32));
|
||||
auto ms_tensor = std::shared_ptr<MSTensor>(new DETensor(t));
|
||||
ASSERT_EQ(ms_tensor->DimensionSize(0) == 2, true);
|
||||
ASSERT_EQ(ms_tensor->DimensionSize(1) == 3, true);
|
||||
ms_tensor->set_shape(std::vector<int>{3, 2});
|
||||
ASSERT_EQ(ms_tensor->DimensionSize(0) == 3, true);
|
||||
ASSERT_EQ(ms_tensor->DimensionSize(1) == 2, true);
|
||||
ms_tensor->set_shape(std::vector<int>{6});
|
||||
ASSERT_EQ(ms_tensor->DimensionSize(0) == 6, true);
|
||||
}
|
||||
|
||||
TEST_F(MindDataTestTensorDE, MSTensorSize) {
|
||||
std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 3}), DataType(DataType::DE_FLOAT32));
|
||||
auto ms_tensor = std::shared_ptr<MSTensor>(new DETensor(t));
|
||||
ASSERT_EQ(ms_tensor->ElementsNum() == 6, true);
|
||||
ASSERT_EQ(ms_tensor->Size() == 24, true);
|
||||
}
|
||||
|
||||
TEST_F(MindDataTestTensorDE, MSTensorDataType) {
|
||||
std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 3}), DataType(DataType::DE_FLOAT32));
|
||||
auto ms_tensor = std::shared_ptr<MSTensor>(new DETensor(t));
|
||||
ASSERT_EQ(ms_tensor->data_type() == mindspore::TypeId::kNumberTypeFloat32, true);
|
||||
ms_tensor->set_data_type(mindspore::TypeId::kNumberTypeInt32);
|
||||
ASSERT_EQ(ms_tensor->data_type() == mindspore::TypeId::kNumberTypeInt32, true);
|
||||
ASSERT_EQ(std::dynamic_pointer_cast<DETensor>(ms_tensor)->tensor()->type() == DataType::DE_INT32, true);
|
||||
}
|
||||
|
||||
TEST_F(MindDataTestTensorDE, MSTensorMutableData) {
|
||||
std::vector<float> x = {2.5, 2.5, 2.5, 2.5};
|
||||
std::shared_ptr<Tensor> t;
|
||||
Tensor::CreateFromVector(x, TensorShape({2, 2}), &t);
|
||||
auto ms_tensor = std::shared_ptr<MSTensor>(new DETensor(t));
|
||||
float *data = static_cast<float*>(ms_tensor->MutableData());
|
||||
std::vector<float> tensor_vec(data, data + ms_tensor->ElementsNum());
|
||||
ASSERT_EQ(x == tensor_vec, true);
|
||||
}
|
||||
|
||||
TEST_F(MindDataTestTensorDE, MSTensorHash) {
|
||||
std::vector<float> x = {2.5, 2.5, 2.5, 2.5};
|
||||
std::shared_ptr<Tensor> t;
|
||||
Tensor::CreateFromVector(x, TensorShape({2, 2}), &t);
|
||||
auto ms_tensor = std::shared_ptr<MSTensor>(new DETensor(t));
|
||||
ASSERT_EQ(ms_tensor->hash() == 11093771382437, true);
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Copyright 2020 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 <chrono>
|
||||
#include "common/common_test.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "./securec.h"
|
||||
#include "minddata/dataset/core/tensor.h"
|
||||
#include "minddata/dataset/core/config_manager.h"
|
||||
#include "minddata/dataset/include/datasets.h"
|
||||
#include "minddata/dataset/include/execute.h"
|
||||
#include "minddata/dataset/util/path.h"
|
||||
|
||||
using MSTensor = mindspore::tensor::MSTensor;
|
||||
using DETensor = mindspore::tensor::DETensor;
|
||||
using mindspore::dataset::api::vision::Decode;
|
||||
using mindspore::dataset::api::vision::Normalize;
|
||||
using mindspore::dataset::api::vision::Resize;
|
||||
using Execute = mindspore::dataset::api::Execute;
|
||||
using Path = mindspore::dataset::Path;
|
||||
|
||||
class MindDataTestEager : public mindspore::Common {
|
||||
public:
|
||||
MindDataTestEager() {}
|
||||
};
|
||||
|
||||
TEST_F(MindDataTestEager, Test1) {
|
||||
#if defined(ENABLE_ARM64) || defined(ENABLE_ARM32)
|
||||
std::string in_dir = "/sdcard/data/testPK/data/class1";
|
||||
#else
|
||||
std::string in_dir = "data/testPK/data/class1";
|
||||
#endif
|
||||
Path base_dir = Path(in_dir);
|
||||
MS_LOG(WARNING) << base_dir.toString() << ".";
|
||||
if (!base_dir.IsDirectory() || !base_dir.Exists()) {
|
||||
MS_LOG(INFO) << "Input dir is not a directory or doesn't exist" << ".";
|
||||
}
|
||||
auto t_start = std::chrono::high_resolution_clock::now();
|
||||
// check if output_dir exists and create it if it does not exist
|
||||
|
||||
// iterate over in dir and create json for all images
|
||||
auto dir_it = Path::DirIterator::OpenDirectory(&base_dir);
|
||||
while (dir_it->hasNext()) {
|
||||
Path v = dir_it->next();
|
||||
MS_LOG(WARNING) << v.toString() << ".";
|
||||
std::shared_ptr<MSTensor> image = std::shared_ptr<MSTensor>(DETensor::CreateTensor(v.toString()));
|
||||
|
||||
image = Execute(Decode())(image);
|
||||
EXPECT_TRUE(image != nullptr);
|
||||
image = Execute(Normalize({121.0, 115.0, 100.0}, {70.0, 68.0, 71.0}))(image);
|
||||
EXPECT_TRUE(image != nullptr);
|
||||
image = Execute(Resize({224, 224}))(image);
|
||||
EXPECT_TRUE(image != nullptr);
|
||||
EXPECT_EQ(image->DimensionSize(0), 224);
|
||||
EXPECT_EQ(image->DimensionSize(1), 224);
|
||||
}
|
||||
auto t_end = std::chrono::high_resolution_clock::now();
|
||||
double elapsed_time_ms = std::chrono::duration<double, std::milli>(t_end-t_start).count();
|
||||
MS_LOG(INFO) << "duration: " << elapsed_time_ms << " ms\n";
|
||||
}
|
@ -0,0 +1 @@
|
||||
Subproject commit daf9bbeca26e98da2eed0058835cbb04e0a30ad8
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue