Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
TheSilverLinings opened this issue Dec 20, 2021 · 3 comments

Comments

@TheSilverLinings
Copy link

TheSilverLinings commented Dec 20, 2021

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
Copy link

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
Copy link

dangmanhtruong1995 commented May 2, 2023

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants