'my code couldn't find instance that was created initialized using pytorch
I implemented dataset class to use model, and When i strated train i get error
Traceback (most recent call last):
File "model.py", line 146, in <module>
train = Train()
File "model.py", line 70, in __init__
self.dataset.get_label()
File "model.py", line 61, in get_label
return self.label
AttributeError: 'MaskDataset' object has no attribute 'label'
and bellow code is maked error. but I never know why it's make problem. I check 'self.imgs' and 'self.label' used print(self.imgs) and print(self.label). and it was perfecte.
So I Mean, I don't know why python interpreter couldn't find instance that created initialized.
class MaskDataset(object):
def __init__(self, transforms,path):
self.data = data.Data()
self.transform = transforms
self.path = path
if 'Validation' in self.path :
self.img_path = "/home/ubuntu/lecttue-diagonosis/YangDongJae/ai/data/Validation/images/"
self.lab_path = "/home/ubuntu/lecttue-diagonosis/YangDongJae/ai/data/Validation/annotations/"
self.label = list(sorted(os.listdir(self.lab_path)))
self.imgs = list(sorted(os.listdir(self.img_path)))
elif 'train' in self.path:
self.img_path = "/home/ubuntu/lecttue-diagonosis/YangDongJae/ai/data/Training/images/"
self.lab_path = "/home/ubuntu/lecttue-diagonosis/YangDongJae/ai/data/Training/annotations/"
self.label = list(sorted(os.listdir(self.lab_path)))
self.imgs = list(sorted(os.listdir(self.img_path)))
def __getitem__(self,idx):
file_image = self.imgs[idx]
file_label = self.label[idx]
img_path = self.img_path+file_image
label_path = self.lab_path + file_label
img = Image.open(img_path).convert("RGB")
target = self.data.generate_target(label_path)
if self.transform is not None:
img = self.transform(img)
return img, target
class Train(MaskDataset):
def __init__(self,epochs = 100, lr = 0.005, momentum = 0.9, weight_decay = 0.0005):
self.data_transform = transforms.Compose([ # transforms.Compose : list 내의 작업을 연달아 할 수 있게 호출하는 클래스
transforms.ToTensor() # ToTensor : numpy 이미지에서 torch 이미지로 변경
])
self.dataset = MaskDataset(self.data_transform,'/home/ubuntu/lecttue-diagonosis/YangDongJae/ai/data/Training/')
self.val_dataset = MaskDataset(self.data_transform, '/home/ubuntu/lecttue-diagonosis/YangDongJae/ai/data/Validation/')
self.data_loader = torch.utils.data.DataLoader(self.dataset, batch_size = 10, collate_fn = self.collate_fn)
self.val_data_loader = torch.utils.data.DataLoader(self.val_dataset, batch_size = 10,collate_fn = self.collate_fn)
self.num_classes = 8
self.epochs = epochs
self.momentum = momentum
self.lr = 0.005
self.weight_decay = weight_decay
Solution 1:[1]
This is happening because elif condition is not True during self.dataset object creation. Note that the self.path has a Train sub-string staring with an uppercase T, while elif is comparing it with lower-case train, which evaluates to False. This can be fixed by changing the elif as:
elif 'train'.lower() in self.path.lower():
self.img_path = "/home/ubuntu/lecttue-diagonosis/YangDongJae/ai/data/Training/images/"
self.lab_path = "/home/ubuntu/lecttue-diagonosis/YangDongJae/ai/data/Training/annotations/"
self.label = list(sorted(os.listdir(self.lab_path)))
self.imgs = list(sorted(os.listdir(self.img_path)
You may also change the if statement for validation case similarly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | asymptote |
