-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdatasets.py
219 lines (183 loc) · 7.72 KB
/
datasets.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
import os
from collections import OrderedDict
from typing import Tuple, List, Dict, Union, Callable, Optional
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import datasets, transforms
from PIL import Image
def image_transform(
image_size: Union[int, List[int]],
augmentation: dict = {},
mean: List[float] = [0.485, 0.456, 0.406],
std: List[float] = [0.229, 0.224, 0.225]) -> Callable:
"""Image transforms.
"""
if isinstance(image_size, int):
image_size = (image_size, image_size)
else:
image_size = tuple(image_size)
# data augmentations
horizontal_flip = augmentation.pop('horizontal_flip', None)
if horizontal_flip is not None:
assert isinstance(horizontal_flip, float) and 0 <= horizontal_flip <= 1
vertical_flip = augmentation.pop('vertical_flip', None)
if vertical_flip is not None:
assert isinstance(vertical_flip, float) and 0 <= vertical_flip <= 1
random_crop = augmentation.pop('random_crop', None)
if random_crop is not None:
assert isinstance(random_crop, dict)
center_crop = augmentation.pop('center_crop', None)
if center_crop is not None:
assert isinstance(center_crop, (int, list))
if len(augmentation) > 0:
raise NotImplementedError('Invalid augmentation options: %s.' % ', '.join(augmentation.keys()))
t = [
transforms.Resize(image_size) if random_crop is None else transforms.RandomResizedCrop(image_size[0], **random_crop),
transforms.CenterCrop(center_crop) if center_crop is not None else None,
transforms.RandomHorizontalFlip(horizontal_flip) if horizontal_flip is not None else None,
transforms.RandomVerticalFlip(vertical_flip) if vertical_flip is not None else None,
transforms.ToTensor(),
transforms.Normalize(mean, std)]
return transforms.Compose([v for v in t if v is not None])
def fetch_data(
dataset: Callable[[str], Dataset],
train_transform: Optional[Callable] = None,
test_transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
num_workers: int = 0,
pin_memory: bool = True,
drop_last: bool = False,
train_splits: List[str] = [],
test_splits: List[str] = [],
train_shuffle: bool = True,
test_shuffle: bool = False,
train_augmentation: dict = {},
test_augmentation: dict = {},
batch_size: int = 1,
test_batch_size: Optional[int] = None) -> Tuple[List[Tuple[str, DataLoader]], List[Tuple[str, DataLoader]]]:
"""Return data loader list.
"""
# fetch training data
# train_transform = transform(augmentation=train_augmentation) if transform else None
train_loader_list = []
for split in train_splits:
train_loader_list.append((split, DataLoader(
dataset = dataset(
split = split,
transform = train_transform,
target_transform = target_transform),
batch_size = batch_size,
num_workers = num_workers,
pin_memory = pin_memory,
drop_last=drop_last,
shuffle = train_shuffle)))
# fetch testing data
# test_transform = transform(augmentation=test_augmentation) if transform else None
test_loader_list = []
for split in test_splits:
test_loader_list.append((split, DataLoader(
dataset = dataset(
split = split,
transform = test_transform,
target_transform = target_transform),
batch_size = batch_size if test_batch_size is None else test_batch_size,
num_workers = num_workers,
pin_memory = pin_memory,
drop_last=drop_last,
shuffle = test_shuffle)))
return train_loader_list, test_loader_list
def fetch_voc(dataset: Callable[[str], Dataset],
num_workers: int = 0,
pin_memory: bool = True,
drop_last: bool = False,
train_splits: List[str] = [],
test_splits: List[str] = [],
train_shuffle: bool = True,
test_shuffle: bool = False,
train_augmentation: dict = {},
test_augmentation: dict = {},
batch_size: int = 1,
test_batch_size: Optional[int] = None) -> Tuple[List[Tuple[str, DataLoader]], List[Tuple[str, DataLoader]]]:
"""Return data loader list.
"""
return DataLoader(
dataset = dataset,
batch_size = batch_size,
num_workers = num_workers,
pin_memory = pin_memory,
drop_last=drop_last,
shuffle = train_shuffle)
def pascal_voc_object_categories(query: Optional[Union[int, str]] = None) -> Union[int, str, List[str]]:
"""PASCAL VOC dataset class names.
"""
categories = [
'aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair',
'cow', 'diningtable', 'dog', 'horse',
'motorbike', 'person', 'pottedplant',
'sheep', 'sofa', 'train', 'tvmonitor']
if query is None:
return categories
else:
for idx, val in enumerate(categories):
if isinstance(query, int) and idx == query:
return val
elif val == query:
return idx
class VOC_Classification(Dataset):
"""Dataset for PASCAL VOC classification.
"""
def __init__(self, data_dir, dataset, split, classes, transform=None, target_transform=None):
self.data_dir = data_dir
self.dataset = dataset
self.split = split
self.image_dir = os.path.join(data_dir, dataset, 'JPEGImages')
assert os.path.isdir(self.image_dir), 'Could not find image folder "%s".' % self.image_dir
self.gt_path = os.path.join(self.data_dir, self.dataset, 'ImageSets', 'Main')
assert os.path.isdir(self.gt_path), 'Could not find ground truth folder "%s".' % self.gt_path
self.transform = transform
self.target_transform = target_transform
self.classes = classes
self.image_labels = self._read_annotations(self.split)
def _read_annotations(self, split):
class_labels = OrderedDict()
num_classes = len(self.classes)
if os.path.exists(os.path.join(self.gt_path, split + '.txt')):
for class_idx in range(num_classes):
filename = os.path.join(
self.gt_path, self.classes[class_idx] + '_' + split + '.txt')
with open(filename, 'r') as f:
for line in f:
name, label = line.split()
if name not in class_labels:
class_labels[name] = np.zeros(num_classes)
class_labels[name][class_idx] = int(label)
else:
raise NotImplementedError(
'Invalid "%s" split for PASCAL %s classification task.' % (split, self.dataset))
return list(class_labels.items())
def __getitem__(self, index):
filename, target = self.image_labels[index]
target = torch.from_numpy(target).float()
img = Image.open(os.path.join(
self.image_dir, filename + '.jpg')).convert('RGB')
if self.transform:
img = self.transform(img)
if self.target_transform:
target = self.target_transform(target)
return img, target
def __len__(self):
return len(self.image_labels)
def pascal_voc_classification(
split: str,
data_dir: str,
year: int = 2007,
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
delay_resolve=True) -> Dataset:
"""PASCAL VOC dataset.
"""
object_categories = pascal_voc_object_categories()
dataset = 'VOC' + str(year)
return VOC_Classification(data_dir, dataset, split, object_categories, transform, target_transform)