zylo117 / Yet-Another-EfficientDet-Pytorch

The pytorch re-implement of the official efficientdet with SOTA performance in real time and pretrained weights.
GNU Lesser General Public License v3.0
5.2k stars 1.27k forks source link

can not load model weights #51

Open wanghuajia opened 4 years ago

wanghuajia commented 4 years ago

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

zylo117 commented 4 years ago

Please provide more info

wanghuajia commented 4 years ago

Please provide more info

OK, as shown above

zylo117 commented 4 years ago

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

prfans commented 4 years ago

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)

plusczh commented 4 years ago

check num_classes or obj_list 810 = 990 = num_anchors num_classes

xtzd commented 4 years ago

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.

zylo117 commented 4 years ago

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

BenBerCao commented 4 years ago

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?

zylo117 commented 4 years ago

pls provide more info

BenBerCao commented 4 years ago

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)

qtw1998 commented 4 years ago

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

BenBerCao commented 4 years ago

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.

ManojKesani commented 4 years ago

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.

williamscott701 commented 4 years ago

any fix?

zylo117 commented 4 years ago

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

ManojKesani commented 4 years ago

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.

williamscott701 commented 4 years ago

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

fardinadii commented 2 years ago

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

ManojKesani commented 2 years ago

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.

fardinadii commented 2 years ago

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 commented 2 years ago

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.