Vscode远程编译调试ncnn

2021-07-16  本文已影响0人  SunJi_

背景:

1、安装软件

vscode、mobaxterm、docker镜像,百度即可解决。
ps:vscode远程连接之后,本地和远程是不一样的,环境变量和插件都需要各一份。

2、vscode远程连接服务器

下载remote-ssh插件,配置config文件里面的host、port,启动即可。密匙配置稍微复杂点,不嫌麻烦可以每次手动输入密码。

3、ncnn安装编译

# gcc/g++
apt-get update  //更新
apt-get install build-essential
# cmake
apt-get install cmake
// 安装依赖
apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
apt-get install libgtk2.0-dev
apt-get install pkg-config
apt-get update
apt-get upgrade
# opencv
unzip opencv-4.3.0.zip
cd opencv-4.3.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local WITH_GTK=ON..
make
make install  // 等待几分钟编译完,安装环境依赖
vi /etc/ld.so.conf.d/opencv.conf  //末尾插入/usr/local/lib
gedit /etc/bash.bashrc  
//末尾插入 
//PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
//export PKG_CONFIG_PATH  
ldconfig
source /etc/bash.bashrc  //更新
pkg-config opencv --modversion  //显示版本表示安装成功
# protobuf
git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf
git submodule update --init --recursive
./autogen.sh
./configure
make
make check
make install
ldconfig  //refresh shared library cache
protoc --version  //显示版本信息表示安装成功
# ncnn
// 详情见:https://github.com/Tencent/ncnn/wiki/how-to-build
git clone https://github.com/Tencent/ncnn
mkdir build && cd build
cmake .. //可以配置DCMAKE_BUILD_TYPE=Debug、Release···
make -j4
make install 

4、测试和工程搭建

测试:ncnn例库里面有模板,可以直接编译运行,测试前几步是否成功。

cd ../ncnn
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Debug ..
make
make install

cd examples
../build/examples/squeezenet ../images/256-ncnn.png
// 显示数据不报错,则编译正确

利用ncnn开发自研工程:首先创建新的文件路径,eg my_example。然后将cpp文件和CMakelist文件放进去,还有ncnn/src里面的所有文件。最后再修改一下工程的include_directories,按照例库一样的方法进行编译运行。

重点文件:

# CMakelist模板
cmake_minimum_required(VERSION 3.5)
find_package(OpenCV REQUIRED core highgui imgproc)

// 需要包含的库和链接
include_directories(xx/.../ncnn/build/install/include/ncnn)
link_directories(xx/.../ncnn/build/install/lib)

FIND_PACKAGE( OpenMP REQUIRED)  
if(OPENMP_FOUND)  
    message("OPENMP FOUND")  
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")  
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")  
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")  
endif()  

add_executable(squeezenet squeezenet.cpp)
target_link_libraries(main ncnn ${OpenCV_LIBS})
// squeezenet.cpp
#include "net.h"
#include <algorithm>
#if defined(USE_NCNN_SIMPLEOCV)
#include "simpleocv.h"
#else
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#endif
#include <stdio.h>
#include <vector>

static int detect_squeezenet(const cv::Mat& bgr, std::vector<float>& cls_scores)
{
    ncnn::Net squeezenet;
    squeezenet.opt.use_vulkan_compute = true;

    // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
    squeezenet.load_param("squeezenet_v1.1.param");
    squeezenet.load_model("squeezenet_v1.1.bin");

    ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 227, 227);

    const float mean_vals[3] = {104.f, 117.f, 123.f};
    in.substract_mean_normalize(mean_vals, 0);

    ncnn::Extractor ex = squeezenet.create_extractor();

    ex.input("data", in);

    ncnn::Mat out;
    ex.extract("prob", out);

    cls_scores.resize(out.w);
    for (int j = 0; j < out.w; j++)
    {
        cls_scores[j] = out[j];
    }
    return 0;
}

static int print_topk(const std::vector<float>& cls_scores, int topk)
{
    // partial sort topk with index
    int size = cls_scores.size();
    std::vector<std::pair<float, int> > vec;
    vec.resize(size);
    for (int i = 0; i < size; i++)
    {
        vec[i] = std::make_pair(cls_scores[i], i);
    }

    std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
                      std::greater<std::pair<float, int> >());

    // print topk and score
    for (int i = 0; i < topk; i++)
    {
        float score = vec[i].first;
        int index = vec[i].second;
        fprintf(stderr, "%d = %f\n", index, score);
    }
    return 0;
}

int main(int argc, char** argv)
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
        return -1;
    }

    const char* imagepath = argv[1];

    cv::Mat m = cv::imread(imagepath, 1);
    if (m.empty())
    {
        fprintf(stderr, "cv::imread %s failed\n", imagepath);
        return -1;
    }

    std::vector<float> cls_scores;
    detect_squeezenet(m, cls_scores);

    print_topk(cls_scores, 3);
    return 0;
}

5、遇到的问题

docker run -ti --cap-add=SYS_PTRACE ubuntu
上一篇下一篇

猜你喜欢

热点阅读