I am new and I am trying to learn pytorch
so I started with creating a costum dataset for my data
and i want to read the labels
for the labels I only have 3 txt files one for the classID ,one for train split and one for the test split
this is the custom data that I wrote so far
import os
from PIL import Image
import numpy as np
from torch.utils.data import Dataset
class MSR_Data(Dataset):
def __init__(self, image_path, transform=None):
# store all image paths in a list
image_files = [os.path.join(image_path, item) for item in os.listdir(image_path)]
self.images = []
for path in image_files:
# Load image using PIL
img = Image.open(path)
if img.mode != 'RGB':
img = img.convert('RGB')
# Convert PIL image to numpy array
im_np = np.array(img)
self.images.append(im_np)
# store transform
#self.transform = transform
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
data = np.array(self.images[idx])
I didn’t do any transformation because I wanted it to understand step by step
can someone help me how to read labels from txt file
or shouls i convert to other file format I saw somone doing it with th json format
#if self.transform:
# Apply transformation if available
# data = self.transform(data)
return data
with open('ids.txt') as labels:
return {line.strip().split(' ')[0]:line.strip().split(' ')[1] for line in labels.readlines()}
labels_dict = {'1': 'CallCellphone', '2': 'cheerUp'}
and Ur converted Dataset can be loaded as below:
# Use ImageFolder to create dataset(s)
from torchvision import datasets
train_data = datasets.ImageFolder(root=train_dir, # target folder of images
transform=data_transform, # transforms to perform on data (images)
target_transform=None) # transforms to perform on labels (if necessary)
test_data = datasets.ImageFolder(root=test_dir,
transform=data_transform)