This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 211
/
adapter.py
210 lines (186 loc) · 7.23 KB
/
adapter.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
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import functools
from typing import Any, Callable, Dict, List, Optional
from torch.utils.data import DataLoader, Sampler
import flash
from flash.core.adapter import Adapter
from flash.core.data.auto_dataset import BaseAutoDataset
from flash.core.data.io.input import DataKeys
from flash.core.integrations.icevision.transforms import from_icevision_predictions, to_icevision_record
from flash.core.model import Task
from flash.core.utilities.imports import _ICEVISION_AVAILABLE
from flash.core.utilities.url_error import catch_url_error
if _ICEVISION_AVAILABLE:
from icevision.metrics import COCOMetric
from icevision.metrics import Metric as IceVisionMetric
else:
COCOMetric = object
class SimpleCOCOMetric(COCOMetric):
def finalize(self) -> Dict[str, float]:
logs = super().finalize()
return {
"Precision (IoU=0.50:0.95,area=all)": logs["AP (IoU=0.50:0.95) area=all"],
"Recall (IoU=0.50:0.95,area=all,maxDets=100)": logs["AR (IoU=0.50:0.95) area=all maxDets=100"],
}
class IceVisionAdapter(Adapter):
"""The ``IceVisionAdapter`` is an :class:`~flash.core.adapter.Adapter` for integrating with IceVision."""
required_extras: str = "image"
def __init__(self, model_type, model, icevision_adapter, backbone):
super().__init__()
self.model_type = model_type
self.model = model
self.icevision_adapter = icevision_adapter
self.backbone = backbone
@classmethod
@catch_url_error
def from_task(
cls,
task: Task,
num_classes: int,
backbone: str,
head: str,
pretrained: bool = True,
metrics: Optional["IceVisionMetric"] = None,
image_size: Optional = None,
**kwargs,
) -> Adapter:
metadata = task.heads.get(head, with_metadata=True)
backbones = metadata["metadata"]["backbones"]
backbone_config = backbones.get(backbone)(pretrained)
model_type, model, icevision_adapter, backbone = metadata["fn"](
backbone_config,
num_classes,
image_size=image_size,
**kwargs,
)
icevision_adapter = icevision_adapter(model=model, metrics=metrics)
return cls(model_type, model, icevision_adapter, backbone)
@staticmethod
def _collate_fn(collate_fn, samples, metadata: Optional[List[Dict[str, Any]]] = None):
metadata = metadata or [None] * len(samples)
return {
DataKeys.INPUT: collate_fn(
[to_icevision_record({**sample, DataKeys.METADATA: m}) for sample, m in zip(samples, metadata)]
),
DataKeys.METADATA: metadata,
}
def process_train_dataset(
self,
dataset: BaseAutoDataset,
trainer: "flash.Trainer",
batch_size: int,
num_workers: int,
pin_memory: bool,
collate_fn: Optional[Callable] = None,
shuffle: bool = False,
drop_last: bool = False,
sampler: Optional[Sampler] = None,
) -> DataLoader:
data_loader = self.model_type.train_dl(
dataset,
batch_size=batch_size,
num_workers=num_workers,
pin_memory=pin_memory,
shuffle=shuffle,
drop_last=drop_last,
sampler=sampler,
)
data_loader.collate_fn = functools.partial(self._collate_fn, data_loader.collate_fn)
return data_loader
def process_val_dataset(
self,
dataset: BaseAutoDataset,
trainer: "flash.Trainer",
batch_size: int,
num_workers: int,
pin_memory: bool,
collate_fn: Optional[Callable] = None,
shuffle: bool = False,
drop_last: bool = False,
sampler: Optional[Sampler] = None,
) -> DataLoader:
data_loader = self.model_type.valid_dl(
dataset,
batch_size=batch_size,
num_workers=num_workers,
pin_memory=pin_memory,
shuffle=shuffle,
drop_last=drop_last,
sampler=sampler,
)
data_loader.collate_fn = functools.partial(self._collate_fn, data_loader.collate_fn)
return data_loader
def process_test_dataset(
self,
dataset: BaseAutoDataset,
trainer: "flash.Trainer",
batch_size: int,
num_workers: int,
pin_memory: bool,
collate_fn: Optional[Callable] = None,
shuffle: bool = False,
drop_last: bool = False,
sampler: Optional[Sampler] = None,
) -> DataLoader:
data_loader = self.model_type.valid_dl(
dataset,
batch_size=batch_size,
num_workers=num_workers,
pin_memory=pin_memory,
shuffle=shuffle,
drop_last=drop_last,
sampler=sampler,
)
data_loader.collate_fn = functools.partial(self._collate_fn, data_loader.collate_fn)
return data_loader
def process_predict_dataset(
self,
dataset: BaseAutoDataset,
batch_size: int = 1,
num_workers: int = 0,
pin_memory: bool = False,
collate_fn: Callable = lambda x: x,
shuffle: bool = False,
drop_last: bool = True,
sampler: Optional[Sampler] = None,
) -> DataLoader:
data_loader = self.model_type.infer_dl(
dataset,
batch_size=batch_size,
num_workers=num_workers,
pin_memory=pin_memory,
shuffle=shuffle,
drop_last=drop_last,
sampler=sampler,
)
data_loader.collate_fn = functools.partial(self._collate_fn, data_loader.collate_fn)
return data_loader
def training_step(self, batch, batch_idx) -> Any:
return self.icevision_adapter.training_step(batch[DataKeys.INPUT], batch_idx)
def validation_step(self, batch, batch_idx):
return self.icevision_adapter.validation_step(batch[DataKeys.INPUT], batch_idx)
def test_step(self, batch, batch_idx):
return self.icevision_adapter.validation_step(batch[DataKeys.INPUT], batch_idx)
def predict_step(self, batch: Any, batch_idx: int, dataloader_idx: int = 0) -> Any:
batch[DataKeys.PREDS] = self(batch[DataKeys.INPUT])
return batch
def forward(self, batch: Any) -> Any:
return from_icevision_predictions(self.model_type.predict_from_dl(self.model, [batch], show_pbar=False))
def training_epoch_end(self, outputs) -> None:
return self.icevision_adapter.training_epoch_end(outputs)
def validation_epoch_end(self, outputs) -> None:
return self.icevision_adapter.validation_epoch_end(outputs)
def test_epoch_end(self, outputs) -> None:
return self.icevision_adapter.validation_epoch_end(outputs)