-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloader.py
56 lines (41 loc) · 1.66 KB
/
dataloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from torch.utils.data import Dataset, DataLoader
import matplotlib.image as mpimg
import pandas as pd
import os
import numpy as np
from data_utils import get_keypoints
from data_utils import get_image
class FacialKeypointsDataset(Dataset):
"""Face Landmarks dataset."""
def __init__(self, csv_file, transform=None):
"""
Args:
csv_file (string): Path to the csv file with annotations.
root_dir (string): Directory with all the images.
custom_point (list): which points to train on
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.key_pts_frame = pd.read_csv(csv_file)
self.key_pts_frame.dropna(inplace=True)
self.key_pts_frame.reset_index(drop=True, inplace=True)
self.transform = transform
def __len__(self):
return len(self.key_pts_frame.index)
def __getitem__(self, key):
if isinstance(key, slice):
# get the start, stop, and step from the slice
return [self[ii] for ii in range(*key.indices(len(self)))]
elif isinstance(key, int):
# handle negative indices
if key < 0:
key += len(self)
if key < 0 or key >= len(self):
raise IndexError("The index (%d) is out of range." % key)
# get the data from direct index
sample = {'image':get_image(key, self.key_pts_frame),
'keypoints':get_keypoints(key, self.key_pts_frame)}
if self.transform:
sample = self.transform(sample)
return sample
return sample