-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.py
147 lines (118 loc) · 5 KB
/
decoder.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import TransformerDecoderLayer, TransformerDecoder
class ResidualBlock(nn.Module):
"""Represents 1D version of the residual block: https://arxiv.org/abs/1512.03385"""
def __init__(self, input_dim):
"""Initializes the module."""
super(ResidualBlock, self).__init__()
self.block = nn.Sequential(
nn.Linear(input_dim, input_dim),
nn.LeakyReLU(),
nn.Linear(input_dim, input_dim),
)
def forward(self, x):
"""Performs forward pass of the module."""
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):
"""Attention is All You Need positional encoding layer"""
def __init__(self, seq_len, d_model, p_dropout):
"""Initializes the layer."""
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):
"""Decoder for image captions.
Generates prediction for next caption word given the prviously
generated word and image features extracted from CNN.
"""
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,
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."""
# 假设 image_features 是 [batch_size, 2048]
# 需要扩展维度为 [1, batch_size, 2048],增加 sequence_length 维度
image_features = image_features.unsqueeze(
0) # 变成 [1, batch_size, 2048]
# 现在可以进行 permute 操作,将其调整为 [batch_size, 1, 2048] 或其他所需形状
image_features = image_features.permute(
1, 0, 2) # 变成 [batch_size, 1, 2048]
# 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