Ink
Ink

Reputation: 963

How to convert BatchNorm weight of caffe to pytorch BathNorm?

BathNorm and Scale weight of caffe model can be read from pycaffe, which are three weights in BatchNorm and two weights in Scale. I tried to copy those weights to pytorch BatchNorm with codes like this:

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

The two BatchNorm acts differently. I also tried to set conv3_final_bn.weight=1 and conv3_final_bn.bias=0 to verify the BN layer of caffe, the results didn't match either.

How should I deal with the wrong matching?

Upvotes: 0

Views: 671

Answers (1)

Ink
Ink

Reputation: 963

Got it! There is still a third parameter in BatchNorm of caffe. Codes should be:

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

Upvotes: 2

Related Questions