Tic商业评论

关注微信公众号【站长自定义模块】,定时推送前沿、专业、深度的商业资讯。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

微信登录

微信扫码,快速开始

  • QQ空间
  • 回复
  • 收藏

在树莓派4上安装ncnn深度学习框架

lijingle 环境搭建 2021-12-25 10:17 4105人围观

介绍
本文将指导您在树莓派 4 上安装腾讯的 ncnn 框架。C++ 代码示例是在树莓派 4 的 Code::Blocks IDE 中编辑。我们只介绍基础知识,因此在 最后,你可以构建你的应用程序。 有关 ncnn 库的更多信息,请参阅:https://github.com/Tencent/ncnn。 安装的是C++版本, 它不适合 Python。

依赖
ncnn 框架几乎没有依赖关系。 它需要 protobuf 来加载 ONNX 模型。 OpenCV 会很有用,但不是必需的。

版本检查
在您的 Raspberry Pi 4 上安装 ncnn 之前,请检查您的操作系统。运行命令 uname -a 并使用下面的例子验证您的版本。


如果是 64 位操作系统,还请使用命令 gcc -v 检查您的 C++ 编译器。 它也必须是 aarch64-linux-gnu 版本。 如果 gcc 版本不同,请使用最新版本重新安装整个操作系统。 该指南位于此处:在 Raspberry Pi 4 上安装 64 位操作系统。必须具有 64 位 C++ 编译器,因为我们将构建 ncnn 库。

还要注意根据我们的说明安装后 swap内存大小超过 3 GB。


RTTI

RTTI 代表运行时类型识别。 它是一种 C++ 机制,用于在运行时获取对象的类型和内存大小,但尚未定义。 通常,程序员知道变量的类型并且可以预先分配保存对象的内存。 从操作系统及其所有进程和线程获取内存可能是一项相对耗时的操作。 现代 C 编译器知道需要多少内存,调用一次内存管理就足够了。 这是与 Python 相比的主要优势之一,Python 在遇到带有变量的代码行之前不会考虑内存要求。

如果想编写尽可能快的代码,最好不要使用 RTTI。 在ncnn框架中也是如此。

默认情况下,它使用 -fno-rtti 标志编译,以防止使用 RTTI。 使用此标志编译后,只有在其余程序仍未使用 RTTI 时,才可以使用像 YOLOV5 中的那些自定义层。.

有时无法避免 RTTI。 尤其是在不需要重写所有代码的情况下需要新功能的成熟代码中。 OpenCV 在某些地方使用了 RTTI 机制。

这里有问题。 当您使用 OpenCV 编译 ncnn 时,编译器会在 -fno-rtti 标志上返回错误。 删除标志有时适用于 ncnn 代码,具体取决于所使用的 DNN 类型。

此时,使用的构建标志 -D NCNN_DISABLE_RTTI=OFF 变得清晰。 它告诉编译器 ncnn 将允许 RTTI。 这意味着您现在可以以完整的功能运行 ncnn,而不会遇到 OpenCV 的问题。 或者,就此而言,任何其他使用 RTTI 的软件。

在性能方面, Raspberry Pi 不会有任何不同。

安装

如果尚未安装 OpenCV,请先安装它。 安装指南在这里,大约需要一个小时。

ncnn在树莓派上的整个安装如下。

Raspberry 64-bit (aarch64)

# check for updates (64-bit OS is still under development!)
$ sudo apt-get update
$ sudo apt-get upgrade
# install dependencies
$ sudo apt-get install cmake wget
$ sudo apt-get install libprotobuf-dev protobuf-compiler
# download ncnn
$ git clone --depth=1 https://github.com/Tencent/ncnn.git
# install ncnn
$ cd ncnn
$ mkdir build
$ cd build
# build 64-bit ncnn
$ cmake -D NCNN_DISABLE_RTTI=OFF \
-D CMAKE_TOOLCHAIN_FILE=../toolchains/aarch64-linux-gnu.toolchain.cmake ..
$ make -j4
$ make install
# copy output to dirs
$ sudo mkdir /usr/local/lib/ncnn
$ sudo cp -r install/include/ncnn /usr/local/include/ncnn
$ sudo cp -r install/lib/libncnn.a /usr/local/lib/ncnn/libncnn.a
# once you've placed the output in your /usr/local directory,
# you may delete the ncnn directory if you have no tools or examples compiled
$ cd ~
$ sudo rm -rf ncnn

Raspberry 32-bit (armv7l)
# check for updates (64-bit OS is still under development!)
$ sudo apt-get update
$ sudo apt-get upgrade
# install dependencies
$ sudo apt-get install cmake wget
$ sudo apt-get install libprotobuf-dev protobuf-compiler
# download ncnn
$ git clone --depth=1 https://github.com/Tencent/ncnn.git
# install ncnn
$ cd ncnn
$ mkdir build
$ cd build
# build 64-bit ncnn
$ cmake -D NCNN_DISABLE_RTTI=OFF \
	-D CMAKE_TOOLCHAIN_FILE=../toolchains/aarch64-linux-gnu.toolchain.cmake ..
$ make -j4
$ make install
# copy output to dirs
$ sudo mkdir /usr/local/lib/ncnn
$ sudo cp -r install/include/ncnn /usr/local/include/ncnn
$ sudo cp -r install/lib/libncnn.a /usr/local/lib/ncnn/libncnn.a
# once you've placed the output in your /usr/local directory,
# you may delete the ncnn directory if you have no tools or examples compiled
$ cd ~
$ sudo rm -rf ncnn

如果一切顺利,您将获得两个文件夹。 一个包含所有头文件,一个包含库,如下图所示。


另请注意带有示例的文件夹。 这里涵盖了许多不同类型的深度学习。 由于 ncnn 库中的版本更改,对实际深度学习模型的引用有时会导致错误。 我们最近从 nihui 收到了以下带有最新模型的存储库:https://github.com/nihui/ncnn-assets/tree/master/models。




路过

雷人

握手

鲜花

鸡蛋
我有话说......
电话咨询: 135xxxxxxx
关注微信