title: How make customised dataset in Pytorch for images and their masks?
body: <p>I have two dataset folder of tif images, one is a folder called BMMCdata, and the other one is the mask of BMMCdata images called BMMCmasks(the name of images are corresponds). I am trying to make a customised dataset and also split the data randomly to train and test. at the moment I am getting an error </p>
  
  <pre><code>self.filenames.append(fn)
  AttributeError: 'CustomDataset' object has no attribute 'filenames'
  </code></pre>
  
  <p>Any comment will be appreciated a lot. </p>
  
  <pre><code>import torch
  from torch.utils.data.dataset import Dataset  # For custom data-sets
  from torchvision import transforms
  from PIL import Image
  import os.path as osp
  import glob
  
  folder_data = "/Users/parto/PycharmProjects/U-net/BMMCdata/data"
  
  class CustomDataset(Dataset):
  def __init__(self, root):
  
      self.filename = folder_data
      self.root = root
      self.to_tensor = transforms.ToTensor()
      filenames = glob.glob(osp.join(folder_data, '*.tif'))
      for fn in filenames:
          self.filenames.append(fn)
      self.len = len(self.filenames)
      print(fn)
  
  def __getitem__(self, index):
      image = Image.open(self.filenames[index])
      return self.transform(image)
  
  def __len__(self):
  
      return self.len
  custom_img = CustomDataset(folder_data)
  # total images in set
  print(custom_img.len)
  
  train_len = int(0.6*custom_img.len)
  test_len = custom_img.len - train_len
  train_set, test_set = CustomDataset.random_split(custom_img, lengths=[train_len, test_len])
  # check lens of subset
  len(train_set), len(test_set)
  
  train_set = CustomDataset(folder_data)
  train_set = torch.utils.data.TensorDataset(train_set, train=True, batch_size=4)
  train_loader = torch.utils.data.DataLoader(train_set, batch_size=4, shuffle=True, num_workers=1)
  print(train_set)
  print(train_loader)
  
  test_set = torch.utils.data.DataLoader(Dataset, batch_size=4, sampler= train_sampler)
  test_loader = torch.utils.data.DataLoader(Dataset, batch_size=4)
  </code></pre>