facebookresearch / vissl

VISSL is FAIR's library of extensible, modular and scalable components for SOTA Self-Supervised Learning with images.
https://vissl.ai
MIT License
3.26k stars 334 forks source link

Attribute Dict cannot have keys starting with underscore #418

Closed ananyahjha93 closed 3 years ago

ananyahjha93 commented 3 years ago

Instructions To Reproduce the πŸ› Bug:

  1. I am calling BaseSSLMultiInputOutputModel as an API call using AttributeDict configs.
BaseSSLMultiInputOutputModel(model_config, optimizer_config)

# where
model_config = AttrDict({
            'SINGLE_PASS_EVERY_CROP': False,
            'INPUT_TYPE': 'rgb',
            'MULTI_INPUT_HEAD_MAPPING': [],
            'TRUNK': AttrDict({
                'NAME': 'vision_transformer',
                'VISION_TRANSFORMERS': AttrDict({
                    'IMAGE_SIZE': 224,
                    'PATCH_SIZE': 16,
                    'NUM_LAYERS': 12,
                    'NUM_HEADS': 6,
                    'HIDDEN_DIM': 384,
                    'MLP_DIM': 1532,
                    'CLASSIFIER': 'token',
                    'DROPOUT_RATE': 0,
                    'ATTENTION_DROPOUT_RATE': 0,
                    'QKV_BIAS': True,
                    'QK_SCALE': False,
                    'DROP_PATH_RATE': 0.1,
                }),
            }),
            'HEAD': AttrDict({
                'PARAMS': [[
                    "swav_head",
                    {
                        "use_weight_norm_prototypes": True,
                        "dims": [384, 2048, 2048, 256],
                        "use_bn": False,
                        "return_embeddings": False,
                        "activation_name": "GELU",
                        "num_clusters": [65536]
                    }
                ]],
            }),
            'FEATURE_EVAL_SETTINGS': AttrDict({
                'EVAL_MODE_ON': False,
                'EXTRACT_TRUNK_FEATURES_ONLY': False,
            }),
            '_MODEL_INIT_SEED': 0,
        })

optimizer_config = AttrDict({})
  1. what exact command you run:
  2. what you observed (including full logs):
    ...
    File "/usr/local/lib/python3.9/site-packages/torch/autograd/grad_mode.py", line 26, in decorate_context
    return func(*args, **kwargs)
    File "/usr/local/lib/python3.9/site-packages/vissl/hooks/dino_hooks.py", line 125, in on_forward
    self._build_momentum_network(task)
    File "/usr/local/lib/python3.9/site-packages/vissl/hooks/dino_hooks.py", line 51, in _build_momentum_network
    task.loss.momentum_teacher = build_model(
    File "/usr/local/lib/python3.9/site-packages/vissl/models/__init__.py", line 19, in build_model
    return BaseSSLMultiInputOutputModel(model_config, optimizer_config)
    File "/usr/local/lib/python3.9/site-packages/classy_vision/models/classy_model.py", line 34, in __call__
    classy_model = super().__call__(*args, **kwargs)
    File "/usr/local/lib/python3.9/site-packages/vissl/models/base_ssl_model.py", line 67, in __init__
    self._get_heads()
    File "/usr/local/lib/python3.9/site-packages/vissl/models/base_ssl_model.py", line 337, in _get_heads
    with set_torch_seed(self.model_config._MODEL_INIT_SEED):
    File "/usr/local/lib/python3.9/site-packages/attrdict/mixins.py", line 80, in __getattr__
    raise AttributeError(
    AttributeError: 'AttrDict' instance has no attribute '_MODEL_INIT_SEED'
  3. please simplify the steps as much as possible so they do not require additional resources to run, such as a private dataset.

Expected behavior:

This API call should work with the config. Solution: Change calls of

self.model_config._MODEL_INIT_SEED

to

self.model_config.MODEL_INIT_SEED

within BaseSSLMultiInputOutputModel and other places where this is referenced.

Environment:

Provide your environment information using the following command:

-------------------  --------------------------------------------------------------------------
sys.platform         darwin
Python               3.9.2 (default, Feb 19 2021, 17:09:53) [Clang 12.0.0 (clang-1200.0.32.29)]
numpy                1.21.2
Pillow               8.1.2
vissl                0.1.5 @/usr/local/lib/python3.9/site-packages/vissl
GPU available        False
torchvision          0.8.2 @/usr/local/lib/python3.9/site-packages/torchvision
hydra                1.0.6 @/usr/local/lib/python3.9/site-packages/hydra
classy_vision        0.6.0 @/usr/local/lib/python3.9/site-packages/classy_vision
tensorboard          2.4.1
apex                 unknown
cv2                  4.5.3
PyTorch              1.7.1 @/usr/local/lib/python3.9/site-packages/torch
PyTorch debug build  False
-------------------  --------------------------------------------------------------------------
PyTorch built with:
  - GCC 4.2
  - C++ Version: 201402
  - clang 11.0.0
  - Intel(R) Math Kernel Library Version 2020.0.1 Product Build 20200208 for Intel(R) 64 architecture applications
  - Intel(R) MKL-DNN v1.6.0 (Git Hash 5ef631a030a6f73131c77892041042805a06064f)
  - OpenMP 201107 (a.k.a. OpenMP 3.1)
  - NNPACK is enabled
  - CPU capability usage: AVX2
  - Build settings: BLAS=MKL, BUILD_TYPE=Release, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -Wno-deprecated-declarations -DUSE_PTHREADPOOL -Xpreprocessor -fopenmp -I/Users/distiller/project/miniconda/envs/wheel_py39/include -DNDEBUG -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DUSE_VULKAN_WRAPPER -O2 -fPIC -Wno-narrowing -Wall -Wextra -Werror=return-type -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-unused-local-typedefs -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -Wno-invalid-partial-specialization -Wno-typedef-redefinition -Wno-unknown-warning-option -Wno-unused-private-field -Wno-inconsistent-missing-override -Wno-aligned-allocation-unavailable -Wno-c++14-extensions -Wno-constexpr-not-const -Wno-missing-braces -Qunused-arguments -fcolor-diagnostics -fno-math-errno -fno-trapping-math -Werror=format -Wno-unused-private-field -Wno-missing-braces -Wno-c++14-extensions -Wno-constexpr-not-const, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, USE_CUDA=OFF, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=OFF, USE_NNPACK=ON, USE_OPENMP=ON, 

CPU info:

When to expect Triage

VISSL devs and contributors aim to triage issues asap however, as a general guideline, we ask users to expect triaging in 1-2 weeks.

ananyahjha93 commented 3 years ago

This is a known issue with AttrDict: https://stackoverflow.com/questions/13693888/accessing-dict-elements-with-leading-underscores-in-django-templates

QuentinDuval commented 3 years ago

Hi @ananyahjha93,

First of all, thank you for using VISSL :)

Regarding the issue, I am not sure I understand how you concluded that we should rename "self.model_config._MODEL_INIT_SEED" to "self.model_config.MODEL_INIT_SEED".

You can make your code pass by renaming the key "MODEL_INIT_SEED" to "_MODEL_INIT_SEED" in the AttrDict dictionary. Indeed, the following code should work to create the model via API (it does at least work on my side):

from vissl.config.attr_dict import AttrDict
from vissl.models import build_model

# where
model_config = AttrDict({
            'SINGLE_PASS_EVERY_CROP': False,
            'INPUT_TYPE': 'rgb',
            'MULTI_INPUT_HEAD_MAPPING': [],
            'TRUNK': AttrDict({
                'NAME': 'vision_transformer',
                'VISION_TRANSFORMERS': AttrDict({
                    'IMAGE_SIZE': 224,
                    'PATCH_SIZE': 16,
                    'NUM_LAYERS': 12,
                    'NUM_HEADS': 6,
                    'HIDDEN_DIM': 384,
                    'MLP_DIM': 1532,
                    'CLASSIFIER': 'token',
                    'DROPOUT_RATE': 0,
                    'ATTENTION_DROPOUT_RATE': 0,
                    'QKV_BIAS': True,
                    'QK_SCALE': False,
                    'DROP_PATH_RATE': 0.1,
                }),
            }),
            'HEAD': AttrDict({
                'PARAMS': [[
                    "swav_head",
                    {
                        "use_weight_norm_prototypes": True,
                        "dims": [384, 2048, 2048, 256],
                        "use_bn": False,
                        "return_embeddings": False,
                        "activation_name": "GELU",
                        "num_clusters": [65536]
                    }
                ]],
            }),
            'FEATURE_EVAL_SETTINGS': AttrDict({
                'EVAL_MODE_ON': False,
                'EXTRACT_TRUNK_FEATURES_ONLY': False,
            }),
            '_MODEL_INIT_SEED': 0,
        })

optimizer_config = AttrDict({})
model = build_model(model_config, optimizer_config)

If it does not work on your side, please tell me, then this is definitely a bug.

As a side note, the reason why we have an underscore in front of "_MODEL_INIT_SEED" is because this attribute is deduced inside hydra_config.py and thus private to the VISSL configuration (but clearly not private for the build_model API, we can work on making that more clear).

ananyahjha93 commented 3 years ago

@QuentinDuval sorry for the confusion, I was using _MODEL_INIT_SEED in my config. Its weird that this works for you. OK like even if I do this, it fails for me

>>> from attrdict.dictionary import AttrDict
>>> a = AttrDict({'_PARAM': 1})
>>> a._PARAM
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/attrdict/mixins.py", line 80, in __getattr__
    raise AttributeError(
AttributeError: 'AttrDict' instance has no attribute '_PARAM'
>>> a
AttrDict({'_PARAM': 1})

so I assumed that AttrDict is not compatible with keys starting with underscores.

QuentinDuval commented 3 years ago

Hi @ananyahjha93 !

Ok, so the issue is probably due to the import:

from vissl.config.attr_dict import AttrDict
a = AttrDict({'_PARAM': 1})
a._PARAM

We have our own AttrDict class :)

Could you try that this works fine?

ananyahjha93 commented 3 years ago

@QuentinDuval thanks, that makes sense, I went through the code of the original AttrDict and found that they do not support keys starting with an underscore because of a regex. Thanks for helping out!