The code is data-dependent so I cannot really write it.
In fact it does not appear how do you load target images.
It should be something like:
class dataset():
def init()
     self.images_list = load the list...
     self.target_list = load the list....
def __len__:
    return len(self.images_list) which should be the same as target list
def __getitem__(self,idx):
   input_image = loadimg(self.images_list[idx])
   target_image = loadimg(self.target_list[idx])

The idx should point to corresponding pairs of img/ground-truth. However this does not depend on pytorch framework but your code in init function. You have to make them match somehow