I solved the issue by also loading the val data set in the train dataset:
class DatasetFromFolder(data.Dataset):
def __init__(self, image_dir_input, image_dir_target, image_dir_input_val, image_dir_target_val, input_transform=None, target_transform=None):
    super(DatasetFromFolder, self).__init__()
    self.image_filenames = [join(image_dir_input, x) for x in listdir(image_dir_input) if is_image_file(x)]
    self.image_filenames.sort()
    self.image_filenames_target = [join(image_dir_target, x) for x in listdir(image_dir_target) if is_normal_file(x)]
    self.image_filenames_target.sort()

    self.image_filenames_val = [join(image_dir_input_val, x) for x in listdir(image_dir_input_val) if is_image_file(x)]
    self.image_filenames_val.sort()
    self.image_filenames_target_val = [join(image_dir_target_val, x) for x in listdir(image_dir_target_val) if is_normal_file(x)]
    self.image_filenames_target_val.sort()
    
    self.input_transform = input_transform
    self.target_transform = target_transform

def __getitem__(self, index):
    input = load_img(self.image_filenames[index])
    target = load_img(self.image_filenames_target[index])
    input_val = load_img(self.image_filenames_val[index % len(self.image_filenames_val)])    
    target_val = load_img(self.image_filenames_target_val[index % len(self.image_filenames_target_val)])

    if self.input_transform:
        #input = input.resize((320, 240))
        input = input.resize((224, 224))
        input = self.input_transform(input)
        input_val = input_val.resize((224, 224))
        input_val = self.input_transform(input_val)
    if self.target_transform:
        #target = target.resize((320, 240))
        target = target.resize((224, 224))
        target = self.target_transform(target)
        target_val = target_val.resize((224, 224))
        target_val = self.target_transform(target_val)
    return input, target, input_val, target_val

def __len__(self):
    return len(self.image_filenames)

And in the getitem function I get the right element with a modulo operation.