Yet-Another-EfficientDet-Pytorch icon indicating copy to clipboard operation
Yet-Another-EfficientDet-Pytorch copied to clipboard

can not load model weights

Open wanghuajia opened this issue 4 years ago • 21 comments

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]).

wanghuajia avatar Apr 13 '20 09:04 wanghuajia

Please provide more info

zylo117 avatar Apr 13 '20 10:04 zylo117

Please provide more info

OK, as shown above

wanghuajia avatar Apr 13 '20 13:04 wanghuajia

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

zylo117 avatar Apr 13 '20 13:04 zylo117

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)

prfans avatar Apr 14 '20 06:04 prfans

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

plusczh avatar Apr 15 '20 09:04 plusczh

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.

xtzd avatar Apr 28 '20 02:04 xtzd

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

zylo117 avatar Apr 28 '20 03:04 zylo117

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:\Users\ASUS\Anaconda3\envs\pytorch-gpu\lib\site-packages\torch\nn\modules\module.py", line 830, in load_state_dict self.class.name, "\n\t".join(error_msgs))) RuntimeError: Error(s) in loading state_dict for EfficientDetBackbone:

It confused me,how to solve this?

BenBerCao avatar Apr 29 '20 01:04 BenBerCao

pls provide more info

zylo117 avatar Apr 29 '20 02:04 zylo117

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:\Users\ASUS\Anaconda3\envs\pytorch-gpu\lib\site-packages\torch\nn\modules\module.py", line 830, in load_state_dict self.class.name, "\n\t".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)

BenBerCao avatar Apr 29 '20 02:04 BenBerCao

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

qtw1998 avatar May 02 '20 03:05 qtw1998

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.

BenBerCao avatar May 02 '20 15:05 BenBerCao

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.

ManojKesani avatar May 03 '20 21:05 ManojKesani

any fix?

williamscott701 avatar May 28 '20 00:05 williamscott701

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

zylo117 avatar May 28 '20 01:05 zylo117

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.

ManojKesani avatar May 28 '20 01:05 ManojKesani

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 ;)

williamscott701 avatar May 28 '20 01:05 williamscott701

Hi all, I have the same problem and I could not find a way to solve it. I downloaded efficiendet_d0.pth and I want to use d0. @williamscott701 @ManojKesani

fardinadii avatar Dec 05 '21 09:12 fardinadii

Hi all, I have the same problem and I could not find a way to solve it. I downloaded efficiendet_d0.pth and I want to use d0. @williamscott701 @ManojKesani

It's been a while since I used this, as far as I remember you need to go through the impots and somewhere in those scripts there will be places u need to change the model u want. Hope this helps.

ManojKesani avatar Dec 05 '21 09:12 ManojKesani

Hi all, I have the same problem and I could not find a way to solve it. I downloaded efficiendet_d0.pth and I want to use d0. @williamscott701 @ManojKesani

It's been a while since I used this, as far as I remember you need to go through the impots and somewhere in those scripts there will be places u need to change the model u want. Hope this helps.

thank you so much @ManojKesani

fardinadii avatar Dec 05 '21 09:12 fardinadii

Hi all, I have the same problem and I could not find a way to solve it. I downloaded efficiendet_d0.pth and I want to use d0. @williamscott701 @ManojKesani

Hi all, i find the problem. This error rise when we use wrong image size. for me i should set image size to 512 to run without any error.

fardinadii avatar Dec 24 '21 09:12 fardinadii