-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.py
50 lines (39 loc) · 1.21 KB
/
encoder.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
import torch.nn as nn
from typing import List
# simple conv encoder class that extracts the features of a picture
class conv_block(nn.Module):
def __init__(self, act_ftn, in_channels, out_channels) -> None:
super().__init__()
self.convlayer = nn.Sequential(
nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=3,
padding="same"
),
# nn.BatchNorm2d(num_features=out_channels),
act_ftn,
nn.MaxPool2d(2, 2, ceil_mode=True)
)
def forward(self, x):
return self.convlayer(x)
class simpleConvEncoder2d(nn.Module):
def __init__(
self,
act_ftn: nn.Module,
nChannels: List,
):
super().__init__()
encoder = []
for c_in, c_out in zip(nChannels[:-1], nChannels[1:]):
encoder.append(
conv_block(
act_ftn=act_ftn,
in_channels=c_in,
out_channels=c_out,
)
)
self.encoder = nn.Sequential(*encoder)
def forward(self, x):
x = self.encoder(x)
return x