Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

custom dataset pytorch

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
Comment

PREVIOUS NEXT
Code Example
Python :: python create pem file 
Python :: python download chromebook 
Python :: bitwise operators in python 
Python :: staticmethod vs classmethod python 
Python :: read file from drive in colab 
Python :: panda loc conditional 
Python :: union type python 
Python :: theme_use() tkinter theme usage 
Python :: design patterns python 
Python :: random forest classifier classification report 
Python :: convert numpy array to HSV cv 
Python :: decode a qrcode inpython 
Python :: Check instance has an attribute in python 
Python :: how to check python version in script 
Python :: install python 3.4 mac terminal 
Python :: python check if value in string 
Python :: __slots__ python example 
Python :: os module 
Python :: pandas rename columns whitespace with underscore 
Python :: capitalise texts 
Python :: pandas pull value from column 
Python :: python - match two df on a variable with different name 
Python :: python np.sum 
Python :: how to define number in python 
Python :: python type hints list of class 
Python :: pandas set one column equal to another 
Python :: pytesseract.image_to_data(img output_type=output.dict) 
Python :: how to sort values in python 
Python :: python datetime to unix timestamp 
Python :: assign multiple columns pandas 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =