mikelzc1990 / nsganetv2

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

torch.nn.modules.module.ModuleAttributeError: 'MobileNetV3' object has no attribute 'reset_classifier' #12

Open TheSilverLinings opened 2 years ago

TheSilverLinings commented 2 years ago

python python msunas.py --sec_obj flops --n_gpus 1 --gpu 1 --n_workers 4 --n_epochs 5 --dataset cifar10 --n_classes 10 --data ./data --predictor as --supernet_path data/ofa_mbv3_d234_e346_k357_w1.0 --save search-cifar10-flops-w1.0 --iterations 30 --vld_size 5000 Evaluation mode: subnet Color jitter: None, resize_scale: 0.08, img_size: 192 Traceback (most recent call last): File "evaluator.py", line 266, in <module> main(cfgs) File "evaluator.py", line 209, in main OFAEvaluator.eval( File "evaluator.py", line 155, in eval subnet.reset_classifier( File ".../anaconda3/envs/horovod/lib/python3.8/site-packages/torch/nn/modules/module.py", line 771, in __getattr__ raise ModuleAttributeError("'{}' object has no attribute '{}'".format( torch.nn.modules.module.ModuleAttributeError: 'MobileNetV3' object has no attribute 'reset_classifier'

I try to search architectures for cifar10 and I have checked the ofa. but I don't find the 'reset_classifier'. Could you please help? Thx

vinh-cao commented 2 years ago

same here. Double checked the ofa repo at current and 0.04* commit version, but can't find something like reset_classifier. Timm library has a function with same name, but MobileNetV3 is based on nn.Module, so not relies on timm.

@mikelzc1990 did you extend the ofa module?

dangmanhtruong1995 commented 1 year ago

Hi, I also had this error. In the end, I found that in codebase/networks/nsganetv2.py there are these lines:

class NSGANetV2(MobileNetV3):
    """
    Modified from https://github.com/mit-han-lab/once-for-all/blob/master/ofa/
    imagenet_codebase/networks/mobilenet_v3.py to include drop path in training
    and option to reset classification layer
    """

and the reset_classifier function:

    @staticmethod
    def reset_classifier(model, last_channel, n_classes, dropout_rate=0.0):
        model.classifier = LinearLayer(last_channel, n_classes, dropout_rate=dropout_rate)

I then downloaded the ofa library code, add this to ofa/imagenet_classification/networks/mobilenet_v3.py, then setup: python setup.py install. However, in line 155 of evaluator.py it's like this:

subnet.reset_classifier(
                last_channel=subnet.classifier.in_features,
                n_classes=run_config.data_provider.n_classes, dropout_rate=cfgs.drop_rate)

It's obvious that we need to add another parameter:

subnet.reset_classifier(
                subnet,
                last_channel=subnet.classifier.in_features,
                n_classes=run_config.data_provider.n_classes, dropout_rate=cfgs.drop_rate)

After that it appears to work fine. I hope this can be of help to anyone who gets into this problem.

GREATLYJDN commented 10 months ago

Hi, I also had this error. In the end, I found that in codebase/networks/nsganetv2.py there are these lines:

class NSGANetV2(MobileNetV3):
    """
    Modified from https://github.com/mit-han-lab/once-for-all/blob/master/ofa/
    imagenet_codebase/networks/mobilenet_v3.py to include drop path in training
    and option to reset classification layer
    """

and the reset_classifier function:

    @staticmethod
    def reset_classifier(model, last_channel, n_classes, dropout_rate=0.0):
        model.classifier = LinearLayer(last_channel, n_classes, dropout_rate=dropout_rate)

I then downloaded the ofa library code, add this to ofa/imagenet_classification/networks/mobilenet_v3.py, then setup: python setup.py install. However, in line 155 of evaluator.py it's like this:

subnet.reset_classifier(
                last_channel=subnet.classifier.in_features,
                n_classes=run_config.data_provider.n_classes, dropout_rate=cfgs.drop_rate)

It's obvious that we need to add another parameter:

subnet.reset_classifier(
                subnet,
                last_channel=subnet.classifier.in_features,
                n_classes=run_config.data_provider.n_classes, dropout_rate=cfgs.drop_rate)

After that it appears to work fine. I hope this can be of help to anyone who gets into this problem.

I tried your method, but I still got the same mistake