如何使用fastai中的标签\ delim修复错误“参数1必须是迭代器”?

2024-04-23 10:47:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试学习亚马逊星球数据集here(也在fastai's documentation中描述)上的图像分类教程,但是我在使用函数label_from_df(label_delim=' ')时遇到了问题,导致了错误TypeError: argument 1 must be an iterator。你知道吗

在寻找解决方案时,我在SO上看到了this question,但是csv文件处理是在fastai上完成的,所以我认为它不适合这里。你知道吗

代码的上下文如下:

src = (ImageList.from_csv(path, 'data.csv', folder='images', suffix='.jpg')
       .split_by_rand_pct()
       .label_from_df(label_delim=' ')
      )

如果需要更多的代码来理解问题,可以提供它。你知道吗

我使用自己的数据集在googlecolab上进行培训,我不认为csv文件或images文件夹有问题,尽管有可能。你知道吗

运行上述行时的错误是:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-69a35763cde8> in <module>()
      2 src = (ImageList.from_csv(path, 'data.csv', folder='images', suffix='.jpg')
      3        .split_by_rand_pct()
----> 4        .label_from_df(label_delim=' ')
      5       )

3 frames
/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in _inner(*args, **kwargs)
    473             assert isinstance(self.train, LabelList)
    474             kwargs['label_cls'] = self.train.y.__class__
--> 475             self.valid = fv(*args, from_item_lists=True, **kwargs)
    476             self.__class__ = LabelLists
    477             self.process()

/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in label_from_df(self, cols, label_cls, **kwargs)
    281             new_kwargs,label_cls = dict(one_hot=True, classes= cols),MultiCategoryList
    282             kwargs = {**new_kwargs, **kwargs}
--> 283         return self._label_from_list(_maybe_squeeze(labels), label_cls=label_cls, **kwargs)
    284 
    285     def label_const(self, const:Any=0, label_cls:Callable=None, **kwargs)->'LabelList':

/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in _label_from_list(self, labels, label_cls, from_item_lists, **kwargs)
    270         labels = array(labels, dtype=object)
    271         label_cls = self.get_label_cls(labels, label_cls=label_cls, **kwargs)
--> 272         y = label_cls(labels, path=self.path, **kwargs)
    273         res = self._label_list(x=self, y=y)
    274         return res

/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in __init__(self, items, classes, label_delim, one_hot, **kwargs)
    407     _processor=MultiCategoryProcessor
    408     def __init__(self, items:Iterator, classes:Collection=None, label_delim:str=None, one_hot:bool=False, **kwargs):
--> 409         if label_delim is not None: items = array(csv.reader(items.astype(str), delimiter=label_delim))
    410         super().__init__(items, classes=classes, **kwargs)
    411         if one_hot:

TypeError: argument 1 must be an iterator

Tags: csvpathinfromselfdfdatalabels