[Pro-test] Compiling NCNN with non-administrative (root) privileges in Centos7 system

foreword

Since you are using a cluster, you do not have administrator privileges, so all the following situations are installed under non-administrator privileges, that is, this installation strategy is only applicable to ordinary users who build their own environments.

Add content

It must be emphasized here that during the following compilation and installation process, the same version of the GCC/G++ compilation environment must be used. Since the current compilation is performed by a non-administrator (root) user, some additional settings are required. Otherwise, there will be various unexpected errors, especially during the NCNN compilation and installation process, there will be some "reference undefined to" problems, which will cause NCNN to make errors during compilation or installation, resulting in incomplete compilation and installation. Here Record the process of my successful installation in the past two days, and here is an example of the successful compilation of NCNN.

insert image description here
insert image description here

What is NCNN

ncnn is a very efficient and easy-to-use deep learning reasoning framework that supports various neural network models, such as pytorch, tensorflow, onnx, etc., and a variety of hardware backends, such as x86, arm, riscv, mips, vulkan, etc. According to the description on its official website: the framework is a high-performance neural network forward computing framework optimized for the mobile terminal, and the deployment and use of the mobile terminal are deeply considered from the beginning of the design. No third-party dependencies, cross-platform, and the speed of the mobile CPU is faster than all known open source frameworks. Based on ncnn, developers can easily transplant deep learning algorithms to mobile phones for efficient execution, develop artificial intelligence APPs, and bring AI to your fingertips.

How to compile and install NCNN

Compile and install documents in the case of Liunx Click to enter

First of all, it is clear that NCNN is implemented in C++, so some dependencies are needed before compiling and installing. In the compilation and installation document recommended by the official website, it is written: Obviously, g++ cmake protobuf opencv is required in Linux systems (macOS requires glslang). Generally
insert image description here
speaking
, The Liunx system generally has built-in g++ and cmake, but the version is generally a relatively low version, mainly for the stability of the system, so we need to upgrade it during the process of compiling and installing NCNN. The configuration and installation of all the following environments are in the normal in the user environment.

Dependent environment configuration and installation

1 g++ environment

Generally speaking, the built-in g++ version of Centos 7 is 4.8.5. This version is generally old. Now many compilations need to be performed in g++ 5.0 and above environments. Here I recommend my previous blog post, under non-root permissions Upgrade gcc g++, click to enter , here you need to pay attention, if you are also a cluster system, you need to pay attention to the following details when compiling GCC or G++, at this time I stepped on the pit, in principle, you can install a higher version Now It seems that G++ has been developed to version 9.xxx.
insert image description here

2 cmake

The built-in Cmake version of Centos7 is generally 2.8.12. This version needs to be upgraded to version 3.*. You can visit this link to download the corresponding version of Cmake. Here I take the cmake 3.17.2 I installed as an example. It is recommended that you directly install it according to the system , and then download the compiled version, so that you can save the source code installation and configure it directly. Of course, you can also download the source code version and compile and install it yourself. Here I briefly introduce the configuration and installation process of the two different methods. Then introduce a very simple method based on yum install

insert image description here
insert image description here

2.1 Cmake compiled version

That is, the above cmake-3.17.2-Linux-x86_64.tar.gz, this is the simplest strategy, you only need to decompress it and configure it in the user's environment to use it. This method is recommended.
2.1.1 You can use the command to download, or you can download it directly and send it to your own server.
If you download directly, the download command:

wget https://cmake.org/files/v3.17/cmake-3.17.2-Linux-x86_64.tar.gz

2.1.2 Unzip, the key execution files are in the bin directory

tar xzvf cmake-3.17.2-Linux-x86_64.tar.gz

insert image description here
2.1.3 Configuring environment variables
Here we need to know that there are many kinds of environment variables in the Linux system,
insert image description here
and there are three main ways to set the system environment variables (requires administrator authority):

1 /etc/profile: When the user logs in, execute the /etc/profile file to set the environment variables of the system. However, Linux does not recommend setting system environment variables in the /etc/profile file.
2 Add an environment variable script file in the /etc/profile.d directory. This is the method recommended by Linux. /etc/profile will execute all the script files under /etc/profile.d every time it starts. /etc/profile.d is easier to maintain than /etc/profile. If you don’t want any variables, just delete the corresponding shell script under /etc/profile.d. There are many script files in the /etc/profile.d directory, for example:
insert image description here
oracle.sh in the /etc/profile.d directory is the environment variable configuration file of the Oracle database.
insert image description here
3 Set the environment variable in the /etc/bashrc file. The environment variables configured in this file will affect the bash shell used by all users. However, Linux does not recommend setting system environment variables in the /etc/bashrc file.

Since we do not have system administrator privileges, we need to set the user environment. User environment variables are only valid for the current user. There are many ways to set user environment variables.

In the user's home directory, there are several special files that cannot be seen with 1s, but can be seen with 1s .bash_*.
insert image description here
1) .bash_profile (recommended and preferred)
is executed when the user logs in, and each user can use this file to configure their own environment variables.
2) The .bashrc
file will be read when the user logs in and every time a new shell is opened. It is not recommended to configure user-specific environment variables in it, because the file will be read once every time a shell is opened, which improves efficiency. Definitely affected.
3) .bash_logout
Execute this file every time you exit the system (exit the bash shell).
4) .bash_history
saves the historical commands used by the current user.

Based on the above content, we can see the execution order of the environment variable script file

/etc/profile->/etc/profile.d->/etc/bashrc->用户的.bash_profile->用户的.bashrc

Here we choose the environment variable configuration method that has the least impact on the system: the user's .bashrc, which is safer and more secure.

vi ~/.bashrc

Add the following content
here, you need to pay attention: This symbol means Chinese semicolon or append in the Liunx system, which refers to adding our cmake/bin folder path to the system path $PATH. Here I set: $PATH Finally, the cmke/bin path is added before the original system PATH, that is, it is loaded first. You can also write like the second method, adding the cmake/bin path after the original PATH.

export PATH=/home/y192353/butterfly/extension/cmake_3_17_2/bin:$PATHexport PATH=$PATH:/home/y192353/butterfly/extension/cmake_3_17_2/bin

After the above configuration is completed, directly run the environment variable effective command

source ~/.bashrc

Then check whether cmake is configured successfully

cmake --version

insert image description here
That is, the installation is successful!

2.2 Cmake source code compilation

This requires you to download the source code of Cmake, the place indicated by the arrow below
insert image description here
After downloading and decompressing, the folder is as follows, including various source code files
insert image description here
Compile and run the command:
If you have not decompressed, first decompress

tar -zxvf cmake-3.17.2.tar.gz

Then compile step by step

#在解压后的cmake-3.17.2文件夹下 创建 build 文件夹
mkdir build
cd build
#这一步很关键,即确定安装路径, 安装至用户对应的文件夹,不然会报权限异常,因为一般默认安装在 (/usr/local/..中)
#这是通过 配置文件进行指定安装目录的
../configure --prefix=/yourPath/cmake-3.17.2
#开启编译安装 这个需要等待一会儿
gmake && gmake install

After the above compilation and installation are completed, you can configure the path. The details are the same as the above section [2.1.3 Configuring environment variables], so I won’t go into details, but at this time, your path needs to be the path specified by --prefix above.

2.3 Yum install Install cmake3 directly (this method needs to be followed if there is cmake operation, you need to call cmake3)

If you don’t want to configure step by step according to the above method, you can directly use the following command

yum install cmake3

Install directly, and then use cmake3 during compilation.

3 OpenCv compilation and installation

First check whether OpenCv has been installed in your system

pkg-config --modversion opencv

If opencv has been installed, there will be a printout, if not installed, you need to install it.

3.1 OpenCv installation (Here we take Opencv3.4.16 as an example, of course OpenCv4.* is also similar), here we suggest you use OpenCv4, according to the official website, OpenCv3 will gradually stop updating, and then launch OpenCv5, pay attention to the version of OpenCv3.4.16 here GCC/G++5.0 or above is required, and you need to set this up.

The compiled source code of OpenCv can be found by clicking
insert image description here
here . After downloading the source code, use the command to decompress it
insert image description here

unzip opencv-3.4.16.zip

Unzip the folder
insert image description here
Compile and install

cd opencv-3.14.6
#创建编译文件夹
mkdir build
cd build
#在编译和安装过程中,从这步开始,最好放在 计算节点(GPU)节点进行编译安装
#使用cmake 进行编译 最后面的两个点也要加上,即默认的安装路径设置为 CMAKE_INSTALL_PREFIX=/YourPath/opencv_3_4_16
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/YourPath/opencv_3_4_16 -D WITH_TBB=ON -D WITH_V4L=ON -D BUILD_TIFF=ON -D BUILD_EXAMPLES=ON -D WITH_OPENGL=ON -D WITH_EIGEN=ON -D WITH_CUDA=ON -D WITH_CUBLAS=ON ..
#这里 可以使用 -j16 -j32 原则上都可以,旨在加速编译,需要较长时间
make -j8
#安装,安装的内容放在了 /YourPath/opencv_3_4_16
make install

After the cmake step succeeds
insert image description here

CMAKE_INSTALL_PREFIX=/YourPath/opencv_3_4_16 is used to control the installation path, otherwise there will be problems with insufficient installation path permissions.
If you are installing OpenCv4, you need to add it to the cmake command:

OPENCV_GENERATE_PKGCONFIG=ON
is used to control whether to generate pkg_config file. This is where opencv3 is different. If this command is not added in opencv4, pkgconfig will not be generated. The opencv.pv file cannot be found after installation is generally caused by not adding this control command.

3.2 Environment variable configuration

Open ~/.bashrc

 vi ~/.bashrc

add content

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/YourPath/opencv_3_4_16/lib64/pkgconfig
export PKG_CONFIG_PATH
export LD_LIBRARY_PATH=/YourPath/opencv_3_4_16/lib64/:$LD_LIBRARY_PATH

The following is my environment variable configuration file. The specific path information has been hidden. This is the overall look.
insert image description here
Make the environment variable take effect

source ~/.bashrc

Then check the installation of opencv

pkg-config --modversion opencv

4 install protobuf

protobuf (Google Protocol Buffer) is a platform-independent, language-independent, scalable, lightweight and efficient serialized data structure protocol that can be used for network communication and data storage. Since we finally convert the model, we need such a serialization tool. NCNN needs to use protobuf for serialization.

protobuf can be entered. Click to download. Here I choose the 21.11-all version. It seems that the latest version (the second picture) has source code and compiled version for the corresponding platform. This is a bit like the installation process of Cmake. Here, I use the source code to install the 21.11-all version,
insert image description here

insert image description here

4.1 Compile and install

1 unzip

tar xzvf protobuf-all-21.11.tar.gz

insert image description here
2 Compile and install

#切换到解压后的 protobuf 文件夹
#这个会自动生成一些文件
./autogen.sh
#配置后期的编译安装过程
./configure --prefix=/YourPath/protobuf_21_11
#编译和安装 
make -j8
make install

after make -j* command:
insert image description here
after make install
insert image description here

After compiling and installing, under the /YourPath/protobuf_21_11 path, you will see
insert image description here

3 Environment variable configuration (still in ~/.bashrc), don't forget to source it.
As shown in the figure, configure the compiled bin path in the $PATH path.
insert image description here
4 Verify that the installation was successful

protoc --version

insert image description here

5 Compile and install NCNN

Click to download the official source code, then decompress and install
insert image description here

You can also use the following command to download and compile in one stop. It should be noted here that the process of downloading and compiling may be slow due to the need to access github. Of course, git clone https://github.com/Tencent/ncnn.git can use git clone https://gitee.com/Tencent/ncnn.git instead, which is faster. But the git submodule update --init command will directly access the github link, so you need to wait patiently.

Compile the configuration before installation, this must be carefully read! ! ! !

Due to the non-administrator privileges we use, this requires our settings in CMakeLists.txt in the NCNN module. This is very important, otherwise there will be compilation problems.

  1. After CMakeLists.txt under the project folder
    insert image description here
    is opened:
    insert image description here
# protobuf path
list(APPEND CMAKE_SYSTEM_PREFIX_PATH "/yourpath_编译好的protobuf/protobuf_21_11/include;/yourpath_编译好的protobuf/protobuf_21_11/lib")
message(STATUS "==== CMAKE_SYSTEM_PREFIX_PATH: ${CMAKE_SYSTEM_PREFIX_PATH}")

The above sentence is mainly for the Protobuf of CMakeLists.txt under tools/onnx in the NCNN project.
insert image description here
insert image description here
insert image description here
2. Configure OpenCV in the environment variable. Note that I added opencv_3_4_16/lib64/ in the system path ~/.bashrc before it can Compile, this is mainly to correspond to
insert image description here
insert image description here
the following in ncnn/examples/CMakeLIsts.txt before compiling:

git clone https://github.com/Tencent/ncnn.git
cd ncnn
#这个是为了下载一些未下载的子包
git submodule update --init
#在文件夹中创建的新文件夹
mkdir build
cd build
#此步之后 , 推荐在 计算节点 含 GPU的节点操作
#注意 cmake 后面有两个点 即 使用 ../CMakeLists.txt 文件作为起始点在当前目录中生成
#这里需要注意,cmake 如果按照默认设置,会加载系统默认的 GCC/G++环境,即Centos7中默认的 4.8.5,这可能与上述其他包 protobuf opencv所使用的编译版本不同,这会导致在编译过程中出错。由于上述安装的 OpenCv版本支持 Gcc/G++ 5.0以上版本,所以这里需要让NCNN在与之对应的版本上进行编译,即gcc/g++ 5.0以上的版本
#我们使用 cmake -D CMAKE_C_COMPILER=/YourPath/gcc53/bin/gcc -D CMAKE_CXX_COMPILER=/YourPath/gcc53/bin/g++ ..
cmake .. (本步如果您 yum install cmake3,可以使用 cmake3 .. )
或下面的命令适用于特制定某个版本的 GCC/G++进行 camke ,直接影响后续的编译安装,具体原因看上面的注释
cmake -D CMAKE_C_COMPILER=/YourPath/gcc53/bin/gcc -D CMAKE_CXX_COMPILER=/YourPath/gcc53/bin/g++ ..
#也可以是 -j16 -j32等 加速编译
make -j8
make install

insert image description here
The above is the whole process, if there are any problems in the future, we will add again!

some exceptions

std::__cxx11 const@GLIBCXX_3.4.21“ 或者 undefined reference' to If "undefined reference to google appears during compilation :
:caffe.pb.cc:(:protobuf::RepeatedPtrField.text<+0x47e3std::__cxx11)::basic_string<char: undefined, std::char_traits reference <charto `> , std:google:::protobuf:allocator: > >::GetRepeatedPtrField<std(int:) const'
is similar to this kind of exception, indicating that there is a problem with the version of gcc and g++ used for your compilation . Please read the above process carefully. Let each process compile and use the same version of GCC/G++. This is what I personally tested. Of course, if you are directly using your own computing node, it doesn’t need to be so complicated. Just follow my article to install it .

Guess you like

Origin blog.csdn.net/qq_29750461/article/details/129342774