Spaces:
Sleeping
Sleeping
File size: 6,052 Bytes
0faaa54 |
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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
import re
import torch
import config
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
from collections import OrderedDict
class DenseNet121(nn.Module):
def __init__(self, out_size=14, checkpoint=None):
super(DenseNet121, self).__init__()
self.densenet121 = models.densenet121(weights='DEFAULT')
num_classes = self.densenet121.classifier.in_features
self.densenet121.classifier = nn.Sequential(
nn.Linear(num_classes, out_size),
nn.Sigmoid()
)
if checkpoint is not None:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
checkpoint = torch.load(checkpoint, map_location=device)
state_dict = checkpoint['state_dict']
new_state_dict = OrderedDict()
for k, v in state_dict.items():
if 'module' not in k:
k = f'module.{k}'
else:
k = k.replace('module.densenet121.features', 'features')
k = k.replace('module.densenet121.classifier', 'classifier')
k = k.replace('.norm.1', '.norm1')
k = k.replace('.conv.1', '.conv1')
k = k.replace('.norm.2', '.norm2')
k = k.replace('.conv.2', '.conv2')
new_state_dict[k] = v
self.densenet121.load_state_dict(new_state_dict)
def forward(self, x):
return self.densenet121(x)
class EncoderCNN(nn.Module):
def __init__(self, checkpoint=None):
super(EncoderCNN, self).__init__()
self.model = DenseNet121(
checkpoint=checkpoint
)
for param in self.model.densenet121.parameters():
param.requires_grad_(False)
def forward(self, images):
features = self.model.densenet121.features(images)
batch, maps, size_1, size_2 = features.size()
features = features.permute(0, 2, 3, 1)
features = features.view(batch, size_1 * size_2, maps)
return features
class Attention(nn.Module):
def __init__(self, features_size, hidden_size, output_size=1):
super(Attention, self).__init__()
self.W = nn.Linear(features_size, hidden_size)
self.U = nn.Linear(hidden_size, hidden_size)
self.v = nn.Linear(hidden_size, output_size)
def forward(self, features, decoder_output):
decoder_output = decoder_output.unsqueeze(1)
w = self.W(features)
u = self.U(decoder_output)
scores = self.v(torch.tanh(w + u))
weights = F.softmax(scores, dim=1)
context = torch.sum(weights * features, dim=1)
weights = weights.squeeze(2)
return context, weights
class DecoderRNN(nn.Module):
def __init__(self, features_size, embed_size, hidden_size, vocab_size):
super(DecoderRNN, self).__init__()
self.vocab_size = vocab_size
self.embedding = nn.Embedding(vocab_size, embed_size)
self.lstm = nn.LSTMCell(embed_size + features_size, hidden_size)
self.fc = nn.Linear(hidden_size, vocab_size)
self.attention = Attention(features_size, hidden_size)
self.init_h = nn.Linear(features_size, hidden_size)
self.init_c = nn.Linear(features_size, hidden_size)
def forward(self, features, captions):
embeddings = self.embedding(captions)
h, c = self.init_hidden(features)
seq_len = len(captions[0]) - 1
features_size = features.size(1)
batch_size = captions.size(0)
outputs = torch.zeros(batch_size, seq_len, self.vocab_size).to(config.DEVICE)
atten_weights = torch.zeros(batch_size, seq_len, features_size).to(config.DEVICE)
for i in range(seq_len):
context, attention = self.attention(features, h)
inputs = torch.cat((embeddings[:, i, :], context), dim=1)
h, c = self.lstm(inputs, (h, c))
h = F.dropout(h, p=0.5)
output = self.fc(h)
outputs[:, i, :] = output
atten_weights[:, i, :] = attention
return outputs, atten_weights
def init_hidden(self, features):
features = torch.mean(features, dim=1)
h = self.init_h(features)
c = self.init_c(features)
return h, c
class EncoderDecoderNet(nn.Module):
def __init__(self, features_size, embed_size, hidden_size, vocabulary, encoder_checkpoint=None):
super(EncoderDecoderNet, self).__init__()
self.vocabulary = vocabulary
self.encoder = EncoderCNN(
checkpoint=encoder_checkpoint
)
self.decoder = DecoderRNN(
features_size=features_size,
embed_size=embed_size,
hidden_size=hidden_size,
vocab_size=len(self.vocabulary)
)
def forward(self, images, captions):
features = self.encoder(images)
outputs, _ = self.decoder(features, captions)
return outputs
def generate_caption(self, image, max_length=25):
caption = []
with torch.no_grad():
features = self.encoder(image)
h, c = self.decoder.init_hidden(features)
word = torch.tensor(self.vocabulary.stoi['<SOS>']).view(1, -1).to(config.DEVICE)
embeddings = self.decoder.embedding(word).squeeze(0)
for _ in range(max_length):
context, _ = self.decoder.attention(features, h)
inputs = torch.cat((embeddings, context), dim=1)
h, c = self.decoder.lstm(inputs, (h, c))
output = self.decoder.fc(F.dropout(h, p=0.5))
output = output.view(1, -1)
predicted = output.argmax(1)
if self.vocabulary.itos[predicted.item()] == '<EOS>':
break
caption.append(predicted.item())
embeddings = self.decoder.embedding(predicted)
return [self.vocabulary.itos[idx] for idx in caption]
|