-
Notifications
You must be signed in to change notification settings - Fork 166
/
load_imglist.py
35 lines (28 loc) · 1.01 KB
/
load_imglist.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
import torch.utils.data as data
from PIL import Image
import os
import os.path
def default_loader(path):
img = Image.open(path).convert('L')
return img
def default_list_reader(fileList):
imgList = []
with open(fileList, 'r') as file:
for line in file.readlines():
imgPath, label = line.strip().split(' ')
imgList.append((imgPath, int(label)))
return imgList
class ImageList(data.Dataset):
def __init__(self, root, fileList, transform=None, list_reader=default_list_reader, loader=default_loader):
self.root = root
self.imgList = list_reader(fileList)
self.transform = transform
self.loader = loader
def __getitem__(self, index):
imgPath, target = self.imgList[index]
img = self.loader(os.path.join(self.root, imgPath))
if self.transform is not None:
img = self.transform(img)
return img, target
def __len__(self):
return len(self.imgList)