diff --git a/config.py b/config.py index 1c3e52e..78623b1 100644 --- a/config.py +++ b/config.py @@ -20,7 +20,7 @@ # angle angle_detect = True -angle_detect_num = 10 +angle_detect_num = 30 angle_net_path = os.path.join(father_path, "models/angle_net.onnx") diff --git a/models/angle_net.onnx b/models/angle_net.onnx new file mode 100755 index 0000000..f004fc1 Binary files /dev/null and b/models/angle_net.onnx differ diff --git a/models/crnn_lite_lstm.onnx b/models/crnn_lite_lstm.onnx new file mode 100644 index 0000000..504f7e1 Binary files /dev/null and b/models/crnn_lite_lstm.onnx differ diff --git a/models/dbnet.onnx b/models/dbnet.onnx new file mode 100644 index 0000000..0b1b162 Binary files /dev/null and b/models/dbnet.onnx differ diff --git a/ncnn_project/ocr/ncnn/include/ncnn/allocator.h b/ncnn_project/ocr/ncnn/include/ncnn/allocator.h deleted file mode 100644 index a70b61c..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/allocator.h +++ /dev/null @@ -1,380 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_ALLOCATOR_H -#define NCNN_ALLOCATOR_H - -#ifdef _WIN32 -#define WIN32_LEAN_AND_MEAN -#include -#else -#include -#endif - -#include -#include -#include -#include "platform.h" - -#if NCNN_VULKAN -#include -#include "gpu.h" -#endif // NCNN_VULKAN - -#if __ANDROID_API__ >= 26 -#include -#endif // __ANDROID_API__ >= 26 - -namespace ncnn { - -// the alignment of all the allocated buffers -#define MALLOC_ALIGN 16 - -// Aligns a pointer to the specified number of bytes -// ptr Aligned pointer -// n Alignment size that must be a power of two -template static inline _Tp* alignPtr(_Tp* ptr, int n=(int)sizeof(_Tp)) -{ - return (_Tp*)(((size_t)ptr + n-1) & -n); -} - -// Aligns a buffer size to the specified number of bytes -// The function returns the minimum number that is greater or equal to sz and is divisible by n -// sz Buffer size to align -// n Alignment size that must be a power of two -static inline size_t alignSize(size_t sz, int n) -{ - return (sz + n-1) & -n; -} - -static inline void* fastMalloc(size_t size) -{ -#if _MSC_VER - return _aligned_malloc(size, MALLOC_ALIGN); -#elif _POSIX_C_SOURCE >= 200112L || (__ANDROID__ && __ANDROID_API__ >= 17) - void* ptr = 0; - if (posix_memalign(&ptr, MALLOC_ALIGN, size)) - ptr = 0; - return ptr; -#elif __ANDROID__ && __ANDROID_API__ < 17 - return memalign(MALLOC_ALIGN, size); -#else - unsigned char* udata = (unsigned char*)malloc(size + sizeof(void*) + MALLOC_ALIGN); - if (!udata) - return 0; - unsigned char** adata = alignPtr((unsigned char**)udata + 1, MALLOC_ALIGN); - adata[-1] = udata; - return adata; -#endif -} - -static inline void fastFree(void* ptr) -{ - if (ptr) - { -#if _MSC_VER - _aligned_free(ptr); -#elif _POSIX_C_SOURCE >= 200112L || (__ANDROID__ && __ANDROID_API__ >= 17) - free(ptr); -#elif __ANDROID__ && __ANDROID_API__ < 17 - free(ptr); -#else - unsigned char* udata = ((unsigned char**)ptr)[-1]; - free(udata); -#endif - } -} - -// exchange-add operation for atomic operations on reference counters -#if defined __INTEL_COMPILER && !(defined WIN32 || defined _WIN32) -// atomic increment on the linux version of the Intel(tm) compiler -# define NCNN_XADD(addr, delta) (int)_InterlockedExchangeAdd(const_cast(reinterpret_cast(addr)), delta) -#elif defined __GNUC__ -# if defined __clang__ && __clang_major__ >= 3 && !defined __ANDROID__ && !defined __EMSCRIPTEN__ && !defined(__CUDACC__) -# ifdef __ATOMIC_ACQ_REL -# define NCNN_XADD(addr, delta) __c11_atomic_fetch_add((_Atomic(int)*)(addr), delta, __ATOMIC_ACQ_REL) -# else -# define NCNN_XADD(addr, delta) __atomic_fetch_add((_Atomic(int)*)(addr), delta, 4) -# endif -# else -# if defined __ATOMIC_ACQ_REL && !defined __clang__ -// version for gcc >= 4.7 -# define NCNN_XADD(addr, delta) (int)__atomic_fetch_add((unsigned*)(addr), (unsigned)(delta), __ATOMIC_ACQ_REL) -# else -# define NCNN_XADD(addr, delta) (int)__sync_fetch_and_add((unsigned*)(addr), (unsigned)(delta)) -# endif -# endif -#elif defined _MSC_VER && !defined RC_INVOKED -# include -# define NCNN_XADD(addr, delta) (int)_InterlockedExchangeAdd((long volatile*)addr, delta) -#else -// thread-unsafe branch -static inline int NCNN_XADD(int* addr, int delta) { int tmp = *addr; *addr += delta; return tmp; } -#endif - -class Allocator -{ -public: - virtual ~Allocator(); - virtual void* fastMalloc(size_t size) = 0; - virtual void fastFree(void* ptr) = 0; -}; - -class PoolAllocator : public Allocator -{ -public: - PoolAllocator(); - ~PoolAllocator(); - - // ratio range 0 ~ 1 - // default cr = 0.75 - void set_size_compare_ratio(float scr); - - // release all budgets immediately - void clear(); - - virtual void* fastMalloc(size_t size); - virtual void fastFree(void* ptr); - -private: - Mutex budgets_lock; - Mutex payouts_lock; - unsigned int size_compare_ratio;// 0~256 - std::list< std::pair > budgets; - std::list< std::pair > payouts; -}; - -class UnlockedPoolAllocator : public Allocator -{ -public: - UnlockedPoolAllocator(); - ~UnlockedPoolAllocator(); - - // ratio range 0 ~ 1 - // default cr = 0.75 - void set_size_compare_ratio(float scr); - - // release all budgets immediately - void clear(); - - virtual void* fastMalloc(size_t size); - virtual void fastFree(void* ptr); - -private: - unsigned int size_compare_ratio;// 0~256 - std::list< std::pair > budgets; - std::list< std::pair > payouts; -}; - -#if NCNN_VULKAN - -class VkBufferMemory -{ -public: - VkBuffer buffer; - - // the base offset assigned by allocator - size_t offset; - size_t capacity; - - VkDeviceMemory memory; - void* mapped_ptr; - - // buffer state, modified by command functions internally - mutable VkAccessFlags access_flags; - mutable VkPipelineStageFlags stage_flags; - - // initialize and modified by mat - int refcount; -}; - -class VkAllocator -{ -public: - VkAllocator(const VulkanDevice* _vkdev); - virtual ~VkAllocator() { clear(); } - virtual void clear() {} - virtual VkBufferMemory* fastMalloc(size_t size) = 0; - virtual void fastFree(VkBufferMemory* ptr) = 0; - virtual int flush(VkBufferMemory* ptr); - virtual int invalidate(VkBufferMemory* ptr); - -public: - const VulkanDevice* vkdev; - uint32_t memory_type_index; - bool mappable; - bool coherent; - -protected: - VkBuffer create_buffer(size_t size, VkBufferUsageFlags usage); - VkDeviceMemory allocate_memory(size_t size); - VkDeviceMemory allocate_dedicated_memory(size_t size, VkBuffer buffer); -}; - -class VkBlobBufferAllocator : public VkAllocator -{ -public: - VkBlobBufferAllocator(const VulkanDevice* vkdev); - virtual ~VkBlobBufferAllocator(); - -public: - // release all budgets immediately - virtual void clear(); - - virtual VkBufferMemory* fastMalloc(size_t size); - virtual void fastFree(VkBufferMemory* ptr); - -private: - size_t block_size; - size_t buffer_offset_alignment; - std::vector< std::list< std::pair > > budgets; - std::vector buffer_blocks; -}; - -class VkWeightBufferAllocator : public VkAllocator -{ -public: - VkWeightBufferAllocator(const VulkanDevice* vkdev); - virtual ~VkWeightBufferAllocator(); - -public: - // release all blocks immediately - virtual void clear(); - -public: - virtual VkBufferMemory* fastMalloc(size_t size); - virtual void fastFree(VkBufferMemory* ptr); - -private: - size_t block_size; - size_t buffer_offset_alignment; - std::vector buffer_block_free_spaces; - std::vector buffer_blocks; - std::vector dedicated_buffer_blocks; -}; - -class VkStagingBufferAllocator : public VkAllocator -{ -public: - VkStagingBufferAllocator(const VulkanDevice* vkdev); - virtual ~VkStagingBufferAllocator(); - -public: - // ratio range 0 ~ 1 - // default cr = 0.75 - void set_size_compare_ratio(float scr); - - // release all budgets immediately - virtual void clear(); - - virtual VkBufferMemory* fastMalloc(size_t size); - virtual void fastFree(VkBufferMemory* ptr); - -private: - unsigned int size_compare_ratio;// 0~256 - std::list budgets; -}; - -class VkWeightStagingBufferAllocator : public VkAllocator -{ -public: - VkWeightStagingBufferAllocator(const VulkanDevice* vkdev); - virtual ~VkWeightStagingBufferAllocator(); - -public: - virtual VkBufferMemory* fastMalloc(size_t size); - virtual void fastFree(VkBufferMemory* ptr); - -private: -}; - -class VkImageMemory -{ -public: - VkImage image; - VkImageView imageview; - - VkDeviceMemory memory; - - // image state, modified by command functions internally - mutable VkAccessFlags access_flags; - mutable VkPipelineStageFlags stage_flags; - - // initialize and modified by mat - int refcount; -}; - -class VkImageAllocator : public VkAllocator -{ -public: - VkImageAllocator(const VulkanDevice* _vkdev); - virtual ~VkImageAllocator() { clear(); } - virtual void clear() {} - virtual VkImageMemory* fastMalloc(int width, int height, VkFormat format) = 0; - virtual void fastFree(VkImageMemory* ptr) = 0; - -protected: - virtual VkBufferMemory* fastMalloc(size_t /*size*/) { return 0; } - virtual void fastFree(VkBufferMemory* /*ptr*/) {} - -protected: - VkImage create_image(int width, int height, VkFormat format, VkImageUsageFlags usage); - VkImageView create_imageview(VkImage image, VkFormat format); - VkDeviceMemory allocate_dedicated_memory(size_t size, VkImage image); -}; - -class VkSimpleImageAllocator : public VkImageAllocator -{ -public: - VkSimpleImageAllocator(const VulkanDevice* vkdev); - virtual ~VkSimpleImageAllocator(); - -public: - virtual VkImageMemory* fastMalloc(int width, int height, VkFormat format); - virtual void fastFree(VkImageMemory* ptr); -}; - -#if __ANDROID_API__ >= 26 -class ImportAndroidHardwareBufferPipeline; -class VkAndroidHardwareBufferImageAllocator : public VkImageAllocator -{ -public: - VkAndroidHardwareBufferImageAllocator(const VulkanDevice* _vkdev, AHardwareBuffer* _hb); - virtual ~VkAndroidHardwareBufferImageAllocator(); - -public: - virtual VkImageMemory* fastMalloc(int width, int height, VkFormat format); - virtual void fastFree(VkImageMemory* ptr); - -public: - int init(); - - int width() const; - int height() const; - uint64_t external_format() const; - -public: - AHardwareBuffer* hb; - AHardwareBuffer_Desc bufferDesc; - VkAndroidHardwareBufferFormatPropertiesANDROID bufferFormatProperties; - VkAndroidHardwareBufferPropertiesANDROID bufferProperties; - VkSamplerYcbcrConversionKHR samplerYcbcrConversion; -}; -#endif // __ANDROID_API__ >= 26 - -#endif // NCNN_VULKAN - -} // namespace ncnn - -#endif // NCNN_ALLOCATOR_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/benchmark.h b/ncnn_project/ocr/ncnn/include/ncnn/benchmark.h deleted file mode 100644 index 63de7cd..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/benchmark.h +++ /dev/null @@ -1,36 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_BENCHMARK_H -#define NCNN_BENCHMARK_H - -#include "platform.h" -#include "mat.h" -#include "layer.h" - -namespace ncnn { - -// get now timestamp in ms -double get_current_time(); - -#if NCNN_BENCHMARK - -void benchmark(const Layer* layer, double start, double end); -void benchmark(const Layer* layer, const Mat& bottom_blob, Mat& top_blob, double start, double end); - -#endif // NCNN_BENCHMARK - -} // namespace ncnn - -#endif // NCNN_BENCHMARK_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/blob.h b/ncnn_project/ocr/ncnn/include/ncnn/blob.h deleted file mode 100644 index 3dc5621..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/blob.h +++ /dev/null @@ -1,46 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_BLOB_H -#define NCNN_BLOB_H - -#include -#include -#include "platform.h" -#include "mat.h" - -namespace ncnn { - -class Blob -{ -public: - // empty - Blob(); - -public: -#if NCNN_STRING - // blob name - std::string name; -#endif // NCNN_STRING - // layer index which produce this blob as output - int producer; - // layer index which need this blob as input - std::vector consumers; - // shape hint - Mat shape; -}; - -} // namespace ncnn - -#endif // NCNN_BLOB_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/command.h b/ncnn_project/ocr/ncnn/include/ncnn/command.h deleted file mode 100644 index 133e279..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/command.h +++ /dev/null @@ -1,174 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_COMMAND_H -#define NCNN_COMMAND_H - -#include "platform.h" - -#if NCNN_VULKAN - -#include -#include -#include "mat.h" - -namespace ncnn { - -class Pipeline; -class VkCompute -{ -public: - VkCompute(const VulkanDevice* vkdev); - virtual ~VkCompute(); - -public: - void record_upload(const Mat& src, VkMat& dst, const Option& opt); - - void record_download(const VkMat& src, Mat& dst, const Option& opt); - - void record_clone(const VkMat& src, VkMat& dst, const Option& opt); - - void record_pipeline(const Pipeline* pipeline, const std::vector& bindings, const std::vector& constants, const VkMat& dispatcher); - -#if NCNN_BENCHMARK - void record_write_timestamp(uint32_t query); -#endif // NCNN_BENCHMARK - -#if __ANDROID_API__ >= 26 - void record_import_android_hardware_buffer(const ImportAndroidHardwareBufferPipeline* pipeline, const VkImageMat& src, const VkMat& dst); -#endif // __ANDROID_API__ >= 26 - - int submit_and_wait(); - - int reset(); - -#if NCNN_BENCHMARK - int create_query_pool(uint32_t query_count); - - int get_query_pool_results(uint32_t first_query, uint32_t query_count, std::vector& results); -#endif // NCNN_BENCHMARK - -protected: - int init(); - int begin_command_buffer(); - int end_command_buffer(); - -protected: - const VulkanDevice* vkdev; - - VkCommandPool compute_command_pool; - - VkCommandBuffer compute_command_buffer; - - VkFence compute_command_fence; - - std::vector upload_staging_buffers; - std::vector download_post_buffers; - std::vector download_post_mats; - - // the good-old path for device without VK_KHR_push_descriptor - std::vector descriptor_pools; - std::vector descriptorsets; - - struct record - { - enum - { - TYPE_copy_buffer, - TYPE_bind_pipeline, - TYPE_bind_descriptorsets, - TYPE_push_constants, - TYPE_dispatch, - TYPE_memory_barrers, - TYPE_buffer_barrers, - TYPE_image_barrers, - -#if NCNN_BENCHMARK - TYPE_write_timestamp, -#endif // NCNN_BENCHMARK - - TYPE_post_download, - }; - - int type; - VkCommandBuffer command_buffer; - - union - { - struct { VkBuffer src; VkBuffer dst; uint32_t region_count; const VkBufferCopy* regions; } copy_buffer; - - struct { VkPipelineBindPoint bind_point; VkPipeline pipeline; } bind_pipeline; - struct { VkPipelineBindPoint bind_point; VkPipelineLayout pipeline_layout; uint32_t descriptorset_count; uint32_t descriptorset_offset; } bind_descriptorsets; - struct { VkPipelineLayout pipeline_layout; VkShaderStageFlags stage_flags; uint32_t size; const void* values; } push_constants; - - struct { uint32_t group_count_x; uint32_t group_count_y; uint32_t group_count_z; } dispatch; - - struct { VkPipelineStageFlags src_stage; VkPipelineStageFlags dst_stage; uint32_t barrier_count; const VkMemoryBarrier* barriers; } memory_barrers; - struct { VkPipelineStageFlags src_stage; VkPipelineStageFlags dst_stage; uint32_t barrier_count; const VkBufferMemoryBarrier* barriers; } buffer_barrers; - struct { VkPipelineStageFlags src_stage; VkPipelineStageFlags dst_stage; uint32_t barrier_count; const VkImageMemoryBarrier* barriers; } image_barrers; - -#if NCNN_BENCHMARK - struct { uint32_t query; } write_timestamp; -#endif // NCNN_BENCHMARK - - struct { uint32_t download_post_buffer_mat_offset; } post_download; - }; - }; - - std::vector delayed_records; - -#if NCNN_BENCHMARK - uint32_t query_count; - VkQueryPool query_pool; -#endif // NCNN_BENCHMARK -}; - -class VkTransfer -{ -public: - VkTransfer(const VulkanDevice* vkdev); - ~VkTransfer(); - -public: - void record_upload(const Mat& src, VkMat& dst, const Option& opt); - - int submit_and_wait(); - -protected: - int init(); - int begin_command_buffer(); - int end_command_buffer(); - -protected: - const VulkanDevice* vkdev; - - VkCommandPool compute_command_pool; - VkCommandPool transfer_command_pool; - - VkCommandBuffer upload_command_buffer; - VkCommandBuffer compute_command_buffer; - - VkSemaphore upload_compute_semaphore; - - VkFence upload_command_fence; - VkFence compute_command_fence; - - std::vector upload_staging_buffers; -}; - -} // namespace ncnn - -#endif // NCNN_VULKAN - -#endif // NCNN_COMMAND_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/cpu.h b/ncnn_project/ocr/ncnn/include/ncnn/cpu.h deleted file mode 100644 index 999e745..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/cpu.h +++ /dev/null @@ -1,61 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_CPU_H -#define NCNN_CPU_H - -#include - -namespace ncnn { - -// test optional cpu features -// neon = armv7 neon or aarch64 asimd -int cpu_support_arm_neon(); -// vfpv4 = armv7 fp16 + fma -int cpu_support_arm_vfpv4(); -// asimdhp = aarch64 asimd half precision -int cpu_support_arm_asimdhp(); - -// cpu info -int get_cpu_count(); - -// bind all threads on little clusters if powersave enabled -// affacts HMP arch cpu like ARM big.LITTLE -// only implemented on android at the moment -// switching powersave is expensive and not thread-safe -// 0 = all cores enabled(default) -// 1 = only little clusters enabled -// 2 = only big clusters enabled -// return 0 if success for setter function -int get_cpu_powersave(); -int set_cpu_powersave(int powersave); - -// convenient wrapper -size_t get_cpu_thread_affinity_mask(int powersave); - -// set explicit thread affinity -int set_cpu_thread_affinity(size_t thread_affinity_mask); - -// misc function wrapper for openmp routines -int get_omp_num_threads(); -void set_omp_num_threads(int num_threads); - -int get_omp_dynamic(); -void set_omp_dynamic(int dynamic); - -int get_omp_thread_num(); - -} // namespace ncnn - -#endif // NCNN_CPU_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/datareader.h b/ncnn_project/ocr/ncnn/include/ncnn/datareader.h deleted file mode 100644 index 401c460..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/datareader.h +++ /dev/null @@ -1,93 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_DATAREADER_H -#define NCNN_DATAREADER_H - -#include -#include "platform.h" - -#if __ANDROID_API__ >= 9 -#include -#endif - -namespace ncnn { - -// data read wrapper -class DataReader -{ -public: - virtual ~DataReader(); - -#if NCNN_STRING - // parse plain param text - // return 1 if scan success - virtual int scan(const char* format, void* p) const; -#endif // NCNN_STRING - - // read binary param and model data - // return bytes read - virtual size_t read(void* buf, size_t size) const; -}; - -#if NCNN_STDIO -class DataReaderFromStdio : public DataReader -{ -public: - DataReaderFromStdio(FILE* fp); - -#if NCNN_STRING - virtual int scan(const char* format, void* p) const; -#endif // NCNN_STRING - virtual size_t read(void* buf, size_t size) const; - -protected: - FILE* fp; -}; -#endif // NCNN_STDIO - -class DataReaderFromMemory : public DataReader -{ -public: - DataReaderFromMemory(const unsigned char*& mem); - -#if NCNN_STRING - virtual int scan(const char* format, void* p) const; -#endif // NCNN_STRING - virtual size_t read(void* buf, size_t size) const; - -protected: - const unsigned char*& mem; -}; - -#if __ANDROID_API__ >= 9 -class DataReaderFromAndroidAsset : public DataReader -{ -public: - DataReaderFromAndroidAsset(AAsset* asset); - -#if NCNN_STRING - virtual int scan(const char* format, void* p) const; -#endif // NCNN_STRING - virtual size_t read(void* buf, size_t size) const; - -protected: - AAsset* asset; - mutable const unsigned char* mem; -}; -#endif // __ANDROID_API__ >= 9 - -} // namespace ncnn - -#endif // NCNN_DATAREADER_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/gpu.h b/ncnn_project/ocr/ncnn/include/ncnn/gpu.h deleted file mode 100644 index 28dd4fc..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/gpu.h +++ /dev/null @@ -1,274 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_GPU_H -#define NCNN_GPU_H - -#include "platform.h" - -#if NCNN_VULKAN - -#include -#include - -namespace ncnn { - -// instance -int create_gpu_instance(); -void destroy_gpu_instance(); - -// instance extension capability -extern int support_VK_KHR_external_memory_capabilities; -extern int support_VK_KHR_get_physical_device_properties2; -extern int support_VK_KHR_get_surface_capabilities2; -extern int support_VK_KHR_surface; -extern int support_VK_EXT_debug_utils; -#if __ANDROID_API__ >= 26 -extern int support_VK_KHR_android_surface; -#endif // __ANDROID_API__ >= 26 - -// VK_KHR_external_memory_capabilities -extern PFN_vkGetPhysicalDeviceExternalBufferPropertiesKHR vkGetPhysicalDeviceExternalBufferPropertiesKHR; - -// VK_KHR_get_physical_device_properties2 -extern PFN_vkGetPhysicalDeviceFeatures2KHR vkGetPhysicalDeviceFeatures2KHR; -extern PFN_vkGetPhysicalDeviceProperties2KHR vkGetPhysicalDeviceProperties2KHR; -extern PFN_vkGetPhysicalDeviceFormatProperties2KHR vkGetPhysicalDeviceFormatProperties2KHR; -extern PFN_vkGetPhysicalDeviceImageFormatProperties2KHR vkGetPhysicalDeviceImageFormatProperties2KHR; -extern PFN_vkGetPhysicalDeviceQueueFamilyProperties2KHR vkGetPhysicalDeviceQueueFamilyProperties2KHR; -extern PFN_vkGetPhysicalDeviceMemoryProperties2KHR vkGetPhysicalDeviceMemoryProperties2KHR; -extern PFN_vkGetPhysicalDeviceSparseImageFormatProperties2KHR vkGetPhysicalDeviceSparseImageFormatProperties2KHR; - -// VK_KHR_get_surface_capabilities2 -extern PFN_vkGetPhysicalDeviceSurfaceCapabilities2KHR vkGetPhysicalDeviceSurfaceCapabilities2KHR; -extern PFN_vkGetPhysicalDeviceSurfaceFormats2KHR vkGetPhysicalDeviceSurfaceFormats2KHR; - -// VK_KHR_surface -extern PFN_vkDestroySurfaceKHR vkDestroySurfaceKHR; -extern PFN_vkGetPhysicalDeviceSurfaceSupportKHR vkGetPhysicalDeviceSurfaceSupportKHR; -extern PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR vkGetPhysicalDeviceSurfaceCapabilitiesKHR; -extern PFN_vkGetPhysicalDeviceSurfaceFormatsKHR vkGetPhysicalDeviceSurfaceFormatsKHR; -extern PFN_vkGetPhysicalDeviceSurfacePresentModesKHR vkGetPhysicalDeviceSurfacePresentModesKHR; - -#if __ANDROID_API__ >= 26 -// VK_KHR_android_surface -extern PFN_vkCreateAndroidSurfaceKHR vkCreateAndroidSurfaceKHR; -#endif // __ANDROID_API__ >= 26 - -// get info -int get_gpu_count(); -int get_default_gpu_index(); - -class GpuInfo -{ -public: - // vulkan physical device - VkPhysicalDevice physical_device; - - // memory properties - VkPhysicalDeviceMemoryProperties physicalDeviceMemoryProperties; - - // info - uint32_t api_version; - uint32_t driver_version; - uint32_t vendor_id; - uint32_t device_id; - uint8_t pipeline_cache_uuid[VK_UUID_SIZE]; - - // 0 = discrete gpu - // 1 = integrated gpu - // 2 = virtual gpu - // 3 = cpu - int type; - - // hardware limit - uint32_t max_shared_memory_size; - uint32_t max_workgroup_count[3]; - uint32_t max_workgroup_invocations; - uint32_t max_workgroup_size[3]; - size_t memory_map_alignment; - size_t buffer_offset_alignment; - size_t non_coherent_atom_size; - float timestamp_period; - - // runtime - uint32_t compute_queue_family_index; - uint32_t graphics_queue_family_index; - uint32_t transfer_queue_family_index; - - uint32_t compute_queue_count; - uint32_t graphics_queue_count; - uint32_t transfer_queue_count; - - // property - bool unified_compute_transfer_queue; - - // bug is not feature - bool bug_local_size_spec_const; - - // but sometimes bug is a feature - bool bug_implicit_fp16_arithmetic; - - // fp16 and int8 feature - bool support_fp16_packed; - bool support_fp16_storage; - bool support_fp16_arithmetic; - bool support_int8_storage; - bool support_int8_arithmetic; - - // ycbcr conversion feature - bool support_ycbcr_conversion; - - // extension capability - int support_VK_KHR_8bit_storage; - int support_VK_KHR_16bit_storage; - int support_VK_KHR_bind_memory2; - int support_VK_KHR_dedicated_allocation; - int support_VK_KHR_descriptor_update_template; - int support_VK_KHR_external_memory; - int support_VK_KHR_get_memory_requirements2; - int support_VK_KHR_maintenance1; - int support_VK_KHR_push_descriptor; - int support_VK_KHR_sampler_ycbcr_conversion; - int support_VK_KHR_shader_float16_int8; - int support_VK_KHR_shader_float_controls; - int support_VK_KHR_storage_buffer_storage_class; - int support_VK_KHR_swapchain; - int support_VK_EXT_queue_family_foreign; -#if __ANDROID_API__ >= 26 - int support_VK_ANDROID_external_memory_android_hardware_buffer; -#endif // __ANDROID_API__ >= 26 -}; - -const GpuInfo& get_gpu_info(int device_index = get_default_gpu_index()); - -class VkAllocator; -class VulkanDevice -{ -public: - VulkanDevice(int device_index = get_default_gpu_index()); - ~VulkanDevice(); - - const GpuInfo& info; - - VkDevice vkdevice() const { return device; } - - VkShaderModule get_shader_module(int shader_type_index) const; - - // with fixed workgroup size - VkShaderModule create_shader_module(int shader_type_index, uint32_t local_size_x, uint32_t local_size_y, uint32_t local_size_z) const; - - VkShaderModule compile_shader_module(const uint32_t* spv_data, size_t spv_data_size) const; - - // with fixed workgroup size - VkShaderModule compile_shader_module(const uint32_t* spv_data, size_t spv_data_size, uint32_t local_size_x, uint32_t local_size_y, uint32_t local_size_z) const; - - uint32_t find_memory_index(uint32_t memory_type_bits, VkFlags required, VkFlags preferred, VkFlags preferred_not) const; - bool is_mappable(uint32_t memory_type_index) const; - bool is_coherent(uint32_t memory_type_index) const; - - VkQueue acquire_queue(uint32_t queue_family_index) const; - void reclaim_queue(uint32_t queue_family_index, VkQueue queue) const; - - // allocator on this device - VkAllocator* acquire_blob_allocator() const; - void reclaim_blob_allocator(VkAllocator* allocator) const; - - VkAllocator* acquire_staging_allocator() const; - void reclaim_staging_allocator(VkAllocator* allocator) const; - - // VK_KHR_bind_memory2 - PFN_vkBindBufferMemory2KHR vkBindBufferMemory2KHR; - PFN_vkBindImageMemory2KHR vkBindImageMemory2KHR; - - // VK_KHR_descriptor_update_template - PFN_vkCreateDescriptorUpdateTemplateKHR vkCreateDescriptorUpdateTemplateKHR; - PFN_vkDestroyDescriptorUpdateTemplateKHR vkDestroyDescriptorUpdateTemplateKHR; - PFN_vkUpdateDescriptorSetWithTemplateKHR vkUpdateDescriptorSetWithTemplateKHR; - - // VK_KHR_get_memory_requirements2 - PFN_vkGetImageMemoryRequirements2KHR vkGetImageMemoryRequirements2KHR; - PFN_vkGetBufferMemoryRequirements2KHR vkGetBufferMemoryRequirements2KHR; - PFN_vkGetImageSparseMemoryRequirements2KHR vkGetImageSparseMemoryRequirements2KHR; - - // VK_KHR_maintenance1 - PFN_vkTrimCommandPoolKHR vkTrimCommandPoolKHR; - - // VK_KHR_push_descriptor - PFN_vkCmdPushDescriptorSetWithTemplateKHR vkCmdPushDescriptorSetWithTemplateKHR; - PFN_vkCmdPushDescriptorSetKHR vkCmdPushDescriptorSetKHR; - - // VK_KHR_sampler_ycbcr_conversion - PFN_vkCreateSamplerYcbcrConversionKHR vkCreateSamplerYcbcrConversionKHR; - PFN_vkDestroySamplerYcbcrConversionKHR vkDestroySamplerYcbcrConversionKHR; - - // VK_KHR_swapchain - PFN_vkCreateSwapchainKHR vkCreateSwapchainKHR; - PFN_vkDestroySwapchainKHR vkDestroySwapchainKHR; - PFN_vkGetSwapchainImagesKHR vkGetSwapchainImagesKHR; - PFN_vkAcquireNextImageKHR vkAcquireNextImageKHR; - PFN_vkQueuePresentKHR vkQueuePresentKHR; - -#if __ANDROID_API__ >= 26 - // VK_ANDROID_external_memory_android_hardware_buffer - PFN_vkGetAndroidHardwareBufferPropertiesANDROID vkGetAndroidHardwareBufferPropertiesANDROID; - PFN_vkGetMemoryAndroidHardwareBufferANDROID vkGetMemoryAndroidHardwareBufferANDROID; -#endif // __ANDROID_API__ >= 26 - -protected: - // shader management - int create_shader_module(); - void destroy_shader_module(); - - // device extension - int init_device_extension(); - -private: - VkDevice device; - std::vector shader_modules; - - // hardware queue - mutable std::vector compute_queues; - mutable std::vector graphics_queues; - mutable std::vector transfer_queues; - mutable Mutex queue_lock; - - // default blob allocator for each queue - mutable std::vector blob_allocators; - mutable Mutex blob_allocator_lock; - - // default staging allocator for each queue - mutable std::vector staging_allocators; - mutable Mutex staging_allocator_lock; -}; - -VulkanDevice* get_gpu_device(int device_index = get_default_gpu_index()); - -// info from spirv -class ShaderInfo -{ -public: - int specialization_count; - int binding_count; - int push_constant_count; -}; - -const ShaderInfo& get_shader_info(int shader_type_index); -ShaderInfo resolve_shader_info(const uint32_t* spv_data, size_t spv_data_size); - -} // namespace ncnn - -#endif // NCNN_VULKAN - -#endif // NCNN_GPU_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/layer.h b/ncnn_project/ocr/ncnn/include/ncnn/layer.h deleted file mode 100644 index 7d90de3..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/layer.h +++ /dev/null @@ -1,153 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_LAYER_H -#define NCNN_LAYER_H - -#include -#include -#include -#include -#include "platform.h" -#include "mat.h" -#include "modelbin.h" -#include "option.h" -#include "paramdict.h" - -#if NCNN_VULKAN -#include -#include "command.h" -#include "pipeline.h" -#endif // NCNN_VULKAN - -namespace ncnn { - -class Layer -{ -public: - // empty - Layer(); - // virtual destructor - virtual ~Layer(); - - // load layer specific parameter from parsed dict - // return 0 if success - virtual int load_param(const ParamDict& pd); - - // load layer specific weight data from model binary - // return 0 if success - virtual int load_model(const ModelBin& mb); - - // layer implementation specific setup - // return 0 if success - virtual int create_pipeline(const Option& opt); - - // layer implementation specific clean - // return 0 if success - virtual int destroy_pipeline(const Option& opt); - -public: - // one input and one output blob - bool one_blob_only; - - // support inplace inference - bool support_inplace; - - // support vulkan compute - bool support_vulkan; - - // accept input blob with packed storage - bool support_packing; - - // accept bf16 - bool support_bf16_storage; - -public: - // implement inference - // return 0 if success - virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, const Option& opt) const; - virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const; - - // implement inplace inference - // return 0 if success - virtual int forward_inplace(std::vector& bottom_top_blobs, const Option& opt) const; - virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const; - -#if NCNN_VULKAN -public: - // upload weight blob from host to device - virtual int upload_model(VkTransfer& cmd, const Option& opt); - -public: - // implement inference - // return 0 if success - virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, VkCompute& cmd, const Option& opt) const; - virtual int forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const; - - // implement inplace inference - // return 0 if success - virtual int forward_inplace(std::vector& bottom_top_blobs, VkCompute& cmd, const Option& opt) const; - virtual int forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const; - -public: - // assigned immediately after creating this layer - const VulkanDevice* vkdev; -#endif // NCNN_VULKAN - -public: - // layer type index - int typeindex; -#if NCNN_STRING - // layer type name - std::string type; - // layer name - std::string name; -#endif // NCNN_STRING - // blob index which this layer needs as input - std::vector bottoms; - // blob index which this layer produces as output - std::vector tops; - // shape hint - std::vector bottom_shapes; - std::vector top_shapes; -}; - -// layer factory function -typedef Layer* (*layer_creator_func)(); - -struct layer_registry_entry -{ -#if NCNN_STRING - // layer type name - const char* name; -#endif // NCNN_STRING - // layer factory entry - layer_creator_func creator; -}; - -#if NCNN_STRING -// get layer type from type name -int layer_to_index(const char* type); -// create layer from type name -Layer* create_layer(const char* type); -#endif // NCNN_STRING -// create layer from layer type -Layer* create_layer(int index); - -#define DEFINE_LAYER_CREATOR(name) \ - ::ncnn::Layer* name##_layer_creator() { return new name; } - -} // namespace ncnn - -#endif // NCNN_LAYER_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/layer_type.h b/ncnn_project/ocr/ncnn/include/ncnn/layer_type.h deleted file mode 100644 index a87d9f4..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/layer_type.h +++ /dev/null @@ -1,30 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_LAYER_TYPE_H -#define NCNN_LAYER_TYPE_H - -namespace ncnn { - -namespace LayerType { -enum LayerType -{ -#include "layer_type_enum.h" - CustomBit = (1<<8), -}; -} // namespace LayerType - -} // namespace ncnn - -#endif // NCNN_LAYER_TYPE_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/layer_type_enum.h b/ncnn_project/ocr/ncnn/include/ncnn/layer_type_enum.h deleted file mode 100644 index 7450f6b..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/layer_type_enum.h +++ /dev/null @@ -1,76 +0,0 @@ -// Layer Type Enum header -// -// This file is auto-generated by cmake, don't edit it. - -AbsVal = 0, -ArgMax = 1, -BatchNorm = 2, -Bias = 3, -BNLL = 4, -Concat = 5, -Convolution = 6, -Crop = 7, -Deconvolution = 8, -Dropout = 9, -Eltwise = 10, -ELU = 11, -Embed = 12, -Exp = 13, -Flatten = 14, -InnerProduct = 15, -Input = 16, -Log = 17, -LRN = 18, -MemoryData = 19, -MVN = 20, -Pooling = 21, -Power = 22, -PReLU = 23, -Proposal = 24, -Reduction = 25, -ReLU = 26, -Reshape = 27, -ROIPooling = 28, -Scale = 29, -Sigmoid = 30, -Slice = 31, -Softmax = 32, -Split = 33, -SPP = 34, -TanH = 35, -Threshold = 36, -Tile = 37, -RNN = 38, -LSTM = 39, -BinaryOp = 40, -UnaryOp = 41, -ConvolutionDepthWise = 42, -Padding = 43, -Squeeze = 44, -ExpandDims = 45, -Normalize = 46, -Permute = 47, -PriorBox = 48, -DetectionOutput = 49, -Interp = 50, -DeconvolutionDepthWise = 51, -ShuffleChannel = 52, -InstanceNorm = 53, -Clip = 54, -Reorg = 55, -YoloDetectionOutput = 56, -Quantize = 57, -Dequantize = 58, -Yolov3DetectionOutput = 59, -PSROIPooling = 60, -ROIAlign = 61, -Packing = 62, -Requantize = 63, -Cast = 64, -HardSigmoid = 65, -SELU = 66, -HardSwish = 67, -Noop = 68, -PixelShuffle = 69, -DeepCopy = 70, - diff --git a/ncnn_project/ocr/ncnn/include/ncnn/mat.h b/ncnn_project/ocr/ncnn/include/ncnn/mat.h deleted file mode 100644 index 6a615ad..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/mat.h +++ /dev/null @@ -1,1748 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_MAT_H -#define NCNN_MAT_H - -#include -#include -#include -#if __ARM_NEON -#include -#endif -#include "platform.h" -#include "allocator.h" -#include "option.h" -#include "gpu.h" - -#if NCNN_VULKAN -#include -#endif // NCNN_VULKAN - -#if NCNN_PIXEL -#if __ANDROID_API__ >= 9 -#include -#include -#endif // __ANDROID_API__ >= 9 -#endif // NCNN_PIXEL - -namespace ncnn { - -#if NCNN_VULKAN -class VkMat; -#endif // NCNN_VULKAN - -// the three dimension matrix -class Mat -{ -public: - // empty - Mat(); - // vec - Mat(int w, size_t elemsize = 4u, Allocator* allocator = 0); - // image - Mat(int w, int h, size_t elemsize = 4u, Allocator* allocator = 0); - // dim - Mat(int w, int h, int c, size_t elemsize = 4u, Allocator* allocator = 0); - // packed vec - Mat(int w, size_t elemsize, int elempack, Allocator* allocator = 0); - // packed image - Mat(int w, int h, size_t elemsize, int elempack, Allocator* allocator = 0); - // packed dim - Mat(int w, int h, int c, size_t elemsize, int elempack, Allocator* allocator = 0); - // copy - Mat(const Mat& m); - // external vec - Mat(int w, void* data, size_t elemsize = 4u, Allocator* allocator = 0); - // external image - Mat(int w, int h, void* data, size_t elemsize = 4u, Allocator* allocator = 0); - // external dim - Mat(int w, int h, int c, void* data, size_t elemsize = 4u, Allocator* allocator = 0); - // external packed vec - Mat(int w, void* data, size_t elemsize, int elempack, Allocator* allocator = 0); - // external packed image - Mat(int w, int h, void* data, size_t elemsize, int elempack, Allocator* allocator = 0); - // external packed dim - Mat(int w, int h, int c, void* data, size_t elemsize, int elempack, Allocator* allocator = 0); - // release - ~Mat(); - // assign - Mat& operator=(const Mat& m); - // set all - void fill(float v); - void fill(int v); -#if __ARM_NEON - void fill(float32x4_t _v); -#endif // __ARM_NEON - template void fill(T v); - // deep copy - Mat clone(Allocator* allocator = 0) const; - // reshape vec - Mat reshape(int w, Allocator* allocator = 0) const; - // reshape image - Mat reshape(int w, int h, Allocator* allocator = 0) const; - // reshape dim - Mat reshape(int w, int h, int c, Allocator* allocator = 0) const; - // allocate vec - void create(int w, size_t elemsize = 4u, Allocator* allocator = 0); - // allocate image - void create(int w, int h, size_t elemsize = 4u, Allocator* allocator = 0); - // allocate dim - void create(int w, int h, int c, size_t elemsize = 4u, Allocator* allocator = 0); - // allocate packed vec - void create(int w, size_t elemsize, int elempack, Allocator* allocator = 0); - // allocate packed image - void create(int w, int h, size_t elemsize, int elempack, Allocator* allocator = 0); - // allocate packed dim - void create(int w, int h, int c, size_t elemsize, int elempack, Allocator* allocator = 0); - // allocate like - void create_like(const Mat& m, Allocator* allocator = 0); -#if NCNN_VULKAN - // allocate like - void create_like(const VkMat& m, Allocator* allocator = 0); -#endif // NCNN_VULKAN - // refcount++ - void addref(); - // refcount-- - void release(); - - bool empty() const; - size_t total() const; - - // shape only - Mat shape() const; - - // data reference - Mat channel(int c); - const Mat channel(int c) const; - float* row(int y); - const float* row(int y) const; - template T* row(int y); - template const T* row(int y) const; - - // range reference - Mat channel_range(int c, int channels); - const Mat channel_range(int c, int channels) const; - Mat row_range(int y, int rows); - const Mat row_range(int y, int rows) const; - Mat range(int x, int n); - const Mat range(int x, int n) const; - - // access raw data - template operator T*(); - template operator const T*() const; - - // convenient access float vec element - float& operator[](size_t i); - const float& operator[](size_t i) const; - -#if NCNN_PIXEL - enum PixelType - { - PIXEL_CONVERT_SHIFT = 16, - PIXEL_FORMAT_MASK = 0x0000ffff, - PIXEL_CONVERT_MASK = 0xffff0000, - - PIXEL_RGB = 1, - PIXEL_BGR = 2, - PIXEL_GRAY = 3, - PIXEL_RGBA = 4, - PIXEL_BGRA = 5, - - PIXEL_RGB2BGR = PIXEL_RGB | (PIXEL_BGR << PIXEL_CONVERT_SHIFT), - PIXEL_RGB2GRAY = PIXEL_RGB | (PIXEL_GRAY << PIXEL_CONVERT_SHIFT), - PIXEL_RGB2RGBA = PIXEL_RGB | (PIXEL_RGBA << PIXEL_CONVERT_SHIFT), - PIXEL_RGB2BGRA = PIXEL_RGB | (PIXEL_BGRA << PIXEL_CONVERT_SHIFT), - - PIXEL_BGR2RGB = PIXEL_BGR | (PIXEL_RGB << PIXEL_CONVERT_SHIFT), - PIXEL_BGR2GRAY = PIXEL_BGR | (PIXEL_GRAY << PIXEL_CONVERT_SHIFT), - PIXEL_BGR2RGBA = PIXEL_BGR | (PIXEL_RGBA << PIXEL_CONVERT_SHIFT), - PIXEL_BGR2BGRA = PIXEL_BGR | (PIXEL_BGRA << PIXEL_CONVERT_SHIFT), - - PIXEL_GRAY2RGB = PIXEL_GRAY | (PIXEL_RGB << PIXEL_CONVERT_SHIFT), - PIXEL_GRAY2BGR = PIXEL_GRAY | (PIXEL_BGR << PIXEL_CONVERT_SHIFT), - PIXEL_GRAY2RGBA = PIXEL_GRAY | (PIXEL_RGBA << PIXEL_CONVERT_SHIFT), - PIXEL_GRAY2BGRA = PIXEL_GRAY | (PIXEL_BGRA << PIXEL_CONVERT_SHIFT), - - PIXEL_RGBA2RGB = PIXEL_RGBA | (PIXEL_RGB << PIXEL_CONVERT_SHIFT), - PIXEL_RGBA2BGR = PIXEL_RGBA | (PIXEL_BGR << PIXEL_CONVERT_SHIFT), - PIXEL_RGBA2GRAY = PIXEL_RGBA | (PIXEL_GRAY << PIXEL_CONVERT_SHIFT), - PIXEL_RGBA2BGRA = PIXEL_RGBA | (PIXEL_BGRA << PIXEL_CONVERT_SHIFT), - - PIXEL_BGRA2RGB = PIXEL_BGRA | (PIXEL_RGB << PIXEL_CONVERT_SHIFT), - PIXEL_BGRA2BGR = PIXEL_BGRA | (PIXEL_BGR << PIXEL_CONVERT_SHIFT), - PIXEL_BGRA2GRAY = PIXEL_BGRA | (PIXEL_GRAY << PIXEL_CONVERT_SHIFT), - PIXEL_BGRA2RGBA = PIXEL_BGRA | (PIXEL_RGBA << PIXEL_CONVERT_SHIFT), - }; - // convenient construct from pixel data - static Mat from_pixels(const unsigned char* pixels, int type, int w, int h, Allocator* allocator = 0); - // convenient construct from pixel data with stride(bytes-per-row) parameter - static Mat from_pixels(const unsigned char* pixels, int type, int w, int h, int stride, Allocator* allocator = 0); - // convenient construct from pixel data and resize to specific size - static Mat from_pixels_resize(const unsigned char* pixels, int type, int w, int h, int target_width, int target_height, Allocator* allocator = 0); - // convenient construct from pixel data and resize to specific size with stride(bytes-per-row) parameter - static Mat from_pixels_resize(const unsigned char* pixels, int type, int w, int h, int stride, int target_width, int target_height, Allocator* allocator = 0); - - // convenient export to pixel data - void to_pixels(unsigned char* pixels, int type) const; - // convenient export to pixel data with stride(bytes-per-row) parameter - void to_pixels(unsigned char* pixels, int type, int stride) const; - // convenient export to pixel data and resize to specific size - void to_pixels_resize(unsigned char* pixels, int type, int target_width, int target_height) const; - // convenient export to pixel data and resize to specific size with stride(bytes-per-row) parameter - void to_pixels_resize(unsigned char* pixels, int type, int target_width, int target_height, int target_stride) const; - -#if __ANDROID_API__ >= 9 - // convenient construct from android Bitmap - static Mat from_android_bitmap(JNIEnv* env, jobject bitmap, int type_to, Allocator* allocator = 0); - // convenient construct from android Bitmap and resize to specific size - static Mat from_android_bitmap_resize(JNIEnv* env, jobject bitmap, int type_to, int target_width, int target_height, Allocator* allocator = 0); - // convenient export to android Bitmap and resize to the android Bitmap size - void to_android_bitmap(JNIEnv* env, jobject bitmap, int type_from) const; -#endif // __ANDROID_API__ >= 9 -#endif // NCNN_PIXEL - - // substract channel-wise mean values, then multiply by normalize values, pass 0 to skip - void substract_mean_normalize(const float* mean_vals, const float* norm_vals); - - // convenient construct from half precisoin floating point data - static Mat from_float16(const unsigned short* data, int size); - - // pointer to the data - void* data; - - // pointer to the reference counter - // when points to user-allocated data, the pointer is NULL - int* refcount; - - // element size in bytes - // 4 = float32/int32 - // 2 = float16 - // 1 = int8/uint8 - // 0 = empty - size_t elemsize; - - // packed count inside element - // c/1-h-w-1 h/1-w-1 w/1-1 scalar - // c/4-h-w-4 h/4-w-4 w/4-4 sse/neon - // c/8-h-w-8 h/8-w-8 w/8-8 avx/fp16 - int elempack; - - // the allocator - Allocator* allocator; - - // the dimension rank - int dims; - - int w; - int h; - int c; - - size_t cstep; -}; - -#if NCNN_VULKAN - -// the three dimension matrix, vulkan version -class VkMat -{ -public: - // empty - VkMat(); - // vec - VkMat(int w, size_t elemsize, VkAllocator* allocator); - // image - VkMat(int w, int h, size_t elemsize, VkAllocator* allocator); - // dim - VkMat(int w, int h, int c, size_t elemsize, VkAllocator* allocator); - // packed vec - VkMat(int w, size_t elemsize, int elempack, VkAllocator* allocator); - // packed image - VkMat(int w, int h, size_t elemsize, int elempack, VkAllocator* allocator); - // packed dim - VkMat(int w, int h, int c, size_t elemsize, int elempack, VkAllocator* allocator); - // copy - VkMat(const VkMat& m); - // external vec - VkMat(int w, VkBufferMemory* data, size_t elemsize, VkAllocator* allocator); - // external image - VkMat(int w, int h, VkBufferMemory* data, size_t elemsize, VkAllocator* allocator); - // external dim - VkMat(int w, int h, int c, VkBufferMemory* data, size_t elemsize, VkAllocator* allocator); - // external packed vec - VkMat(int w, VkBufferMemory* data, size_t elemsize, int elempack, VkAllocator* allocator); - // external packed image - VkMat(int w, int h, VkBufferMemory* data, size_t elemsize, int elempack, VkAllocator* allocator); - // external packed dim - VkMat(int w, int h, int c, VkBufferMemory* data, size_t elemsize, int elempack, VkAllocator* allocator); - // release - ~VkMat(); - // assign - VkMat& operator=(const VkMat& m); - // allocate vec - void create(int w, size_t elemsize, VkAllocator* allocator); - // allocate image - void create(int w, int h, size_t elemsize, VkAllocator* allocator); - // allocate dim - void create(int w, int h, int c, size_t elemsize, VkAllocator* allocator); - // allocate packed vec - void create(int w, size_t elemsize, int elempack, VkAllocator* allocator); - // allocate packed image - void create(int w, int h, size_t elemsize, int elempack, VkAllocator* allocator); - // allocate packed dim - void create(int w, int h, int c, size_t elemsize, int elempack, VkAllocator* allocator); - // allocate like - void create_like(const Mat& m, VkAllocator* allocator); - // allocate like - void create_like(const VkMat& m, VkAllocator* allocator); - - // mapped - Mat mapped() const; - void* mapped_ptr() const; - - // refcount++ - void addref(); - // refcount-- - void release(); - - bool empty() const; - size_t total() const; - - // shape only - Mat shape() const; - - // low-level reference - VkBuffer buffer() const; - size_t buffer_offset() const; - size_t buffer_capacity() const; - - // device buffer - VkBufferMemory* data; - - // pointer to the reference counter - // when points to user-allocated data, the pointer is NULL - int* refcount; - - // element size in bytes - // 4 = float32/int32 - // 2 = float16 - // 1 = int8/uint8 - // 0 = empty - size_t elemsize; - - // packed count inside element - // c/1-h-w-1 h/1-w-1 w/1-1 scalar - // c/4-h-w-4 h/4-w-4 w/4-4 sse/neon - // c/8-h-w-8 h/8-w-8 w/8-8 avx/fp16 - int elempack; - - // the allocator - VkAllocator* allocator; - - // the dimension rank - int dims; - - int w; - int h; - int c; - - size_t cstep; -}; - -class VkImageMat -{ -public: - // empty - VkImageMat(); - // image - VkImageMat(int width, int height, VkFormat format, VkImageAllocator* allocator); - // copy - VkImageMat(const VkImageMat& m); - // external image - VkImageMat(int width, int height, VkImageMemory* data, VkFormat format, VkImageAllocator* allocator); - // release - ~VkImageMat(); - // assign - VkImageMat& operator=(const VkImageMat& m); - // allocate image - void create(int width, int height, VkFormat format, VkImageAllocator* allocator); - - // refcount++ - void addref(); - // refcount-- - void release(); - - bool empty() const; - size_t total() const; - - // low-level reference - VkImage image() const; - VkImageView imageview() const; - -#if __ANDROID_API__ >= 26 - // convenient construct from android hardware buffer - static VkImageMat from_android_hardware_buffer(VkAndroidHardwareBufferImageAllocator* allocator); -#endif // __ANDROID_API__ >= 26 - - // device image - VkImageMemory* data; - - // pointer to the reference counter - // when points to user-allocated data, the pointer is NULL - int* refcount; - - // the allocator - VkImageAllocator* allocator; - - int width; - int height; - VkFormat format; -}; - -// type for vulkan specialization constant and push constant -union vk_specialization_type { int i; float f; uint32_t u32; }; -union vk_constant_type { int i; float f; }; -#endif // NCNN_VULKAN - -// misc function -#if NCNN_PIXEL -// convert yuv420sp(nv21) to rgb, the fast approximate version -void yuv420sp2rgb(const unsigned char* yuv420sp, int w, int h, unsigned char* rgb); -// image pixel bilinear resize -void resize_bilinear_c1(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h); -void resize_bilinear_c2(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h); -void resize_bilinear_c3(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h); -void resize_bilinear_c4(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h); -// image pixel bilinear resize with stride(bytes-per-row) parameter -void resize_bilinear_c1(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride); -void resize_bilinear_c2(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride); -void resize_bilinear_c3(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride); -void resize_bilinear_c4(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride); -// image pixel bilinear resize, convenient wrapper for yuv420sp(nv21) -void resize_bilinear_yuv420sp(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h); -#endif // NCNN_PIXEL -#if NCNN_PIXEL_ROTATE -// type is the from type, 6 means rotating from 6 to 1 -// -// 1 2 3 4 5 6 7 8 -// -// 888888 888888 88 88 8888888888 88 88 8888888888 -// 88 88 88 88 88 88 88 88 88 88 88 88 -// 8888 8888 8888 8888 88 8888888888 8888888888 88 -// 88 88 88 88 -// 88 88 888888 888888 -// -// ref http://sylvana.net/jpegcrop/exif_orientation.html -// image pixel kanna rotate -void kanna_rotate_c1(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, int type); -void kanna_rotate_c2(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, int type); -void kanna_rotate_c3(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, int type); -void kanna_rotate_c4(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, int type); -// image pixel kanna rotate with stride(bytes-per-row) parameter -void kanna_rotate_c1(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, int type); -void kanna_rotate_c2(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, int type); -void kanna_rotate_c3(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, int type); -void kanna_rotate_c4(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, int type); -// image pixel kanna rotate, convenient wrapper for yuv420sp(nv21) -void kanna_rotate_yuv420sp(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, int type); -#endif // NCNN_PIXEL_ROTATE - -// type conversion -// convert float to half precision floating point -unsigned short float32_to_float16(float value); -// convert half precision floating point to float -float float16_to_float32(unsigned short value); -// convert float to brain half -inline unsigned short float32_to_bfloat16(float value) -{ - // 16 : 16 - union { unsigned int u; float f; } tmp; - tmp.f = value; - return tmp.u >> 16; -} -// convert brain half to float -inline float bfloat16_to_float32(unsigned short value) -{ - // 16 : 16 - union { unsigned int u; float f; } tmp; - tmp.u = value << 16; - return tmp.f; -} - -// mat process -enum BorderType -{ - BORDER_CONSTANT = 0, - BORDER_REPLICATE = 1, -}; -void copy_make_border(const Mat& src, Mat& dst, int top, int bottom, int left, int right, int type, float v, const Option& opt = Option()); -void copy_cut_border(const Mat& src, Mat& dst, int top, int bottom, int left, int right, const Option& opt = Option()); -void resize_bilinear(const Mat& src, Mat& dst, int w, int h, const Option& opt = Option()); -void resize_bicubic(const Mat& src, Mat& dst, int w, int h, const Option& opt = Option()); -void convert_packing(const Mat& src, Mat& dst, int elempack, const Option& opt = Option()); -void cast_float32_to_float16(const Mat& src, Mat& dst, const Option& opt = Option()); -void cast_float16_to_float32(const Mat& src, Mat& dst, const Option& opt = Option()); -void cast_int8_to_float32(const Mat& src, Mat& dst, const Option& opt = Option()); -void cast_float32_to_bfloat16(const Mat& src, Mat& dst, const Option& opt = Option()); -void cast_bfloat16_to_float32(const Mat& src, Mat& dst, const Option& opt = Option()); -void quantize_float32_to_int8(const Mat& src, Mat& dst, float scale, const Option& opt = Option()); -void dequantize_int32_to_float32(Mat& m, float scale, const float* bias, int bias_data_size, const Option& opt = Option()); -void requantize_int8_to_int8(const Mat& src, Mat& dst, float scale_in, float scale_out, const float* bias, int bias_data_size, int fusion_relu, const Option& opt = Option()); - -inline Mat::Mat() - : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0) -{ -} - -inline Mat::Mat(int _w, size_t _elemsize, Allocator* _allocator) - : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0) -{ - create(_w, _elemsize, _allocator); -} - -inline Mat::Mat(int _w, int _h, size_t _elemsize, Allocator* _allocator) - : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0) -{ - create(_w, _h, _elemsize, _allocator); -} - -inline Mat::Mat(int _w, int _h, int _c, size_t _elemsize, Allocator* _allocator) - : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0) -{ - create(_w, _h, _c, _elemsize, _allocator); -} - -inline Mat::Mat(int _w, size_t _elemsize, int _elempack, Allocator* _allocator) - : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0) -{ - create(_w, _elemsize, _elempack, _allocator); -} - -inline Mat::Mat(int _w, int _h, size_t _elemsize, int _elempack, Allocator* _allocator) - : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0) -{ - create(_w, _h, _elemsize, _elempack, _allocator); -} - -inline Mat::Mat(int _w, int _h, int _c, size_t _elemsize, int _elempack, Allocator* _allocator) - : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0) -{ - create(_w, _h, _c, _elemsize, _elempack, _allocator); -} - -inline Mat::Mat(const Mat& m) - : data(m.data), refcount(m.refcount), elemsize(m.elemsize), elempack(m.elempack), allocator(m.allocator), dims(m.dims), w(m.w), h(m.h), c(m.c), cstep(m.cstep) -{ - if (refcount) - NCNN_XADD(refcount, 1); -} - -inline Mat::Mat(int _w, void* _data, size_t _elemsize, Allocator* _allocator) - : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(1), w(_w), h(1), c(1) -{ - cstep = w; -} - -inline Mat::Mat(int _w, int _h, void* _data, size_t _elemsize, Allocator* _allocator) - : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(2), w(_w), h(_h), c(1) -{ - cstep = w * h; -} - -inline Mat::Mat(int _w, int _h, int _c, void* _data, size_t _elemsize, Allocator* _allocator) - : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(3), w(_w), h(_h), c(_c) -{ - cstep = alignSize(w * h * elemsize, 16) / elemsize; -} - -inline Mat::Mat(int _w, void* _data, size_t _elemsize, int _elempack, Allocator* _allocator) - : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(1), w(_w), h(1), c(1) -{ - cstep = w; -} - -inline Mat::Mat(int _w, int _h, void* _data, size_t _elemsize, int _elempack, Allocator* _allocator) - : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(2), w(_w), h(_h), c(1) -{ - cstep = w * h; -} - -inline Mat::Mat(int _w, int _h, int _c, void* _data, size_t _elemsize, int _elempack, Allocator* _allocator) - : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(3), w(_w), h(_h), c(_c) -{ - cstep = alignSize(w * h * elemsize, 16) / elemsize; -} - -inline Mat::~Mat() -{ - release(); -} - -inline Mat& Mat::operator=(const Mat& m) -{ - if (this == &m) - return *this; - - if (m.refcount) - NCNN_XADD(m.refcount, 1); - - release(); - - data = m.data; - refcount = m.refcount; - elemsize = m.elemsize; - elempack = m.elempack; - allocator = m.allocator; - - dims = m.dims; - w = m.w; - h = m.h; - c = m.c; - - cstep = m.cstep; - - return *this; -} - -inline void Mat::fill(float _v) -{ - int size = (int)total(); - float* ptr = (float*)data; - -#if __ARM_NEON - int nn = size >> 2; - int remain = size - (nn << 2); -#else - int remain = size; -#endif // __ARM_NEON - -#if __ARM_NEON - float32x4_t _c = vdupq_n_f32(_v); -#if __aarch64__ - if (nn > 0) - { - asm volatile ( - "0: \n" - "subs %w0, %w0, #1 \n" - "st1 {%4.4s}, [%1], #16 \n" - "bne 0b \n" - : "=r"(nn), // %0 - "=r"(ptr) // %1 - : "0"(nn), - "1"(ptr), - "w"(_c) // %4 - : "cc", "memory" - ); - } -#else - if (nn > 0) - { - asm volatile( - "0: \n" - "subs %0, #1 \n" - "vst1.f32 {%e4-%f4}, [%1 :128]!\n" - "bne 0b \n" - : "=r"(nn), // %0 - "=r"(ptr) // %1 - : "0"(nn), - "1"(ptr), - "w"(_c) // %4 - : "cc", "memory" - ); - } -#endif // __aarch64__ -#endif // __ARM_NEON - for (; remain>0; remain--) - { - *ptr++ = _v; - } -} - -inline void Mat::fill(int _v) -{ - int size = (int)total(); - int* ptr = (int*)data; - -#if __ARM_NEON - int nn = size >> 2; - int remain = size - (nn << 2); -#else - int remain = size; -#endif // __ARM_NEON - -#if __ARM_NEON - int32x4_t _c = vdupq_n_s32(_v); -#if __aarch64__ - if (nn > 0) - { - asm volatile ( - "0: \n" - "subs %w0, %w0, #1 \n" - "st1 {%4.4s}, [%1], #16 \n" - "bne 0b \n" - : "=r"(nn), // %0 - "=r"(ptr) // %1 - : "0"(nn), - "1"(ptr), - "w"(_c) // %4 - : "cc", "memory" - ); - } -#else - if (nn > 0) - { - asm volatile( - "0: \n" - "subs %0, #1 \n" - "vst1.s32 {%e4-%f4}, [%1 :128]!\n" - "bne 0b \n" - : "=r"(nn), // %0 - "=r"(ptr) // %1 - : "0"(nn), - "1"(ptr), - "w"(_c) // %4 - : "cc", "memory" - ); - } -#endif // __aarch64__ -#endif // __ARM_NEON - for (; remain>0; remain--) - { - *ptr++ = _v; - } -} - -#if __ARM_NEON -inline void Mat::fill(float32x4_t _v) -{ - int size = total(); - float* ptr = (float*)data; - for (int i=0; i -inline void Mat::fill(T _v) -{ - int size = total(); - T* ptr = (T*)data; - for (int i=0; i 0) - { - memcpy(m.data, data, total() * elemsize); - } - - return m; -} - -inline Mat Mat::reshape(int _w, Allocator* _allocator) const -{ - if (w * h * c != _w) - return Mat(); - - if (dims == 3 && cstep != (size_t)w * h) - { - Mat m; - m.create(_w, elemsize, elempack, _allocator); - - // flatten - for (int i=0; i 0) - { - size_t totalsize = alignSize(total() * elemsize, 4); - if (allocator) - data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount)); - else - data = fastMalloc(totalsize + (int)sizeof(*refcount)); - refcount = (int*)(((unsigned char*)data) + totalsize); - *refcount = 1; - } -} - -inline void Mat::create(int _w, int _h, size_t _elemsize, Allocator* _allocator) -{ - if (dims == 2 && w == _w && h == _h && elemsize == _elemsize && elempack == 1 && allocator == _allocator) - return; - - release(); - - elemsize = _elemsize; - elempack = 1; - allocator = _allocator; - - dims = 2; - w = _w; - h = _h; - c = 1; - - cstep = w * h; - - if (total() > 0) - { - size_t totalsize = alignSize(total() * elemsize, 4); - if (allocator) - data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount)); - else - data = fastMalloc(totalsize + (int)sizeof(*refcount)); - refcount = (int*)(((unsigned char*)data) + totalsize); - *refcount = 1; - } -} - -inline void Mat::create(int _w, int _h, int _c, size_t _elemsize, Allocator* _allocator) -{ - if (dims == 3 && w == _w && h == _h && c == _c && elemsize == _elemsize && elempack == 1 && allocator == _allocator) - return; - - release(); - - elemsize = _elemsize; - elempack = 1; - allocator = _allocator; - - dims = 3; - w = _w; - h = _h; - c = _c; - - cstep = alignSize(w * h * elemsize, 16) / elemsize; - - if (total() > 0) - { - size_t totalsize = alignSize(total() * elemsize, 4); - if (allocator) - data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount)); - else - data = fastMalloc(totalsize + (int)sizeof(*refcount)); - refcount = (int*)(((unsigned char*)data) + totalsize); - *refcount = 1; - } -} - -inline void Mat::create(int _w, size_t _elemsize, int _elempack, Allocator* _allocator) -{ - if (dims == 1 && w == _w && elemsize == _elemsize && elempack == _elempack && allocator == _allocator) - return; - - release(); - - elemsize = _elemsize; - elempack = _elempack; - allocator = _allocator; - - dims = 1; - w = _w; - h = 1; - c = 1; - - cstep = w; - - if (total() > 0) - { - size_t totalsize = alignSize(total() * elemsize, 4); - if (allocator) - data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount)); - else - data = fastMalloc(totalsize + (int)sizeof(*refcount)); - refcount = (int*)(((unsigned char*)data) + totalsize); - *refcount = 1; - } -} - -inline void Mat::create(int _w, int _h, size_t _elemsize, int _elempack, Allocator* _allocator) -{ - if (dims == 2 && w == _w && h == _h && elemsize == _elemsize && elempack == _elempack && allocator == _allocator) - return; - - release(); - - elemsize = _elemsize; - elempack = _elempack; - allocator = _allocator; - - dims = 2; - w = _w; - h = _h; - c = 1; - - cstep = w * h; - - if (total() > 0) - { - size_t totalsize = alignSize(total() * elemsize, 4); - if (allocator) - data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount)); - else - data = fastMalloc(totalsize + (int)sizeof(*refcount)); - refcount = (int*)(((unsigned char*)data) + totalsize); - *refcount = 1; - } -} - -inline void Mat::create(int _w, int _h, int _c, size_t _elemsize, int _elempack, Allocator* _allocator) -{ - if (dims == 3 && w == _w && h == _h && c == _c && elemsize == _elemsize && elempack == _elempack && allocator == _allocator) - return; - - release(); - - elemsize = _elemsize; - elempack = _elempack; - allocator = _allocator; - - dims = 3; - w = _w; - h = _h; - c = _c; - - cstep = alignSize(w * h * elemsize, 16) / elemsize; - - if (total() > 0) - { - size_t totalsize = alignSize(total() * elemsize, 4); - if (allocator) - data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount)); - else - data = fastMalloc(totalsize + (int)sizeof(*refcount)); - refcount = (int*)(((unsigned char*)data) + totalsize); - *refcount = 1; - } -} - -inline void Mat::create_like(const Mat& m, Allocator* _allocator) -{ - int _dims = m.dims; - if (_dims == 1) - create(m.w, m.elemsize, m.elempack, _allocator); - if (_dims == 2) - create(m.w, m.h, m.elemsize, m.elempack, _allocator); - if (_dims == 3) - create(m.w, m.h, m.c, m.elemsize, m.elempack, _allocator); -} - -#if NCNN_VULKAN -inline void Mat::create_like(const VkMat& m, Allocator* _allocator) -{ - int _dims = m.dims; - if (_dims == 1) - create(m.w, m.elemsize, m.elempack, _allocator); - if (_dims == 2) - create(m.w, m.h, m.elemsize, m.elempack, _allocator); - if (_dims == 3) - create(m.w, m.h, m.c, m.elemsize, m.elempack, _allocator); -} -#endif // NCNN_VULKAN - -inline void Mat::addref() -{ - if (refcount) - NCNN_XADD(refcount, 1); -} - -inline void Mat::release() -{ - if (refcount && NCNN_XADD(refcount, -1) == 1) - { - if (allocator) - allocator->fastFree(data); - else - fastFree(data); - } - - data = 0; - - elemsize = 0; - elempack = 0; - - dims = 0; - w = 0; - h = 0; - c = 0; - - cstep = 0; - - refcount = 0; -} - -inline bool Mat::empty() const -{ - return data == 0 || total() == 0; -} - -inline size_t Mat::total() const -{ - return cstep * c; -} - -inline Mat Mat::shape() const -{ - if (dims == 1) - return Mat(w * elempack, (void*)0); - if (dims == 2) - return Mat(w, h * elempack, (void*)0); - if (dims == 3) - return Mat(w, h, c * elempack, (void*)0); - - return Mat(); -} - -inline Mat Mat::channel(int _c) -{ - return Mat(w, h, (unsigned char*)data + cstep * _c * elemsize, elemsize, elempack, allocator); -} - -inline const Mat Mat::channel(int _c) const -{ - return Mat(w, h, (unsigned char*)data + cstep * _c * elemsize, elemsize, elempack, allocator); -} - -inline float* Mat::row(int y) -{ - return (float*)((unsigned char*)data + w * y * elemsize); -} - -inline const float* Mat::row(int y) const -{ - return (const float*)((unsigned char*)data + w * y * elemsize); -} - -template -inline T* Mat::row(int y) -{ - return (T*)((unsigned char*)data + w * y * elemsize); -} - -template -inline const T* Mat::row(int y) const -{ - return (const T*)((unsigned char*)data + w * y * elemsize); -} - -inline Mat Mat::channel_range(int _c, int channels) -{ - return Mat(w, h, channels, (unsigned char*)data + cstep * _c * elemsize, elemsize, elempack, allocator); -} - -inline const Mat Mat::channel_range(int _c, int channels) const -{ - return Mat(w, h, channels, (unsigned char*)data + cstep * _c * elemsize, elemsize, elempack, allocator); -} - -inline Mat Mat::row_range(int y, int rows) -{ - return Mat(w, rows, (unsigned char*)data + w * y * elemsize, elemsize, elempack, allocator); -} - -inline const Mat Mat::row_range(int y, int rows) const -{ - return Mat(w, rows, (unsigned char*)data + w * y * elemsize, elemsize, elempack, allocator); -} - -inline Mat Mat::range(int x, int n) -{ - return Mat(n, (unsigned char*)data + x * elemsize, elemsize, elempack, allocator); -} - -inline const Mat Mat::range(int x, int n) const -{ - return Mat(n, (unsigned char*)data + x * elemsize, elemsize, elempack, allocator); -} - -template -inline Mat::operator T*() -{ - return (T*)data; -} - -template -inline Mat::operator const T*() const -{ - return (const T*)data; -} - -inline float& Mat::operator[](size_t i) -{ - return ((float*)data)[i]; -} - -inline const float& Mat::operator[](size_t i) const -{ - return ((const float*)data)[i]; -} - -#if NCNN_VULKAN - -inline VkMat::VkMat() - : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0) -{ -} - -inline VkMat::VkMat(int _w, size_t _elemsize, VkAllocator* _allocator) - : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0) -{ - create(_w, _elemsize, _allocator); -} - -inline VkMat::VkMat(int _w, int _h, size_t _elemsize, VkAllocator* _allocator) - : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0) -{ - create(_w, _h, _elemsize, _allocator); -} - -inline VkMat::VkMat(int _w, int _h, int _c, size_t _elemsize, VkAllocator* _allocator) - : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0) -{ - create(_w, _h, _c, _elemsize, _allocator); -} - -inline VkMat::VkMat(int _w, size_t _elemsize, int _elempack, VkAllocator* _allocator) - : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0) -{ - create(_w, _elemsize, _elempack, _allocator); -} - -inline VkMat::VkMat(int _w, int _h, size_t _elemsize, int _elempack, VkAllocator* _allocator) - : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0) -{ - create(_w, _h, _elemsize, _elempack, _allocator); -} - -inline VkMat::VkMat(int _w, int _h, int _c, size_t _elemsize, int _elempack, VkAllocator* _allocator) - : data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0) -{ - create(_w, _h, _c, _elemsize, _elempack, _allocator); -} - -inline VkMat::VkMat(const VkMat& m) - : data(m.data), refcount(m.refcount), elemsize(m.elemsize), elempack(m.elempack), allocator(m.allocator), dims(m.dims), w(m.w), h(m.h), c(m.c) -{ - if (refcount) - NCNN_XADD(refcount, 1); - - cstep = m.cstep; -} - -inline VkMat::VkMat(int _w, VkBufferMemory* _data, size_t _elemsize, VkAllocator* _allocator) - : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(1), w(_w), h(1), c(1) -{ - cstep = w; -} - -inline VkMat::VkMat(int _w, int _h, VkBufferMemory* _data, size_t _elemsize, VkAllocator* _allocator) - : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(2), w(_w), h(_h), c(1) -{ - cstep = w * h; -} - -inline VkMat::VkMat(int _w, int _h, int _c, VkBufferMemory* _data, size_t _elemsize, VkAllocator* _allocator) - : data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(3), w(_w), h(_h), c(_c) -{ - cstep = alignSize(w * h * elemsize, 16) / elemsize; -} - -inline VkMat::VkMat(int _w, VkBufferMemory* _data, size_t _elemsize, int _elempack, VkAllocator* _allocator) - : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(1), w(_w), h(1), c(1) -{ - cstep = w; -} - -inline VkMat::VkMat(int _w, int _h, VkBufferMemory* _data, size_t _elemsize, int _elempack, VkAllocator* _allocator) - : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(2), w(_w), h(_h), c(1) -{ - cstep = w * h; -} - -inline VkMat::VkMat(int _w, int _h, int _c, VkBufferMemory* _data, size_t _elemsize, int _elempack, VkAllocator* _allocator) - : data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(3), w(_w), h(_h), c(_c) -{ - cstep = alignSize(w * h * elemsize, 16) / elemsize; -} - -inline VkMat::~VkMat() -{ - release(); -} - -inline VkMat& VkMat::operator=(const VkMat& m) -{ - if (this == &m) - return *this; - - if (m.refcount) - NCNN_XADD(m.refcount, 1); - - release(); - - data = m.data; - refcount = m.refcount; - elemsize = m.elemsize; - elempack = m.elempack; - allocator = m.allocator; - - dims = m.dims; - w = m.w; - h = m.h; - c = m.c; - - cstep = m.cstep; - - return *this; -} - -inline void VkMat::create(int _w, size_t _elemsize, VkAllocator* _allocator) -{ - if (dims == 1 && w == _w && elemsize == _elemsize && elempack == 1 && allocator == _allocator) - return; - - release(); - - elemsize = _elemsize; - elempack = 1; - allocator = _allocator; - - dims = 1; - w = _w; - h = 1; - c = 1; - - cstep = w; - - if (total() > 0) - { - size_t totalsize = alignSize(total() * elemsize, 4); - - data = allocator->fastMalloc(totalsize); - - refcount = (int*)((unsigned char*)data + offsetof(VkBufferMemory, refcount)); - *refcount = 1; - } -} - -inline void VkMat::create(int _w, int _h, size_t _elemsize, VkAllocator* _allocator) -{ - if (dims == 2 && w == _w && h == _h && elemsize == _elemsize && elempack == 1 && allocator == _allocator) - return; - - release(); - - elemsize = _elemsize; - elempack = 1; - allocator = _allocator; - - dims = 2; - w = _w; - h = _h; - c = 1; - - cstep = w * h; - - if (total() > 0) - { - size_t totalsize = alignSize(total() * elemsize, 4); - - data = allocator->fastMalloc(totalsize); - - refcount = (int*)((unsigned char*)data + offsetof(VkBufferMemory, refcount)); - *refcount = 1; - } -} - -inline void VkMat::create(int _w, int _h, int _c, size_t _elemsize, VkAllocator* _allocator) -{ - if (dims == 3 && w == _w && h == _h && c == _c && elemsize == _elemsize && elempack == 1 && allocator == _allocator) - return; - - release(); - - elemsize = _elemsize; - elempack = 1; - allocator = _allocator; - - dims = 3; - w = _w; - h = _h; - c = _c; - - cstep = alignSize(w * h * elemsize, 16) / elemsize; - - if (total() > 0) - { - size_t totalsize = alignSize(total() * elemsize, 4); - - data = allocator->fastMalloc(totalsize); - - refcount = (int*)((unsigned char*)data + offsetof(VkBufferMemory, refcount)); - *refcount = 1; - } -} - -inline void VkMat::create(int _w, size_t _elemsize, int _elempack, VkAllocator* _allocator) -{ - if (dims == 1 && w == _w && elemsize == _elemsize && elempack == _elempack && allocator == _allocator) - return; - - release(); - - elemsize = _elemsize; - elempack = _elempack; - allocator = _allocator; - - dims = 1; - w = _w; - h = 1; - c = 1; - - cstep = w; - - if (total() > 0) - { - size_t totalsize = alignSize(total() * elemsize, 4); - - data = allocator->fastMalloc(totalsize); - - refcount = (int*)((unsigned char*)data + offsetof(VkBufferMemory, refcount)); - *refcount = 1; - } -} - -inline void VkMat::create(int _w, int _h, size_t _elemsize, int _elempack, VkAllocator* _allocator) -{ - if (dims == 2 && w == _w && h == _h && elemsize == _elemsize && elempack == _elempack && allocator == _allocator) - return; - - release(); - - elemsize = _elemsize; - elempack = _elempack; - allocator = _allocator; - - dims = 2; - w = _w; - h = _h; - c = 1; - - cstep = w * h; - - if (total() > 0) - { - size_t totalsize = alignSize(total() * elemsize, 4); - - data = allocator->fastMalloc(totalsize); - - refcount = (int*)((unsigned char*)data + offsetof(VkBufferMemory, refcount)); - *refcount = 1; - } -} - -inline void VkMat::create(int _w, int _h, int _c, size_t _elemsize, int _elempack, VkAllocator* _allocator) -{ - if (dims == 3 && w == _w && h == _h && c == _c && elemsize == _elemsize && elempack == _elempack && allocator == _allocator) - return; - - release(); - - elemsize = _elemsize; - elempack = _elempack; - allocator = _allocator; - - dims = 3; - w = _w; - h = _h; - c = _c; - - cstep = alignSize(w * h * elemsize, 16) / elemsize; - - if (total() > 0) - { - size_t totalsize = alignSize(total() * elemsize, 4); - - data = allocator->fastMalloc(totalsize); - - refcount = (int*)((unsigned char*)data + offsetof(VkBufferMemory, refcount)); - *refcount = 1; - } -} - -inline void VkMat::create_like(const Mat& m, VkAllocator* _allocator) -{ - int _dims = m.dims; - if (_dims == 1) - create(m.w, m.elemsize, m.elempack, _allocator); - if (_dims == 2) - create(m.w, m.h, m.elemsize, m.elempack, _allocator); - if (_dims == 3) - create(m.w, m.h, m.c, m.elemsize, m.elempack, _allocator); -} - -inline void VkMat::create_like(const VkMat& m, VkAllocator* _allocator) -{ - int _dims = m.dims; - if (_dims == 1) - create(m.w, m.elemsize, m.elempack, _allocator); - if (_dims == 2) - create(m.w, m.h, m.elemsize, m.elempack, _allocator); - if (_dims == 3) - create(m.w, m.h, m.c, m.elemsize, m.elempack, _allocator); -} - -inline Mat VkMat::mapped() const -{ - if (!allocator->mappable) - return Mat(); - - if (dims == 1) - return Mat(w, mapped_ptr(), elemsize, elempack, 0); - - if (dims == 2) - return Mat(w, h, mapped_ptr(), elemsize, elempack, 0); - - if (dims == 3) - return Mat(w, h, c, mapped_ptr(), elemsize, elempack, 0); - - return Mat(); -} - -inline void* VkMat::mapped_ptr() const -{ - if (!allocator->mappable) - return 0; - - return (unsigned char*)data->mapped_ptr + data->offset; -} - -inline void VkMat::addref() -{ - if (refcount) - NCNN_XADD(refcount, 1); -} - -inline void VkMat::release() -{ - if (refcount && NCNN_XADD(refcount, -1) == 1) - { - if (allocator && data) - { - allocator->fastFree(data); - } - } - - data = 0; - - elemsize = 0; - elempack = 0; - - dims = 0; - w = 0; - h = 0; - c = 0; - - cstep = 0; - - refcount = 0; -} - -inline bool VkMat::empty() const -{ - return data == 0 || total() == 0; -} - -inline size_t VkMat::total() const -{ - return cstep * c; -} - -inline Mat VkMat::shape() const -{ - if (dims == 1) - return Mat(w * elempack, (void*)0); - if (dims == 2) - return Mat(w, h * elempack, (void*)0); - if (dims == 3) - return Mat(w, h, c * elempack, (void*)0); - - return Mat(); -} - -inline VkBuffer VkMat::buffer() const -{ - return data->buffer; -} - -inline size_t VkMat::buffer_offset() const -{ - return data->offset; -} - -inline size_t VkMat::buffer_capacity() const -{ - return data->capacity; -} - -inline VkImageMat::VkImageMat() - : data(0), refcount(0), allocator(0), width(0), height(0), format(VK_FORMAT_UNDEFINED) -{ -} - -inline VkImageMat::VkImageMat(int _width, int _height, VkFormat _format, VkImageAllocator* _allocator) - : data(0), refcount(0), allocator(0), width(0), height(0), format(VK_FORMAT_UNDEFINED) -{ - create(_width, _height, _format, _allocator); -} - -inline VkImageMat::VkImageMat(const VkImageMat& m) - : data(m.data), refcount(m.refcount), allocator(m.allocator), width(m.width), height(m.height), format(m.format) -{ - if (refcount) - NCNN_XADD(refcount, 1); -} - -inline VkImageMat::VkImageMat(int _width, int _height, VkImageMemory* _data, VkFormat _format, VkImageAllocator* _allocator) - : data(_data), refcount(0), allocator(_allocator), width(_width), height(_height), format(_format) -{ -} - -inline VkImageMat::~VkImageMat() -{ - release(); -} - -inline VkImageMat& VkImageMat::operator=(const VkImageMat& m) -{ - if (this == &m) - return *this; - - if (m.refcount) - NCNN_XADD(m.refcount, 1); - - release(); - - data = m.data; - refcount = m.refcount; - allocator = m.allocator; - - width = m.width; - height = m.height; - format = m.format; - - return *this; -} - -inline void VkImageMat::create(int _width, int _height, VkFormat _format, VkImageAllocator* _allocator) -{ - if (width == _width && height == _height && format == _format && allocator == _allocator) - return; - - release(); - - allocator = _allocator; - - width = _width; - height = _height; - format = _format; - - if (total() > 0) - { - data = allocator->fastMalloc(width, height, format); - - refcount = (int*)((unsigned char*)data + offsetof(VkImageMemory, refcount)); - *refcount = 1; - } -} - -inline void VkImageMat::addref() -{ - if (refcount) - NCNN_XADD(refcount, 1); -} - -inline void VkImageMat::release() -{ - if (refcount && NCNN_XADD(refcount, -1) == 1) - { - if (allocator && data) - { - allocator->fastFree(data); - } - } - - data = 0; - - width = 0; - height = 0; - format = VK_FORMAT_UNDEFINED; - - refcount = 0; -} - -inline bool VkImageMat::empty() const -{ - return data == 0 || total() == 0; -} - -inline size_t VkImageMat::total() const -{ - return width * height; -} - -inline VkImage VkImageMat::image() const -{ - return data->image; -} - -inline VkImageView VkImageMat::imageview() const -{ - return data->imageview; -} - -#endif // NCNN_VULKAN - -} // namespace ncnn - -#endif // NCNN_MAT_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/modelbin.h b/ncnn_project/ocr/ncnn/include/ncnn/modelbin.h deleted file mode 100644 index 5c4253a..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/modelbin.h +++ /dev/null @@ -1,65 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_MODELBIN_H -#define NCNN_MODELBIN_H - -#include "mat.h" - -namespace ncnn { - -class DataReader; -class ModelBin -{ -public: - virtual ~ModelBin(); - // element type - // 0 = auto - // 1 = float32 - // 2 = float16 - // 3 = int8 - // load vec - virtual Mat load(int w, int type) const = 0; - // load image - virtual Mat load(int w, int h, int type) const; - // load dim - virtual Mat load(int w, int h, int c, int type) const; -}; - -class ModelBinFromDataReader : public ModelBin -{ -public: - ModelBinFromDataReader(const DataReader& dr); - - virtual Mat load(int w, int type) const; - -protected: - const DataReader& dr; -}; - -class ModelBinFromMatArray : public ModelBin -{ -public: - // construct from weight blob array - ModelBinFromMatArray(const Mat* weights); - - virtual Mat load(int w, int type) const; - -protected: - mutable const Mat* weights; -}; - -} // namespace ncnn - -#endif // NCNN_MODELBIN_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/net.h b/ncnn_project/ocr/ncnn/include/ncnn/net.h deleted file mode 100644 index dbeb854..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/net.h +++ /dev/null @@ -1,265 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_NET_H -#define NCNN_NET_H - -#include -#include -#include "platform.h" -#include "blob.h" -#include "layer.h" -#include "mat.h" -#include "option.h" - -#if __ANDROID_API__ >= 9 -#include -#endif // __ANDROID_API__ >= 9 - -namespace ncnn { - -#if NCNN_VULKAN -class VkCompute; -#endif // NCNN_VULKAN -class DataReader; -class Extractor; -class Net -{ -public: - // empty init - Net(); - // clear and destroy - ~Net(); - -public: - // option can be changed before loading - Option opt; - -#if NCNN_VULKAN - // set gpu device by index - void set_vulkan_device(int device_index); - - // set gpu device by device handle, no owner transfer - void set_vulkan_device(const VulkanDevice* vkdev); - - const VulkanDevice* vulkan_device() const; -#endif // NCNN_VULKAN - -#if NCNN_STRING - // register custom layer by layer type name - // return 0 if success - int register_custom_layer(const char* type, layer_creator_func creator); -#endif // NCNN_STRING - // register custom layer by layer type - // return 0 if success - int register_custom_layer(int index, layer_creator_func creator); - -#if NCNN_STRING - int load_param(const DataReader& dr); -#endif // NCNN_STRING - - int load_param_bin(const DataReader& dr); - - int load_model(const DataReader& dr); - -#if NCNN_STDIO -#if NCNN_STRING - // load network structure from plain param file - // return 0 if success - int load_param(FILE* fp); - int load_param(const char* protopath); - int load_param_mem(const char* mem); -#endif // NCNN_STRING - // load network structure from binary param file - // return 0 if success - int load_param_bin(FILE* fp); - int load_param_bin(const char* protopath); - - // load network weight data from model file - // return 0 if success - int load_model(FILE* fp); - int load_model(const char* modelpath); -#endif // NCNN_STDIO - - // load network structure from external memory - // memory pointer must be 32-bit aligned - // return bytes consumed - int load_param(const unsigned char* mem); - - // reference network weight data from external memory - // weight data is not copied but referenced - // so external memory should be retained when used - // memory pointer must be 32-bit aligned - // return bytes consumed - int load_model(const unsigned char* mem); - -#if __ANDROID_API__ >= 9 -#if NCNN_STRING - // convenient load network structure from android asset plain param file - int load_param(AAsset* asset); - int load_param(AAssetManager* mgr, const char* assetpath); -#endif // NCNN_STRING - // convenient load network structure from android asset binary param file - int load_param_bin(AAsset* asset); - int load_param_bin(AAssetManager* mgr, const char* assetpath); - - // convenient load network weight data from android asset model file - int load_model(AAsset* asset); - int load_model(AAssetManager* mgr, const char* assetpath); -#endif // __ANDROID_API__ >= 9 - - // unload network structure and weight data - void clear(); - - // construct an Extractor from network - Extractor create_extractor() const; - -protected: - // parse the structure of network - // fuse int8 op dequantize and quantize by requantize - int fuse_network(); - -#if NCNN_VULKAN - - int upload_model(); - - int create_pipeline(); - - int destroy_pipeline(); - -#endif // NCNN_VULKAN - - friend class Extractor; -#if NCNN_STRING - int find_blob_index_by_name(const char* name) const; - int find_layer_index_by_name(const char* name) const; - int custom_layer_to_index(const char* type); - Layer* create_custom_layer(const char* type); -#endif // NCNN_STRING - Layer* create_custom_layer(int index); - int forward_layer(int layer_index, std::vector& blob_mats, Option& opt) const; - -#if NCNN_VULKAN - int forward_layer(int layer_index, std::vector& blob_mats, std::vector& blob_mats_gpu, VkCompute& cmd, Option& opt) const; -#endif // NCNN_VULKAN - -protected: - std::vector blobs; - std::vector layers; - - std::vector custom_layer_registry; - -#if NCNN_VULKAN - const VulkanDevice* vkdev; - - VkAllocator* weight_vkallocator; - VkAllocator* weight_staging_vkallocator; - - ncnn::Layer* cast_float32_to_float16; - ncnn::Layer* cast_float16_to_float32; - ncnn::Layer* packing_pack1; - ncnn::Layer* packing_pack4; - ncnn::Layer* packing_pack8; -#endif // NCNN_VULKAN -}; - -class Extractor -{ -public: - ~Extractor(); - - // enable light mode - // intermediate blob will be recycled when enabled - // enabled by default - void set_light_mode(bool enable); - - // set thread count for this extractor - // this will overwrite the global setting - // default count is system depended - void set_num_threads(int num_threads); - - // set blob memory allocator - void set_blob_allocator(Allocator* allocator); - - // set workspace memory allocator - void set_workspace_allocator(Allocator* allocator); - -#if NCNN_VULKAN - void set_vulkan_compute(bool enable); - - void set_blob_vkallocator(VkAllocator* allocator); - - void set_workspace_vkallocator(VkAllocator* allocator); - - void set_staging_vkallocator(VkAllocator* allocator); -#endif // NCNN_VULKAN - -#if NCNN_STRING - // set input by blob name - // return 0 if success - int input(const char* blob_name, const Mat& in); - - // get result by blob name - // return 0 if success - int extract(const char* blob_name, Mat& feat); -#endif // NCNN_STRING - - // set input by blob index - // return 0 if success - int input(int blob_index, const Mat& in); - - // get result by blob index - // return 0 if success - int extract(int blob_index, Mat& feat); - -#if NCNN_VULKAN -#if NCNN_STRING - // set input by blob name - // return 0 if success - int input(const char* blob_name, const VkMat& in); - - // get result by blob name - // return 0 if success - int extract(const char* blob_name, VkMat& feat, VkCompute& cmd); -#endif // NCNN_STRING - - // set input by blob index - // return 0 if success - int input(int blob_index, const VkMat& in); - - // get result by blob index - // return 0 if success - int extract(int blob_index, VkMat& feat, VkCompute& cmd); -#endif // NCNN_VULKAN - -protected: - friend Extractor Net::create_extractor() const; - Extractor(const Net* net, size_t blob_count); - -private: - const Net* net; - std::vector blob_mats; - Option opt; - -#if NCNN_VULKAN - VkAllocator* local_blob_vkallocator; - VkAllocator* local_staging_vkallocator; - - std::vector blob_mats_gpu; -#endif // NCNN_VULKAN -}; - -} // namespace ncnn - -#endif // NCNN_NET_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/opencv.h b/ncnn_project/ocr/ncnn/include/ncnn/opencv.h deleted file mode 100644 index f0c4aff..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/opencv.h +++ /dev/null @@ -1,278 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_OPENCV_H -#define NCNN_OPENCV_H - -#include "platform.h" - -#if NCNN_OPENCV - -#include -#include -#include "mat.h" - -#if defined(_MSC_VER) || defined(__GNUC__) -#pragma push_macro("min") -#pragma push_macro("max") -#undef min -#undef max -#endif - -// minimal opencv style data structure implementation -namespace cv -{ - -struct Size -{ - Size() : width(0), height(0) {} - Size(int _w, int _h) : width(_w), height(_h) {} - - int width; - int height; -}; - -template -struct Rect_ -{ - Rect_() : x(0), y(0), width(0), height(0) {} - Rect_(_Tp _x, _Tp _y, _Tp _w, _Tp _h) : x(_x), y(_y), width(_w), height(_h) {} - - _Tp x; - _Tp y; - _Tp width; - _Tp height; - - // area - _Tp area() const - { - return width * height; - } -}; - -template static inline Rect_<_Tp>& operator &= ( Rect_<_Tp>& a, const Rect_<_Tp>& b ) -{ - _Tp x1 = std::max(a.x, b.x), y1 = std::max(a.y, b.y); - a.width = std::min(a.x + a.width, b.x + b.width) - x1; - a.height = std::min(a.y + a.height, b.y + b.height) - y1; - a.x = x1; a.y = y1; - if( a.width <= 0 || a.height <= 0 ) - a = Rect_<_Tp>(); - return a; -} - -template static inline Rect_<_Tp>& operator |= ( Rect_<_Tp>& a, const Rect_<_Tp>& b ) -{ - _Tp x1 = std::min(a.x, b.x), y1 = std::min(a.y, b.y); - a.width = std::max(a.x + a.width, b.x + b.width) - x1; - a.height = std::max(a.y + a.height, b.y + b.height) - y1; - a.x = x1; a.y = y1; - return a; -} - -template static inline Rect_<_Tp> operator & (const Rect_<_Tp>& a, const Rect_<_Tp>& b) -{ - Rect_<_Tp> c = a; - return c &= b; -} - -template static inline Rect_<_Tp> operator | (const Rect_<_Tp>& a, const Rect_<_Tp>& b) -{ - Rect_<_Tp> c = a; - return c |= b; -} - -typedef Rect_ Rect; -typedef Rect_ Rect2f; - -template -struct Point_ -{ - Point_() : x(0), y(0) {} - Point_(_Tp _x, _Tp _y) : x(_x), y(_y) {} - - _Tp x; - _Tp y; -}; - -typedef Point_ Point; -typedef Point_ Point2f; - -#define CV_8UC1 1 -#define CV_8UC3 3 -#define CV_8UC4 4 -#define CV_32FC1 4 - -struct Mat -{ - Mat() : data(0), refcount(0), rows(0), cols(0), c(0) {} - - Mat(int _rows, int _cols, int flags) : data(0), refcount(0) - { - create(_rows, _cols, flags); - } - - // copy - Mat(const Mat& m) : data(m.data), refcount(m.refcount) - { - if (refcount) - NCNN_XADD(refcount, 1); - - rows = m.rows; - cols = m.cols; - c = m.c; - } - - Mat(int _rows, int _cols, int flags, void* _data) : data((unsigned char*)_data), refcount(0) - { - rows = _rows; - cols = _cols; - c = flags; - } - - ~Mat() - { - release(); - } - - // assign - Mat& operator=(const Mat& m) - { - if (this == &m) - return *this; - - if (m.refcount) - NCNN_XADD(m.refcount, 1); - - release(); - - data = m.data; - refcount = m.refcount; - - rows = m.rows; - cols = m.cols; - c = m.c; - - return *this; - } - - void create(int _rows, int _cols, int flags) - { - release(); - - rows = _rows; - cols = _cols; - c = flags; - - if (total() > 0) - { - // refcount address must be aligned, so we expand totalsize here - size_t totalsize = (total() + 3) >> 2 << 2; - data = (unsigned char*)ncnn::fastMalloc(totalsize + (int)sizeof(*refcount)); - refcount = (int*)(((unsigned char*)data) + totalsize); - *refcount = 1; - } - } - - void release() - { - if (refcount && NCNN_XADD(refcount, -1) == 1) - ncnn::fastFree(data); - - data = 0; - - rows = 0; - cols = 0; - c = 0; - - refcount = 0; - } - - Mat clone() const - { - if (empty()) - return Mat(); - - Mat m(rows, cols, c); - - if (total() > 0) - { - memcpy(m.data, data, total()); - } - - return m; - } - - bool empty() const { return data == 0 || total() == 0; } - - int channels() const { return c; } - - size_t total() const { return cols * rows * c; } - - const unsigned char* ptr(int y) const { return data + y * cols * c; } - - unsigned char* ptr(int y) { return data + y * cols * c; } - - // roi - Mat operator()( const Rect& roi ) const - { - if (empty()) - return Mat(); - - Mat m(roi.height, roi.width, c); - - int sy = roi.y; - for (int y = 0; y < roi.height; y++) - { - const unsigned char* sptr = ptr(sy) + roi.x * c; - unsigned char* dptr = m.ptr(y); - memcpy(dptr, sptr, roi.width * c); - sy++; - } - - return m; - } - - unsigned char* data; - - // pointer to the reference counter; - // when points to user-allocated data, the pointer is NULL - int* refcount; - - int rows; - int cols; - - int c; - -}; - -#define CV_LOAD_IMAGE_GRAYSCALE 1 -#define CV_LOAD_IMAGE_COLOR 3 -Mat imread(const std::string& path, int flags); -void imwrite(const std::string& path, const Mat& m); - -#if NCNN_PIXEL -void resize(const Mat& src, Mat& dst, const Size& size, float sw = 0.f, float sh = 0.f, int flags = 0); -#endif // NCNN_PIXEL - -} // namespace cv - -#if defined(_MSC_VER) || defined(__GNUC__) -#pragma pop_macro("min") -#pragma pop_macro("max") -#endif - -#endif // NCNN_OPENCV - -#endif // NCNN_OPENCV_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/option.h b/ncnn_project/ocr/ncnn/include/ncnn/option.h deleted file mode 100644 index ec87c5a..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/option.h +++ /dev/null @@ -1,103 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_OPTION_H -#define NCNN_OPTION_H - -#include "platform.h" - -namespace ncnn { - -#if NCNN_VULKAN -class VkAllocator; -#endif // NCNN_VULKAN - -class Allocator; -class Option -{ -public: - // default option - Option(); - -public: - // light mode - // intermediate blob will be recycled when enabled - // enabled by default - bool lightmode; - - // thread count - // default value is the one returned by get_cpu_count() - int num_threads; - - // blob memory allocator - Allocator* blob_allocator; - - // workspace memory allocator - Allocator* workspace_allocator; - -#if NCNN_VULKAN - // blob memory allocator - VkAllocator* blob_vkallocator; - - // workspace memory allocator - VkAllocator* workspace_vkallocator; - - // staging memory allocator - VkAllocator* staging_vkallocator; -#endif // NCNN_VULKAN - - // enable winograd convolution optimization - // improve convolution 3x3 stride1 performace, may consume more memory - // changes should be applied before loading network structure and weight - // enabled by default - bool use_winograd_convolution; - - // enable sgemm convolution optimization - // improve convolution 1x1 stride1 performace, may consume more memory - // changes should be applied before loading network structure and weight - // enabled by default - bool use_sgemm_convolution; - - // enable quantized int8 inference - // use low-precision int8 path for quantized model - // changes should be applied before loading network structure and weight - // enabled by default - bool use_int8_inference; - - // enable vulkan compute - bool use_vulkan_compute; - - // enable options for gpu inference - bool use_fp16_packed; - bool use_fp16_storage; - bool use_fp16_arithmetic; - bool use_int8_storage; - bool use_int8_arithmetic; - - // enable simd-friendly packed memory layout - // improve all operator performace on all arm devices, will consume more memory - // changes should be applied before loading network structure and weight - // enabled by default - bool use_packing_layout; - - bool use_shader_pack8; - - // enable bf16 data type for storage - // improve most operator performace on all arm devices, may consume more memory - bool use_bf16_storage; -}; - -} // namespace ncnn - -#endif // NCNN_OPTION_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/paramdict.h b/ncnn_project/ocr/ncnn/include/ncnn/paramdict.h deleted file mode 100644 index 81dbffc..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/paramdict.h +++ /dev/null @@ -1,73 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_PARAMDICT_H -#define NCNN_PARAMDICT_H - -#include "mat.h" - -// at most 32 parameters -#define NCNN_MAX_PARAM_COUNT 32 - -namespace ncnn { - -class DataReader; -class Net; -class ParamDict -{ -public: - // empty - ParamDict(); - - // get int - int get(int id, int def) const; - // get float - float get(int id, float def) const; - // get array - Mat get(int id, const Mat& def) const; - - // set int - void set(int id, int i); - // set float - void set(int id, float f); - // set array - void set(int id, const Mat& v); - -protected: - friend class Net; - - void clear(); - - int load_param(const DataReader& dr); - int load_param_bin(const DataReader& dr); - -protected: - struct - { - // 0 = null - // 1 = int/float - // 2 = int - // 3 = float - // 4 = array of int/float - // 5 = array of int - // 6 = array of float - int type; - union { int i; float f; }; - Mat v; - } params[NCNN_MAX_PARAM_COUNT]; -}; - -} // namespace ncnn - -#endif // NCNN_PARAMDICT_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/pipeline.h b/ncnn_project/ocr/ncnn/include/ncnn/pipeline.h deleted file mode 100644 index 7f895d8..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/pipeline.h +++ /dev/null @@ -1,104 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_PIPELINE_H -#define NCNN_PIPELINE_H - -#include "platform.h" -#include "mat.h" -#if NCNN_VULKAN -#include -#include "gpu.h" -#endif // NCNN_VULKAN - -namespace ncnn { - -#if NCNN_VULKAN -class Option; -class Pipeline -{ -public: - Pipeline(const VulkanDevice* vkdev); - virtual ~Pipeline(); - -public: - void set_optimal_local_size_xyz(int w = 4, int h = 4, int c = 4); - void set_optimal_local_size_xyz(const Mat& local_size_xyz); - void set_local_size_xyz(int w, int h, int c); - - int create(const uint32_t* spv_data, size_t spv_data_size, const std::vector& specializations); - - int create(int shader_type_index, const Option& opt, const std::vector& specializations); - - int create(VkShaderModule shader_module, const std::vector& specializations, int binding_count, int push_constant_count); - - void destroy(); - -protected: - int create_descriptorset_layout(int binding_count); - int create_pipeline_layout(int push_constant_count); - int create_pipeline(VkShaderModule shader_module, const std::vector& specializations); - int create_descriptor_update_template(int binding_count); - -public: - const VulkanDevice* vkdev; - - // local shader module - VkShaderModule local_shader_module; - - VkDescriptorSetLayout descriptorset_layout; - VkPipelineLayout pipeline_layout; - - // op forward TODO use pipeline cache ? - VkPipeline pipeline; - - VkDescriptorUpdateTemplateKHR descriptor_update_template; - - uint32_t local_size_x; - uint32_t local_size_y; - uint32_t local_size_z; -}; - -#if __ANDROID_API__ >= 26 -class VkCompute; -class ImportAndroidHardwareBufferPipeline : private Pipeline -{ -public: - ImportAndroidHardwareBufferPipeline(const VulkanDevice* vkdev); - ~ImportAndroidHardwareBufferPipeline(); - - int create(VkAndroidHardwareBufferImageAllocator* ahb_im_allocator, int type_to, int rotate_from, const Option& opt); - int create(VkAndroidHardwareBufferImageAllocator* ahb_im_allocator, int type_to, int rotate_from, int target_width, int target_height, const Option& opt); - void destroy(); - - friend class VkCompute; - -protected: - int create_sampler(VkAndroidHardwareBufferImageAllocator* ahb_im_allocator); - int create_descriptorset_layout(); - int create_descriptor_update_template(); - -public: - int type_to; - int rotate_from; - bool need_resize; - - VkSampler sampler; -}; -#endif // __ANDROID_API__ >= 26 -#endif // NCNN_VULKAN - -} // namespace ncnn - -#endif // NCNN_PIPELINE_H diff --git a/ncnn_project/ocr/ncnn/include/ncnn/platform.h b/ncnn_project/ocr/ncnn/include/ncnn/platform.h deleted file mode 100644 index 06c1ee4..0000000 --- a/ncnn_project/ocr/ncnn/include/ncnn/platform.h +++ /dev/null @@ -1,138 +0,0 @@ -// Tencent is pleased to support the open source community by making ncnn available. -// -// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. -// -// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// https://opensource.org/licenses/BSD-3-Clause -// -// 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 NCNN_PLATFORM_H -#define NCNN_PLATFORM_H - -#define NCNN_STDIO 1 -#define NCNN_STRING 1 -#define NCNN_OPENCV 0 -#define NCNN_BENCHMARK 0 -#define NCNN_PIXEL 1 -#define NCNN_PIXEL_ROTATE 1 -#define NCNN_VULKAN 0 -#define NCNN_REQUANT 0 -#define NCNN_AVX2 0 - -#if (defined _WIN32 && !(defined __MINGW32__)) -#define WIN32_LEAN_AND_MEAN -#include -#include -#else -#include -#endif - -#if __ANDROID_API__ >= 26 -#define VK_USE_PLATFORM_ANDROID_KHR -#endif // __ANDROID_API__ >= 26 - -namespace ncnn { - -#if (defined _WIN32 && !(defined __MINGW32__)) -class Mutex -{ -public: - Mutex() { InitializeSRWLock(&srwlock); } - ~Mutex() {} - void lock() { AcquireSRWLockExclusive(&srwlock); } - void unlock() { ReleaseSRWLockExclusive(&srwlock); } -private: - friend class ConditionVariable; - // NOTE SRWLock is available from windows vista - SRWLOCK srwlock; -}; -#else // _WIN32 -class Mutex -{ -public: - Mutex() { pthread_mutex_init(&mutex, 0); } - ~Mutex() { pthread_mutex_destroy(&mutex); } - void lock() { pthread_mutex_lock(&mutex); } - void unlock() { pthread_mutex_unlock(&mutex); } -private: - friend class ConditionVariable; - pthread_mutex_t mutex; -}; -#endif // _WIN32 - -class MutexLockGuard -{ -public: - MutexLockGuard(Mutex& _mutex) : mutex(_mutex) { mutex.lock(); } - ~MutexLockGuard() { mutex.unlock(); } -private: - Mutex& mutex; -}; - -#if (defined _WIN32 && !(defined __MINGW32__)) -class ConditionVariable -{ -public: - ConditionVariable() { InitializeConditionVariable(&condvar); } - ~ConditionVariable() {} - void wait(Mutex& mutex) { SleepConditionVariableSRW(&condvar, &mutex.srwlock, INFINITE, 0); } - void broadcast() { WakeAllConditionVariable(&condvar); } - void signal() { WakeConditionVariable(&condvar); } -private: - CONDITION_VARIABLE condvar; -}; -#else // _WIN32 -class ConditionVariable -{ -public: - ConditionVariable() { pthread_cond_init(&cond, 0); } - ~ConditionVariable() { pthread_cond_destroy(&cond); } - void wait(Mutex& mutex) { pthread_cond_wait(&cond, &mutex.mutex); } - void broadcast() { pthread_cond_broadcast(&cond); } - void signal() { pthread_cond_signal(&cond); } -private: - pthread_cond_t cond; -}; -#endif // _WIN32 - -#if (defined _WIN32 && !(defined __MINGW32__)) -static unsigned __stdcall start_wrapper(void* args); -class Thread -{ -public: - Thread(void* (*start)(void*), void* args = 0) { _start = start; _args = args; handle = (HANDLE)_beginthreadex(0, 0, start_wrapper, this, 0, 0); } - ~Thread() {} - void join() { WaitForSingleObject(handle, INFINITE); CloseHandle(handle); } -private: - friend unsigned __stdcall start_wrapper(void* args) - { - Thread* t = (Thread*)args; - t->_start(t->_args); - return 0; - } - HANDLE handle; - void* (*_start)(void*); - void* _args; -}; - -#else // _WIN32 -class Thread -{ -public: - Thread(void* (*start)(void*), void* args = 0) { pthread_create(&t, 0, start, args); } - ~Thread() {} - void join() { pthread_join(t, 0); } -private: - pthread_t t; -}; -#endif // _WIN32 - -} // namespace ncnn - -#endif // NCNN_PLATFORM_H