AttributeError:尝试加载 pickle 模型时,模块“collections”没有属性“Sized”

问题描述 投票:0回答:1

我正在尝试加载预训练模型,但这样做时遇到了错误。 AttributeError:模块“集合”没有属性“大小”

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 pytorch fast-ai
1个回答
0
投票

将此行添加到代码顶部:

from collections.abc import Sized

并且

if
条件将只是(“删除集合”):

if not isinstance(a,Sized) and not getattr(a,'__array_interface__',False) :
    a = list(a)

链接到大小:https://docs.python.org/3/library/collections.abc.html#collections.abc.Sized

© www.soinside.com 2019 - 2024. All rights reserved.