Newer
Older
# SPDX-FileCopyrightText: Copyright © 2023 Idiap Research Institute <contact@idiap.ch>
#
# SPDX-License-Identifier: GPL-3.0-or-later
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
"""Converts TBX11k JSON annotation files into simplified JSON datasets for
ptbench.
Requires ``datadir.tbx11k`` to be set on your configuration file, or that you
are sitting at the root directory of the database.
Because the test set does not have annotations, we generate train, validation
and test datasets as such:
1. The original validation set becomes the test set.
2. The original training set is split into new training and validation
sets (validation ration = 0.203 by default). The selection of samples is
stratified (respects class proportions in Özgür's way - see comments through
the code.)
Our output format is the following:
.. code:: json
{
"train": [
[
<filename-from-root>,
# label is one of:
# 0: healthy / 1: active-tb / 2: active-and-latent-tb
# 3: latent-tb / 4: sick (no tb)
<label>, [
# bounding-box annotations follow. Box-labels are:
# 0: latent-tb sign / 1: active-tb sign
[<box-label>, <xmin>, <ymin>, <width>, <height>],
[0, <xmin>, <ymin>, <width>, <height>],
[1, <xmin>, <ymin>, <width>, <height>],
...
],
],
...
],
"validation": [
# same format as for train
...
]
"test": [
# same format as for train
...
]
"""
import collections
import json
import os
import pathlib
import sys
import typing
from sklearn.model_selection import StratifiedKFold, train_test_split
def reorder(data: dict) -> list:
"""Reorders data from TBX11K into a sample-based organisation."""
categories = {k["id"]: k["name"] for k in data["categories"]}
assert len(set(categories.values())) == len(
categories
), "Category ids are not unique"
# reset category values, so latent-tb = 0, and active-tb = 1
cat_translator = {
"ActiveTuberculosis": 1,
"ObsoletePulmonaryTuberculosis": 0,
"PulmonaryTuberculosis": 2, # this should NOT exist anywhere!
}
categories = {k: cat_translator[v] for k, v in categories.items()}
images = {k["id"]: k["file_name"] for k in data["images"]}
assert len(set(images.values())) == len(images), "Image ids are not unique"
retval: dict[str, list[typing.Any]] = {
k["file_name"]: [-1, []] for k in data["images"]
}
# we now "consume" all annotations and assign each to an image
for annotation in data["annotations"]:
int_bbox: list[int] = [
categories[annotation["category_id"]],
*[round(k) for k in annotation["bbox"]],
]
retval[images[annotation["image_id"]]][1].append(int_bbox)
# remove empty bounding-box entries to save space on final JSON
for v in retval.values():
if not v[1]:
del v[1]
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
return sorted([["imgs/" + k, *v] for k, v in retval.items()])
def normalize_labels(data: list) -> list:
"""Decides on the final labels for each sample.
Categories are decided on the following principles:
0: healthy, no other bounding box detected, comes from the imgs/health
subdir
1: active-tb, no latent tb, comes from the imgs/tb subdir, has one or more
bounding boxes with label 1, and no bounding box with label 0
2: active-tb and latent tb, comes from the imgs/tb subdir, has one or more
bounding boxes with label 1 and one or more with label 0
3: latent tb, comes from the imgs/tb subdir, has one or more
bounding boxes with label 0 and no bounding box with label 1
4: sick (but no tb), comes from the imgs/sick subdir, does not have any
annotated bounding box.
"""
def _set_label(s: list) -> int:
if s[0].startswith("imgs/health"):
assert (
len(s) == 2
), f"Image {s[0]} is healthy, but contains tb bbox annotations"
return 0 # patient is healthy
elif s[0].startswith("imgs/sick"):
assert (
len(s) == 2
), f"Image {s[0]} is sick (no tb), but contains tb bbox annotations"
return 4 # patient is sick
elif s[0].startswith("imgs/tb"):
if len(s) == 2:
print(
f"WARNING: Image {s[0]} is from the tb subdir, "
f"but contains no tb bbox annotations"
)
return -1 # unknown diagnosis
bbx_labels: list[int] = [k[0] for k in s[2]]
tb_counts = collections.Counter(bbx_labels)
assert 2 not in tb_counts, (
f"Label 2 (PulmonaryTuberculosis) was used in image {s[0]} "
f"- please check!"
)
if 0 in tb_counts:
if 1 not in tb_counts:
return 3 # patient has latent tb
else:
print(
f"WARNING: Image {s[0]} has bboxes with both "
f"active and latent tb."
)
return 2 # patient has active and latent tb
else: # 1 in tb_counts:
assert 0 not in tb_counts # cannot really happen, but check...
return 1 # patient has only active tb
else:
raise RuntimeError("Cannot happen - please check")
for k in data:
k[1] = _set_label(k)
return data
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
def print_statistics(d: dict):
"""Print some statistics about the dataset."""
label_translations = {
-1: "Unknown",
0: "Healthy",
1: "Active TB only",
2: "Both active and latent TB",
3: "Latent TB only",
4: "Sick (but no TB)",
}
def _print_dataset(ds: list):
"""Print stats only for the dataset."""
class_count = collections.Counter([k[1] for k in ds])
for k, v in class_count.items():
print(f" - {label_translations[k]}: {v}")
print(f" - Total: {len(ds)}")
print("Training set statistics:")
_print_dataset(d["train"])
print("\nValidation set statistics:")
_print_dataset(d["validation"])
print("\nTest set statistics:")
_print_dataset(d["test"])
total_samples = sum(len(ds) for ds in d.values())
print(f"\nTotal samples in database: {total_samples}")
def create_v1_default_split(d: dict, seed: int, validation_size: float) -> dict:
"""In the v1 split, we consider active-tb cases against healthy.
Because the test set is not annotated we do the following:
1. The original validation set becomes the test set.
2. The original training set is split into new training and validation
sets. The selection of samples is stratified (respects class
proportions in Özgür's way - see comments)
Parameters
----------
d
The original dataset that will be split
seed
The seed to use at the relevant RNG
validation_size
The proportion of data when we split the training set to make a
train and validation sets.
"""
# filter cases (only interested in labels 0:healthy or 1:active-tb)
use_data = {
"train": [k for k in d["train"] if k[1] in (0, 1)],
"validation": [k for k in d["validation"] if k[1] in (0, 1)],
}
# Required to repeat Özgür's heuristic with labels that reverse somehow the
# sorting for "no_tb" (instead of 0), and "active_tb" (instead of 1).
# Reversing the labels used in the stratification process solves this
# issue.
targets = {0: 1, 1: 0}
train, val = train_test_split(
use_data["train"],
test_size=validation_size,
random_state=seed,
stratify=[targets[k[1]] for k in use_data["train"]],
)
return {
"train": train,
"validation": val,
"test": use_data["validation"],
}
def create_v2_default_split(d: dict, seed: int, validation_size) -> dict:
"""In the v2 split, we consider active-tb cases against healthy, sick and
latent-tb cases.
Because the test set is not annotated we do the following:
1. The original validation set becomes the test set.
2. The original training set is split into new training and validation
sets. The selection of samples is stratified (respects class
proportions in Özgür's way - see comments)
"""
# filter cases (only interested in labels 0:healthy or 1:active-tb)
use_data = {
"train": [k for k in d["train"] if k[1] in (0, 1, 3, 4)],
"validation": [k for k in d["validation"] if k[1] in (0, 1, 3, 4)],
}
# Required to repeat Özgür's heuristic with labels that reverse somehow the
# sorting for "no_tb" (instead of 0, 3 or 4), and "active_tb" (instead of
# 1). Reversing the labels used in the stratification process solves this
# issue.
targets = {0: 1, 1: 0, 3: 1, 4: 1}
train, val = train_test_split(
use_data["train"],
test_size=validation_size,
random_state=seed,
stratify=[targets[k[1]] for k in use_data["train"]],
)
# These are the targets that will show up in the split. We make everything
# that is not active-tb to be label=0.
split_targets = {0: 0, 1: 1, 3: 0, 4: 0}
return {
"train": [[k[0], split_targets[k[1]], *k[2:]] for k in train],
"validation": [[k[0], split_targets[k[1]], *k[2:]] for k in val],
"test": [
[k[0], split_targets[k[1]], *k[2:]] for k in use_data["validation"]
],
}
def create_folds(
d: dict, n: int, seed: int, validation_size: float
) -> list[dict]:
"""Creates folds from existing splits.
Parameters
----------
d
The original split to consider
n
The number of folds to produce
Returns
-------
folds
All the ``n`` folds
"""
X = d["train"] + d["validation"] + d["test"]
y = [[k[1]] for k in X]
# Initializes a StratifiedKFold object with 10 folds
skf = StratifiedKFold(n_splits=n, shuffle=True, random_state=seed)
# Required to repeat Özgür's heuristic with labels that reverse somehow the
# sorting for "no_tb" (instead of 0), and "active_tb" (instead of 1).
# Reversing the labels used in the stratification process solves this
# issue.
targets = {0: 1, 1: 0}
# Loops over the 10 folds and split the data
retval = []
for train_idx, test_idx in skf.split(X, y):
# Get the training and test data for this fold
train_dataset = [X[k] for k in train_idx]
test_dataset = [X[k] for k in test_idx]
# Split the training data into training and validation sets
train_dataset, val_dataset = train_test_split(
train_dataset,
test_size=validation_size,
random_state=seed,
stratify=[targets[k[1]] for k in train_dataset],
)
retval.append(
{
"train": train_dataset,
"validation": val_dataset,
"test": test_dataset,
}
)
return retval
def main():
if len(sys.argv) != 1:
print(__doc__)
print(f"Usage: python3 {sys.argv[0]} ")
sys.exit(0)
# program constants used by Özgür
seed = 42 # used to seed the relevant RNG
validation_size = 0.203 # proportion for test when splitting
n_folds = 10 # number of folds to create
from clapper.rc import UserDefaults
datadir = pathlib.Path(
UserDefaults("ptbench.toml").get(
"datadir.tbx11k", os.path.realpath(os.curdir)
)
)
train_filename = datadir / "annotations" / "json" / "TBX11K_train.json"
val_filename = datadir / "annotations" / "json" / "TBX11K_val.json"
test_filename = datadir / "annotations" / "json" / "all_test.json"
with open(train_filename) as f:
print(f"Loading {str(train_filename)}...")
data = json.load(f)
train_data = normalize_labels(reorder(data))
with open(val_filename) as f:
print(f"Loading {str(val_filename)}...")
data = json.load(f)
val_data = normalize_labels(reorder(data))
with open(test_filename) as f:
print(f"Loading {str(test_filename)}...")
data = json.load(f)
test_data = reorder(data)
final_data = {
"train": train_data,
"validation": val_data,
"test": test_data,
}
print_statistics(final_data)
# No need to record the re-processed data.
# with open(sys.argv[4], "w") as fout:
# json.dump(final_data, fout, indent=2)
print("\nGenerating v1 split...")
v1_split = create_v1_default_split(
final_data, seed=seed, validation_size=validation_size
)
print_statistics(v1_split)
with open("v1-healthy-vs-atb.json", "w") as v1def:
json.dump(v1_split, v1def, indent=2)
# folds for the v1 split
print(f"\nGenerating {n_folds} v1 split folds...")
v1_folds = create_folds(
v1_split, n=n_folds, seed=seed, validation_size=validation_size
)
for i, k in enumerate(v1_folds):
with open(f"v1-fold-{i}.json", "w") as v1fold:
json.dump(k, v1fold, indent=2)
print("\nGenerating v2 split...")
v2_split = create_v2_default_split(
final_data, seed=seed, validation_size=validation_size
)
print_statistics(v2_split)
with open("v2-others-vs-atb.json", "w") as v2def:
json.dump(v2_split, v2def, indent=2)
# folds for the v2 split
print(f"\nGenerating {n_folds} v2 split folds...")
v2_folds = create_folds(
v2_split, n=n_folds, seed=seed, validation_size=validation_size
)
for i, k in enumerate(v2_folds):
with open(f"v2-fold-{i}.json", "w") as v2fold:
json.dump(k, v2fold, indent=2)
if __name__ == "__main__":
main()