Expected Tensor as element 0 in argument 0, but got JpegImageFile

Hello guys

I am using this example,

import torch
import os
from PIL import Image
import random

list_file='C/.../file_names.txt'    #A txt's files path that contains the names of the images Dataset
raw_dir='C/.../DirImageA'           # the path of the folder where the A type images of the dataset are located
expert_dir='C/.../DirImageB'        # the path of the folder where the B type images of the dataset are located
join = os.path.join


file_list = [  ]
with open(list_file) as f:
    for line in f:                
        name = line.strip()            
        if name:
            p = (join(raw_dir, name), join(expert_dir, name), name)
            file_list.append(p)
            
transformation= [  ]
size=256

transformation.append(transforms.Resize((size,size)))
transformation.append(transforms.RandomHorizontalFlip(0.5))

transformation.append(transforms.ToTensor())
transform=transforms.Compose(transformation)

index=random.randint(1, 1000) #random sample image for training

raw = Image.open(file_list[index][0])
expert =  Image.open(file_list[index][1])

raw_exp = transform(torch.stack([raw, expert])) #The error appears on this line  to torch.stack([raw, expert]) ```

and I am getting this error

expected Tensor as element 0 in argument 0, but got JpegImageFile

Any suggestions?

Transform the images to tensors first e.g. via:

image = PIL.Image.open("image.jpeg")

torch.stack([image, image])
# TypeError: expected Tensor as element 0 in argument 0, but got JpegImageFile

x = transforms.ToTensor()(image)
y = torch.stack([x, x])
print(y.shape)
# torch.Size([2, 3, 800, 800])

Tank you very much for your answer and help!!