add init code

pull/5961/head
xulei2020 5 years ago
parent 47ca1e3128
commit b356211413

@ -116,7 +116,7 @@ checkopts()
DEBUG_MODE="on"
;;
n)
if [[ "X$OPTARG" == "Xoff" || "X$OPTARG" == "Xlite" || "X$OPTARG" == "Xfull" ]]; then
if [[ "X$OPTARG" == "Xoff" || "X$OPTARG" == "Xlite" || "X$OPTARG" == "Xfull" || "X$OPTARG" == "Xlite_cv" ]]; then
COMPILE_MINDDATA_LITE="$OPTARG"
else
echo "Invalid value ${OPTARG} for option -n"

@ -40,6 +40,18 @@ if (BUILD_MINDDATA STREQUAL "lite")
endif ()
endif ()
if (BUILD_MINDDATA STREQUAL "lite_cv")
install(DIRECTORY ${TOP_DIR}/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv DESTINATION ${INC_DIR} COMPONENT ${COMPONENT_NAME} FILES_MATCHING PATTERN "*.h")
if (PLATFORM_ARM64)
install(FILES ${TOP_DIR}/mindspore/lite/build/minddata/libminddata-lite.so DESTINATION ${LIB_DIR} COMPONENT ${COMPONENT_NAME})
elseif (PLATFORM_ARM32)
install(FILES ${TOP_DIR}/mindspore/lite/build/minddata/libminddata-lite.so DESTINATION ${LIB_DIR} COMPONENT ${COMPONENT_NAME})
else ()
install(FILES ${TOP_DIR}/mindspore/lite/build/minddata/libminddata-lite.so DESTINATION ${LIB_DIR_RUN_X86} COMPONENT ${RUN_X86_COMPONENT_NAME})
endif ()
endif ()
if (PLATFORM_ARM64)
install(FILES ${TOP_DIR}/mindspore/lite/build/src/libmindspore-lite.so DESTINATION ${LIB_DIR} COMPONENT ${COMPONENT_NAME})
install(FILES ${TOP_DIR}/mindspore/core/ir/dtype/type_id.h DESTINATION ${INC_DIR}/ir/dtype COMPONENT ${COMPONENT_NAME})

@ -43,6 +43,8 @@ include_directories(${CMAKE_BINARY_DIR}) # for protobuf generated .h
include_directories(${CMAKE_SOURCE_DIR}/mindspore/ccsrc/minddata/mindrecord/include)
include_directories(${CMAKE_SOURCE_DIR}/mindspore/ccsrc/minddata/dataset/include)
include_directories(${CMAKE_SOURCE_DIR}/mindspore/ccsrc/minddata/dataset/kernels/image)
######################################################################
####################### Flags ########################################
@ -94,6 +96,7 @@ set(submodules
$<TARGET_OBJECTS:utils>
$<TARGET_OBJECTS:kernels>
$<TARGET_OBJECTS:kernels-image>
$<TARGET_OBJECTS:lite-cv>
$<TARGET_OBJECTS:kernels-data>
$<TARGET_OBJECTS:cpp-API>
$<TARGET_OBJECTS:kernels-soft-dvpp-image>

@ -1,6 +1,7 @@
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_subdirectory(soft_dvpp)
add_subdirectory(lite_cv)
add_library(kernels-image OBJECT
affine_op.cc
auto_contrast_op.cc
@ -49,4 +50,4 @@ add_library(kernels-image OBJECT
random_resize_with_bbox_op.cc
random_color_op.cc
)
add_dependencies(kernels-image kernels-soft-dvpp-image)
add_dependencies(kernels-image kernels-soft-dvpp-image )

@ -0,0 +1,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(lite-cv OBJECT
image_process.cc
lite_mat.cc)

@ -0,0 +1,83 @@
/**
* 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 IMAGE_PROCESS_H_
#define IMAGE_PROCESS_H_
#include <math.h>
#include <vector>
#include <algorithm>
#include "lite_cv/lite_mat.h"
namespace mindspore {
namespace dataset {
#define INT16_CAST(X) \
static_cast<int16_t>(::std::min(::std::max(static_cast<int>(X + (X >= 0.f ? 0.5f : -0.5f)), -32768), 32767));
#define R2GRAY 77
#define G2GRAY 150
#define B2GRAY 29
#define GRAYSHIFT 8
enum PaddBorderType { PADD_BORDER_CONSTANT = 0, PADD_BORDER_REPLICATE = 1 };
struct BoxesConfig {
public:
std::vector<size_t> img_shape;
std::vector<int> num_default;
std::vector<int> feature_size;
float min_scale;
float max_scale;
std::vector<std::vector<float>> aspect_rations;
std::vector<int> steps;
std::vector<float> prior_scaling;
};
/// \brief resizing image by bilinear algorithm, the data type of currently only supports is uint8,
/// the channel of currently supports is 3 and 1
bool ResizeBilinear(const LiteMat &src, LiteMat &dst, int dst_w, int dst_h);
/// \brief Init Lite Mat from pixel, the conversion of currently supports is rbgaTorgb and rgbaTobgr
bool InitFromPixel(const unsigned char *data, LPixelType pixel_type, LDataType data_type, int w, int h, LiteMat &m);
/// \brief convert the data type, the conversion of currently supports is uint8 to float
bool ConvertTo(const LiteMat &src, LiteMat &dst, double scale = 1.0);
/// \brief crop image, the channel supports is 3 and 1
bool Crop(const LiteMat &src, LiteMat &dst, int x, int y, int w, int h);
/// \brief normalize image, currently the supports data type is float
bool SubStractMeanNormalize(const LiteMat &src, LiteMat &dst, float *mean, float *norm);
/// \brief padd image, the channel supports is 3 and 1
bool Padd(const LiteMat &src, LiteMat &dst, int top, int bottom, int left, int right, PaddBorderType pad_type,
uint8_t fill_b_or_gray, uint8_t fill_g, uint8_t fill_r);
void WarpAffine(const LiteMat &src, LiteMat &out_img, double M[6], std::vector<size_t> dsize, uint8_t borderValue[3]);
std::vector<std::vector<float>> GetDefaultBoxes(const BoxesConfig config);
void ConvertBoxes(std::vector<std::vector<float>> &boxes, const std::vector<std::vector<float>> &default_boxes,
const BoxesConfig config);
std::vector<int> ApplyNms(const std::vector<std::vector<float>> &all_boxes, std::vector<float> &all_scores, float thres,
int max_boxes);
} // namespace dataset
} // namespace mindspore
#endif // IMAGE_PROCESS_H_

@ -0,0 +1,207 @@
/**
* 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 "lite_cv/lite_mat.h"
namespace mindspore {
namespace dataset {
LiteMat::LiteMat() {
data_ptr_ = 0;
elem_size_ = 0;
width_ = 0;
height_ = 0;
channel_ = 0;
c_step_ = 0;
dims_ = 0;
data_type_ = LDataType::UINT8;
ref_count_ = 0;
}
LiteMat::LiteMat(int width, LDataType data_type) {
data_ptr_ = 0;
elem_size_ = 0;
width_ = 0;
height_ = 0;
channel_ = 0;
c_step_ = 0;
dims_ = 0;
data_type_ = LDataType::UINT8;
ref_count_ = 0;
Init(width, data_type);
}
LiteMat::LiteMat(int width, int height, LDataType data_type) {
data_ptr_ = 0;
elem_size_ = 0;
width_ = 0;
height_ = 0;
channel_ = 0;
c_step_ = 0;
dims_ = 0;
data_type_ = LDataType::UINT8;
ref_count_ = 0;
Init(width, height, data_type);
}
LiteMat::LiteMat(int width, int height, int channel, LDataType data_type) {
data_ptr_ = 0;
elem_size_ = 0;
width_ = 0;
height_ = 0;
channel_ = 0;
c_step_ = 0;
dims_ = 0;
data_type_ = LDataType::UINT8;
ref_count_ = 0;
Init(width, height, channel, data_type);
}
LiteMat::~LiteMat() { Release(); }
int LiteMat::addRef(int *p, int value) {
int v = *p;
*p += value;
return v;
}
LiteMat::LiteMat(const LiteMat &m) {
data_ptr_ = m.data_ptr_;
elem_size_ = m.elem_size_;
width_ = m.width_;
height_ = m.height_;
channel_ = m.channel_;
c_step_ = m.c_step_;
dims_ = m.dims_;
data_type_ = m.data_type_;
ref_count_ = m.ref_count_;
if (ref_count_) {
addRef(ref_count_, 1);
}
}
LiteMat &LiteMat::operator=(const LiteMat &m) {
if (this == &m) {
return *this;
}
if (m.ref_count_) {
addRef(m.ref_count_, 1);
}
Release();
data_ptr_ = m.data_ptr_;
elem_size_ = m.elem_size_;
width_ = m.width_;
height_ = m.height_;
channel_ = m.channel_;
c_step_ = m.c_step_;
dims_ = m.dims_;
data_type_ = m.data_type_;
ref_count_ = m.ref_count_;
return *this;
}
void LiteMat::Init(int width, LDataType data_type) {
Release();
data_type_ = data_type;
InitElemSize(data_type);
width_ = width;
dims_ = 1;
height_ = 1;
channel_ = 1;
c_step_ = width;
size_ = c_step_ * elem_size_;
data_ptr_ = AlignMalloc(size_);
ref_count_ = new int[1];
*ref_count_ = 1;
}
void LiteMat::Init(int width, int height, LDataType data_type) {
Release();
data_type_ = data_type;
InitElemSize(data_type);
width_ = width;
height_ = height;
dims_ = 2;
channel_ = 1;
c_step_ = width_ * height_;
size_ = c_step_ * elem_size_;
data_ptr_ = AlignMalloc(size_);
ref_count_ = new int[1];
*ref_count_ = 1;
}
void LiteMat::Init(int width, int height, int channel, LDataType data_type) {
Release();
data_type_ = data_type;
InitElemSize(data_type);
width_ = width;
height_ = height;
dims_ = 3;
channel_ = channel;
c_step_ = ((height_ * width_ * elem_size_ + ALIGN - 1) & (-ALIGN)) / elem_size_;
size_ = c_step_ * channel_ * elem_size_;
data_ptr_ = AlignMalloc(size_);
ref_count_ = new int[1];
*ref_count_ = 1;
}
bool LiteMat::IsEmpty() const { return data_ptr_ == 0 || data_ptr_ == nullptr || c_step_ * channel_ == 0; }
void LiteMat::Release() {
if (ref_count_ && (addRef(ref_count_, -1) == 1)) {
if (data_ptr_) {
AlignFree(data_ptr_);
}
if (ref_count_) {
delete[] ref_count_;
}
}
data_ptr_ = 0;
elem_size_ = 0;
width_ = 0;
height_ = 0;
channel_ = 0;
c_step_ = 0;
ref_count_ = 0;
}
void *LiteMat::AlignMalloc(unsigned int size) {
unsigned int length = sizeof(void *) + ALIGN - 1;
void *p_raw = reinterpret_cast<void *>(malloc(size + length));
if (p_raw) {
void **p_algin = reinterpret_cast<void **>(((size_t)(p_raw) + length) & ~(ALIGN - 1));
p_algin[-1] = p_raw;
return p_algin;
}
return nullptr;
}
void LiteMat::AlignFree(void *ptr) { (void)free(reinterpret_cast<void **>(ptr)[-1]); }
inline void LiteMat::InitElemSize(LDataType data_type) {
if (data_type == LDataType::UINT8) {
elem_size_ = 1;
} else if (data_type == LDataType::UINT16) {
elem_size_ = 2;
} else if (data_type == LDataType::FLOAT32) {
elem_size_ = 4;
} else {
}
}
} // namespace dataset
} // namespace mindspore

@ -0,0 +1,219 @@
/**
* 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 MINI_MAT_H_
#define MINI_MAT_H_
#include <string>
#include <memory>
namespace mindspore {
namespace dataset {
#define ALIGN 16
template <typename T>
struct Chn1 {
T c1;
};
template <typename T>
struct Chn2 {
T c1;
T c2;
};
template <typename T>
struct Chn3 {
T c1;
T c2;
T c3;
};
template <typename T>
struct Chn4 {
T c1;
T c2;
T c3;
T c4;
};
using UINT8_C1 = Chn1<uint8_t>;
using UINT8_C2 = Chn2<uint8_t>;
using UINT8_C3 = Chn3<uint8_t>;
using UINT8_C4 = Chn4<uint8_t>;
using INT8_C1 = Chn1<int8_t>;
using INT8_C2 = Chn2<int8_t>;
using INT8_C3 = Chn3<int8_t>;
using INT8_C4 = Chn4<int8_t>;
using UINT32_C1 = Chn1<uint32_t>;
using UINT32_C2 = Chn2<uint32_t>;
using UINT32_C3 = Chn3<uint32_t>;
using UINT32_C4 = Chn4<uint32_t>;
using INT32_C1 = Chn1<int32_t>;
using INT32_C2 = Chn2<int32_t>;
using INT32_C3 = Chn3<int32_t>;
using INT32_C4 = Chn4<int32_t>;
using FLOAT32_C1 = Chn1<float>;
using FLOAT32_C2 = Chn2<float>;
using FLOAT32_C3 = Chn3<float>;
using FLOAT32_C4 = Chn4<float>;
using FLOAT64_C1 = Chn1<double>;
using FLOAT64_C2 = Chn2<double>;
using FLOAT64_C3 = Chn3<double>;
using FLOAT64_C4 = Chn4<double>;
enum LPixelType {
BGR = 0,
RGB = 1,
RGBA = 2,
RGBA2GRAY = 3,
RGBA2BGR = 4,
};
class LDataType {
public:
enum Type : uint8_t {
UNKNOWN = 0,
BOOL,
INT8,
UINT8,
INT16,
UINT16,
INT32,
UINT32,
INT64,
UINT64,
FLOAT16,
FLOAT32,
FLOAT64,
NUM_OF_TYPES
};
LDataType() : type_(UNKNOWN) {}
LDataType(Type d) : type_(d) {}
~LDataType() = default;
inline Type Value() const { return type_; }
inline bool operator==(const LDataType &ps) const {
if (this->type_ == ps.type_) return true;
return false;
}
inline bool operator!=(const LDataType &ps) const {
if (this->type_ != ps.type_) return true;
return false;
}
uint8_t SizeInBytes() const {
if (type_ < LDataType::NUM_OF_TYPES)
return SIZE_IN_BYTES[type_];
else
return 0;
}
public:
static inline const uint8_t SIZE_IN_BYTES[] = {
0, // UNKNOWN
1, // BOOL
1, // INT8
1, // UINT8
2, // INT16
2, // UINT16
4, // INT32
4, // UINT32
8, // INT64
8, // UINT64
2, // FLOAT16
4, // FLOAT32
8, // FLOAT64
};
Type type_;
};
class LiteMat {
// Class that represents a lite Mat of a Image.
// -# The pixel type of Lite Mat is RGBRGB...RGB.
public:
LiteMat();
explicit LiteMat(int width, LDataType data_type = LDataType::UINT8);
LiteMat(int width, int height, LDataType data_type = LDataType::UINT8);
LiteMat(int width, int height, int channel, LDataType data_type = LDataType::UINT8);
~LiteMat();
LiteMat(const LiteMat &m);
void Init(int width, LDataType data_type = LDataType::UINT8);
void Init(int width, int height, LDataType data_type = LDataType::UINT8);
void Init(int width, int height, int channel, LDataType data_type = LDataType::UINT8);
bool IsEmpty() const;
void Release();
LiteMat &operator=(const LiteMat &m);
template <typename T>
operator T *() {
return reinterpret_cast<T *>(data_ptr_);
}
template <typename T>
operator const T *() const {
return reinterpret_cast<const T *>(data_ptr_);
}
private:
/// \brief apply for memory alignment
void *AlignMalloc(unsigned int size);
/// \brief free memory
void AlignFree(void *ptr);
void InitElemSize(LDataType data_type);
/// \brief add reference
int addRef(int *p, int value);
public:
void *data_ptr_ = nullptr;
int elem_size_;
int width_;
int height_;
int channel_;
int c_step_;
int dims_;
size_t size_;
LDataType data_type_;
int *ref_count_;
};
} // namespace dataset
} // namespace mindspore
#endif // MINI_MAT_H_

@ -76,7 +76,7 @@ option(BUILD_CONVERTER "if build converter" on)
option(ENABLE_FP16 "if build fp16 ops" off)
option(SUPPORT_GPU "if support gpu" off)
option(OFFLINE_COMPILE "if offline compile OpenCL kernel" off)
set(BUILD_MINDDATA "off" CACHE STRING "off, lite, or full")
set(BUILD_MINDDATA "off" CACHE STRING "off, lite, lite_cv or full")
option(BUILD_MINDDATA_EXAMPLE "" on)
set(CMAKE_VERBOSE_MAKEFILE on)
@ -215,6 +215,13 @@ if (BUILD_MINDDATA STREQUAL "lite" OR BUILD_MINDDATA STREQUAL "full")
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/minddata)
endif ()
if (BUILD_MINDDATA STREQUAL "lite_cv")
# TODO: add sentencepiece dependency
#include(${TOP_DIR}/cmake/external_libs/sentencepiece.cmake)
add_compile_definitions(ENABLE_ANDROID)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/minddata)
endif ()
if (BUILD_DEVICE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/nnacl)

@ -70,6 +70,9 @@ AUX_SOURCE_DIRECTORY(${MINDDATA_DIR}/text/kernels MINDDATA_TEXT_KERNELS_SRC_FILE
AUX_SOURCE_DIRECTORY(${MINDDATA_DIR}/util MINDDATA_UTIL_SRC_FILES)
AUX_SOURCE_DIRECTORY(${MINDDATA_DIR}/kernels/image/lite_cv MINDDATA_KERNELS_IMAGE_LITE_CV_FILES)
if (BUILD_MINDDATA STREQUAL "full")
list(REMOVE_ITEM MINDDATA_API_SRC_FILES
"${MINDDATA_DIR}/api/text.cc"
@ -175,7 +178,6 @@ if (BUILD_MINDDATA STREQUAL "full")
COMMAND cp -rf ${CMAKE_CURRENT_SOURCE_DIR}/example/testCifar10Data ${CMAKE_BINARY_DIR}/minddata
)
endif()
elseif (BUILD_MINDDATA STREQUAL "lite")
list(REMOVE_ITEM MINDDATA_CORE_SRC_FILES "${MINDDATA_DIR}/core/client.cc")
list(REMOVE_ITEM MINDDATA_KERNELS_SRC_FILES "${MINDDATA_DIR}/kernels/py_func_op.cc")
@ -207,6 +209,21 @@ elseif (BUILD_MINDDATA STREQUAL "lite")
mindspore::json
)
# ref: https://github.com/android/ndk/issues/1202
if (PLATFORM_ARM32)
file(GLOB_RECURSE LIBCLANG_RT_LIB $ENV{ANDROID_NDK}/libclang_rt.builtins-arm-android.a)
if (LIBCLANG_RT_LIB STREQUAL "")
MESSAGE(FATAL_ERROR "Cannot find libclang_rt.builtins-arm-androi2d.a in $ENV{ANDROID_NDK}")
endif()
target_link_libraries(minddata-lite ${LIBCLANG_RT_LIB})
endif()
elseif (BUILD_MINDDATA STREQUAL "lite_cv")
include_directories(${MINDDATA_DIR}/kernels/image)
message(STATUS ${MINDDATA_DIR}/kernels/image)
add_library(minddata-lite SHARED
${MINDDATA_KERNELS_IMAGE_LITE_CV_FILES}
)
# ref: https://github.com/android/ndk/issues/1202
if (PLATFORM_ARM32)
file(GLOB_RECURSE LIBCLANG_RT_LIB $ENV{ANDROID_NDK}/libclang_rt.builtins-arm-android.a)

@ -33,12 +33,14 @@ if(ENABLE_MINDDATA)
endif()
# fetch ut test files
if(ENABLE_MINDDATA)
include_directories(${CMAKE_SOURCE_DIR}/mindspore/ccsrc/minddata/dataset/kernels/image)
file(GLOB_RECURSE UT_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
./stub/*.cc
./common/*.cc
./abstract/*.cc
./base/*.cc
./dataset/*.cc
${CMAKE_SOURCE_DIR}/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/*.cc
./debug/*.cc
./device/*.cc
./ir/*.cc
@ -55,6 +57,7 @@ if(ENABLE_MINDDATA)
./utils/*.cc
./vm/*.cc
)
if(NOT ENABLE_PYTHON)
set(PYTHON_RELATED_SRCS
dataset/filter_op_test.cc
@ -159,6 +162,11 @@ endif()
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
target_link_libraries(ut_tests PRIVATE mindspore::gtest mindspore::event mindspore::event_pthreads mindspore_gvar ${PYTHON_LIBRARIES} pthread util dl)
if (ENABLE_MINDDATA)
# AUX_SOURCE_DIRECTORY(LITE_CV_FILES)
# message(STATUS "xxxxxxxxxxxxxxxxx"${LITE_CV_FILES} )
# add_library(_live_cv OBJECT ${LITE_CV_FILES})
target_link_libraries(ut_tests PRIVATE _c_dataengine _c_mindrecord)
endif()
else()

@ -123,6 +123,7 @@ SET(DE_UT_SRCS
swap_red_blue_test.cc
distributed_sampler_test.cc
data_helper_test.cc
image_process_test.cc
)
if (ENABLE_PYTHON)

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save