如何将 caffe prototxt 转换为 pytorch 模型?

Posted

技术标签:

【中文标题】如何将 caffe prototxt 转换为 pytorch 模型?【英文标题】:How to convert caffe prototxt to pytorch model? 【发布时间】:2018-05-21 07:30:50 【问题描述】:

到目前为止,我使用的是 pytorch-caffe-darknet-convert 存储库。在克服了许多问题(concat 和 eltwise 层不可转换)之后,我最终得到了一个看起来像暗网配置文件的东西:

python caffe2darknet.py my_prototxt.txt my_caffemodel.caffemodel new_net_file.cfg new_model.weights

有人知道如何将输出 new_net_file.cfg 转换为 pytorch 吗?或者还有另一种将 caffe prototxt 文件转换为 pytorch 的方法吗? 我希望有与caffe-tensorflow 相同的行为 我将在下面发布我的 caffe prototxt 和输出 new_net_file.cfg 作为参考。

my_prototxt:

input: "data"
input_shape 
  dim: 1
  dim: 240
  dim: 144
  dim: 240


layer 
  name: "conv1_1"
  type: "Convolution"
  bottom: "data"
  top: "conv1_1"
  convolution_param 
    num_output: 16
    pad: 3
    pad: 3
    pad: 3
    kernel_size: 7
    kernel_size: 7
    kernel_size: 7
    stride: 2
    weight_filler 
      type: "xavier"
    
    bias_filler 
      type: "constant"
    
    engine: CUDNN
    axis: 1
  

layer 
  name: "relu1_1"
  type: "ReLU"
  bottom: "conv1_1"
  top: "conv1_1"

layer 
  name: "reduction2_1"
  type: "Convolution"
  bottom: "conv1_1"
  top: "reduction2_1"
  convolution_param 
    num_output: 32
    bias_term: false
    pad: 0
    kernel_size: 1
    stride: 1
    weight_filler 
      type: "xavier"
    
    bias_filler 
      type: "constant"
    
  

layer 
  name: "conv2_1"
  type: "Convolution"
  bottom: "conv1_1"
  top: "conv2_1"
  convolution_param 
    num_output: 32
    pad: 1
    pad: 1
    pad: 1
    kernel_size: 3
    kernel_size: 3
    kernel_size: 3
    stride: 1
    weight_filler 
      type: "xavier"
    
    bias_filler 
      type: "constant"
    
    engine: CUDNN
    axis: 1
  

layer 
  name: "relu2_1"
  type: "ReLU"
  bottom: "conv2_1"
  top: "conv2_1"

layer 
  name: "conv2_2"
  type: "Convolution"
  bottom: "conv2_1"
  top: "conv2_2"
  convolution_param 
    num_output: 32
    pad: 1
    pad: 1
    pad: 1
    kernel_size: 3
    kernel_size: 3
    kernel_size: 3
    stride: 1
    weight_filler 
      type: "xavier"
    
    bias_filler 
      type: "constant"
    
    axis: 1
  

layer 
  name: "res2_2"
  type: "Eltwise"
  bottom: "reduction2_1"
  bottom: "conv2_2"
  top: "res2_2"
  eltwise_param  operation: SUM 

layer 
  name: "add2_2"
  type: "ReLU"
  bottom: "res2_2"
  top: "res2_2"

layer 
  name: "pool2"
  type: "Pooling"
  bottom: "res2_2"
  top: "pool2"
  pooling_param 
    pool: MAX
    kernel_size: 2
    stride: 2
    engine: CUDNN
  

[...] # I cropped it here, since file is too lengthy

(暗网)配置文件:

[net]
batch=1
channels=240
height=144
width=240

[convolutional]
filters=16
size=['7', '7', '7']
stride=2
pad=1
activation=relu

[convolutional]
filters=32
size=1
stride=1
pad=1
activation=linear

[route]
layers=-2

[convolutional]
filters=32
size=['3', '3', '3']
stride=1
pad=1
activation=relu

[convolutional]
filters=32
size=['3', '3', '3']
stride=1
pad=1
activation=linear

[shortcut]
from=-4
activation=relu

[maxpool]
size=2
stride=2

[...] # I cropped it here, since file is too lengthy

【问题讨论】:

我没有仔细看,但你可以转换Caffe to Caffe2,Caffe2 to ONNX, then to PyTorch。或者,从Caffe2 to ONNX 查看此引用,然后查看Pytorch。不过,一定有更好的方法。 或者,您可以从其他人的暗网 pytorch 实施开始,例如:github.com/ayooshkathuria/pytorch-yolo-v3/blob/master/… 【参考方案1】:

您可以使用以下库之一:

caffemodel2pytorch

Caffe2Pytorch

用法

转化

python caffe2pth_convertor.py \
--prototxt=YOUT_PROTOTXT_PATH \
--caffemodel=YOUT_CAFFEMODEL_PATH \
--pthmodel=OUTPUT_PTHMODEL_PATH

在 Pytorch 中使用模型

from caffe2pth.caffenet import *

net = CaffeNet(YOUT_PROTOTXT_PATH)
net.load_state_dict(torch.load(OUTPUT_PTHMODEL_PATH))

【讨论】:

以上是关于如何将 caffe prototxt 转换为 pytorch 模型?的主要内容,如果未能解决你的问题,请参考以下文章

Caffe---Pycaffe进行网络结构(xxx.prototxt)可视化

caffe的model和prototxt转pytorch的model

caffe - 训练前如何修改 prototxt

Caffe详解Caffe的lenet_solver.prototxt

解析Caffe框架的prototxt模型文件

解析Caffe框架的prototxt模型文件