Hi Eqy,
Thanks for the response.  All the files are in subdirectories like this:
root/first/second/third/image.png
So I wrote the loop:



 Jennifer_Davis:

for file in files:
            row = self.df.iloc[index].squeeze()
            id = row.image_id
            #print(os.path.join(root, file))
            image = os.path.join(root, file)
            image = Image.open(image).convert('RGB')    
            caption = row.InChI # here need caption to match image_id
            tokens = str(caption).lower().split()
            target = []
            target.append(vocab.stoi['<start>'])
            target.extend([vocab.stoi[token] for token in tokens])
            target.append(vocab.stoi['<end>'])
            target = torch.Tensor(target).long()



Which loops through all directories and opens the images.  It was also supposed to assign a caption to each based on the dataframe’s ‘image_id’.   The problem is exactly as you described.  There’s an index for each image, but the loop just opens the images randomly until it loops through all files.  It doesn’t match on the index as I would like.  The index has the appropriate caption.
Hope this makes sense.