AttributeError:数据集对象没有属性“c”FastAI

Posted

技术标签:

【中文标题】AttributeError:数据集对象没有属性“c”FastAI【英文标题】:AttributeError: dataset object has no attribute 'c' FastAI 【发布时间】:2020-06-03 09:47:09 【问题描述】:

我正在尝试训练基于UNetResNet 进行图像分割。我在csv 文件中有图像和遮罩图像的位置,这就是我创建自己的dataloader 的原因,如下所示:

X = list(df['input_img'])
y = list(df['mask_img'])

X_train, X_valid, y_train, y_valid = train_test_split(
     X, y, test_size=0.33, random_state=42)

class NumbersDataset():
    def __init__(self, inputs, labels):
        self.X = inputs
        self.y = labels

    def __len__(self):
        return len(self.X)

    def __getitem__(self, idx):
        img_train = cv2.imread(self.X[idx])
        img_mask = cv2.imread(self.y[idx])
        img_train = cv2.resize(img_train, (427,240), interpolation = cv2.INTER_LANCZOS4) 
        img_mask = cv2.resize(img_mask, (427,240), interpolation = cv2.INTER_LANCZOS4) 
        return img_train, img_mask

然后我在__main__ 函数中调用这个datagenerator

if __name__ == '__main__':
    dataset_train = NumbersDataset(X_train, y_train)
    dataloader_train = DataLoader(dataset_train, batch_size=4, shuffle=True, num_workers=2)

    dataset_valid = NumbersDataset(X_valid, y_valid)
    dataloader_valid = DataLoader(dataset_valid, batch_size=4, shuffle=True, num_workers=2)

    datas = DataBunch(train_dl = dataloader_train, valid_dl = dataloader_valid)
    leaner = unet_learner(data = datas, arch = models.resnet34)

但我最终得到以下错误:

Traceback (most recent call last):
  File "dataset_test.py", line 70, in <module>
    leaner = unet_learner(data = datas, arch = models.resnet34)
  File "/home/sarvagya/miniconda3/envs/gr/lib/python3.6/site-packages/fastai/vision/learner.py", line 118, in unet_learner
    model = to_device(models.unet.DynamicUnet(body, n_classes=data.c, img_size=size, blur=blur, blur_final=blur_final,
  File "/home/sarvagya/miniconda3/envs/gr/lib/python3.6/site-packages/fastai/basic_data.py", line 122, in __getattr__
    def __getattr__(self,k:int)->Any: return getattr(self.train_dl, k)
  File "/home/sarvagya/miniconda3/envs/gr/lib/python3.6/site-packages/fastai/basic_data.py", line 38, in __getattr__
    def __getattr__(self,k:str)->Any: return getattr(self.dl, k)
  File "/home/sarvagya/miniconda3/envs/gr/lib/python3.6/site-packages/fastai/basic_data.py", line 20, in DataLoader___getattr__
    def DataLoader___getattr__(dl, k:str)->Any: return getattr(dl.dataset, k)

AttributeError: 'NumbersDataset' 对象没有属性 'c'

我尝试搜索,甚至尝试使用SegmentationItemList.from_df,但没有任何帮助。我在这里做错了什么?

【问题讨论】:

【参考方案1】:

您应该将属性 c 添加到您的 NumbersDataset 中,如下所示:

def __init__(self, inputs, labels, c):
  self.inputs = inputs
  self.labels = labels
  self.c = c

【讨论】:

以上是关于AttributeError:数据集对象没有属性“c”FastAI的主要内容,如果未能解决你的问题,请参考以下文章

xgboost: AttributeError: 'DMatrix' 对象没有属性 'handle'

AttributeError:“numpy.ndarray”对象没有属性“transform”

AttributeError:“TensorSliceDataset”对象没有属性“get_shape”

获取 AttributeError:“OneHotEncoder”对象没有属性“pyspark 中的 _jdf”

AttributeError:“系列”对象没有属性“to_coo”

AttributeError: 'QuerySet' 对象没有属性 'add'