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

时间:2017-12-07 08:41:59

标签: caffe torch pytorch darknet

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

"******st"

有人知道如何将输出python caffe2darknet.py my_prototxt.txt my_caffemodel.caffemodel new_net_file.cfg new_model.weights 转换为pytorch吗?另外还有另一种将caffe原型文件转换成pytorch的方法吗? 我希望与caffe-tensorflow具有相同的行为 我将发布我的caffe原型文本和下面的输出new_net_file.cfg作为参考。

my_prototxt:

new_net_file.cfg

(darknet)配置文件:

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

1 个答案:

答案 0 :(得分:0)

您可以使用以下库之一:

<块引用>

用法

转化

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))
相关问题