pytorch-image-models icon indicating copy to clipboard operation
pytorch-image-models copied to clipboard

[BUG] Python 3.11 Error >>> import timm ValueError: mutable default <class 'timm.models.maxxvit.MaxxVitConvCfg'> for field conv_cfg is not allowed: use default_factory

Open makao007 opened this issue 2 years ago • 1 comments

import timm Traceback (most recent call last): File "", line 1, in File "/home/ubuntu/.local/lib/python3.11/site-packages/timm/init.py", line 2, in from .models import create_model, list_models, is_model, list_modules, model _entrypoint,
File "/home/ubuntu/.local/lib/python3.11/site-packages/timm/models/init.py ", line 28, in from .maxxvit import * File "/home/ubuntu/.local/lib/python3.11/site-packages/timm/models/maxxvit.py" , line 216, in @dataclass ^^^^^^^^^ File "/usr/lib/python3.11/dataclasses.py", line 1221, in dataclass return wrap(cls) ^^^^^^^^^ File "/usr/lib/python3.11/dataclasses.py", line 1211, in wrap return _process_class(cls, init, repr, eq, order, unsafe_hash, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/dataclasses.py", line 959, in _process_class cls_fields.append(_get_field(cls, name, type, kw_only)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/dataclasses.py", line 816, in _get_field raise ValueError(f'mutable default {type(f.default)} for field ' ValueError: mutable default <class 'timm.models.maxxvit.MaxxVitConvCfg'> for fie ld conv_cfg is not allowed: use default_factory

makao007 avatar Nov 03 '22 08:11 makao007

@makao007 will look at this, haven't tried w/ python 3.11 yet as there wasn't a torchvision build available, is there a 3.11 torchvision?

rwightman avatar Nov 04 '22 03:11 rwightman

The fix should be relatively straightforward:

@dataclass
class MaxxVitCfg:
    embed_dim: Tuple[int, ...] = (96, 192, 384, 768)
    depths: Tuple[int, ...] = (2, 3, 5, 2)
    block_type: Tuple[Union[str, Tuple[str, ...]], ...] = ('C', 'C', 'T', 'T')
    stem_width: Union[int, Tuple[int, int]] = 64
    stem_bias: bool = True
    conv_cfg: MaxxVitConvCfg = field(default_factory=MaxxVitConvCfg) # <--- we need field here
    transformer_cfg: MaxxVitTransformerCfg = field(default_factory=MaxxVitTransformerCfg) # <--- and here
    weight_init: str = 'vit_eff'

futurisold avatar Feb 01 '23 09:02 futurisold

Can confirm the fix does indeed work, also include 'field' in the "from dataclass import ..." statement.

The script you need to place this in is located at "kohya_ss-22.1.0\venv\Lib\site-packages\timm\models\maxxvit.py"

SPOOKEXE avatar Oct 27 '23 19:10 SPOOKEXE

Can confirm the fix does indeed work, also include 'field' in the "from dataclass import ..." statement.

The script you need to place this in is located at "kohya_ss-22.1.0\venv\Lib\site-packages\timm\models\maxxvit.py"

For anyone who doesn't know an ounce or a pound of python (like me), this means that in addition to the code at the top that needs to be changed as stated, also change this line (use find to get to it) from dataclasses import dataclass, replace Add field to the end. So it will look like this.

from dataclasses import dataclass, replace, field

Don't give up. I struggled with how to interpret this and other advice all morning and finally got it to work thanks to the straightforward answers above.

I finally got captions to work! yay! (make a copy of that file first, jic)

glacier434 avatar Oct 28 '23 22:10 glacier434

Thanks, works for me now also.

plaidpants avatar Dec 24 '23 18:12 plaidpants

Perfect, works for me! For those who will find this later I would like to point out that conv_cfg and transformer_cfg don't only need the change of inner arguments - the whole rows look a bit different. Besides, as I am using python 3.11 the path is a bit different. So, summary of how to apply this change (step by step) - on the example of Kohya. Specifically for those who are unfamiliar with Python :)

  1. Navigate to Kohya folder (for example, /opt/kohya):
cd /opt/kohya
  1. Navigate to models in virtuan env:
cd venv/lib/python3.11/site-packages/timm/models
  1. Edit maxxvit.py file (I prefer nano editor)
nano maxxvit.py
  1. press Ctrl+W and type "dataclasses" (without quotes)
  2. Append ", field" to the end of the found string. It has tol look like that after this:
from dataclasses import dataclass, replace, field
  1. again press Ctrl+W and type "class MaxxVitCfg" (without quotes)
  2. comment out strings starting with "conf_cfg" and "transformer_cfg" by appending hash symbol at the beginning
  3. append the following two strings afterwards:
    conv_cfg: MaxxVitConvCfg = field(default_factory=MaxxVitConvCfg) # <--- we need field here
    transformer_cfg: MaxxVitTransformerCfg = field(default_factory=MaxxVitTransformerCfg) # <--- and here
  1. Please pay attention that there are 4 spaces at the beginning of the string. It is important!
  2. Press Ctrl+O and then press Ctrl+X in order to save changes and close. Done!

Eugeniusz-Gienek avatar Dec 31 '23 13:12 Eugeniusz-Gienek

Perfect, works for me! For those who will find this later I would like to point out that conv_cfg and transformer_cfg don't only need the change of inner arguments - the whole rows look a bit different. Besides, as I am using python 3.11 the path is a bit different. So, summary of how to apply this change (step by step) - on the example of Kohya. Specifically for those who are unfamiliar with Python :)

  1. Navigate to Kohya folder (for example, /opt/kohya):
cd /opt/kohya
  1. Navigate to models in virtuan env:
cd venv/lib/python3.11/site-packages/timm/models
  1. Edit maxxvit.py file (I prefer nano editor)
nano maxxvit.py
  1. press Ctrl+W and type "dataclasses" (without quotes)
  2. Append ", field" to the end of the found string. It has tol look like that after this:
from dataclasses import dataclass, replace, field
  1. again press Ctrl+W and type "class MaxxVitCfg" (without quotes)
  2. comment out strings starting with "conf_cfg" and "transformer_cfg" by appending hash symbol at the beginning
  3. append the following two strings afterwards:
    conv_cfg: MaxxVitConvCfg = field(default_factory=MaxxVitConvCfg) # <--- we need field here
    transformer_cfg: MaxxVitTransformerCfg = field(default_factory=MaxxVitTransformerCfg) # <--- and here
  1. Please pay attention that there are 4 spaces at the beginning of the string. It is important!
  2. Press Ctrl+O and then press Ctrl+X in order to save changes and close. Done!

I have solved my problem with this help! Thank you very much! Remember to add [field] and then release the codes respectively 4 times!

xingyouxin avatar Apr 30 '24 07:04 xingyouxin