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

[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 #1530

Closed
makao007 opened this issue Nov 3, 2022 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@makao007
Copy link

makao007 commented Nov 3, 2022

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 makao007 added the bug Something isn't working label Nov 3, 2022
@rwightman
Copy link
Collaborator

@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?

@futurisold
Copy link

futurisold commented Feb 1, 2023

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'

@SPOOKEXE
Copy link

SPOOKEXE commented Oct 27, 2023

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"

@glacier434
Copy link

glacier434 commented Oct 28, 2023

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)

@plaidpants
Copy link

Thanks, works for me now also.

@Eugeniusz-Gienek
Copy link

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!

@xingyouxin
Copy link

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!

@TaciteOFF
Copy link

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 :)

This didn't work for me but downgrading timm from 0.6.12 to 0.6.7 worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants