net surgery pycaffe 复制权重和重塑

net surgery pycaffe copy weights and reshape

我正在尝试将预训练模型大小为 3x3x3 的层 'con_1' 的学习权重复制到新层 'con_1_1',这样新层的大小将为 6x3x3 (6频道)。我实际上是在尝试将大小为 3x3x3 的权重复制到 6x3x3。我怎样才能使用 pycaffe 做到这一点。

      layer name: 'con_1'
      size: 3x3x3

      new layer name: 'con_1_1'
      size: 6x3x3

      con_1_1 should be [con_1, con_1] % just concatenation of two con_1 weights

您必须使用 .prototxt 文件和 .caffemodel 文件读取网络。然后将原始网络中的权重复制到一个变量中,然后将它们复制到编辑后的网络中。

net = caffe.Net('path/to/conv.prototxt', 'path/to/conv.caffemodel', caffe.TEST)
W = net.params['con_1'][0].data[...]
b = net.params['con_1'][1].data[...]

net = caffe.Net('path/to/conv2.prototxt', 'path/to/conv2.caffemodel', caffe.TEST)
W_1 = numpy.concatenate(W, W, axis=2)
b_1 = numpy.concatenate(b, b, axis=0)
net.params['con_1_1'][0].data[...] = W_1
net.params['con_1_1'][1].data[...] = b_1

查看此 link and this link 了解更多信息。