Training data of two parallel datasets - GAN

Hello,
I have a dataset of images and their corresponding .txt data files: each image has its own .txt file which contains a vector of numbers.
I would like to load the images and train them in their net (let’s call it the generator) and the txt files to another net, as vectors (let’s call their net the simulator).
The important thing here, is that the order of training matters, every image has its own vector of numbers. Meaning, if my CNN creates a new vector, the generator has to create a new image correspondingly.
Could anyone help me understand how can I load the data and use it convinently?

class MyDataset(torch.utils.data.Dataset):
    def __init__(self, img2txt):
        """
        img2text :mapping that maps an image file to text file, 
        containing path to your image and text file as a tuple
        """
        self.img2txt = img2txt
    
    def __getitem__(self, idx):
        img_path, txt_path = img2txt[idx]
        # Load image
        # img = ...
        
        # Open text file
        # txt = ...
        
        # Return them as a tuple
        return img, txt
    
dataset = MyDataset(img2txt)
data = torch.utils.data.DataLoader(dataset)

img, txt = next(iter(data))
# You get your img and text

Thanks Kushaj, but I don’t think you understood what I meant, so I’ll explain again.
I don’t need to map the image to a text file. Let’s assume I have 5 pairs of images and text files. I want to load an image randomly for training, and only after that- to load the matching text file. So if for example the loader loaded randomly the third image, I would like to pick and load now the corresponding third text file.
In my folders, the images and the corresponding text files are in the same order, if it helps.
Sorry if I wasn’t clear at the begining.

If I am right, you have a list of path to all images. You also have a list with paths to all text files. An image path at index i corresponds to same index i in the list that contains paths to text files.
So generating a random number and using it as an index would solve the problem.

If you have already have 5 pairs of images and text files, then generating a random number and using it to access the image and path would solve it.

I may have not understood your problem correctly, but I answered what I could infer.