forked from Orion-AI-Lab/S4A-Models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coco_data_split.py
330 lines (270 loc) · 15 KB
/
coco_data_split.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
'''
Splits the data into train/val/test sets in a stratified or random way and exports
the corresponding COCO json files into the specified folder.
Stratification is applied on the unique labels contained in each image, not
on a pixel basis.
The user can choose among three experiment settings:
- Experiment 1: All tiles over all years are sampled randomly
- Experiment 2: Train/Val sets contain only Catalonia tiles for both years,
and test set contains only France tiles for a single year
- Experiment 3: Train/Val sets contain only France tiles for a single year,
and test set contains only Catalonia tiles for a different year.
'''
import argparse
from datetime import datetime
from pathlib import Path
import pandas as pd
from pycocotools.coco import COCO
import netCDF4
import xarray as xr
import numpy as np
import random
from utils.tools import keep_tile, common_labels
from utils.coco_tools import create_coco_dataframe, create_coco_netcdf
from utils.settings.mappings.mappings_cat import SAMPLE_TILES as CAT_TILES
from utils.settings.mappings.mappings_fr import SAMPLE_TILES as FR_TILES
from utils.settings.mappings.encodings_en import CROP_ENCODING
from utils.settings.config import LINEAR_ENCODER
from sklearn.preprocessing import MultiLabelBinarizer
from skmultilearn.model_selection.iterative_stratification import IterativeStratification
netCDF4.default_encoding = 'utf-8'
def plot_label_frequencies(coco, data_path, title, ax, labels_common=None):
if labels_common is not None:
label_freqs = {l: 0 for l in labels_common}
else:
label_freqs = {l: 0 for l in CROP_ENCODING.values()}
label_freqs[0] = 0
for img in coco.imgs.values():
fname = Path(img['file_name']).name
patch_netcdf = netCDF4.Dataset(data_path / fname, 'r')
labels = xr.open_dataset(xr.backends.NetCDF4DataStore(patch_netcdf['labels']))
for label in list(np.unique(labels.labels.data)):
label_freqs[label] += 1
ax.bar(list(range(1, len(label_freqs) + 1)), label_freqs.values())
ax.set_xticks(list(range(1, len(label_freqs) + 1)))
ax.set_xticklabels(list(label_freqs.keys()), rotation=90, fontsize=7)
ax.set_title(title)
def create_dataframe(data_path, tiles, years, common_labels=None):
'''
Reads the labels from the netCDF files and inserts them into a dataframe.
'''
data = pd.DataFrame(columns=['patch_path', 'labels'])
patch_paths = list(data_path.glob('*.nc'))
random.shuffle(patch_paths)
for i, patch_path in enumerate(patch_paths):
year, tile = patch_path.stem.split('_')[:2]
if not keep_tile(tile, year, tiles, years): continue
patch_netcdf = netCDF4.Dataset(patch_path, 'r')
labels = xr.open_dataset(xr.backends.NetCDF4DataStore(patch_netcdf['labels']))
unique_labels = set(np.unique(labels.labels.data))
# Keep only labels contained in LINEAR_ENCODER
unique_labels = unique_labels & set(LINEAR_ENCODER.keys())
if (unique_labels) == 0: continue
if common_labels is not None:
# We want the train/val and test tiles to have common labels
if unique_labels.isdisjoint(common_labels): continue
# Note only the common labels, we don't want to stratify on the non-common
data.loc[i] = [patch_path, list(unique_labels & common_labels)]
else:
# Data comes from all tiles so all labels are common
data.loc[i] = [patch_path, list(unique_labels)]
return data
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--how', type=str, required=True, help='Perform a stratified split.',
choices=['stratified', 'random'])
parser.add_argument('--data_path', type=str, default='dataset/netcdf/', required=False,
help='The path containing the data in netCDF format. Default "dataset/netcdf/".')
parser.add_argument('--coco_path', type=str, default='coco_files/', required=False,
help='The path to export the COCO files into. Default "coco_files/"')
parser.add_argument('--ratio', nargs='+', default=['60', '20', '20'], required=False,
help='The train/val/test ratio. Default is 60/20/20.')
parser.add_argument('--prefix', type=str, default=None, required=False,
help='The prefix to use for the exported files. If none is given, \
then the current timestamp is used.')
parser.add_argument('--plot_distros', action='store_true', default=False, required=False,
help='Plot label distributions.')
parser.add_argument('--tiles', nargs='+', default='all', required=False,
help='space-separated list of tiles to use, e.g. "31TCG 31TDG". If none given, \
all tiles found will be used.')
parser.add_argument('--years', nargs='+', default='all', required=False,
help='space-separated list of years to use, e.g. "2019 2020". If none given, \
all years found will be used.')
parser.add_argument('--num_patches', type=int, default=None, required=False,
help='The number of patches to use overall. Default all.')
parser.add_argument('--experiment', type=int, choices=[1, 2, 3], default=None, required=False,
help='The type of experiment to create COCO files for. \
If it is given, any other specified tiles/years are ignored. \
Type 1: Train/val/test with all tiles and all years. \
Type 2: Train/val with Catalonia for all years, test with France. \
Type 3: Train/val with France for 2019, test with Catalonia for 2020.')
parser.add_argument('--seed', type=int, default=None, required=False,
help='The seed to use for random patch selection. Defauly None (random).')
args = parser.parse_args()
# Define paths
data_path = Path(args.data_path)
coco_path = Path(args.coco_path)
# Ignore tile/year filtering in case an explicit experiment scheme is selected
if args.experiment is not None:
args.tiles, args.years = 'all', 'all'
train_tiles, train_years, test_tiles, test_years, common_lbls = None, None, None, None, None
# Define years and tiles based on the selected experiment
if args.experiment == 1:
# Train/val/test with all tiles/years randomly
train_tiles, train_years = set(FR_TILES + CAT_TILES), set(['2019', '2020'])
test_tiles, test_years = set(FR_TILES + CAT_TILES), set(['2019', '2020'])
elif args.experiment == 2:
# Train/val with Catalonia for all years, test with France for 2019
train_tiles, train_years = set(CAT_TILES), set(['2019', '2020'])
test_tiles, test_years = set(FR_TILES), set(['2019'])
common_lbls = common_labels(train_tiles | test_tiles)
elif args.experiment == 3:
# Train/val with France for 2019, test with Catalonia for 2020
train_tiles, train_years = set(FR_TILES), set(['2019'])
test_tiles, test_years = set(CAT_TILES), set(['2020'])
common_lbls = common_labels(train_tiles | test_tiles)
else:
train_tiles, train_years = set(args.tiles), set(args.years)
test_tiles, test_years = set(args.tiles), set(args.years)
common_lbls = common_labels(train_tiles | test_tiles)
# Define prefix
if args.prefix is None:
# No prefix given, use current timestamp
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
prefix = timestamp
else:
prefix = args.prefix
# Define ratios
train_r, val_r, test_r = [int(i) for i in args.ratio]
if args.how == 'stratified':
if args.seed is not None:
# Set the seed
random.seed(args.seed)
# Create a dataframe containing patch paths and the labels of each patch
if args.experiment == 1:
data = create_dataframe(data_path, train_tiles | test_tiles, train_years | test_years, common_labels=common_lbls)
all_patches = data.shape[0]
else:
data = create_dataframe(data_path, train_tiles, train_years, common_labels=common_lbls)
test_data = create_dataframe(data_path, test_tiles, test_years, common_labels=common_lbls)
all_patches = data.shape[0]
test_patches = test_data.shape[0]
# Convert labels to one-hot encoding
mlb = MultiLabelBinarizer()
labels_onehot = pd.DataFrame(mlb.fit_transform(data.labels), columns=mlb.classes_)
data = data.drop(columns=['labels'])
if args.num_patches is None:
new_train_r = train_r
else:
new_train_r = (args.num_patches * train_r) / all_patches
# Select a subset of tiles in a stratified way based on the given ratio
stratifier = IterativeStratification(n_splits=2, order=1, sample_distribution_per_fold=[1 - (new_train_r / 100), new_train_r / 100])
train_idx, val_test_idx = next(stratifier.split(data.values[:, np.newaxis], labels_onehot.values))
# `IterativeStratification` returns a train-test split, so we must further
# split the test set into test and validation
X_val_test = data.iloc[val_test_idx, :]
y_val_test = labels_onehot.iloc[val_test_idx, :]
if args.num_patches is None:
new_val_r = (X_val_test.shape[0] * val_r) / 100
else:
new_val_r = (args.num_patches * val_r) / X_val_test.shape[0]
stratifier = IterativeStratification(n_splits=2, order=1, sample_distribution_per_fold=[1 - (new_val_r / 100), new_val_r / 100])
val_idx, test_idx = next(stratifier.split(X_val_test.values[:, np.newaxis], y_val_test))
if args.experiment in [2, 3]:
# We will split the test data so that one of the splits will have the
# appropriate test size
if args.num_patches is None:
test_size = (test_r / 100) * all_patches
new_test_r = (test_size / test_patches) * 100
else:
new_test_r = (args.num_patches * test_r) / test_patches
mlb = MultiLabelBinarizer()
labels_onehot = pd.DataFrame(mlb.fit_transform(test_data.labels), columns=mlb.classes_)
stratifier = IterativeStratification(n_splits=2, order=1, sample_distribution_per_fold=[1 - (new_test_r / 100), new_test_r / 100])
test_idx, _ = next(stratifier.split(test_data.values[:, np.newaxis], labels_onehot.values))
X_train = data.iloc[train_idx, :]
X_val = data.iloc[val_idx, :]
X_test = test_data.iloc[test_idx, :]
else:
X_test = X_val_test.iloc[test_idx, :]
y_test = y_val_test.iloc[test_idx, :]
if args.num_patches is None:
new_test_r = (X_test.shape[0] * test_r) / 100
else:
# We must further split the test set in order to obtain the required test size
new_test_r = (args.num_patches * test_r) / X_test.shape[0]
stratifier = IterativeStratification(n_splits=2, order=1, sample_distribution_per_fold=[1 - (new_test_r / 100), new_test_r / 100])
test_idx, _ = next(stratifier.split(X_test.values[:, np.newaxis], y_test))
X_train = data.iloc[train_idx, :]
X_val = X_val_test.iloc[val_idx, :]
X_test = X_val_test.iloc[test_idx, :]
# Export COCO files
create_coco_dataframe(df=X_train,
path_coco=coco_path / f'{prefix}_coco_train.json',
keep_tiles=train_tiles,
keep_years=train_years,
common_labels=common_lbls
)
create_coco_dataframe(df=X_val,
path_coco=coco_path / f'{prefix}_coco_val.json',
keep_tiles=train_tiles,
keep_years=train_years,
common_labels=common_lbls
)
create_coco_dataframe(df=X_test,
path_coco=coco_path / f'{prefix}_coco_test.json',
keep_tiles=test_tiles,
keep_years=test_years,
common_labels=common_lbls
)
elif args.how == 'random':
create_coco_netcdf(netcdf_path=data_path,
path_train=coco_path / f'{prefix}_coco_train.json',
path_test=coco_path / f'{prefix}_coco_test.json',
path_val=coco_path / f'{prefix}_coco_val.json',
train_r=train_r,
val_r=val_r,
keep_tiles=args.tiles,
keep_years=args.years,
experiment=args.experiment,
train_tiles=train_tiles,
test_tiles=test_tiles,
train_years=train_years,
test_years=test_years,
common_labels=common_lbls,
num_patches=args.num_patches
)
# Plot label distributions of the produced files.
if args.plot_distros:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2)
if args.experiment == 1:
plot_label_frequencies(COCO(coco_path / f'{prefix}_coco_train.json'),
data_path,
"Train set",
axes[0, 0])
plot_label_frequencies(COCO(coco_path / f'{prefix}_coco_val.json'),
data_path,
"Validation set",
axes[1, 0])
plot_label_frequencies(COCO(coco_path / f'{prefix}_coco_test.json'),
data_path,
"Test set",
axes[1, 1])
else:
plot_label_frequencies(COCO(coco_path / f'{prefix}_coco_train.json'),
data_path,
"Train set",
axes[0, 0],
labels_common=common_lbls)
plot_label_frequencies(COCO(coco_path / f'{prefix}_coco_val.json'),
data_path,
"Validation set",
axes[1, 0],
labels_common=common_lbls)
plot_label_frequencies(COCO(coco_path / f'{prefix}_coco_test.json'),
data_path,
"Test set",
axes[1, 1],
labels_common=common_lbls)
plt.show()