-
Notifications
You must be signed in to change notification settings - Fork 12
/
dataset.py
94 lines (73 loc) · 2.58 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from torch.utils.data import Dataset
from PIL import Image
import os
import random
import numpy as np
def pil_loader(img_str, str='RGB'):
with Image.open(img_str) as img:
img = img.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:
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]
class DatasetWithMetaGroup(Dataset):
def __init__(self, root_dir, meta_file, transform=None, num_group=8):
super(DatasetWithMetaGroup, 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()
self.num_group = num_group
for line in lines:
segs = line.strip().split(' ')
self.images.append(' '.join(segs[:-2]))
group_idx = int(segs[-2])
sub_cls_idx = int(segs[-1])
self.cls_idx.append((group_idx, sub_cls_idx))
self.classes.add((group_idx, sub_cls_idx))
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:
print(filename)
return self.__getitem__(random.randint(0, self.__len__() - 1))
# transform
if self.transform is not None:
img = self.transform(img)
group_id, cls_id = self.cls_idx[idx]
labels = np.zeros(self.num_group, dtype=np.int)
labels[group_id] = cls_id + 1
return img, labels