ml-networks 0.1.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ml_networks/__init__.py +63 -0
- ml_networks/callbacks.py +29 -0
- ml_networks/config.py +556 -0
- ml_networks/jax/__init__.py +103 -0
- ml_networks/jax/activations.py +226 -0
- ml_networks/jax/base.py +170 -0
- ml_networks/jax/contrastive.py +286 -0
- ml_networks/jax/distributions.py +529 -0
- ml_networks/jax/hypernetworks.py +339 -0
- ml_networks/jax/jax_utils.py +481 -0
- ml_networks/jax/layers.py +1643 -0
- ml_networks/jax/loss.py +355 -0
- ml_networks/jax/unet.py +886 -0
- ml_networks/jax/vision.py +929 -0
- ml_networks/py.typed +0 -0
- ml_networks/torch/__init__.py +97 -0
- ml_networks/torch/activations.py +202 -0
- ml_networks/torch/base.py +31 -0
- ml_networks/torch/contrastive.py +337 -0
- ml_networks/torch/distributions.py +1016 -0
- ml_networks/torch/hypernetworks.py +623 -0
- ml_networks/torch/layers.py +1557 -0
- ml_networks/torch/loss.py +359 -0
- ml_networks/torch/torch_utils.py +299 -0
- ml_networks/torch/unet.py +879 -0
- ml_networks/torch/vision.py +1276 -0
- ml_networks/utils.py +464 -0
- ml_networks-0.1.9.dist-info/METADATA +421 -0
- ml_networks-0.1.9.dist-info/RECORD +31 -0
- ml_networks-0.1.9.dist-info/WHEEL +4 -0
- ml_networks-0.1.9.dist-info/licenses/LICENSE +21 -0
ml_networks/__init__.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from .callbacks import ProgressBarCallback
|
|
2
|
+
from .config import (
|
|
3
|
+
AdaptiveAveragePoolingConfig,
|
|
4
|
+
AttentionConfig,
|
|
5
|
+
ContrastiveLearningConfig,
|
|
6
|
+
ConvConfig,
|
|
7
|
+
ConvNetConfig,
|
|
8
|
+
DecoderConfig,
|
|
9
|
+
EncoderConfig,
|
|
10
|
+
LinearConfig,
|
|
11
|
+
MLPConfig,
|
|
12
|
+
ResNetConfig,
|
|
13
|
+
SoftmaxTransConfig,
|
|
14
|
+
SpatialSoftmaxConfig,
|
|
15
|
+
TransformerConfig,
|
|
16
|
+
UNetConfig,
|
|
17
|
+
ViTConfig,
|
|
18
|
+
)
|
|
19
|
+
from .utils import (
|
|
20
|
+
conv_out,
|
|
21
|
+
conv_out_shape,
|
|
22
|
+
conv_transpose_in,
|
|
23
|
+
conv_transpose_in_shape,
|
|
24
|
+
conv_transpose_out,
|
|
25
|
+
conv_transpose_out_shape,
|
|
26
|
+
determine_loader,
|
|
27
|
+
load_blosc2,
|
|
28
|
+
output_padding,
|
|
29
|
+
output_padding_shape,
|
|
30
|
+
save_blosc2,
|
|
31
|
+
seed_worker,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
__all__ = [
|
|
35
|
+
"AdaptiveAveragePoolingConfig",
|
|
36
|
+
"AttentionConfig",
|
|
37
|
+
"ContrastiveLearningConfig",
|
|
38
|
+
"ConvConfig",
|
|
39
|
+
"ConvNetConfig",
|
|
40
|
+
"DecoderConfig",
|
|
41
|
+
"EncoderConfig",
|
|
42
|
+
"LinearConfig",
|
|
43
|
+
"MLPConfig",
|
|
44
|
+
"ProgressBarCallback",
|
|
45
|
+
"ResNetConfig",
|
|
46
|
+
"SoftmaxTransConfig",
|
|
47
|
+
"SpatialSoftmaxConfig",
|
|
48
|
+
"TransformerConfig",
|
|
49
|
+
"UNetConfig",
|
|
50
|
+
"ViTConfig",
|
|
51
|
+
"conv_out",
|
|
52
|
+
"conv_out_shape",
|
|
53
|
+
"conv_transpose_in",
|
|
54
|
+
"conv_transpose_in_shape",
|
|
55
|
+
"conv_transpose_out",
|
|
56
|
+
"conv_transpose_out_shape",
|
|
57
|
+
"determine_loader",
|
|
58
|
+
"load_blosc2",
|
|
59
|
+
"output_padding",
|
|
60
|
+
"output_padding_shape",
|
|
61
|
+
"save_blosc2",
|
|
62
|
+
"seed_worker",
|
|
63
|
+
]
|
ml_networks/callbacks.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""コールバックを扱うモジュール."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pytorch_lightning.callbacks import RichProgressBar
|
|
6
|
+
from pytorch_lightning.callbacks.progress.rich_progress import RichProgressBarTheme
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ProgressBarCallback(RichProgressBar):
|
|
10
|
+
"""
|
|
11
|
+
Make the progress bar richer.
|
|
12
|
+
|
|
13
|
+
References
|
|
14
|
+
----------
|
|
15
|
+
* https://qiita.com/akihironitta/items/edfd6b29dfb67b17fb00
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
def __init__(self) -> None:
|
|
19
|
+
"""Rich progress bar with custom theme."""
|
|
20
|
+
theme = RichProgressBarTheme(
|
|
21
|
+
description="green_yellow",
|
|
22
|
+
progress_bar="green1",
|
|
23
|
+
progress_bar_finished="green1",
|
|
24
|
+
batch_progress="green_yellow",
|
|
25
|
+
time="grey82",
|
|
26
|
+
processing_speed="grey82",
|
|
27
|
+
metrics="grey82",
|
|
28
|
+
)
|
|
29
|
+
super().__init__(theme=theme)
|
ml_networks/config.py
ADDED
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
"""設定を扱うモジュール."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any, Literal
|
|
8
|
+
|
|
9
|
+
from omegaconf import DictConfig, ListConfig, OmegaConf
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def load_config(path: str) -> DictConfig | ListConfig:
|
|
13
|
+
"""
|
|
14
|
+
Convert model config `.yaml` to `dictconfig` with custom resolvers.
|
|
15
|
+
|
|
16
|
+
Returns
|
|
17
|
+
-------
|
|
18
|
+
DictConfig
|
|
19
|
+
"""
|
|
20
|
+
return OmegaConf.load(Path(path))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def convert_dictconfig_to_dict(obj: Any) -> Any:
|
|
24
|
+
"""Recursively convert dictConfig to dict in any iterable object.
|
|
25
|
+
|
|
26
|
+
Parameters
|
|
27
|
+
----------
|
|
28
|
+
obj : Any
|
|
29
|
+
Object to convert.
|
|
30
|
+
|
|
31
|
+
Returns
|
|
32
|
+
-------
|
|
33
|
+
Any
|
|
34
|
+
Converted object.
|
|
35
|
+
"""
|
|
36
|
+
if isinstance(obj, DictConfig):
|
|
37
|
+
return dict(obj)
|
|
38
|
+
if isinstance(obj, list | tuple):
|
|
39
|
+
return type(obj)(convert_dictconfig_to_dict(item) for item in obj)
|
|
40
|
+
if isinstance(obj, dict):
|
|
41
|
+
return {k: convert_dictconfig_to_dict(v) for k, v in obj.items()}
|
|
42
|
+
return obj
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass
|
|
46
|
+
class ContrastiveLearningConfig:
|
|
47
|
+
"""Contrastive learning configuration."""
|
|
48
|
+
|
|
49
|
+
dim_feature: int
|
|
50
|
+
eval_func: MLPConfig
|
|
51
|
+
dim_input2: int | None = None
|
|
52
|
+
cross_entropy_like: bool = False
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass
|
|
56
|
+
class AttentionConfig:
|
|
57
|
+
"""
|
|
58
|
+
Attention configuration.
|
|
59
|
+
|
|
60
|
+
Attributes
|
|
61
|
+
----------
|
|
62
|
+
nhead : int
|
|
63
|
+
Number of heads.
|
|
64
|
+
patch_size : int
|
|
65
|
+
Patch size.
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
nhead: int
|
|
69
|
+
patch_size: int
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclass
|
|
73
|
+
class SoftmaxTransConfig:
|
|
74
|
+
"""
|
|
75
|
+
Softmax transformation configuration.
|
|
76
|
+
|
|
77
|
+
Attributes
|
|
78
|
+
----------
|
|
79
|
+
vector : int
|
|
80
|
+
Vector size.
|
|
81
|
+
sigma : float
|
|
82
|
+
Sigma value.
|
|
83
|
+
n_ignore : int
|
|
84
|
+
Number of ignored elements. Default is 0.
|
|
85
|
+
max : float
|
|
86
|
+
Maximum value. Default is 1.0.
|
|
87
|
+
min : float
|
|
88
|
+
Minimum value. Default is -1.0.
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
vector: int
|
|
92
|
+
sigma: float
|
|
93
|
+
n_ignore: int = 0
|
|
94
|
+
max: float = 1.0
|
|
95
|
+
min: float = -1.0
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@dataclass
|
|
99
|
+
class ConvConfig:
|
|
100
|
+
"""
|
|
101
|
+
A convolutional layer configuration.
|
|
102
|
+
|
|
103
|
+
Attributes
|
|
104
|
+
----------
|
|
105
|
+
activation : str
|
|
106
|
+
Activation function.
|
|
107
|
+
kernel_size : int
|
|
108
|
+
Kernel size.
|
|
109
|
+
stride : int
|
|
110
|
+
Stride.
|
|
111
|
+
padding : int
|
|
112
|
+
Padding.
|
|
113
|
+
output_padding : int
|
|
114
|
+
Output padding, especially for transposed convolution. Default is 0.
|
|
115
|
+
dilation : int
|
|
116
|
+
Dilation.
|
|
117
|
+
groups : int
|
|
118
|
+
Number of groups. Default is 1. See https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html.
|
|
119
|
+
bias : bool
|
|
120
|
+
Whether to use bias. Default is True.
|
|
121
|
+
dropout : float
|
|
122
|
+
Dropout rate. If it's set to 0.0, dropout is not applied. Default is 0.0.
|
|
123
|
+
norm : Literal["batch", "group", "none"]
|
|
124
|
+
Normalization layer. If it's set to "none", normalization is not applied. Default is "none".
|
|
125
|
+
norm_cfg : dict
|
|
126
|
+
Normalization layer configuration. If you want to use Instance, Layer, or Group normalization,
|
|
127
|
+
set norm to "group" and set norm_cfg with "num_groups=$in_channel, 1, or any value". Default is {}.
|
|
128
|
+
scale_factor : int
|
|
129
|
+
Scale factor for upsample, especially for PixelShuffle or PixelUnshuffle.
|
|
130
|
+
If it's set to >0, upsample is applied. If it's set to <0 downsample is applied.
|
|
131
|
+
Otherwise, no upsample or downsample is applied. Default is 0.
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
"""
|
|
135
|
+
|
|
136
|
+
activation: str
|
|
137
|
+
kernel_size: int
|
|
138
|
+
stride: int
|
|
139
|
+
padding: int
|
|
140
|
+
output_padding: int = 0
|
|
141
|
+
dilation: int = 1
|
|
142
|
+
groups: int = 1
|
|
143
|
+
bias: bool = True
|
|
144
|
+
padding_mode: Literal["zeros", "reflect", "replicate", "circular"] = "zeros"
|
|
145
|
+
dropout: float = 0.0
|
|
146
|
+
norm: Literal["batch", "group", "none"] = "none"
|
|
147
|
+
norm_cfg: dict[str, Any] = field(default_factory=dict)
|
|
148
|
+
norm_first: bool = False
|
|
149
|
+
scale_factor: int = 0
|
|
150
|
+
|
|
151
|
+
def __post_init__(self) -> None:
|
|
152
|
+
"""Set `.norm_cfg`."""
|
|
153
|
+
if self.norm == "none":
|
|
154
|
+
self.norm_cfg = {}
|
|
155
|
+
else:
|
|
156
|
+
self.norm_cfg = dict(self.norm_cfg)
|
|
157
|
+
|
|
158
|
+
def dictcfg2dict(self) -> None:
|
|
159
|
+
"""Convert dictConfig to dict for `ConvConfig`."""
|
|
160
|
+
self.norm_cfg = dict(self.norm_cfg)
|
|
161
|
+
|
|
162
|
+
for key, value in self.__dict__.items():
|
|
163
|
+
if isinstance(value, DictConfig | ListConfig | list | tuple | dict):
|
|
164
|
+
setattr(self, key, convert_dictconfig_to_dict(value))
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@dataclass
|
|
168
|
+
class ConvNetConfig:
|
|
169
|
+
"""
|
|
170
|
+
Convolutional neural network layers configuration.
|
|
171
|
+
|
|
172
|
+
Attributes
|
|
173
|
+
----------
|
|
174
|
+
channels : Tuple[int, ...]
|
|
175
|
+
Number of channels for each layer.
|
|
176
|
+
conv_cfgs : Tuple[ConvConfig, ...]
|
|
177
|
+
Convolutional layer configurations.
|
|
178
|
+
The length of conv_cfgs should be the same as the length of channels.
|
|
179
|
+
init_channel : int
|
|
180
|
+
Initial number of channels, especially for transposed convolution.
|
|
181
|
+
"""
|
|
182
|
+
|
|
183
|
+
channels: tuple[int, ...]
|
|
184
|
+
conv_cfgs: tuple[ConvConfig, ...]
|
|
185
|
+
attention: AttentionConfig | None = None
|
|
186
|
+
init_channel: int = 16
|
|
187
|
+
|
|
188
|
+
def __post_init__(self) -> None:
|
|
189
|
+
"""Set `channels` and `conv_cfgs` as tuple."""
|
|
190
|
+
self.conv_cfgs = tuple(self.conv_cfgs)
|
|
191
|
+
self.channels = tuple(self.channels)
|
|
192
|
+
|
|
193
|
+
def dictcfg2dict(self) -> None:
|
|
194
|
+
"""Convert dictConfig to dict for `ConvNetConfig`."""
|
|
195
|
+
self.channels = tuple(self.channels)
|
|
196
|
+
conv_cfgs = []
|
|
197
|
+
for cfg_item in self.conv_cfgs:
|
|
198
|
+
conv_cfg = cfg_item
|
|
199
|
+
if isinstance(conv_cfg, DictConfig):
|
|
200
|
+
conv_cfg_dict = convert_dictconfig_to_dict(conv_cfg)
|
|
201
|
+
conv_cfg = ConvConfig(**conv_cfg_dict)
|
|
202
|
+
conv_cfg.dictcfg2dict()
|
|
203
|
+
conv_cfgs.append(conv_cfg)
|
|
204
|
+
self.conv_cfgs = tuple(conv_cfgs)
|
|
205
|
+
for key, value in self.__dict__.items():
|
|
206
|
+
if isinstance(value, DictConfig | ListConfig | list | tuple | dict):
|
|
207
|
+
setattr(self, key, convert_dictconfig_to_dict(value))
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
@dataclass
|
|
211
|
+
class ResNetConfig:
|
|
212
|
+
"""
|
|
213
|
+
Residual neural network layers configuration.
|
|
214
|
+
|
|
215
|
+
Attributes
|
|
216
|
+
----------
|
|
217
|
+
conv_channel : int
|
|
218
|
+
Number of channels for convolutional layer.
|
|
219
|
+
In ResNet, common number of channels is used for all layers.
|
|
220
|
+
conv_kernel : int
|
|
221
|
+
Kernel size for convolutional layer.
|
|
222
|
+
In ResNet, common kernel size is used for all layers.
|
|
223
|
+
f_kernel : int
|
|
224
|
+
Kernel size for final or first convolutional layer.
|
|
225
|
+
This depends on whether PixelShuffle or PixelUnshuffle is used.
|
|
226
|
+
conv_activation : str
|
|
227
|
+
Activation function for convolutional layer.
|
|
228
|
+
out_activation : str
|
|
229
|
+
Activation function for output layer.
|
|
230
|
+
n_res_blocks : int
|
|
231
|
+
Number of residual blocks.
|
|
232
|
+
scale_factor : int
|
|
233
|
+
Scale factor for upsample, especially for PixelShuffle or PixelUnshuffle.
|
|
234
|
+
n_scaling : int
|
|
235
|
+
Number of upsample or downsample layers. The image size is scaled by scalefactor^n_scaling.
|
|
236
|
+
norm : Literal["batch", "group", "none"]
|
|
237
|
+
Normalization layer. If it's set to "none", normalization is not applied. Default is "none".
|
|
238
|
+
norm_cfg : dict
|
|
239
|
+
Normalization layer configuration. If you want to use Instance, Layer, or Group normalization,
|
|
240
|
+
set norm to "group" and set norm_cfg with "num_groups=$in_channel, 1, or any value". Default is {}.
|
|
241
|
+
dropout : float
|
|
242
|
+
Dropout rate. If it's set to 0.0, dropout is not applied. Default is 0.0.
|
|
243
|
+
init_channel : int
|
|
244
|
+
Initial number of channels, especially for decoder.
|
|
245
|
+
"""
|
|
246
|
+
|
|
247
|
+
conv_channel: int
|
|
248
|
+
conv_kernel: int
|
|
249
|
+
f_kernel: int
|
|
250
|
+
conv_activation: str
|
|
251
|
+
out_activation: str
|
|
252
|
+
n_res_blocks: int
|
|
253
|
+
scale_factor: int = 2
|
|
254
|
+
n_scaling: int = 2
|
|
255
|
+
norm: Literal["batch", "group", "none"] = "none"
|
|
256
|
+
norm_cfg: dict[str, Any] = field(default_factory=dict)
|
|
257
|
+
dropout: float = 0.0
|
|
258
|
+
init_channel: int = 16
|
|
259
|
+
padding_mode: Literal["zeros", "reflect", "replicate", "circular"] = "zeros"
|
|
260
|
+
attention: AttentionConfig | None = None
|
|
261
|
+
|
|
262
|
+
def __post_init__(self) -> None:
|
|
263
|
+
"""Set `norm_cfg`."""
|
|
264
|
+
if self.norm == "none":
|
|
265
|
+
self.norm_cfg = {}
|
|
266
|
+
else:
|
|
267
|
+
self.norm_cfg = dict(self.norm_cfg)
|
|
268
|
+
|
|
269
|
+
def dictcfg2dict(self) -> None:
|
|
270
|
+
"""Convert dictConfig to dict for `ResNetConfig`."""
|
|
271
|
+
self.norm_cfg = dict(self.norm_cfg)
|
|
272
|
+
for key, value in self.__dict__.items():
|
|
273
|
+
if isinstance(value, DictConfig | ListConfig | list | tuple | dict):
|
|
274
|
+
setattr(self, key, convert_dictconfig_to_dict(value))
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
@dataclass
|
|
278
|
+
class UNetConfig:
|
|
279
|
+
"""
|
|
280
|
+
UNet configuration.
|
|
281
|
+
|
|
282
|
+
Attributes
|
|
283
|
+
----------
|
|
284
|
+
channels : Tuple[int, ...]
|
|
285
|
+
Number of channels for each layer.
|
|
286
|
+
conv_cfg : ConvConfig
|
|
287
|
+
Convolutional layer configuration.
|
|
288
|
+
cond_cfg : MLPConfig
|
|
289
|
+
Conditional configuration for UNet.
|
|
290
|
+
cond_pred_scale : bool
|
|
291
|
+
Whether to scale the conditional prediction. Default is False.
|
|
292
|
+
nhead : Optional[int]
|
|
293
|
+
Number of heads for attention mechanism. If it's set to None, attention is not applied.
|
|
294
|
+
Default is None.
|
|
295
|
+
has_attn : bool
|
|
296
|
+
Whether to use attention mechanism. Default is False.
|
|
297
|
+
use_shuffle : bool
|
|
298
|
+
Whether to use PixelShuffle or PixelUnshuffle. Default is False.
|
|
299
|
+
use_hypernet : bool
|
|
300
|
+
Whether to use hypernetwork. Default is False.
|
|
301
|
+
hyper_mlp_cfg : Optional[MLPConfig]
|
|
302
|
+
Hypernetwork configuration. If it's set to None, hypernetwork is not used.
|
|
303
|
+
Default is None.
|
|
304
|
+
"""
|
|
305
|
+
|
|
306
|
+
channels: tuple[int, ...]
|
|
307
|
+
conv_cfg: ConvConfig
|
|
308
|
+
cond_pred_scale: bool = False
|
|
309
|
+
nhead: int | None = None
|
|
310
|
+
has_attn: bool = False
|
|
311
|
+
use_shuffle: bool = False
|
|
312
|
+
use_hypernet: bool = False
|
|
313
|
+
hyper_mlp_cfg: MLPConfig | None = None
|
|
314
|
+
|
|
315
|
+
def __post_init__(self) -> None:
|
|
316
|
+
"""Post-initialization processing."""
|
|
317
|
+
if self.has_attn:
|
|
318
|
+
assert self.nhead is not None, "nhead must be specified when has_attn is True."
|
|
319
|
+
if isinstance(self.channels, list | ListConfig):
|
|
320
|
+
self.channels = tuple(self.channels)
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
@dataclass
|
|
324
|
+
class LinearConfig:
|
|
325
|
+
"""
|
|
326
|
+
A linear layer configuration.
|
|
327
|
+
|
|
328
|
+
Attributes
|
|
329
|
+
----------
|
|
330
|
+
activation : str
|
|
331
|
+
Activation function.
|
|
332
|
+
norm : Literal["layer", "rms", "none"]
|
|
333
|
+
Normalization layer. If it's set to "none", normalization is not applied. Default is "none".
|
|
334
|
+
norm_cfg : dict
|
|
335
|
+
Normalization layer configuration. Default is {}.
|
|
336
|
+
dropout : float
|
|
337
|
+
Dropout rate. If it's set to 0.0, dropout is not applied. Default is 0.0.
|
|
338
|
+
norm_first : bool
|
|
339
|
+
Whether to apply normalization before linear layer. Default is False.
|
|
340
|
+
bias : bool
|
|
341
|
+
Whether to use bias. Default is True.
|
|
342
|
+
"""
|
|
343
|
+
|
|
344
|
+
activation: str
|
|
345
|
+
norm: Literal["layer", "rms", "none"] = "none"
|
|
346
|
+
norm_cfg: dict[str, Any] = field(default_factory=dict)
|
|
347
|
+
dropout: float = 0.0
|
|
348
|
+
norm_first: bool = False
|
|
349
|
+
bias: bool = True
|
|
350
|
+
|
|
351
|
+
def __post_init__(self) -> None:
|
|
352
|
+
"""Set `norm_cfg`."""
|
|
353
|
+
if self.norm == "none":
|
|
354
|
+
self.norm_cfg = {}
|
|
355
|
+
else:
|
|
356
|
+
self.norm_cfg = dict(self.norm_cfg)
|
|
357
|
+
|
|
358
|
+
def dictcfg2dict(self) -> None:
|
|
359
|
+
"""Convert dictConfig to dict for `LinearConfig`."""
|
|
360
|
+
self.norm_cfg = dict(self.norm_cfg)
|
|
361
|
+
for key, value in self.__dict__.items():
|
|
362
|
+
if isinstance(value, DictConfig | ListConfig | list | tuple | dict):
|
|
363
|
+
setattr(self, key, convert_dictconfig_to_dict(value))
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
@dataclass
|
|
367
|
+
class MLPConfig:
|
|
368
|
+
"""
|
|
369
|
+
Multi-layer perceptron configuration.
|
|
370
|
+
|
|
371
|
+
Attributes
|
|
372
|
+
----------
|
|
373
|
+
hidden_dim : int
|
|
374
|
+
Number of hidden units.
|
|
375
|
+
n_layers : int
|
|
376
|
+
Number of layers.
|
|
377
|
+
output_activation : str
|
|
378
|
+
Activation function for output layer.
|
|
379
|
+
linear_cfg : LinearConfig
|
|
380
|
+
Linear layer configuration.
|
|
381
|
+
|
|
382
|
+
"""
|
|
383
|
+
|
|
384
|
+
hidden_dim: int
|
|
385
|
+
n_layers: int
|
|
386
|
+
output_activation: str
|
|
387
|
+
linear_cfg: LinearConfig
|
|
388
|
+
|
|
389
|
+
def dictcfg2dict(self) -> None:
|
|
390
|
+
"""Convert dictConfig to dict for `MLPConfig`."""
|
|
391
|
+
self.linear_cfg.dictcfg2dict()
|
|
392
|
+
for key, value in self.__dict__.items():
|
|
393
|
+
if isinstance(value, DictConfig | ListConfig | list | tuple | dict):
|
|
394
|
+
setattr(self, key, convert_dictconfig_to_dict(value))
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
@dataclass
|
|
398
|
+
class TransformerConfig:
|
|
399
|
+
"""
|
|
400
|
+
Transformer configuration.
|
|
401
|
+
|
|
402
|
+
Attributes
|
|
403
|
+
----------
|
|
404
|
+
d_model : int
|
|
405
|
+
Dimension of model.
|
|
406
|
+
nhead : int
|
|
407
|
+
Number of heads.
|
|
408
|
+
dim_ff : int
|
|
409
|
+
Dimension of feedforward network.
|
|
410
|
+
n_layers : int
|
|
411
|
+
Number of layers.
|
|
412
|
+
dropout : float
|
|
413
|
+
Dropout rate. Default is 0.1.
|
|
414
|
+
hidden_activation : Literal["ReLU", "GELU"]
|
|
415
|
+
Activation function for hidden layer. Default is "GELU".
|
|
416
|
+
output_activation : str
|
|
417
|
+
Activation function for output layer. Default is "GeLU".
|
|
418
|
+
|
|
419
|
+
"""
|
|
420
|
+
|
|
421
|
+
d_model: int
|
|
422
|
+
nhead: int
|
|
423
|
+
dim_ff: int
|
|
424
|
+
n_layers: int
|
|
425
|
+
dropout: float = 0.1
|
|
426
|
+
hidden_activation: Literal["ReLU", "GELU"] = "GELU"
|
|
427
|
+
output_activation: str = "GeLU"
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
@dataclass
|
|
431
|
+
class ViTConfig:
|
|
432
|
+
"""
|
|
433
|
+
Vision Transformer configuration.
|
|
434
|
+
|
|
435
|
+
Attributes
|
|
436
|
+
----------
|
|
437
|
+
patch_size : int
|
|
438
|
+
Patch size.
|
|
439
|
+
transformer_cfg : TransformerConfig
|
|
440
|
+
Transformer configuration.
|
|
441
|
+
cls_token : bool
|
|
442
|
+
Whether to use class token. Default is True.
|
|
443
|
+
init_channel : int
|
|
444
|
+
Initial number of channels. Default is 16.
|
|
445
|
+
"""
|
|
446
|
+
|
|
447
|
+
patch_size: int
|
|
448
|
+
transformer_cfg: TransformerConfig
|
|
449
|
+
cls_token: bool = True
|
|
450
|
+
init_channel: int = 16
|
|
451
|
+
unpatchify: bool = False
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
@dataclass
|
|
455
|
+
class AdaptiveAveragePoolingConfig:
|
|
456
|
+
"""
|
|
457
|
+
Adaptive average pooling configuration.
|
|
458
|
+
|
|
459
|
+
Attributes
|
|
460
|
+
----------
|
|
461
|
+
output_size : Union[int, Tuple[int, ...]]
|
|
462
|
+
Output size of the pooling layer. If it's an integer, it will be used for both height and width.
|
|
463
|
+
If it's a tuple, it should contain two integers for height and width.
|
|
464
|
+
"""
|
|
465
|
+
|
|
466
|
+
output_size: int | tuple[int, ...] = (1, 1)
|
|
467
|
+
additional_layer: (MLPConfig | LinearConfig) | None = None
|
|
468
|
+
|
|
469
|
+
def __post_init__(self) -> None:
|
|
470
|
+
"""Ensure output_size is a tuple."""
|
|
471
|
+
if isinstance(self.output_size, int):
|
|
472
|
+
self.output_size = (self.output_size, self.output_size)
|
|
473
|
+
elif isinstance(self.output_size, list | ListConfig):
|
|
474
|
+
self.output_size = tuple(self.output_size)
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
@dataclass
|
|
478
|
+
class SpatialSoftmaxConfig:
|
|
479
|
+
"""
|
|
480
|
+
Spatial softmax configuration.
|
|
481
|
+
|
|
482
|
+
Attributes
|
|
483
|
+
----------
|
|
484
|
+
temperature : float
|
|
485
|
+
Softmax temperature. If it's set to 0.0, the layer outputs the coordinates of the maximum value.
|
|
486
|
+
Otherwise, the layer outputs the expectation of the coordinates with softmax function.
|
|
487
|
+
Default is 0.0.
|
|
488
|
+
eps : float
|
|
489
|
+
Epsilon value for numerical stability in softmax. Default is 1e-6.
|
|
490
|
+
is_argmax : bool
|
|
491
|
+
Whether to use argmax instead of softmax. Default is False.
|
|
492
|
+
is_straight_through : bool
|
|
493
|
+
Whether to use straight-through estimator for backpropagation. Default is False.
|
|
494
|
+
additional_layer : Optional[Union[MLPConfig, LinearConfig]]
|
|
495
|
+
Additional layer configuration. If it's set to None, no additional layer is applied.
|
|
496
|
+
Default is None.
|
|
497
|
+
"""
|
|
498
|
+
|
|
499
|
+
temperature: float = 1.0
|
|
500
|
+
eps: float = 1e-6
|
|
501
|
+
is_argmax: bool = False
|
|
502
|
+
is_straight_through: bool = False
|
|
503
|
+
additional_layer: (MLPConfig | LinearConfig) | None = None
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
@dataclass
|
|
507
|
+
class EncoderConfig:
|
|
508
|
+
"""
|
|
509
|
+
Encoder configuration.
|
|
510
|
+
|
|
511
|
+
Attributes
|
|
512
|
+
----------
|
|
513
|
+
backbone: Union[ConvNetConfig, ResNetConfig]
|
|
514
|
+
Backbone configuration.
|
|
515
|
+
full_connection: Union[MLPConfig, LinearConfig, SpatialSoftmaxConfig]
|
|
516
|
+
Full connection configuration.
|
|
517
|
+
"""
|
|
518
|
+
|
|
519
|
+
backbone: ConvNetConfig | ResNetConfig
|
|
520
|
+
full_connection: MLPConfig | LinearConfig | SpatialSoftmaxConfig
|
|
521
|
+
|
|
522
|
+
def dictcfg2dict(self) -> None:
|
|
523
|
+
"""Convert dictConfig to dict for `EncoderConfig`."""
|
|
524
|
+
if hasattr(self.backbone, "dictcfg2dict"):
|
|
525
|
+
self.backbone.dictcfg2dict()
|
|
526
|
+
if hasattr(self.full_connection, "dictcfg2dict"):
|
|
527
|
+
self.full_connection.dictcfg2dict()
|
|
528
|
+
for key, value in self.__dict__.items():
|
|
529
|
+
if isinstance(value, DictConfig | ListConfig | list | tuple | dict):
|
|
530
|
+
setattr(self, key, convert_dictconfig_to_dict(value))
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
@dataclass
|
|
534
|
+
class DecoderConfig:
|
|
535
|
+
"""
|
|
536
|
+
Decoder configuration.
|
|
537
|
+
|
|
538
|
+
Attributes
|
|
539
|
+
----------
|
|
540
|
+
backbone: Union[ConvNetConfig, ResNetConfig]
|
|
541
|
+
Backbone configuration.
|
|
542
|
+
full_connection: Union[MLPConfig, LinearConfig, SpatialSoftmaxConfig]
|
|
543
|
+
Full connection configuration.
|
|
544
|
+
"""
|
|
545
|
+
|
|
546
|
+
backbone: ConvNetConfig | ResNetConfig
|
|
547
|
+
full_connection: MLPConfig | LinearConfig
|
|
548
|
+
|
|
549
|
+
def dictcfg2dict(self) -> None:
|
|
550
|
+
"""Convert dictConfig to dict for `DecoderConfig`."""
|
|
551
|
+
self.backbone.dictcfg2dict()
|
|
552
|
+
if hasattr(self.full_connection, "dictcfg2dict"):
|
|
553
|
+
self.full_connection.dictcfg2dict()
|
|
554
|
+
for key, value in self.__dict__.items():
|
|
555
|
+
if isinstance(value, DictConfig | ListConfig | list | tuple | dict):
|
|
556
|
+
setattr(self, key, convert_dictconfig_to_dict(value))
|