Convert Caffe to PyTorch

Hello. I want to convert the model from Caffe to PyTorch. Model is resnet50_1by2 custom dataset.

Variant 1. Using torch and loadcaffe converted model.
Result: not all layers were converted.
Example:

– warning: module ‘bn_stage0_block0_branch2c [type BatchNorm]’ not found
– warning: module ‘scale_stage0_block0_branch2c [type Scale]’ not found
– warning: module ‘eltwise_stage0_block0 [type Eltwise]’ not found

Variant 2. Install Caffe and convert params to numpy. Load numpy arrays to PyTorch.
Example:
Caffe → PyTorch

conv_1_0 → conv_1.weight
conv_1_1 → conv_1.bias
bn_1_0 → bn_1.running_mean
bn_1_1 → bn_1.running_var
scale_1_0 → bn_1.weight
scale_1_1 → bn_1.bias

Result: All the sizes of layers coincide, architecture too. All weights correspond! PyTorch model working! But when I make the profiling, the result is different after each layer and as a result the values are completely wrong.

What could be wrong with the Variant 2?

P.S. Pictures for testing are the same, normalization too. 3x224x224 (0, 255)

More information here github

Maybe it is because Caffe assume image is in BGR format while PyTorch is assuming Image is in RGB format? So you may need to swap the order of first conv layer weight or you need to make sure Image feed to network is in BGR format in PyTorch.

1 Like

You have to make sure you have converted it correctly.

Then you have to make sure the image to the model in PyTorch is also in BGR format and has been pre-processed in the same way as in Caffe (for example, normalization, data augmentation, etc.)

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)