-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
50 lines (41 loc) · 1.42 KB
/
dataset.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
from torch.utils.data import Dataset
import os
import random
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
def pil_loader(img_str, str='RGB'):
with Image.open(img_str) as img:
img = img.load().convert(str)
return img
class DatasetWithMeta(Dataset):
def __init__(self, root_dir, meta_file, transform=None):
super(DatasetWithMeta, self).__init__()
self.root_dir = root_dir
self.transform = transform
with open(meta_file) as f:
lines = f.readlines()
self.images = []
self.cls_idx = []
self.classes = set()
for line in lines:
segs = line.strip().split(' ')
self.images.append(' '.join(segs[:-1]))
self.cls_idx.append(int(segs[-1]))
self.classes.add(int(segs[-1]))
self.num = len(self.images)
def __len__(self):
return self.num
def __getitem__(self, idx):
filename = os.path.join(self.root_dir, self.images[idx])
try:
img = pil_loader(filename)
#except OSError:
# print("Cannot load : {}".format(filename))
# return self.__getitem__(idx+1)
except:
print(filename)
return self.__getitem__(random.randint(0, self.__len__() - 1))
# transform
if self.transform is not None:
img = self.transform(img)
return img, self.cls_idx[idx]