gpt4 book ai didi

python-3.x - 属性错误 : dataset object has no attribute 'c' FastAI

转载 作者:行者123 更新时间:2023-12-04 12:59:32 25 4
gpt4 key购买 nike

我正在尝试训练 ResNet基于 UNet用于图像分割。我在 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

然后我称之为 datagenerator__main__功能:
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' object has no attribute 'c'



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

最佳答案

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

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

关于python-3.x - 属性错误 : dataset object has no attribute 'c' FastAI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60296710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com