Skip to content

[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

Description

@makao007

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

Activity

rwightman

rwightman commented on Nov 4, 2022

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

futurisold commented on Feb 1, 2023

@futurisold

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

SPOOKEXE commented on Oct 28, 2023

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

glacier434

glacier434 commented on Oct 29, 2023

@glacier434

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

plaidpants commented on Dec 25, 2023

@plaidpants

Thanks, works for me now also.

Eugeniusz-Gienek

Eugeniusz-Gienek commented on Dec 31, 2023

@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!
xingyouxin

xingyouxin commented on Apr 30, 2024

@xingyouxin

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

TaciteOFF commented on Dec 16, 2024

@TaciteOFF

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

    Participants

    @makao007@glacier434@Eugeniusz-Gienek@rwightman@plaidpants

    Issue actions

      [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 ยท Issue #1530 ยท huggingface/pytorch-image-models