-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathloader.py
48 lines (41 loc) · 1.5 KB
/
loader.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
import os
from PIL import Image
from torch.utils import data
import pandas as pd
from torchvision import transforms as T
# load images with true labels
class ImageNet(data.Dataset):
def __init__(self, dir, csv_path, transforms = None):
self.dir = dir
self.csv = pd.read_csv(csv_path)
self.transforms = transforms
def __getitem__(self, index):
img_obj = self.csv.loc[index]
ImageID = img_obj['ImageId'] + '.png'
Truelabel = img_obj['TrueLabel'] - 1
TargetClass = img_obj['TargetClass'] - 1
img_path = os.path.join(self.dir, ImageID)
pil_img = Image.open(img_path).convert('RGB')
if self.transforms:
data = self.transforms(pil_img)
return data, ImageID, Truelabel
def __len__(self):
return len(self.csv)
# load images with target labels
class ImageNet2(data.Dataset):
def __init__(self, dir, csv_path, transforms = None):
self.dir = dir
self.csv = pd.read_csv(csv_path)
self.transforms = transforms
def __getitem__(self, index):
img_obj = self.csv.loc[index]
ImageID = img_obj['ImageId'] + '.png'
Truelabel = img_obj['TrueLabel'] - 1
TargetClass = img_obj['TargetClass'] - 1
img_path = os.path.join(self.dir, ImageID)
pil_img = Image.open(img_path).convert('RGB')
if self.transforms:
data = self.transforms(pil_img)
return data, ImageID, TargetClass
def __len__(self):
return len(self.csv)