Yet-another-efficientdet-pytorch: can not load model weights

Created on 13 Apr 2020  ·  17Comments  ·  Source: zylo117/Yet-Another-EfficientDet-Pytorch

Thanks for sharing your code. When I loaded the code weights, I found that the dimensions were wrong, but I strictly followed your code to load.

`
def get. net():

 nun_classes = 7
 anchors_ratios = '[(1.0, 1.0),(1.4, 0.7),(0.7, 1.4)]'
 anchors_scales = '[2 ** 0, 2 ** (1.0 1 3.0), 2 ** (2.0 / 3.0)] '
 compound_coef = 2
 my_model = EfficientDetBackbone(nun_ classes = nun_ classes, compound_coef = 
                       compound_coef, ratios = eval(anchors_ ratios), scales = eval(anchor))
weights_path = ' ./efficientdet-d2 . pth'
my_model.load_state_dict(torch. load(weights_ path), strict = False)

return my_model

if __ name__== "__ main__" :
model = get_net()
`

RuntineError: Error(s) in loading state_ dict for EfficientDetBackbone:
size mismatch for classifier .header . pointwise_ _conv . conv . weight :
copying a paran with shape torch.size([810, 112,1,1]) from
checkpoint, the shape in current moJdeil is torch. size([63, 112, 1, 1]).
size mismatch for classifier .header . pointwise_ conv. conv. bias:
copying a param with shape torch. Size([810]) from checkpoint, the shape
in current model is torch. Size([63]).

Most helpful comment

Go through your imports , some where in them you might be still using d0 rather than d1,d2....
Change it as required and it might solve this.

All 17 comments

Please provide more info

Please provide more info

OK, as shown above

There is no method or function named 'get_net' in this repo.

You can try this.

state_dict = torch.load(weights_path)
state_dict.pop('classifier.header.pointwise_conv.conv.weight')
state_dict.pop('classifier.header.pointwise_conv.conv.bias')
model.load_state_dict(state_dict, strict=False)

check num_classes or obj_list
810 = 9*90 = num_anchors * num_classes

I guess you use your own dataset but you wanted to use pretrained model. The point is num_classes is not the same. According to your issues, your dataset seems to have 7 classes, but coco pretrained model have 80 classes. Maybe you can try to use only the backbone.

It doesn't matter, the training program will skip the classifier's header if num_classes don't match while loading weights.

I used my own data to train(two classes) and got similar problem:

Traceback (most recent call last):
File "coco_eval.py", line 156, in
model.load_state_dict(torch.load(weights_path, map_location=torch.device('cpu')))
File "C:UsersASUSAnaconda3envspytorch-gpulibsite-packagestorchnnmodulesmodule.py", line 830, in load_state_dict
self.__class__.__name__, "nt".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for EfficientDetBackbone:

It confused me,how to solve this?

pls provide more info

Thank you for your reply

Traceback (most recent call last):
File "coco_eval.py", line 156, in
model.load_state_dict(torch.load(weights_path, map_location=torch.device('cpu')))
File "C:UsersASUSAnaconda3envspytorch-gpulibsite-packagestorchnnmodulesmodule.py", line 830, in load_state_dict
self.__class__.__name__, "nt".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for EfficientDetBackbone:
Missing key(s) in state_dict: "bifpn.4.p6_w1", "bifpn.4.p5_w1", "bifpn.4.p4_w1", "bifpn.4.p3_w1", "bifpn.4.p4_w2", "bifpn.4.p5_w2", "bifpn.4.p6_w2", "bifpn.4.p7_w2", "bifpn.4.conv6_up.depthwise_conv.conv.weight", "bifpn.4.conv6_up.pointwise_conv.conv.weight", "bifpn.4.conv6_up.pointwise_conv.conv.bias", "bifpn.4.conv6_up.bn.weight", "bifpn.4.conv6_up.bn.bias", "bifpn.4.conv6_up.bn.running_mean", "bifpn.4.conv6_up.bn.running_var", "bifpn.4.conv5_up.depthwise_conv.conv.weight", "bifpn.4.conv5_up.pointwise_conv.conv.weight", "bifpn.4.conv5_up.pointwise_conv.conv.bias", "bifpn.4.conv5_up.bn.weight", "bifpn.4.conv5_up.bn.bias", "bifpn.4.conv5_up.bn.running_mean", "bifpn.4.conv5_up.bn.running_var", "bifpn.4.conv4_up.depthwise_conv.conv.weight", "bifpn.4.conv4_up.pointwise_conv.conv.weight", "bifpn.4.conv4_up.pointwise_conv.conv.bias", "bifpn.4.conv4_up.bn.weight", "bifpn.4.conv4_up.bn.bias", "bifpn.4.conv4_up.bn.running_mean", "bifpn.4.conv4_up.bn.running_var", "bifpn.4.conv3_up.depthwise_conv.conv.weight", "bifpn.4.conv3_up.pointwise_conv.conv.weight", "bifpn.4.conv3_up.pointwise_conv.conv.bias", "bifpn.4.conv3_up.bn.weight", "bifpn.4.conv3_up.bn.bias", "bifpn.4.conv3_up.bn.running_mean", "bifpn.4.conv3_up.bn.running_var", "bifpn.4.conv4_down.depthwise_conv.conv.weight", "bifpn.4.conv4_down.pointwise_conv.conv.weight", "bifpn.4.conv4_down.pointwise_conv.conv.bias", "bifpn.4.conv4_down.bn.weight", "bifpn.4.conv4_down.bn.bias", "bifpn.4.conv4_down.bn.running_mean", "bifpn.4.conv4_down.bn.running_var", "bifpn.4.conv5_down.depthwise_conv.conv.weight", "bifpn.4.conv5_down.pointwise_conv.conv.weight", "bifpn.4.conv5_down.pointwise_conv.conv.bias",

the rest is too long
this happened when I run coco_eval.py(used the model I trained)

pls provide more info
@BenBerCao I met the same problems @wanghuajia

pls provide more info
@BenBerCao I met the same problems @wanghuajia

I tried auther's EfficientDet Training On A Custom Dataset(https://github.com/zylo117/Yet-Another-EfficientDet-Pytorch/blob/master/tutorial/train_shape.ipynb) , I trained it and evalled successfully, However,when I evalled my own model ,I still have this problem, if you have any solutions,please reply to me,thanks a lot.

pls provide more info
@BenBerCao I met the same problems @wanghuajia

I tried auther's EfficientDet Training On A Custom Dataset(https://github.com/zylo117/Yet-Another-EfficientDet-Pytorch/blob/master/tutorial/train_shape.ipynb) , I trained it and evalled successfully, However,when I evalled my own model ,I still have this problem, if you have any solutions,please reply to me,thanks a lot.

i met the same problem as well,
After training on a custom dataset with 2 objects . I used the efficientdet_test_video.py script to test the models. D0 seems to work fine but D1,D2,.. gives this error.

any fix?

you need to specify the network architecture before inference, loading a d1 weights into a d0 network is not possible

Go through your imports , some where in them you might be still using d0 rather than d1,d2....
Change it as required and it might solve this.

Go through your imports , some where in them you might be still using d0 rather than d1,d2....
Change it as required and it might solve this.

Found it, Thank you ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HsLOL picture HsLOL  ·  8Comments

you-old picture you-old  ·  4Comments

AlexeyAB picture AlexeyAB  ·  9Comments

mjanddy picture mjanddy  ·  4Comments

CuiZhiying picture CuiZhiying  ·  8Comments