-
Notifications
You must be signed in to change notification settings - Fork 56
/
data_loaders.py
242 lines (205 loc) · 8.25 KB
/
data_loaders.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""
Copyright (c) 2020-present NAVER Corp.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import munch
import numpy as np
import os
from PIL import Image
from torch.utils.data import DataLoader
from torch.utils.data import Dataset
from torchvision import transforms
_IMAGE_MEAN_VALUE = [0.485, 0.456, 0.406]
_IMAGE_STD_VALUE = [0.229, 0.224, 0.225]
_SPLITS = ('train', 'val', 'test')
def mch(**kwargs):
return munch.Munch(dict(**kwargs))
def configure_metadata(metadata_root):
metadata = mch()
metadata.image_ids = os.path.join(metadata_root, 'image_ids.txt')
metadata.image_ids_proxy = os.path.join(metadata_root,
'image_ids_proxy.txt')
metadata.class_labels = os.path.join(metadata_root, 'class_labels.txt')
metadata.image_sizes = os.path.join(metadata_root, 'image_sizes.txt')
metadata.localization = os.path.join(metadata_root, 'localization.txt')
return metadata
def get_image_ids(metadata, proxy=False):
"""
image_ids.txt has the structure
<path>
path/to/image1.jpg
path/to/image2.jpg
path/to/image3.jpg
...
"""
image_ids = []
suffix = '_proxy' if proxy else ''
with open(metadata['image_ids' + suffix]) as f:
for line in f.readlines():
image_ids.append(line.strip('\n'))
return image_ids
def get_class_labels(metadata):
"""
image_ids.txt has the structure
<path>,<integer_class_label>
path/to/image1.jpg,0
path/to/image2.jpg,1
path/to/image3.jpg,1
...
"""
class_labels = {}
with open(metadata.class_labels) as f:
for line in f.readlines():
image_id, class_label_string = line.strip('\n').split(',')
class_labels[image_id] = int(class_label_string)
return class_labels
def get_bounding_boxes(metadata):
"""
localization.txt (for bounding box) has the structure
<path>,<x0>,<y0>,<x1>,<y1>
path/to/image1.jpg,156,163,318,230
path/to/image1.jpg,23,12,101,259
path/to/image2.jpg,143,142,394,248
path/to/image3.jpg,28,94,485,303
...
One image may contain multiple boxes (multiple boxes for the same path).
"""
boxes = {}
with open(metadata.localization) as f:
for line in f.readlines():
image_id, x0s, x1s, y0s, y1s = line.strip('\n').split(',')
x0, x1, y0, y1 = int(x0s), int(x1s), int(y0s), int(y1s)
if image_id in boxes:
boxes[image_id].append((x0, x1, y0, y1))
else:
boxes[image_id] = [(x0, x1, y0, y1)]
return boxes
def get_mask_paths(metadata):
"""
localization.txt (for masks) has the structure
<path>,<link_to_mask_file>,<link_to_ignore_mask_file>
path/to/image1.jpg,path/to/mask1a.png,path/to/ignore1.png
path/to/image1.jpg,path/to/mask1b.png,
path/to/image2.jpg,path/to/mask2a.png,path/to/ignore2.png
path/to/image3.jpg,path/to/mask3a.png,path/to/ignore3.png
...
One image may contain multiple masks (multiple mask paths for same image).
One image contains only one ignore mask.
"""
mask_paths = {}
ignore_paths = {}
with open(metadata.localization) as f:
for line in f.readlines():
image_id, mask_path, ignore_path = line.strip('\n').split(',')
if image_id in mask_paths:
mask_paths[image_id].append(mask_path)
assert (len(ignore_path) == 0)
else:
mask_paths[image_id] = [mask_path]
ignore_paths[image_id] = ignore_path
return mask_paths, ignore_paths
def get_image_sizes(metadata):
"""
image_sizes.txt has the structure
<path>,<w>,<h>
path/to/image1.jpg,500,300
path/to/image2.jpg,1000,600
path/to/image3.jpg,500,300
...
"""
image_sizes = {}
with open(metadata.image_sizes) as f:
for line in f.readlines():
image_id, ws, hs = line.strip('\n').split(',')
w, h = int(ws), int(hs)
image_sizes[image_id] = (w, h)
return image_sizes
class WSOLImageLabelDataset(Dataset):
def __init__(self, data_root, metadata_root, transform, proxy,
num_sample_per_class=0):
self.data_root = data_root
self.metadata = configure_metadata(metadata_root)
self.transform = transform
self.image_ids = get_image_ids(self.metadata, proxy=proxy)
self.image_labels = get_class_labels(self.metadata)
self.num_sample_per_class = num_sample_per_class
self._adjust_samples_per_class()
def _adjust_samples_per_class(self):
if self.num_sample_per_class == 0:
return
image_ids = np.array(self.image_ids)
image_labels = np.array([self.image_labels[_image_id]
for _image_id in self.image_ids])
unique_labels = np.unique(image_labels)
new_image_ids = []
new_image_labels = {}
for _label in unique_labels:
indices = np.where(image_labels == _label)[0]
sampled_indices = np.random.choice(
indices, self.num_sample_per_class, replace=False)
sampled_image_ids = image_ids[sampled_indices].tolist()
sampled_image_labels = image_labels[sampled_indices].tolist()
new_image_ids += sampled_image_ids
new_image_labels.update(
**dict(zip(sampled_image_ids, sampled_image_labels)))
self.image_ids = new_image_ids
self.image_labels = new_image_labels
def __getitem__(self, idx):
image_id = self.image_ids[idx]
image_label = self.image_labels[image_id]
image = Image.open(os.path.join(self.data_root, image_id))
image = image.convert('RGB')
image = self.transform(image)
return image, image_label, image_id
def __len__(self):
return len(self.image_ids)
def get_data_loader(data_roots, metadata_root, batch_size, workers,
resize_size, crop_size, proxy_training_set,
num_val_sample_per_class=0):
dataset_transforms = dict(
train=transforms.Compose([
transforms.Resize((resize_size, resize_size)),
transforms.RandomCrop(crop_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(_IMAGE_MEAN_VALUE, _IMAGE_STD_VALUE)
]),
val=transforms.Compose([
transforms.Resize((crop_size, crop_size)),
transforms.ToTensor(),
transforms.Normalize(_IMAGE_MEAN_VALUE, _IMAGE_STD_VALUE)
]),
test=transforms.Compose([
transforms.Resize((crop_size, crop_size)),
transforms.ToTensor(),
transforms.Normalize(_IMAGE_MEAN_VALUE, _IMAGE_STD_VALUE)
]))
loaders = {
split: DataLoader(
WSOLImageLabelDataset(
data_root=data_roots[split],
metadata_root=os.path.join(metadata_root, split),
transform=dataset_transforms[split],
proxy=proxy_training_set and split == 'train',
num_sample_per_class=(num_val_sample_per_class
if split == 'val' else 0)
),
batch_size=batch_size,
shuffle=split == 'train',
num_workers=workers)
for split in _SPLITS
}
return loaders