Hi,
Thanks for replying.
I am trying to change this code (which is generated for csv training dataset) to take png inputs  as the format of my training and validation datasets (CamVid image frames+ Semantic Labels) both are in png. I tried different approaches such as PIL.open() and cv2.imread() to adopt it with my dataset but it didn’t happen however It took me around 6 hours
I would appreciate your help
This is the link for the dataset:

  
      
      kaggle.com
  
  
    

CamVid (Cambridge-Driving Labeled Video Database) 17

Over 700 images and masks for semantic segmentation


  
  
    
    
  
  


class CamVid(Dataset):
  
  def __init__(self, csv_file, phase):
    self.phase = phase
    self.data = pd.read_csv(csv_file)
    
    if phase == 'train':
      self.input_shape = (480, 640)
    elif phase == 'valid':
      self.input_shape = (704, 960)
  
  def __getitem__(self, index):
    image, label = self.data.iloc[index, 0], self.data.iloc[index, 1]
    image = scipy.misc.imread("data/"+image, mode='RGB')
    label = np.load("data/"+label)
        
    if self.phase == "train":
      # RandomCrop
      h, w, _ = image.shape
      new_h, new_w = self.input_shape
      
      top = random.randint(0, h - new_h)
      left = random.randint(0, w - new_w)

      image = image[top:top + new_h, left:left + new_w]
      label = label[top:top + new_h, left:left + new_w]
      
      # RandomHorizontalFlip
      if random.random() < 0.5:
        image = np.fliplr(image)
        label = np.fliplr(label)
        
    if self.phase == "valid":
      # "Resize"
      new_h, new_w = self.input_shape
      
      image = image[0:new_h, 0:new_w]
      label = label[0:new_h, 0:new_w]
          
    # Normalization 
    mean=[0.485, 0.456, 0.460]
    std =[0.229, 0.224, 0.225]
    
    image = np.transpose(image, (2, 0, 1)) / 255.
    image[0] = (image[0] - mean[0]) / std[0]
    image[1] = (image[1] - mean[1]) / std[1]
    image[2] = (image[2] - mean[2]) / std[2]
    
    # ToTensor 
    image = torch.from_numpy(image.copy()).float()
    label = torch.from_numpy(label.copy()).long()
  
    # One-hot encoding 
    h, w = label.size()
    target = torch.zeros(32, h, w)
    for c in range(32):
      target[c][label == c] = 1
      
    return {'x': image, 'y':target, 'l':label} 
  
  def __len__(self):
    return len(self.data) 

train_data = CamVid("D:\\Data Science\\Python Assignment\\Pytorch\\FCN-master\\CamVid\\train", phase="train")
valid_data = CamVid("D:\\Data Science\\Python Assignment\\Pytorch\\FCN-master\\CamVid\\val", phase="valid")

trainloader = torch.utils.data.DataLoader(train_data, batch_size=5, shuffle=True)
validloader = torch.utils.data.DataLoader(valid_data, batch_size=1)```