forked from Lydorn/Polygonization-by-Frame-Field-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_folds.py
234 lines (194 loc) · 11.2 KB
/
dataset_folds.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
import functools
import torch
import torch.utils.data
from frame_field_learning import data_transforms
from lydorn_utils import print_utils
def inria_aerial_train_tile_filter(tile, train_val_split_point):
return tile["number"] <= train_val_split_point
def inria_aerial_val_tile_filter(tile, train_val_split_point):
return train_val_split_point < tile["number"]
def get_inria_aerial_folds(config, root_dir, folds):
from torch_lydorn.torchvision.datasets import InriaAerial
# --- Online transform done on the host (CPU):
online_cpu_transform = data_transforms.get_online_cpu_transform(config,
augmentations=config["data_aug_params"]["enable"])
mask_only = config["dataset_params"]["mask_only"]
kwargs = {
"pre_process": config["dataset_params"]["pre_process"],
"transform": online_cpu_transform,
"patch_size": config["dataset_params"]["data_patch_size"],
"patch_stride": config["dataset_params"]["input_patch_size"],
"pre_transform": data_transforms.get_offline_transform_patch(distances=not mask_only, sizes=not mask_only),
"small": config["dataset_params"]["small"],
"pool_size": config["num_workers"],
"gt_source": config["dataset_params"]["gt_source"],
"gt_type": config["dataset_params"]["gt_type"],
"gt_dirname": config["dataset_params"]["gt_dirname"],
"mask_only": mask_only,
}
train_val_split_point = config["dataset_params"]["train_fraction"] * 36
partial_train_tile_filter = functools.partial(inria_aerial_train_tile_filter, train_val_split_point=train_val_split_point)
partial_val_tile_filter = functools.partial(inria_aerial_val_tile_filter, train_val_split_point=train_val_split_point)
ds_list = []
for fold in folds:
if fold == "train":
ds = InriaAerial(root_dir, fold="train", tile_filter=partial_train_tile_filter, **kwargs)
ds_list.append(ds)
elif fold == "val":
ds = InriaAerial(root_dir, fold="train", tile_filter=partial_val_tile_filter, **kwargs)
ds_list.append(ds)
elif fold == "train_val":
ds = InriaAerial(root_dir, fold="train", **kwargs)
ds_list.append(ds)
elif fold == "test":
ds = InriaAerial(root_dir, fold="test", **kwargs)
ds_list.append(ds)
else:
print_utils.print_error("ERROR: fold \"{}\" not recognized, implement it in dataset_folds.py.".format(fold))
return ds_list
def get_luxcarta_buildings(config, root_dir, folds):
from torch_lydorn.torchvision.datasets import LuxcartaBuildings
# --- Online transform done on the host (CPU):
online_cpu_transform = data_transforms.get_online_cpu_transform(config,
augmentations=config["data_aug_params"]["enable"])
data_patch_size = config["dataset_params"]["data_patch_size"] if config["data_aug_params"]["enable"] else config[
"input_patch_size"]
ds = LuxcartaBuildings(root_dir,
transform=online_cpu_transform,
patch_size=data_patch_size,
patch_stride=config["dataset_params"]["input_patch_size"],
pre_transform=data_transforms.get_offline_transform_patch(),
fold="train",
pool_size=config["num_workers"])
torch.manual_seed(config["dataset_params"]["seed"]) # Ensure a seed is set
train_split_length = int(round(config["dataset_params"]["train_fraction"] * len(ds)))
val_split_length = len(ds) - train_split_length
train_ds, val_ds = torch.utils.data.random_split(ds, [train_split_length, val_split_length])
ds_list = []
for fold in folds:
if fold == "train":
ds_list.append(train_ds)
elif fold == "val":
ds_list.append(val_ds)
elif fold == "test":
# TODO: handle patching with multi-GPU processing
print_utils.print_error("WARNING: handle patching with multi-GPU processing")
ds = LuxcartaBuildings(root_dir,
transform=online_cpu_transform,
pre_transform=data_transforms.get_offline_transform_patch(),
fold="test",
pool_size=config["num_workers"])
ds_list.append(ds)
else:
print_utils.print_error("ERROR: fold \"{}\" not recognized, implement it in dataset_folds.py.".format(fold))
return ds_list
def get_mapping_challenge(config, root_dir, folds):
from torch_lydorn.torchvision.datasets import MappingChallenge
if "train" in folds or "val" in folds or "train_val" in folds:
train_online_cpu_transform = data_transforms.get_online_cpu_transform(config,
augmentations=config["data_aug_params"][
"enable"])
ds = MappingChallenge(root_dir,
transform=train_online_cpu_transform,
pre_transform=data_transforms.get_offline_transform_patch(),
small=config["dataset_params"]["small"],
fold="train",
pool_size=config["num_workers"])
torch.manual_seed(config["dataset_params"]["seed"]) # Ensure a seed is set
train_split_length = int(round(config["dataset_params"]["train_fraction"] * len(ds)))
val_split_length = len(ds) - train_split_length
train_ds, val_ds = torch.utils.data.random_split(ds, [train_split_length, val_split_length])
ds_list = []
for fold in folds:
if fold == "train":
ds_list.append(train_ds)
elif fold == "val":
ds_list.append(val_ds)
elif fold == "train_val":
ds_list.append(ds)
elif fold == "test":
# The val fold from the original challenge is used as test here
# because we don't have the ground truth for the test_images fold of the challenge:
test_online_cpu_transform = data_transforms.get_eval_online_cpu_transform()
test_ds = MappingChallenge(root_dir,
transform=test_online_cpu_transform,
pre_transform=data_transforms.get_offline_transform_patch(),
small=config["dataset_params"]["small"],
fold="val",
pool_size=config["num_workers"])
ds_list.append(test_ds)
else:
print_utils.print_error("ERROR: fold \"{}\" not recognized, implement it in dataset_folds.py.".format(fold))
exit()
return ds_list
def get_opencities_competition(config, root_dir, folds):
from torch_lydorn.torchvision.datasets import RasterizedOpenCities, OpenCitiesTestDataset
data_patch_size = config["dataset_params"]["data_patch_size"] if config["data_aug_params"]["enable"] else config[
"input_patch_size"]
ds_list = []
for fold in folds:
if fold == "train":
train_ds = RasterizedOpenCities(tier=1, augment=False, small_subset=False, resize_size=data_patch_size,
data_dir=root_dir, baseline_mode=False, val=False,
val_split=config["dataset_params"]["val_fraction"])
ds_list.append(train_ds)
elif fold == "val":
val_ds = RasterizedOpenCities(tier=1, augment=False, small_subset=False, resize_size=data_patch_size,
data_dir=root_dir, baseline_mode=False, val=True,
val_split=config["dataset_params"]["val_fraction"])
ds_list.append(val_ds)
elif fold == "test":
test_ds = OpenCitiesTestDataset(root_dir + "/test/", 1024)
ds_list.append(test_ds)
else:
print_utils.print_error("ERROR: fold \"{}\" not recognized, implement it in dataset_folds.py.".format(fold))
return ds_list
def get_xview2_dataset(config, root_dir, folds):
from torch_lydorn.torchvision.datasets import xView2Dataset
if "train" in folds or "val" in folds or "train_val" in folds:
train_online_cpu_transform = data_transforms.get_online_cpu_transform(config,
augmentations=config["data_aug_params"][
"enable"])
ds = xView2Dataset(root_dir, fold="train", pre_process=True,
patch_size=config["dataset_params"]["data_patch_size"],
pre_transform=data_transforms.get_offline_transform_patch(),
transform=train_online_cpu_transform,
small=config["dataset_params"]["small"], pool_size=config["num_workers"])
torch.manual_seed(config["dataset_params"]["seed"]) # Ensure a seed is set
train_split_length = int(round(config["dataset_params"]["train_fraction"] * len(ds)))
val_split_length = len(ds) - train_split_length
train_ds, val_ds = torch.utils.data.random_split(ds, [train_split_length, val_split_length])
ds_list = []
for fold in folds:
if fold == "train":
ds_list.append(train_ds)
elif fold == "val":
ds_list.append(val_ds)
elif fold == "train_val":
ds_list.append(ds)
elif fold == "test":
raise NotImplementedError("Test fold not yet implemented (skip pre-processing?)")
elif fold == "hold":
raise NotImplementedError("Hold fold not yet implemented (skip pre-processing?)")
else:
print_utils.print_error("ERROR: fold \"{}\" not recognized, implement it in dataset_folds.py.".format(fold))
exit()
return ds_list
def get_folds(config, root_dir, folds):
assert set(folds).issubset({"train", "val", "train_val", "test"}), \
'fold in folds should be in ["train", "val", "train_val", "test"]'
if config["dataset_params"]["root_dirname"] == "AerialImageDataset":
return get_inria_aerial_folds(config, root_dir, folds)
elif config["dataset_params"]["root_dirname"] == "luxcarta_precise_buildings":
return get_luxcarta_buildings(config, root_dir, folds)
elif config["dataset_params"]["root_dirname"] == "mapping_challenge_dataset":
return get_mapping_challenge(config, root_dir, folds)
elif config["dataset_params"]["root_dirname"] == "segbuildings":
return get_opencities_competition(config, root_dir, folds)
elif config["dataset_params"]["root_dirname"] == "xview2_xbd_dataset":
return get_xview2_dataset(config, root_dir, folds)
else:
print_utils.print_error("ERROR: config[\"data_root_partial_dirpath\"] = \"{}\" is an unknown dataset! "
"If it is a new dataset, add it in dataset_folds.py's get_folds() function.".format(
config["dataset_params"]["root_dirname"]))
exit()