Repository navigation
Expand file tree
/
Copy pathdetection_examples_utils.py
More file actions
110 lines (92 loc) · 3.29 KB
/
Copy pathdetection_examples_utils.py
File metadata and controls
110 lines (92 loc) · 3.29 KB
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
import torch
from avalanche.benchmarks import StreamUserDef
from avalanche.benchmarks.scenarios.detection_scenario import (
DetectionScenario,
)
from avalanche.benchmarks.utils.collate_functions import detection_collate_fn
from avalanche.benchmarks.utils.detection_dataset import (
detection_subset,
make_detection_dataset,
)
def split_detection_benchmark(
n_experiences: int,
train_dataset,
test_dataset,
n_classes: int,
train_transform=None,
eval_transform=None,
shuffle=True,
):
"""
Creates an example object detection/segmentation benchmark.
This is a generator for toy benchmarks and should be used only to
show how a detection benchmark can be created. It was not meant to be
used for research purposes!
:param n_experiences: The number of train experiences to create.
:param train_dataset: The training dataset.
:param test_dataset: The test dataset.
:param n_classes: The number of categories (excluding the background).
:param train_transform: The train transformation.
:param eval_transform: The eval transformation.
:param shuffle: If True, the dataset will be split randomly
:return: A :class:`DetectionScenario` instance.
"""
transform_groups = dict(
train=(train_transform, None),
eval=(eval_transform, None),
)
exp_n_imgs = len(train_dataset) // n_experiences
remaining = len(train_dataset) % n_experiences
# Note: in future versions of Avalanche, the make_classification_dataset
# function will be replaced with a more specific function for object
# detection datasets.
train_dataset_avl = make_detection_dataset(
train_dataset,
transform_groups=transform_groups,
initial_transform_group="train",
collate_fn=detection_collate_fn,
)
test_dataset_avl = make_detection_dataset(
test_dataset,
transform_groups=transform_groups,
initial_transform_group="eval",
collate_fn=detection_collate_fn,
)
exp_sz = [exp_n_imgs for _ in range(n_experiences)]
for exp_id in range(n_experiences):
if remaining == 0:
break
exp_sz[exp_id] += 1
remaining -= 1
train_indices = [i for i in range(len(train_dataset_avl))]
if shuffle:
train_indices = torch.as_tensor(train_indices)[
torch.randperm(len(train_indices))
].tolist()
train_exps_datasets = []
last_slice_idx = 0
for exp_id in range(n_experiences):
n_imgs = exp_sz[exp_id]
idx_range = train_indices[last_slice_idx : last_slice_idx + n_imgs]
train_exps_datasets.append(
detection_subset(train_dataset_avl, indices=idx_range)
)
last_slice_idx += n_imgs
train_def = StreamUserDef(
exps_data=train_exps_datasets,
exps_task_labels=[0 for _ in range(len(train_exps_datasets))],
origin_dataset=train_dataset,
is_lazy=False,
)
test_def = StreamUserDef(
exps_data=[test_dataset_avl],
exps_task_labels=[0],
origin_dataset=test_dataset,
is_lazy=False,
)
return DetectionScenario(
n_classes=n_classes,
stream_definitions={"train": train_def, "test": test_def},
complete_test_set_only=True,
)
__all__ = ["split_detection_benchmark"]