How to convert image from folder into tensors using torch?
I'm trying to convert images in a folder to tensors, save it and load them later, as shown below
transform = transforms.Compose([
  transforms.ToTensor()])
dataset = datasets.ImageFolder(
  r'imagedata', transform=transform) 
torch.save(dataset, 'train_data.pt')

But I get a value error when trying to load the trained file as below:
train_codes = torch.Tensor(torch.load(os.path.join(self.data_dir, &quot;train_data.pt&quot;)))

ValueError: only one element tensors can be converted to Python scalars
Any help or suggestion to fix this will be highly appreciated.

You met this problem because train_data.pt was not saved as a Tensor, since that variable was read the data by ImageFolder which was inherited from DatasetFolder, it should be loaded and used as a Torch Dataset. The example below use DataLoader as documents:
import torch
from torchvision import transforms, datasets

# Saving part
transform = transforms.Compose([
                                transforms.ToTensor()
                               ])
dataset = datasets.ImageFolder(r'imagedata', transform=transform) 
torch.save(dataset,'train_data.pt')

# Loading part
data = torch.load(torch.load(os.path.join(self.data_dir, &quot;train_data.pt&quot;)))
loader = torch.utils.data.DataLoader(data, batch_size = 32)
for image, label in loader:
   # Processing....

