日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一步一步教你如何将 yolov3/yolov4 转为 caffe 模型

發布時間:2025/3/15 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一步一步教你如何将 yolov3/yolov4 转为 caffe 模型 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實際工作中,目標檢測 yolov3 或者 yolov4 模型移植到 AI 芯片中,經常需要將其先轉換為 caffe1.x 模型,大家可能或多或少也有這方面的需求。例如華為海思 NNIE 只支持caffe1.x 模型,所以 yolov3/yolov4 模型要想在海思芯片上部署,轉換為 caffe1.x 模型是必須的。

今天,專門給大家寫一下將? yolov3/yolov4 模型轉為 caffe 模型的詳細步驟。

0.?系統環境

  • Ubuntu 16.04

  • cuda?9.x

1. 部署 caffe 環境

常規的方法是下載 caffe 的源碼,進行編譯安裝。但是,這種方法比較復雜,不太容易成功。更簡單的方法是直接下載 caffe1.x 的docker 鏡像。這種方法需要你提前在 Ubuntu 里安裝了 nvidia-docker。

打開 docker hub 網站:

https://registry.hub.docker.com/

搜索 caffe,看到這個鏡像:

點擊進去,看到該鏡像的下拉命令:

sudo docker?pull?bvlc/caffe

打開 Ubuntu 終端,輸入上面的下拉命令,將 caffe 的 docker 鏡像下載下來。

下載完成之后,終端輸入命令:

sudo?docker?images

就可以看到已下載的 caffe 鏡像了。

為鏡像創建容器:

docker run --runtime=nvidia --name caffe -i -t bvlc/caffe /bin/bash

這里我們給該容器起的名字是 caffe,大家可以自由設置,根據?`sudo docker ps -a` 這條命令來查看各個容器。

這樣,我們就進入了 caffe1.x 的 docker 容器之內了。

該容器已部署好了 caffe1.x,caffe1.x 路徑為:

2. caffe 源碼修改

因為官方 caffe1.x 框架不支持 yolo3/yolov4 的 upsample 層,所以需要手動增加 upsample 層,對 caffe 源碼進行修改。

克隆 GitHub 上的轉換工具項目:

git clone https://github.com/ChenYingpeng/darknet2caffe.git

將?darknet2caffe/caffe_layers/mish_layer?下的?mish_layer.hpp?文件和 darknet2caffe/tree/master/caffe_layers/upsample_layer 下的?upsample_layer.hpp? 拷貝到容器的路徑:/opt/caffe/include/caffe/layers??下。

將?darknet2caffe/caffe_layers/mish_layer?下的?mish_layer.cpp、mish_layer.cu?文件和 darknet2caffe/tree/master/caffe_layers/upsample_layer 下的?upsample_layer.cpp、upsample_layer.cu? 拷貝到容器的路徑:/opt/caffe/src/caffe/layers/??下。

將?darknet2caffe/caffe_layers/pooling_layer?下的?pooling_layer.cpp?拷貝到容器的路徑:/opt/caffe/src/caffe/layers/??下。

然后,打開容器內的 caffe 文件:/opt/caffe/src/caffe/proto/caffe.proto。按照如下說明修改相應字段的程序。

// LayerParameter next available layer-specific ID: 147 (last added: recurrent_param) message LayerParameter {optional TileParameter tile_param = 138;optional VideoDataParameter video_data_param = 207;optional WindowDataParameter window_data_param = 129; ++optional UpsampleParameter upsample_param = 149; //added by chen for Yolov3, make sure this id 149 not the same as before. ++optional MishParameter mish_param = 150; //added by chen for yolov4,make sure this id 150 not the same as before. }// added by chen for YoloV3 ++message UpsampleParameter{ ++ optional int32 scale = 1 [default = 1]; ++}// Message that stores parameters used by MishLayer ++message MishParameter { ++ enum Engine { ++ DEFAULT = 0; ++ CAFFE = 1; ++ CUDNN = 2; ++ } ++ optional Engine engine = 2 [default = DEFAULT]; ++}

其中,++ 表示該行是增加的內容。

3. caffe 重新編譯

修改完 caffe 的一些源碼之后,需要對 caffe 重新編譯。

進入?/opt/caffe/build 目錄,輸入以下命令:

make clean make all -j8 make pycaffe -j8

caffe 重新編譯之后,就可以對 yolov3/yolov4 模型進行 caffe 轉換了。

4.?模型轉換

準備好我們已有的 yolov3 模型的配置文件和權重文件,例如:yolov3.cfg 和 yolov3.weights。在?darknet2caffe?目錄下,輸入以下命令:

python darknet2caffe.py ./yolov3.cfg ./yolov3.weights ./yolov3.prototxt ./yolov3.caffemodel

如果輸出類似下面的語句,則證明轉換成功!

I0522 10:19:19.015708 25251 net.cpp:228] layer1-act does not need backward computation. I0522 10:19:19.015712 25251 net.cpp:228] layer1-scale does not need backward computation. I0522 10:19:19.015714 25251 net.cpp:228] layer1-bn does not need backward computation. I0522 10:19:19.015718 25251 net.cpp:228] layer1-conv does not need backward computation. I0522 10:19:19.015722 25251 net.cpp:228] input does not need backward computation. I0522 10:19:19.015725 25251 net.cpp:270] This network produces output layer139-conv I0522 10:19:19.015731 25251 net.cpp:270] This network produces output layer150-conv I0522 10:19:19.015736 25251 net.cpp:270] This network produces output layer161-conv I0522 10:19:19.015911 25251 net.cpp:283] Network initialization done. unknow layer type yolo unknow layer type yolo save?prototxt?to?./yolov3.prototxt save?caffemodel?to?./yolov3.caffemodel

其中,yolov3.prototxt 和 yolov3.caffemodel 為轉換后的 caffe 模型。

至此,yolov3/yolov4 轉換為 caffe 模型完成!


AI 角:機器人遛彎

總結

以上是生活随笔為你收集整理的一步一步教你如何将 yolov3/yolov4 转为 caffe 模型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。