Hello,
I have some images in a folder. So, I am trying to create a custom dataset with taking help from this post. But, I am getting some errors.
My custom dataset class is given below:
class CustomDataSet(Dataset):
def __init__(self, main_dir, transform):
self.main_dir = main_dir
self.transform = transform
all_imgs = os.listdir(main_dir)
self.total_imgs = natsort.natsorted(all_imgs) //Error-1
def __len__(self):
return len(self.all_imgs)
def __getitem__(self, idx):
img_loc = os.path.join(self.main_dir, self.all_imgs[idx])
image = Image.open(img_loc).convert("RGB")
tensor_image = self.transform(image)
return tensor_image
My loader method
train_data_dir = '/home/Houses-dataset-master'
# Transformation
transform = transforms.Compose([
transforms.Resize((256, 256)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
# Giving the path
train_data_tensor = CustomDataSet(train_data_dir, transform=transform)
# trying to print the length of the train_data_tensor
print(len(train_data_tensor)) //Getting Error-2
# Converting labels into tensoe
train_label_price_tensor = torch.tensor(label_price.values)
print(train_label_price_tensor.size())
print(train_data_tensor.size()) //getting Error-3
First I was getting an error for natsort
Error-1: NameError: name 'natsort' is not defined
Then, I remove the natsort
and replaced all total_imgs
with all_imags
. Getting
Error-2: AttributeError: 'CustomDataSet' object has no attribute 'all_imgs'
Finally, I removed the error-2 code line
from the program and getting another error at the time of printing the size of my images tensor
.
Error-3: AttributeError: 'CustomDataSet' object has no attribute 'size'
Does it mean, my custom dataset is not working? Transformation is not working?
How can I solve these issues?
Thank you.