-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathembedder.py
161 lines (140 loc) Β· 4.93 KB
/
embedder.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
import torch
from loguru import logger
from torch import nn
from torch.nn.functional import relu
def get_conv_bn(
in_channels: int,
out_channels: int,
kernel_size: int,
padding: int | str = 0,
stride: int = 1,
dilation: int = 1,
):
return nn.Sequential(
nn.Conv2d(
in_channels,
out_channels,
padding=padding,
kernel_size=kernel_size,
stride=stride,
dilation=dilation,
),
nn.BatchNorm2d(out_channels),
)
def get_conv_bn_relu(
in_channels: int,
out_channels: int,
kernel_size: int,
padding: int | str = 0,
stride: int = 1,
dilation: int = 1,
):
return nn.Sequential(
get_conv_bn(in_channels, out_channels, kernel_size, padding, stride, dilation),
nn.ReLU(),
)
class ResBlock(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int,
padding: int | str,
stride: int,
dilation: int,
):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.blocks = nn.Sequential(
get_conv_bn_relu(in_channels, out_channels, kernel_size, padding, stride, dilation),
get_conv_bn(out_channels, out_channels, kernel_size, padding, stride, dilation),
)
self.shortcut = (
nn.Identity()
if in_channels == out_channels
else nn.Sequential(
nn.Conv2d(self.in_channels, self.out_channels, kernel_size=1, bias=False),
nn.BatchNorm2d(self.out_channels),
)
)
def forward(self, x):
residual = self.shortcut(x)
x = self.blocks(x)
x += residual
x = relu(x)
return x
def get_res_block_with_pooling(
in_channels: int,
out_channels: int,
conv_kernel_size: int,
pool_kernel_size: int,
padding: int | str = 0,
stride: int = 1,
dilation: int = 1,
):
return nn.Sequential(
ResBlock(in_channels, out_channels, conv_kernel_size, padding, stride, dilation),
nn.MaxPool2d(pool_kernel_size),
)
class VisualEmbedder(nn.Module):
def __init__(
self,
height: int,
width: int,
conv_kernel_size: int = 3,
pool_kernel_size: int = 2,
emb_size: int = 768,
channels: tuple = (1, 64, 128, 256),
):
super().__init__()
logger.info(
f"Initializing VisualEmbedder | number of layers: {len(channels)-1}, emb size: {emb_size}, "
f"convolutional kernel size: {conv_kernel_size}, pooling kernel size: {pool_kernel_size}"
)
layers = [
get_res_block_with_pooling(channels[i], channels[i + 1], conv_kernel_size, pool_kernel_size, padding="same")
for i in range(len(channels) - 1)
]
self.slice_conv = nn.Sequential(*layers)
self.linear_bridge = nn.Linear(
(height // (pool_kernel_size ** (len(channels) - 1)))
* (width // (pool_kernel_size ** (len(channels) - 1)))
* channels[-1],
emb_size,
)
def forward(self, slices):
batch_size, slice_count, height, width = slices.shape
conv = self.slice_conv(slices.view(batch_size * slice_count, 1, height, width))
_, channels_count, h_out, w_out = conv.shape
batched_conv = conv.view(batch_size, slice_count, channels_count * h_out * w_out)
return self.linear_bridge(batched_conv), conv # [batch size, slice count, emb size],
# [batch size * slice count, out channels, emb height, emb width]
class VisualEmbedderSL(VisualEmbedder):
def __init__(
self,
height: int,
width: int,
kernel_size: int = 3,
emb_size: int = 768,
channels: tuple = (1, 64, 128, 256),
):
logger.info(f"Initializing VisualEmbedderSL")
super().__init__(height=height, width=width, conv_kernel_size=kernel_size, emb_size=emb_size, channels=channels)
def forward(self, batch: dict[str, torch.Tensor]):
slices = batch["slices"]
slice_embeddings = super().__call__(slices) # [batch size, slice count, emb size]
batch_size, slice_count, emb_size = slice_embeddings.shape
masked_slice_embeddings = slice_embeddings * batch["tokens_mask"][:, :, None]
max_word_len = int(batch["max_word_len"].item())
masked_slice_embeddings_splitted_into_words = masked_slice_embeddings.view(
batch_size, slice_count // max_word_len, max_word_len, emb_size
)
tokens_count_in_each_word = (
batch["tokens_mask"].view(batch_size, slice_count // max_word_len, max_word_len).sum(dim=2)
)
word_embeddings = (
torch.mean(masked_slice_embeddings_splitted_into_words, 2)
/ torch.max(torch.tensor(1), tokens_count_in_each_word)[:, :, None]
)
return word_embeddings