如何将Caffe的prototxt转换为PyTorch模型?

16

到目前为止,我一直在使用pytorch-caffe-darknet-convert存储库。在克服了许多问题(无法转换的concat和eltwise层)后,我最终得到了类似于darknet配置文件的东西:

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

4
我还没有仔细查看,但你可以将Caffe转换为Caffe2,再将Caffe2转换为ONNX,然后转换为PyTorch。或者,参考这个Caffe2转换为ONNX的教程,然后再转换为PyTorch。虽然肯定有更好的方法。 - Yamaneko
或者你可以从别人的darknet pytorch实现开始,比如:https://github.com/ayooshkathuria/pytorch-yolo-v3/blob/master/darknet.py - Steven
1个回答

1
您可以使用以下其中一个库:

Usage

Conversion

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

Use the model in Pytorch

from caffe2pth.caffenet import *

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

作为一个火炬迷,Caffe模型让我有些担心,但后来发现这是一个多么美妙的世界! - vyi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接