xxxxxxxxxx
class MyDataset(torch.utils.data.Dataset):
def __init__(self, data, labels):
self.data = data
self.labels = labels
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
return self.data[idx], self.labels[idx]
xxxxxxxxxx
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
# Custom dataset class
class MyDataset(Dataset):
def __init__(self, data,transform=None):
self.data = data
self.transform = transform
def __len__(self):
return len(self.data)
def __getitem__(self, index):
return self.data[index]
# Generating a random dataset
data = torch.randn(100, 3) # Example: 100 samples, each with 3 features
# Creating an instance of the custom dataset
dataset = MyDataset(data)
# Custom transform class for normalization
class CustomNormalize(object):
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, sample):
# Perform the normalization operation
normalized_sample = (sample - self.mean) / self.std
return normalized_sample
# Applying transformations
transform = transforms.Compose([
transforms.ToTensor(), # Convert data to tensor
CustomNormalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) # Custom normalization
])
transformed_dataset = MyDataset(data,transform=transform)
print(transformed_dataset)
print(*transformed_dataset)
# Creating a data loader for batch processing
dataloader = DataLoader(transformed_dataset, batch_size=4, shuffle=True)
# Iterating through the data loader
for batch in dataloader:
# Accessing the batched data
print(batch)
xxxxxxxxxx
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
# Custom dataset class
class MyDataset(Dataset):
def __init__(self, data,transform=None):
self.data = data
self.transform = transform
def __len__(self):
return len(self.data)
def __getitem__(self, index):
return self.data[index]
# Generating a random dataset
data = torch.randn(100, 3) # Example: 100 samples, each with 3 features
# Creating an instance of the custom dataset
dataset = MyDataset(data)
# Custom transform class for normalization
class CustomNormalize(object):
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, sample):
# Perform the normalization operation
normalized_sample = (sample - self.mean) / self.std
return normalized_sample
# Applying transformations
transform = transforms.Compose([
transforms.ToTensor(), # Convert data to tensor
CustomNormalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) # Custom normalization
])
transformed_dataset = MyDataset(data,transform=transform)
print(transformed_dataset)
print(*transformed_dataset)
# Creating a data loader for batch processing
dataloader = DataLoader(transformed_dataset, batch_size=4, shuffle=True)
# Iterating through the data loader
for batch in dataloader:
# Accessing the batched data
print(batch)
xxxxxxxxxx
# Create custom dataset objectfor image
class Dataset(Dataset):
def __init__(self, csv_file, data_dir, transform=None): # Constructor
self.data_dir=data_dir # Image directory
self.transform = transform # The transform is goint to be used on image
data_dircsv_file=os.path.join(self.data_dir,csv_file)
self.data_name= pd.read_csv(data_dircsv_file) # Load the CSV file contians image info
self.len=self.data_name.shape[0] # Number of images in dataset
def __len__(self): # Get the length
return self.len
def __getitem__(self, idx): # Getter
img_name=os.path.join(self.data_dir,self.data_name.iloc[idx, 1]) # Image file path
image = Image.open(img_name) # Open image file
y = self.data_name.iloc[idx, 0] # The class label for the image
if self.transform: # If there is any transform method, apply it onto the image
image = self.transform(image)
return image, y # getter will return both image and label
# Create the dataset objects
dataset = Dataset(csv_file=csv_file, data_dir=directory)
image=dataset[0][0] # First image in dataset
y=dataset[0][1] # First image's label in dataset
import torchvision.transforms as transforms
# Combine two transforms: crop and convert to tensor. Apply the compose to MNIST dataset
croptensor_data_transform = transforms.Compose([transforms.CenterCrop(20), transforms.ToTensor()])
dataset = Dataset(csv_file=csv_file , data_dir=directory,transform=croptensor_data_transform )
print("The shape of the first element tensor: ", dataset[0][0].shape)
def show_data(data_sample, shape = (28, 28)): # Helper func to show image
plt.imshow(data_sample[0].numpy().reshape(shape), cmap='gray')
plt.title('y = ' + data_sample[1])
show_data(dataset[0],shape = (20, 20)) # Plot the first element in the dataset
# Construct the compose for multiple transformations back to back.
fliptensor_data_transform = transforms.Compose([transforms.RandomVerticalFlip(p=1),transforms.ToTensor()])
dataset = Dataset(csv_file=csv_file , data_dir=directory,transform=fliptensor_data_transform )
show_data(dataset[1])
xxxxxxxxxx
class FaceLandmarksDataset(Dataset):
"""Face Landmarks dataset."""
def __init__(self, csv_file, root_dir, transform=None):
"""
Args:
csv_file (string): Path to the csv file with annotations.
root_dir (string): Directory with all the images.
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.landmarks_frame = pd.read_csv(csv_file)
self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.landmarks_frame)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
img_name = os.path.join(self.root_dir,
self.landmarks_frame.iloc[idx, 0])
image = io.imread(img_name)
landmarks = self.landmarks_frame.iloc[idx, 1:]
landmarks = np.array([landmarks])
landmarks = landmarks.astype('float').reshape(-1, 2)
sample = {'image': image, 'landmarks': landmarks}
if self.transform:
sample = self.transform(sample)
return sample