caffeモデル回転ncnnモデル

3420 ワード

ncnnはテンセント優図の最近のオープンソースの移動端に適した深さ学習フレームワークである.MobileNetは、Googleが2017年4月に発表した論文MobileNets:Efficient Convolutional Neural Networks for Mobile Vision Applicationsで提案したネットワークです.depthwise convolutionが導入されたため、mobileNetのモデルは非常に小さく、1000クラスの分類モデルは16.9 Mしかなく、モバイル側での導入に適しています.この文書では、Mac上でmobileNetをncnnで実行することを試みます.
1.ncnnをダウンロードしてコンパイルする
git clone https://github.com/Tencent/ncnn
cd ncnn
mkdir build && cd build
cmake ..
make -j
make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

  • コンパイルが完了するとncnn/build/toolsディレクトリの下にcaffe 2 ncnnとncnn 2 memの2つの実行可能ファイルが生成されていることがわかります.caffe 2 ncnnの役割はcaffeモデルをncnnモデルに生成し、ncnn 2 memはモデルを暗号化することができる.
    2.MobileNetのcaffeモデルとプロファイルのダウンロード
    からhttps://github.com/shicai/MobileNet-Caffeでダウンロードして、ダウンロードしてmobilenetを得るdeploy.その通りだcaffemodelの2つのファイル.
    3.旧版caffeモデルから新版caffeモデルへ
    ncnnは新しいcaffeモデルの変換しかサポートしていないので、まず第2ステップでダウンロードしたcaffeモデルを新しいcaffeモデルに変換する必要があります.新しいcaffeフレームワークには変換ツールが付属しており、使用姿勢は以下の通りです.
    $ ~/caffe/build/tools/upgrade_net_proto_text mobilenet_deploy.prototxt mobilenet_deploy_new.prototxt
    $ ~/caffe/build/tools/upgrade_net_proto_binary mobilenet.caffemodel mobilenet_new.caffemodel
  • 1
  • 2

  • 4.新版caffeモデル転ncnnモデル
    最初のステップで生成されたncnn/build/toolsディレクトリの下でcaffe 2 ncnnを使用して、新しいmobileNetモデルを変換します.
    $./caffe2ncnn mobilenet_deploy_new.prototxt mobilenet_new.caffemodel mobilenet.param mobilenet.bin
  • 1

  • 生成されたncnnフォーマットのモデルでは、.paramはネットワークのプロファイルとして理解できる.binは、ネットワークのパラメータ(様々な重み)ファイルとして理解できる.モデルを暗号化する必要がある場合は、次のコマンドを使用します.
     $./ncnn2mem mobilenet.param mobilenet.bin mobilenet.id.h mobilenet.mem.h
  • 1

  • 最後にmobilenetを生成することができる.param.binのようなバイナリ暗号化ファイル.ncnnは暗号化と非暗号化の2つのファイルの読み取り方法が異なる.
    //load    ncnn  
    ncnn::Net net;
    net.load_param("mobilenet.param");
    net.load_model("mobilenet.bin");
    //load   ncnn  
    ncnn::Net net;
    net.load_param_bin("mobilenet.param.bin");
    net.load_model("mobilenet.bin");
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

  • 5.着工:Xcodeを使用してコードを作成して運行する
    Xcodeを使用してプロジェクトを新規作成し、最初のステップでコンパイルされたncnnライブラリをプロジェクトにインポートします.コンパイルが完了したncnn libはncnn/build/installディレクトリの下にあります.ncnnライブラリを構成したら、exampleの下のsqueezenet.を参照してください.cppコードはmobileNetモデルの導入を行う.修正したコードは以下の通りです.
    static int detect_mobileNet(const cv::Mat& bgr, std::vector& cls_scores)
    {
        ncnn::Net mobileNet;
        mobileNet.load_param("/Users/Guigu/Documents/projects/ncnn_mobileNet/mobilenet.param");
        mobileNet.load_model("/Users/Guigu/Documents/projects/ncnn_mobileNet/mobilenet.bin");
    
        ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 224, 224);
    
        const float mean_vals[3] = {103.94f, 116.78f, 123.68f};
        const float norm_vals[3] = {0.017f,0.017f,0.017f};
        in.substract_mean_normalize(mean_vals, norm_vals);
    
    
        ncnn::Extractor ex = mobileNet.create_extractor();
        ex.set_light_mode(true);
    
        ex.input("data", in);
    
        ncnn::Mat out;
        ex.extract("fc7", out);  //   squeezenet  
    
        cls_scores.resize(out.c);
        for (int j=0; j