pytorch-image-models
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
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, infrom .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 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?
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'
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"
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)
Thanks, works for me now also.
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 :)
- Navigate to Kohya folder (for example, /opt/kohya):
cd /opt/kohya
- Navigate to models in virtuan env:
cd venv/lib/python3.11/site-packages/timm/models
- Edit maxxvit.py file (I prefer nano editor)
nano maxxvit.py
- press Ctrl+W and type "dataclasses" (without quotes)
- Append ", field" to the end of the found string. It has tol look like that after this:
from dataclasses import dataclass, replace, field
- again press Ctrl+W and type "class MaxxVitCfg" (without quotes)
- comment out strings starting with "conf_cfg" and "transformer_cfg" by appending hash symbol at the beginning
- 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
- Please pay attention that there are 4 spaces at the beginning of the string. It is important!
- Press Ctrl+O and then press Ctrl+X in order to save changes and close. Done!
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 :)
- Navigate to Kohya folder (for example, /opt/kohya):
cd /opt/kohya
- Navigate to models in virtuan env:
cd venv/lib/python3.11/site-packages/timm/models
- Edit maxxvit.py file (I prefer nano editor)
nano maxxvit.py
- press Ctrl+W and type "dataclasses" (without quotes)
- Append ", field" to the end of the found string. It has tol look like that after this:
from dataclasses import dataclass, replace, field
- again press Ctrl+W and type "class MaxxVitCfg" (without quotes)
- comment out strings starting with "conf_cfg" and "transformer_cfg" by appending hash symbol at the beginning
- 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
- Please pay attention that there are 4 spaces at the beginning of the string. It is important!
- 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!