import os import torch import skimage import numpy as np import pandas as pd import matplotlib.pyplot as plt from torchvision import transforms, utils from torch.utils.data import Dataset, DataLoader
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): returnlen(self.landmarks_frame)
def__getitem__(self, idx): if torch.is_tensor(idx): idx = idx.tolist()
classRescale(object): """Rescale the image in a sample to a given size. Args: output_size (tuple or int): Desired output size. If tuple, output is matched to output_size. If int, smaller of image edges is matched to output_size keeping aspect ratio the same. """ def__init__(self, output_size): assertisinstance(output_size, tuple) self.output_size = output_size
classToTensor(object): """Convert ndarrays in sample to Tensors.""" def__call__(self, sample): image, landmarks = sample['image'], sample['landmarks'] # swap color axis because # numpy image: H x W x C # torch image: C X H X W image = image.transpose((2, 0, 1)) return {'image': torch.from_numpy(image), 'landmarks': torch.from_numpy(landmarks)}