怎么把Caffe的BatchNorm重量转换成pytorch BathNorm?

时间:2019-04-12 03:32:23

标签: caffe pytorch pycaffe

可以从pycaffe读取caffe模型的BathNorm和Scale重量,这是BatchNorm中的三个权重和Scale中的两个权重。我尝试使用以下代码将这些砝码复制到pytorch BatchNorm:

if 'conv3_final_bn' == name:
    assert len(blobs) == 3, '{} layer blob count: {}'.format(name, len(blobs))
    torch_mod['conv3_final_bn.running_mean'] = blobs[0].data
    torch_mod['conv3_final_bn.running_var'] = blobs[1].data
elif 'conv3_final_scale' == name:
    assert len(blobs) == 2, '{} layer blob count: {}'.format(name, len(blobs))
    torch_mod['conv3_final_bn.weight'] = blobs[0].data
    torch_mod['conv3_final_bn.bias'] = blobs[1].data

两个BatchNorm的行为不同。我还尝试设置conv3_final_bn.weight = 1和conv3_final_bn.bias = 0来验证caffe的BN层,结果也不匹配。

我应该如何处理错误的匹配?

1 个答案:

答案 0 :(得分:0)

知道了! caffe的BatchNorm中仍有第三个参数。代码应为:

if 'conv3_final_bn' == name:
    assert len(blobs) == 3, '{} layer blob count: {}'.format(name, len(blobs))
    torch_mod['conv3_final_bn.running_mean'] = blobs[0].data / blobs[2].data[0]
    torch_mod['conv3_final_bn.running_var'] = blobs[1].data / blobs[2].data[0]
elif 'conv3_final_scale' == name:
    assert len(blobs) == 2, '{} layer blob count: {}'.format(name, len(blobs))
    torch_mod['conv3_final_bn.weight'] = blobs[0].data
    torch_mod['conv3_final_bn.bias'] = blobs[1].data