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

标签 python pytorch caffe torch darknet

到目前为止,我使用的是 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下面作为引用。

我的_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

最佳答案

您可以使用以下库之一:

  • caffemodel2pytorch
  • Caffe2Pytorch

  • 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))
    

    关于python - 如何将caffe prototxt转换为pytorch模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47690871/

    相关文章:

    numpy - InfogainLoss 层

    python - 在 Python 中将字符串转换为日期时间

    python - WebDriverWait 未按预期工作

    python - networkx 说我的节点比实际的少

    python - Pytorch - 在 softmax 层之后选择最佳概率

    neural-network - 如何在 Caffe 的网络中出现多次损失?

    Python、pywin32、Windows 服务和多处理

    initialization - 如何决定使用哪种模式进行 'kaiming_normal' 初始化

    python - Pytorch 中对张量进行上采样并将其转换为 RGB 的最佳方法?

    machine-learning - 理解pycaffe中的load_image()方法