Obtain train and test data from Image and a section in the image name

I have images, that i need to classify. the target data is included in the image name, but not the entire image name, so i extract this by splitting the image name.
However, I am challenged on how to move this to the dataloader, and later split into train and test sets. The code is attached below.

After this split, i would like to use the dataloader, as below:
data loader
mean = 0.5
std = 0.5
transformTrain = transforms.Compose(
transforms.RandomVerticalFlip(0.5), # Vertical flip
transforms.RandomHorizontalFlip(0.5), # Horizontally flip the given PIL Image randomly with a given probability.
transforms.RandomRotation(degrees=45), # Rotate the image by angle.
transforms.Normalize((mean, mean, mean), (std, std, std))
X_train, X_test, y_train, y_test = train_test_split(images, nsex, test_size=0.2)

#X_train = ImageFolder(X_train,transform=transformTrain)
#train_data = custom_dataset(X_train, transform=transformTrain, train=True) # Resize transform
#test_data = custom_dataset(X_test, transform=transform) # Resize transform
X_train = torch.Tensor(X_train)
X_test = torch.Tensor(X_test)
y_train = torch.Tensor(y_train)
y_test = torch.Tensor(y_test)
X_train = X_train.permute(0,3, 1, 2)
X_test = X_test.permute(0,3, 1, 2)

train_dataset = TensorDataset(X_train, y_train)
test_dataset = TensorDataset(X_test, y_test)
train_loader = DataLoader(train_dataset, batch_size=16, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=16, shuffle=False)

you should turn it into a tensor so that it is easier for pytorch to handle the data in a multiprocessing environment like dataloaders.

The following snippet is taken from Converting an image to a Torch Tensor in Python - GeeksforGeeks

# Import necessary libraries
import torch
from PIL import Image
import torchvision.transforms as transforms
# Read a PIL image
image = Image.open('iceland.jpg')
# Define a transform to convert PIL 
# image to a Torch tensor
transform = transforms.Compose([
# transform = transforms.PILToTensor()
# Convert the PIL image to Torch tensor
img_tensor = transform(image)
# print the converted Torch tensor

in short: You need to apply the transforms in your dataset. At the moment you only store them as a member variable without ever touching them again

Update: Btw: what happens if your path does not exists? Looks like a possible exception for me

1 Like