torch-utils 1.0.0__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.
- torch_utils/__init__.py +4 -0
- torch_utils/_layer.py +145 -0
- torch_utils/_residual.py +55 -0
- torch_utils/batch/__init__.py +6 -0
- torch_utils/batch/batch_generator.py +506 -0
- torch_utils/batch/ipc_batch_generator.py +24 -0
- torch_utils/batch/sequence_batch_generator.py +105 -0
- torch_utils/dataset/mnist.py +59 -0
- torch_utils/layer.py +2 -0
- torch_utils/ssd/_box.py +76 -0
- torch_utils/ssd/loss.py +112 -0
- torch_utils/ssd/ssd.py +169 -0
- torch_utils/trainer.py +322 -0
- torch_utils/transformer/vision_transformer.py +199 -0
- torch_utils/utils/logger.py +102 -0
- torch_utils/utils/memory.py +28 -0
- torch_utils/utils/train.py +55 -0
- torch_utils-1.0.0.dist-info/METADATA +302 -0
- torch_utils-1.0.0.dist-info/RECORD +21 -0
- torch_utils-1.0.0.dist-info/WHEEL +4 -0
- torch_utils-1.0.0.dist-info/licenses/LICENSE +30 -0
torch_utils/__init__.py
ADDED
torch_utils/_layer.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import ClassVar
|
|
4
|
+
|
|
5
|
+
import torch
|
|
6
|
+
from torch import nn
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
__all__ = ['Conv1d', 'Conv2d', 'Conv3d', 'Deconv2d', 'Layer', 'Linear']
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Layer(nn.Module):
|
|
13
|
+
class Inherit:
|
|
14
|
+
__slots__ = ()
|
|
15
|
+
|
|
16
|
+
# Default layer arguments
|
|
17
|
+
ACTIVATION: type[nn.Module] | None = nn.LeakyReLU
|
|
18
|
+
ACTIVATION_KWARGS: ClassVar[dict] = {'negative_slope': 0.1}
|
|
19
|
+
|
|
20
|
+
USE_BATCH_NORM = True
|
|
21
|
+
BATCH_NORM_TRAINING = True
|
|
22
|
+
BATCH_NORM_MOMENTUM = 0.01
|
|
23
|
+
|
|
24
|
+
def __init__(self, activation: type[nn.Module] | nn.Module | type[Layer.Inherit] | None):
|
|
25
|
+
super().__init__()
|
|
26
|
+
# Preload default
|
|
27
|
+
if activation == Layer.Inherit:
|
|
28
|
+
activation = Layer.ACTIVATION
|
|
29
|
+
self.activation: nn.Module | None = (
|
|
30
|
+
activation(**Layer.ACTIVATION_KWARGS) if isinstance(activation, type) else activation) # ty: ignore[invalid-assignment]
|
|
31
|
+
self.batch_norm: nn.modules.batchnorm._BatchNorm | None = None
|
|
32
|
+
|
|
33
|
+
def forward(self, input_data: torch.Tensor) -> torch.Tensor:
|
|
34
|
+
output = input_data
|
|
35
|
+
if self.activation:
|
|
36
|
+
output = self.activation(output)
|
|
37
|
+
if self.batch_norm:
|
|
38
|
+
output = self.batch_norm(output)
|
|
39
|
+
return output
|
|
40
|
+
|
|
41
|
+
@staticmethod
|
|
42
|
+
def add_weight_decay(module: nn.Module, weight_decay: float, exclude=()):
|
|
43
|
+
decay = []
|
|
44
|
+
no_decay = []
|
|
45
|
+
for name, param in module.named_parameters():
|
|
46
|
+
if not param.requires_grad:
|
|
47
|
+
continue
|
|
48
|
+
if len(param.shape) == 1 or name.endswith('.bias') or name in exclude:
|
|
49
|
+
no_decay.append(param)
|
|
50
|
+
else:
|
|
51
|
+
decay.append(param)
|
|
52
|
+
return [
|
|
53
|
+
{'params': no_decay, 'weight_decay': 0.0},
|
|
54
|
+
{'params': decay, 'weight_decay': weight_decay}]
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class Linear(Layer):
|
|
58
|
+
def __init__(self, in_channels: int, out_channels: int,
|
|
59
|
+
activation: nn.Module | type[Layer.Inherit] | None = Layer.Inherit,
|
|
60
|
+
use_batch_norm: bool | None = None, **kwargs):
|
|
61
|
+
super().__init__(activation)
|
|
62
|
+
|
|
63
|
+
use_batch_norm = Layer.USE_BATCH_NORM if use_batch_norm is None else use_batch_norm
|
|
64
|
+
self.linear = nn.Linear(in_channels, out_channels, bias=not use_batch_norm, **kwargs)
|
|
65
|
+
self.batch_norm = nn.BatchNorm1d(
|
|
66
|
+
out_channels,
|
|
67
|
+
momentum=Layer.BATCH_NORM_MOMENTUM,
|
|
68
|
+
track_running_stats=Layer.BATCH_NORM_TRAINING) if use_batch_norm else None
|
|
69
|
+
|
|
70
|
+
def forward(self, input_data: torch.Tensor) -> torch.Tensor:
|
|
71
|
+
return super().forward(self.linear(input_data))
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class Conv1d(Layer):
|
|
75
|
+
def __init__(self, in_channels: int, out_channels: int, kernel_size: int = 3,
|
|
76
|
+
stride: int | tuple[int] = 1, activation: nn.Module | type[Layer.Inherit] | None = Layer.Inherit,
|
|
77
|
+
use_batch_norm: bool | None = None, **kwargs):
|
|
78
|
+
super().__init__(activation)
|
|
79
|
+
|
|
80
|
+
use_batch_norm = Layer.USE_BATCH_NORM if use_batch_norm is None else use_batch_norm
|
|
81
|
+
self.conv = nn.Conv1d(in_channels, out_channels, kernel_size, stride=stride,
|
|
82
|
+
bias=use_batch_norm, **kwargs)
|
|
83
|
+
self.batch_norm = nn.BatchNorm1d(
|
|
84
|
+
out_channels,
|
|
85
|
+
momentum=Layer.BATCH_NORM_MOMENTUM,
|
|
86
|
+
track_running_stats=Layer.BATCH_NORM_TRAINING) if use_batch_norm else None
|
|
87
|
+
|
|
88
|
+
def forward(self, input_data: torch.Tensor) -> torch.Tensor:
|
|
89
|
+
return super().forward(self.conv(input_data))
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class Conv2d(Layer):
|
|
93
|
+
def __init__(self, in_channels: int, out_channels: int, kernel_size: int | tuple[int, int] = 3,
|
|
94
|
+
stride: int | tuple[int, int] = 1, activation: nn.Module | type[Layer.Inherit] | None = Layer.Inherit,
|
|
95
|
+
use_batch_norm: bool | None = None, **kwargs):
|
|
96
|
+
super().__init__(activation)
|
|
97
|
+
|
|
98
|
+
use_batch_norm = Layer.USE_BATCH_NORM if use_batch_norm is None else use_batch_norm
|
|
99
|
+
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride,
|
|
100
|
+
bias=not use_batch_norm, **kwargs)
|
|
101
|
+
self.batch_norm = nn.BatchNorm2d(
|
|
102
|
+
out_channels,
|
|
103
|
+
momentum=Layer.BATCH_NORM_MOMENTUM,
|
|
104
|
+
track_running_stats=Layer.BATCH_NORM_TRAINING) if use_batch_norm else None
|
|
105
|
+
|
|
106
|
+
def forward(self, input_data: torch.Tensor) -> torch.Tensor:
|
|
107
|
+
return super().forward(self.conv(input_data))
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class Conv3d(Layer):
|
|
111
|
+
def __init__(self, in_channels: int, out_channels: int, kernel_size: int | tuple[int, int, int] = 3,
|
|
112
|
+
stride: int | tuple[int, int, int] = 1,
|
|
113
|
+
activation: nn.Module | type[Layer.Inherit] | None = Layer.Inherit, use_batch_norm: bool | None = None,
|
|
114
|
+
**kwargs):
|
|
115
|
+
super().__init__(activation)
|
|
116
|
+
|
|
117
|
+
use_batch_norm = Layer.USE_BATCH_NORM if use_batch_norm is None else use_batch_norm
|
|
118
|
+
self.conv = nn.Conv3d(in_channels, out_channels, kernel_size, stride=stride,
|
|
119
|
+
bias=use_batch_norm, **kwargs)
|
|
120
|
+
self.batch_norm = nn.BatchNorm3d(
|
|
121
|
+
out_channels,
|
|
122
|
+
momentum=Layer.BATCH_NORM_MOMENTUM,
|
|
123
|
+
track_running_stats=Layer.BATCH_NORM_TRAINING) if use_batch_norm else None
|
|
124
|
+
|
|
125
|
+
def forward(self, input_data: torch.Tensor) -> torch.Tensor:
|
|
126
|
+
return super().forward(self.conv(input_data))
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class Deconv2d(Layer):
|
|
130
|
+
def __init__(self, in_channels: int, out_channels: int, kernel_size: int = 3,
|
|
131
|
+
stride: int | tuple[int, int] = 1, activation: nn.Module | type[Layer.Inherit] | None = Layer.Inherit,
|
|
132
|
+
use_batch_norm: bool | None = None, **kwargs):
|
|
133
|
+
super().__init__(activation)
|
|
134
|
+
|
|
135
|
+
use_batch_norm = Layer.USE_BATCH_NORM if use_batch_norm is None else use_batch_norm
|
|
136
|
+
self.deconv = nn.ConvTranspose2d(
|
|
137
|
+
in_channels, out_channels, kernel_size, stride=stride,
|
|
138
|
+
bias=not use_batch_norm, **kwargs)
|
|
139
|
+
self.batch_norm = nn.BatchNorm2d(
|
|
140
|
+
out_channels,
|
|
141
|
+
momentum=Layer.BATCH_NORM_MOMENTUM,
|
|
142
|
+
track_running_stats=Layer.BATCH_NORM_TRAINING) if use_batch_norm else None
|
|
143
|
+
|
|
144
|
+
def forward(self, input_data: torch.Tensor) -> torch.Tensor:
|
|
145
|
+
return super().forward(self.deconv(input_data))
|
torch_utils/_residual.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from torch import nn
|
|
3
|
+
|
|
4
|
+
from ._layer import Conv2d, Layer
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
__all__ = ['ResBlock', 'ResBottleneck']
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ResBlock(Layer):
|
|
11
|
+
def __init__(self, in_channels: int, out_channels: int = -1, kernel_size: int = 3, padding: int = 1,
|
|
12
|
+
stride: int | tuple[int, int] = 1, activation: nn.Module | type[Layer.Inherit] | None = Layer.Inherit,
|
|
13
|
+
batch_norm=None, **kwargs):
|
|
14
|
+
super().__init__(activation)
|
|
15
|
+
self.batch_norm = None
|
|
16
|
+
if out_channels == -1:
|
|
17
|
+
out_channels = in_channels
|
|
18
|
+
|
|
19
|
+
self.seq = nn.Sequential(
|
|
20
|
+
Conv2d(in_channels, in_channels, kernel_size=kernel_size, stride=stride, padding=padding, **kwargs),
|
|
21
|
+
Conv2d(in_channels, out_channels, kernel_size=3, padding=1,
|
|
22
|
+
activation=None, batch_norm=batch_norm))
|
|
23
|
+
self.residual = Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, activation=None) if (
|
|
24
|
+
out_channels != in_channels or stride != 1) else None
|
|
25
|
+
|
|
26
|
+
def forward(self, input_data: torch.Tensor) -> torch.Tensor:
|
|
27
|
+
if self.residual is not None:
|
|
28
|
+
return super().forward(self.residual(input_data) + self.seq(input_data))
|
|
29
|
+
return super().forward(input_data + self.seq(input_data))
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ResBottleneck(Layer):
|
|
33
|
+
def __init__(self, in_channels: int, out_channels: int = -1, bottleneck_channels: int = -1, kernel_size: int = 3,
|
|
34
|
+
stride: int | tuple[int, int] = 1, padding=1,
|
|
35
|
+
activation: nn.Module | type[Layer.Inherit] | None = Layer.Inherit, batch_norm=None, **kwargs):
|
|
36
|
+
super().__init__(activation)
|
|
37
|
+
self.batch_norm = None
|
|
38
|
+
if out_channels == -1:
|
|
39
|
+
out_channels = in_channels
|
|
40
|
+
if bottleneck_channels == -1:
|
|
41
|
+
bottleneck_channels = in_channels // 4
|
|
42
|
+
|
|
43
|
+
self.seq = nn.Sequential(
|
|
44
|
+
Conv2d(in_channels, bottleneck_channels, kernel_size=1),
|
|
45
|
+
Conv2d(bottleneck_channels, bottleneck_channels, kernel_size=kernel_size,
|
|
46
|
+
stride=stride, padding=padding, **kwargs),
|
|
47
|
+
Conv2d(bottleneck_channels, out_channels, kernel_size=1,
|
|
48
|
+
activation=None, batch_norm=batch_norm))
|
|
49
|
+
self.residual = Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, activation=None) if (
|
|
50
|
+
out_channels != in_channels or stride != 1) else None
|
|
51
|
+
|
|
52
|
+
def forward(self, input_data: torch.Tensor) -> torch.Tensor:
|
|
53
|
+
if self.residual is not None:
|
|
54
|
+
return super().forward(self.residual(input_data) + self.seq(input_data))
|
|
55
|
+
return super().forward(input_data + self.seq(input_data))
|