-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconvblock.py
More file actions
50 lines (42 loc) · 1.28 KB
/
convblock.py
File metadata and controls
50 lines (42 loc) · 1.28 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
import torch.nn as nn
from torch import Tensor
from .activations import ACTIVATION_FROM_NAME
class ConvBlock(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
*,
filter_size: int,
final: bool = False,
activation: str = "ReLU",
) -> None:
super().__init__()
activation_layer = ACTIVATION_FROM_NAME[activation]
layers = [
nn.Conv2d(
in_channels, out_channels, kernel_size=filter_size, padding="same"
),
activation_layer(inplace=True),
nn.Conv2d(
out_channels, out_channels, kernel_size=filter_size, padding="same"
),
activation_layer(inplace=True),
]
if final:
layers += [
nn.Conv2d(
out_channels,
out_channels,
kernel_size=filter_size,
padding="same",
),
activation_layer(inplace=True),
]
else:
layers.append(
nn.BatchNorm2d(num_features=out_channels),
)
self.model = nn.Sequential(*layers)
def forward(self, x: Tensor) -> Tensor:
return self.model(x)