You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

加载预训练模型时遇AttributeError: module 'collections' has no attribute 'Sized'

AttributeError: module 'collections' has no attribute 'Sized' 解决方法

问题

加载预训练模型时触发以下错误:

AttributeError: module 'collections' has no attribute 'Sized'

使用代码

from fastai import *
from fastai.vision import *
from matplotlib.pyplot import imshow
import numpy as np
import matplotlib.pyplot as plt
from skimage.transform import resize
from PIL import Image
learn = load_learner("", "model.pkl")

环境版本信息

torch                     1.11.0                 
torchvision               0.12.0                   
python                    3.10.14   
fastai                    1.0.60  

报错堆栈

File c:\Users\lib\site-packages\fastai\basic_train.py:620, in load_learner(path, file, test, tfm_y, **db_kwargs)
    618 state = torch.load(source, map_location='cpu') if defaults.device == torch.device('cpu') else torch.load(source)
    619 model = state.pop('model')
--> 620 src = LabelLists.load_state(path, state.pop('data'))
    621 if test is not None: src.add_test(test, tfm_y=tfm_y)
    622 data = src.databunch(**db_kwargs)

File c:\Users\lib\site-packages\fastai\data_block.py:578, in LabelLists.load_state(cls, path, state)
    576 "Create a `LabelLists` with empty sets from the serialized `state`."
    577 path = Path(path)
--> 578 train_ds = LabelList.load_state(path, state)
    579 valid_ds = LabelList.load_state(path, state)
    580 return LabelLists(path, train=train_ds, valid=valid_ds)

File c:\Users\lib\site-packages\fastai\data_block.py:690, in LabelList.load_state(cls, path, state)
    687 @classmethod
    688 def load_state(cls, path:PathOrStr, state:dict) -> 'LabelList':
    689     "Create a `LabelList` from `state`."
--> 690     x = state['x_cls']([], path=path, processor=state['x_proc'], ignore_empty=True)
    691     y = state['y_cls']([], path=path, processor=state['y_proc'], ignore_empty=True)
...
--> 298     if not isinstance(a, collections.Sized) and not getattr(a,'__array_interface__',False):
    299         a = list(a)
    300     if np.int_==np.int32 and dtype is None and is_listy(a) and len(a) and isinstance(a[0],int):

AttributeError: module 'collections' has no attribute 'Sized'

解决方案

这个错误的核心原因是Python 3.10 中collections.Sized已被移至collections.abc.Sized,而 fastai 1.0.60 未适配该变更,以下是三种可行解决方式:

1. 降级 Python 版本到 3.8/3.9

fastai 1.x 系列官方支持的最高 Python 版本是 3.9,降级后可直接兼容:

  • 使用 conda 创建新环境:
    conda create -n fastai_env python=3.8
    conda activate fastai_env
    pip install fastai==1.0.60 torch==1.11.0 torchvision==0.12.0
    
  • 或用 pyenv 管理多版本 Python,切换到 3.8/3.9 后重新安装依赖。

2. 修改 fastai 源码适配 Python 3.10

找到报错堆栈中涉及的文件(如路径c:\Users\lib\site-packages\fastai\下的相关模块),将所有collections.Sized替换为collections.abc.Sized:

  • 打开对应文件,找到包含collections.Sized的行(如报错中的第298行)
  • 将代码中的collections.Sized改为collections.abc.Sized
  • 保存文件后重新运行代码。

3. 升级 fastai 到 2.x 版本

fastai 2.x 已适配 Python 3.10,但注意 API 与 1.x 不兼容,需调整代码:

  • 卸载旧版本:pip uninstall fastai
  • 安装新版本:pip install fastai
  • 参考 fastai 2.x 文档调整模型加载代码(如load_learner的使用方式可能变化)。

内容的提问来源于stack exchange,提问作者the phoenix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 21:53:18