-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
165 lines (127 loc) · 5.46 KB
/
model.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torch.nn import TransformerDecoderLayer, TransformerDecoder
from torchvision.models import resnet50, ResNet50_Weights
class Encoder(nn.Module):
"""
Encoder.
"""
def __init__(self, encoded_image_size=8):
super(Encoder, self).__init__()
self.enc_image_size = encoded_image_size
resnet = torchvision.models.resnet50(weights = ResNet50_Weights.DEFAULT)
# Remove linear and pool layers (since we're not doing classification)
modules = list(resnet.children())[:-2]
self.resnet = nn.Sequential(*modules)
# Resize image to fixed size to allow input images of variable size
self.adaptive_pool = nn.AdaptiveAvgPool2d((encoded_image_size, encoded_image_size))
for p in self.resnet.parameters():
p.requires_grad = False
def forward(self, images):
"""
Forward propagation.
:param images: images, a tensor of dimensions (batch_size, 3, image_size, image_size)
:return: encoded images
"""
out = self.resnet(images) # (batch_size, 2048, image_size/32, image_size/32)
out = self.adaptive_pool(out) # (batch_size, 2048, encoded_image_size, encoded_image_size)
# out = out.permute(0, 2, 3, 1) # (batch_size, encoded_image_size, encoded_image_size, 2048)
return out
class ResidualBlock(nn.Module):
def __init__(self, input_dim):
super(ResidualBlock,self).__init__()
self.block = nn.Sequential(
nn.Linear(input_dim, input_dim),
nn.ReLU(),
nn.Linear(input_dim,input_dim)
)
def forward(self, x):
skip_connection = x
x = self.block(x)
x = skip_connection + x
return x
class Normalize(nn.Module):
def __init__(self, eps = 1e-5):
super(Normalize, self).__init__()
self.register_buffer("eps", torch.Tensor([eps]))
def forward(self, x, dim = -1):
norm = x.norm(2, dim = dim).unsqueeze(-1)
x = self.eps * (x / norm)
return x
class PositionalEncodings(nn.Module):
def __init__(self, seq_len, d_model, p_dropout):
super(PositionalEncodings, self).__init__()
token_positions = torch.arange(start=0, end=seq_len).view(-1, 1)
dim_positions = torch.arange(start=0, end=d_model).view(1, -1)
angles = token_positions / (10000 ** ((2 * dim_positions) / d_model))
encodings = torch.zeros(1, seq_len, d_model)
encodings[0, :, ::2] = torch.cos(angles[:, ::2])
encodings[0, :, 1::2] = torch.sin(angles[:, 1::2])
encodings.requires_grad = False
self.register_buffer("positional_encodings", encodings)
self.dropout = nn.Dropout(p_dropout)
def forward(self, x):
"""Performs forward pass of the module."""
x = x + self.positional_encodings
x = self.dropout(x)
return x
class CaptionDecoder(nn.Module):
def __init__(self, config):
"""Initializes the model."""
super(CaptionDecoder, self).__init__()
model_config = config["model_configuration"]
decoder_layers = model_config["decoder_layers"]
attention_heads = model_config["attention_heads"]
d_model = model_config["d_model"]
ff_dim = model_config["ff_dim"]
dropout = model_config["dropout"]
embedding_dim = config["embeddings"]["size"]
vocab_size = config["vocab_size"]
img_feature_channels = config["image_specs"]["img_feature_channels"]
# Load pretrained word embeddings
word_embeddings = torch.Tensor(np.loadtxt(config["embeddings"]["path"]))
self.embedding_layer = nn.Embedding.from_pretrained(
word_embeddings,
freeze=True,
padding_idx=config["PAD_idx"]
)
self.entry_mapping_words = nn.Linear(embedding_dim, d_model)
self.entry_mapping_img = nn.Linear(img_feature_channels, d_model)
self.res_block = ResidualBlock(d_model)
self.positional_encodings = PositionalEncodings(config["max_len"], d_model, dropout)
transformer_decoder_layer = TransformerDecoderLayer(
d_model=d_model,
nhead=attention_heads,
activation="relu",
dim_feedforward=ff_dim,
dropout=dropout
)
self.decoder = TransformerDecoder(transformer_decoder_layer, decoder_layers)
self.classifier = nn.Linear(d_model, vocab_size)
def forward(self, x, image_features, tgt_padding_mask=None, tgt_mask=None):
"""Performs forward pass of the module."""
# Adapt the dimensionality of the features for image patches
image_features = self.entry_mapping_img(image_features)
image_features = image_features.permute(1, 0, 2)
image_features = F.leaky_relu(image_features)
# Entry mapping for word tokens
x = self.embedding_layer(x)
x = self.entry_mapping_words(x)
x = F.leaky_relu(x)
x = self.res_block(x)
x = F.leaky_relu(x)
x = self.positional_encodings(x)
# Get output from the decoder
x = x.permute(1, 0, 2)
x = self.decoder(
tgt=x,
memory=image_features,
tgt_key_padding_mask=tgt_padding_mask,
tgt_mask=tgt_mask
)
x = x.permute(1, 0, 2)
x = self.classifier(x)
return x