mikelzc1990 / nsganetv2

[ECCV2020] NSGANetV2: Evolutionary Multi-Objective Surrogate-Assisted Neural Architecture Search
Apache License 2.0
148 stars 34 forks source link

Size mismatch error #6

Open winstondcosta opened 3 years ago

winstondcosta commented 3 years ago

Traceback (most recent call last): File "msunas.py", line 355, in main(cfgs) File "msunas.py", line 310, in main engine.search() File "msunas.py", line 83, in search candidates, c_top1_err_pred = self._next(archive, acc_predictor, self.n_iter) File "msunas.py", line 197, in _next {'n_classes': self.n_classes, 'model_path': self.supernet_path}) File "msunas.py", line 266, in init n_classes=supernet['n_classes'], model_path=supernet['model_path']) File "C:\Users\hp\nsganetv2-master\evaluator.py", line 102, in init self.engine.load_weights_from_net(init) File "E:\Installed_Softwares\lib\site-packages\ofa\elastic_nn\networks\ofa_mbv3.py", line 211, in load_weights_from_net self.load_state_dict(model_dict) File "E:\Installed_Softwares\lib\site-packages\torch\nn\modules\module.py", line 1052, in load_state_dict self.class.name, "\n\t".join(error_msgs))) RuntimeError: Error(s) in loading state_dict for OFAMobileNetV3: size mismatch for classifier.linear.weight: copying a param with shape torch.Size([1000, 1280]) from checkpoint, the shape in current model is torch.Size([10, 1280]). size mismatch for classifier.linear.bias: copying a param with shape torch.Size([1000]) from checkpoint, the shape in current model is torch.Size([10]).

alex-stoica commented 3 years ago

I had the same issue, I suppose you tried the bi-objective search code with the argument --n_classes=10. A quick ugly fix which worked for me was changing some lines of code from evaluator.py, after initialization of init variable: init = torch.load(model_path, map_location='cpu')['state_dict'] Init is a dict having a classifier with 1000 outputs: classifier.linear.weight torch.Size([1000, 1280]) classifier.linear.bias torch.Size([1000]) Here is the place you may do some changes. Just for illustration purposes, modifying

init['classifier.linear.weight'] = init['classifier.linear.weight'][:10]
init['classifier.linear.bias'] = init['classifier.linear.bias'][:10]

resolves your problem.

euminds commented 3 years ago

Did you encounter this kind of error again when searching? An error is reported when the configured ofa version is ofa 0.1.0-202012082159. Then try ofa 0.0.4-2012082155 but the same error still occurs. Traceback (most recent call last): File "msunas.py", line 8, in from evaluator import OFAEvaluator, get_net_info File "/data8T/nsganetv2-master/evaluator.py", line 8, in from codebase.networks import NSGANetV2 File "/data8T/nsganetv2-master/codebase/networks/init.py", line 1, in from ofa.imagenet_codebase.networks.proxyless_nets import ProxylessNASNets, proxyless_base, MobileNetV2 ModuleNotFoundError: No module named 'ofa.imagenet_codebase'

alex-stoica commented 3 years ago

@euminds - I had plenty of imports errors related with ofa. I solved them by looking at their github repo: https://github.com/mit-han-lab/once-for-all If you have the last version of ofa, it's easy to look to your required files. In your specific case, from ofa.imagenet_codebase.networks.proxyless_nets import ProxylessNASNets, proxyless_base, MobileNetV2 shoud be replaced with from ofa.imagenet_classification.networks.proxyless_nets import ProxylessNASNets, proxyless_base, MobileNetV2 If you check the github repo, you see that there is no folder "imagenet_codebase", but there is a folder "imagenet_classification", which contains what you need

euminds commented 3 years ago

@euminds - I had plenty of imports errors related with ofa. I solved them by looking at their github repo: https://github.com/mit-han-lab/once-for-all If you have the last version of ofa, it's easy to look to your required files. In your specific case, from ofa.imagenet_codebase.networks.proxyless_nets import ProxylessNASNets, proxyless_base, MobileNetV2 shoud be replaced with from ofa.imagenet_classification.networks.proxyless_nets import ProxylessNASNets, proxyless_base, MobileNetV2 If you check the github repo, you see that there is no folder "imagenet_codebase", but there is a folder "imagenet_classification", which contains what you need

Thanks. I will try.