16

So far I was using the pytorch-caffe-darknet-convert repository. After overcoming numerous problems (concat and eltwise layers not convertible) I ended up with something that looks like a darknet config file:

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

Does someone know how to convert the output new_net_file.cfg into pytorch? Alternatively is there another way of converting caffe prototxt files into pytorch?
I would like to have the same behaviour as caffe-tensorflow I'll post both my caffe prototxt and the output new_net_file.cfg below as reference.

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

the (darknet) config file:

[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
iacob
  • 20,084
  • 6
  • 92
  • 119
mcExchange
  • 6,154
  • 12
  • 57
  • 103
  • 4
    I haven't looked thoroughly, but you could convert [Caffe to Caffe2](https://caffe2.ai/docs/caffe-migration.html#caffe-to-caffe2), [Caffe2 to ONNX, then to PyTorch](http://pytorch.org/tutorials/advanced/super_resolution_with_caffe2.html). Or, check this reference from [Caffe2 to ONNX](https://github.com/onnx/tutorials/blob/master/tutorials/Caffe2OnnxExport.ipynb), then to [Pytorch](http://pytorch.org/docs/master/onnx.html). There must be a better way, though. – Yamaneko Dec 07 '17 at 17:23
  • Alternatively you could start with someone else's darknet pytorch implementation like: https://github.com/ayooshkathuria/pytorch-yolo-v3/blob/master/darknet.py for example – Steven Dec 07 '20 at 15:40

1 Answers1

1

You can use one of the following libraries:

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))
iacob
  • 20,084
  • 6
  • 92
  • 119
  • Being a torch guy, the Caffe model had me worried, but then what a wonderful world ! – vyi Jul 03 '23 at 14:44