-
Notifications
You must be signed in to change notification settings - Fork 11
/
modules.py
185 lines (150 loc) · 5.11 KB
/
modules.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import torch
from torch import nn
import timm
import config as CFG
class ImageEncoder(nn.Module):
"""
Encode images to a fixed size vector
"""
def __init__(
self, model_name=CFG.model_name, pretrained=CFG.pretrained, trainable=CFG.trainable
):
super().__init__()
self.model = timm.create_model(
model_name, pretrained, num_classes=0, global_pool="avg"
)
for p in self.model.parameters():
p.requires_grad = trainable
def forward(self, x):
return self.model(x)
class ImageEncoder_resnet50(nn.Module):
"""
Encode images to a fixed size vector
"""
def __init__(
self, model_name='resnet50', pretrained=CFG.pretrained, trainable=CFG.trainable
):
super().__init__()
self.model = timm.create_model(
model_name, pretrained, num_classes=0, global_pool="avg"
)
for p in self.model.parameters():
p.requires_grad = trainable
def forward(self, x):
return self.model(x)
class ImageEncoder_resnet101(nn.Module):
"""
Encode images to a fixed size vector
"""
def __init__(
self, model_name='resnet101', pretrained=True, trainable=CFG.trainable
):
super().__init__()
self.model = timm.create_model(
model_name, pretrained, num_classes=0, global_pool="avg"
)
for p in self.model.parameters():
p.requires_grad = trainable
def forward(self, x):
return self.model(x)
class ImageEncoder_resnet152(nn.Module):
"""
Encode images to a fixed size vector
"""
def __init__(
self, model_name='resnet152', pretrained=True, trainable=CFG.trainable
):
super().__init__()
self.model = timm.create_model(
model_name, pretrained, num_classes=0, global_pool="avg"
)
for p in self.model.parameters():
p.requires_grad = trainable
def forward(self, x):
return self.model(x)
class ImageEncoder_ViT(nn.Module):
"""
Encode images to a fixed size vector
"""
def __init__(
self, model_name="vit_base_patch32_224", pretrained=False, trainable=CFG.trainable
):
super().__init__()
self.model = timm.create_model(
model_name, pretrained, num_classes=0, global_pool="avg"
)
for p in self.model.parameters():
p.requires_grad = trainable
def forward(self, x):
return self.model(x)
class ImageEncoder_CLIP(nn.Module):
"""
Encode images to a fixed size vector
"""
def __init__(
self, model_name="vit_base_patch32_224_clip_laion2b", pretrained=True, trainable=CFG.trainable
):
super().__init__()
self.model = timm.create_model(
model_name, pretrained, num_classes=0, global_pool="avg"
)
for p in self.model.parameters():
p.requires_grad = trainable
def forward(self, x):
return self.model(x)
class ImageEncoder_ViT_L(nn.Module):
"""
Encode images to a fixed size vector
"""
def __init__(
self, model_name="vit_large_patch32_224_in21k", pretrained=False, trainable=CFG.trainable
):
super().__init__()
self.model = timm.create_model(
model_name, pretrained, num_classes=0, global_pool="avg"
)
for p in self.model.parameters():
p.requires_grad = trainable
def forward(self, x):
return self.model(x)
# 'vit_base_patch32_224',
# 'vit_base_patch32_224_clip_laion2b',
# 'vit_base_patch32_224_in21k',
# 'vit_base_patch32_224_sam',
# class SpotEncoder(nn.Module):
# #to change...
# def __init__(self, model_name=CFG.spot_encoder_model, pretrained=CFG.pretrained, trainable=CFG.trainable):
# super().__init__()
# if pretrained:
# self.model = DistilBertModel.from_pretrained(model_name)
# else:
# self.model = DistilBertModel(config=DistilBertConfig())
# for p in self.model.parameters():
# p.requires_grad = trainable
# # we are using the CLS token hidden representation as the sentence's embedding
# self.target_token_idx = 0
# def forward(self, input_ids, attention_mask):
# output = self.model(input_ids=input_ids, attention_mask=attention_mask)
# last_hidden_state = output.last_hidden_state
# return last_hidden_state[:, self.target_token_idx, :]
class ProjectionHead(nn.Module):
def __init__(
self,
embedding_dim,
projection_dim=CFG.projection_dim,
dropout=CFG.dropout
):
super().__init__()
self.projection = nn.Linear(embedding_dim, projection_dim)
self.gelu = nn.GELU()
self.fc = nn.Linear(projection_dim, projection_dim)
self.dropout = nn.Dropout(dropout)
self.layer_norm = nn.LayerNorm(projection_dim)
def forward(self, x):
projected = self.projection(x)
x = self.gelu(projected)
x = self.fc(x)
x = self.dropout(x)
x = x + projected
x = self.layer_norm(x)
return x