microsoft / Cream

This is a collection of our NAS and Vision Transformer work.
MIT License
1.62k stars 220 forks source link

Transplant code error #180

Closed zbl929 closed 1 year ago

zbl929 commented 1 year ago

I want to transplant EfficientViT to mmseg for semantic segmentation. When registering the model, it shows that '' TypeError: module must be a class, but got <class 'function'> '' image Do you know what the reason is ? Thank you!

xinyuliu-jeffrey commented 1 year ago

Hi @zbl929 ,

Thanks for your interest. I am wondering if you have encountered this error for the object detection with mmdet? mmseg shall use a similar backbone registry as mmdet. Btw, could your please provide the versions of the packages in your env by pip list and conda list? Thanks!

Best, Xinyu

zbl929 commented 1 year ago

Package Version


absl-py 1.4.0 addict 2.4.0 cachetools 5.3.0 certifi 2023.5.7 charset-normalizer 2.0.12 contourpy 1.0.7 cycler 0.11.0 einops 0.4.1 filelock 3.12.0 fonttools 4.39.4 future 0.18.3 fvcore 0.1.5.post20220512 google-auth 2.18.0 google-auth-oauthlib 1.0.0 grpcio 1.54.0 idna 3.4 importlib-metadata 6.6.0 importlib-resources 5.12.0 iopath 0.1.10 Jinja2 3.1.2 kiwisolver 1.4.4 Markdown 3.4.3 markdown-it-py 3.0.0 MarkupSafe 2.1.2 matplotlib 3.7.1 mdurl 0.1.2 mmcls 0.25.0 mmcv-full 1.5.0 mmdet 2.23.0 mmengine 0.7.4 mmsegmentation 0.23.0 mpmath 1.3.0 networkx 3.1 numpy 1.24.3 oauthlib 3.2.2 opencv-python 4.7.0.72 packaging 23.1 pandas 2.0.2 Pillow 9.5.0 pip 23.0.1 portalocker 2.7.0 prettytable 3.7.0 protobuf 4.23.0 pyasn1 0.5.0 pyasn1-modules 0.3.0 pycocotools 2.0.6 Pygments 2.15.1 pynvml 11.4.1 pyparsing 3.0.9 python-dateutil 2.8.2 pytz 2021.3 pywin32 306 PyYAML 6.0 regex 2023.6.3 requests 2.27.1 requests-oauthlib 1.3.1 rich 13.4.2 rsa 4.9 seaborn 0.12.2 setuptools 59.5.0 six 1.16.0 sympy 1.12 tabulate 0.9.0 tensorboard 2.13.0 tensorboard-data-server 0.7.0 termcolor 1.1.0 terminaltables 3.1.10 timm 0.5.4 tomli 2.0.1 torch 1.10.0+cu111 torchprofile 0.0.4 torchvision 0.15.2 tqdm 4.59.0 typing_extensions 4.6.3 tzdata 2023.3 urllib3 1.26.15 utils 1.0.1 wcwidth 0.2.6 Werkzeug 2.3.4 wheel 0.38.4 yacs 0.1.8 yapf 0.33.0 zipp 3.15.0


I did not use mmdet, above is my package version, thank you for replying !

xinyuliu-jeffrey commented 1 year ago

@zbl929 Thank you for the information. Could you try mmcv-full==1.3.0 and mmseg <0.19.0 to see if the error still occurs? Thanks.

zbl929 commented 1 year ago

When I try mmcv _ full = 1.3.0 and mmseg ==0.18.0 , the problem looks more and the package looks more mismatched.

xinyuliu-jeffrey commented 1 year ago

Hi @zbl929 , since the issue is highly related to the mmcv package, it is recommended to reproduce the provided scripts first here to see if your environment works well for the detection task.

zbl929 commented 1 year ago

I have successfully registered this model using my current environment, and this model is like this image And your model is like this, it does not look like a class. image If you solve my initial problem by modifying your code, do you have any suggestions?thank

xinyuliu-jeffrey commented 1 year ago

Hi @zbl929 , I've reproduced your error with mmcv==1.5.0 and mmdet==2.19.0. Please replace the function with the following class:

@BACKBONES.register_module()
class EfficientViT_M4(EfficientViT):

    def __init__(self, pretrained=False, frozen_stages=0, distillation=False, fuse=False, pretrained_cfg=None, model_cfg=EfficientViT_m4):
        super().__init__(frozen_stages=frozen_stages, distillation=distillation, pretrained=pretrained, **model_cfg)
        if fuse:
            replace_batchnorm(self)

And modify this line https://github.com/microsoft/Cream/blob/f50ae3ad4e1c3a5c2780f9a7171a7e906114de84/EfficientViT/downstream/efficientvit.py#L385-L386 to

if list(state_dict.keys())[0].startswith('module.'): 
     state_dict = {k[7:]: v for k, v in state_dict.items()} 
if list(state_dict.keys())[0].startswith('backbone.'):
    state_dict = {k[9:]: v for k, v in state_dict.items()}

in case you need to use the pretrained models.

zbl929 commented 1 year ago

Thank you very much, I have successfully solved the problem, look forward to EfficientViT has an excellent effect, thank you again !

xinyuliu-jeffrey commented 1 year ago

@zbl929 Glad to hear that! Closing the issue for now. Feel free to reopen it if you have further problems.

zbl929 commented 1 year ago

A question:why did you not find this problem when you were writing code? best wish!

xinyuliu-jeffrey commented 1 year ago

The current implementation is compatible with old versions of mmcv and mmdet. So there was no error then.

zbl929 commented 1 year ago

Got it, thanks again !