Caffe学习系列(十):腾讯ncnn框架

时间:2024-04-09 09:22:55

Caffe学习系列(十):腾讯ncnn框架

1.ncnn安装

安装依赖

sudo apt-get install -y gfortran 
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler 
sudo apt-get install --no-install-recommends libboost-all-dev 
sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev libatlas-base-dev

下载ncnn

git clone https://github.com/Tencent/ncnn
cd ncnn

在编译之前,我们希望和示例程序一起编译
需要修改CmakeList.txt文件。去掉下面两段代码前面的#

add_subdirectory(examples)
add_subdirectory(benchmark)

开始编译

mkdir build
cd build
cmake ..
make -j4
make install 

编译完成后,在 build 目录下看到会有:

# 示例程序的可执行文件全部在examples内
# ncnn库文件以及头文件全部在install目录下
# tools目录下是一些转化工具
examples  install   tools
2.YOLOv2测试

下载训练好的yolo模型:
https://github.com/eric612/MobileNet-YOLO/tree/master/models/yolov2
这里我们下载:
mobilenet_yolo_deploy_iter_80000.caffemodel mobilenet_yolo_deploy.prototxt
使用 caffe2ncnn工具进行转换

tools/caffe/caffe2ncnn mobilenet_yolo_deploy.prototxt mobilenet_yolo_deploy_iter_80000.caffemodel mobilenet_yolo.param mobilenet_yolo.bin

将生成的转换文件复制到 build/examples 目录下,运行以下命令

./yolov2 fish-bike.jpg

Caffe学习系列(十):腾讯ncnn框架

3.MobileNet_SSD测试

同样地,将自己训练好的模型进行转换

tools/caffe/caffe2ncnn MobileNetSSD_deploy.prototxt mobilenet_iter_14000.caffemodel mobilenet_ssd_voc_ncnn.param mobilenet_ssd_voc_ncnn.bin

复制到 build/examples 目录下

./mobilenetssd 00338.jpg

Caffe学习系列(十):腾讯ncnn框架

4. 问题及解决办法
4.1 mobilenetssd检测报错
find_blob_index_by_name data_splitncnn_6 failed
find_blob_index_by_name data_splitncnn_5 failed
find_blob_index_by_name data_splitncnn_4 failed
find_blob_index_by_name data_splitncnn_3 failed
find_blob_index_by_name data_splitncnn_2 failed
find_blob_index_by_name data_splitncnn_1 failed
find_blob_index_by_name data_splitncnn_0 failed
段错误 (核心已转储)

将 MobileNetSSD_deploy.prototxt 的数据输入层进行修改

# 修改前
#input: "data"
#input_shape {
#  dim: 1
#  dim: 3
#  dim: 300
#  dim: 300
#}
# 修改后
layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 1 dim: 3 dim: 300 dim: 300 } }
}
4.2 标签问题

修改 ncnn/examples/mobilessd.cpp 文件

static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects)
{
    static const char* class_names[] = {"background",
        "aeroplane", "bicycle", "bird", "boat",
        "bottle", "bus", "car", "cat", "chair",
        "cow", "diningtable", "dog", "horse",
        "motorbike", "person", "pottedplant",
        "sheep", "sofa", "train", "tvmonitor"};

修改后再重新编译。

5.后续工作

视频流检测
ncnn上基于Caffe用MobileNet_SSD训练和测试自己的数据