-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
71 lines (53 loc) · 2.46 KB
/
model.py
File metadata and controls
71 lines (53 loc) · 2.46 KB
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
import torch
import torch.nn as nn
import torchvision.models as models
class EncoderCNN(nn.Module):
def __init__(self, embed_size, trainCNN = False):
super(EncoderCNN, self).__init__()
self.trainCNN = trainCNN
self.inception = models.inception_v3(pretrained=True, aux_logits = False)
self.inception.fc = nn.Linear(self.inception.fc.in_features, embed_size)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(0.5)
def forward(self, x):
x = self.inception(x)
for name, param in self.inception.named_parameters():
if "fc.weight" in name or "fc.bias" in name:
param.requires_grad = True
else:
param.requires_grad = self.trainCNN
return self.dropout(self.relu(x))
class DecoderRNN(nn.Module):
def __init__(self, embed_size, hidden_size, vocab_size, num_layers):
super(DecoderRNN, self).__init__()
self.embedding = nn.Embedding(vocab_size, embed_size)
self.lstm = nn.LSTM(embed_size, hidden_size, num_layers)
self.linear = nn.Linear(hidden_size, vocab_size)
self.dropout = nn.Dropout(0.5)
def forward(self, x, captions):
embeddings = self.dropout(self.embedding(captions))
embeddings = torch.cat((x.unsqueeze(0), embeddings), dim=0)
hiddens, _ = self.lstm(embeddings)
return self.linear(hiddens)
class CNNtoRNN(nn.Module):
def __init__(self, embed_size, hidden_size, vocab_size, num_layers):
super(CNNtoRNN, self).__init__()
self.EncoderCNN = EncoderCNN(embed_size)
self.DecoderRNN = DecoderRNN(embed_size, hidden_size, vocab_size, num_layers)
def forward(self, x, captions):
features = self.EncoderCNN(x)
return self.DecoderRNN(features, captions)
def caption_image(self, image, vocabulary, max_length = 50):
result = []
with torch.no_grad():
x = self.EncoderCNN(image).unsqueeze(0)
states = None
for _ in range(max_length):
hiddens, states = self.DecoderRNN.lstm(x, states)
output = self.DecoderRNN.linear(hiddens.unsqueeze(0))
predicted = output.argmax(1)
result.append(predicted.item())
x = self.DecoderRNN.embed(predicted).unsqueeze(0)
if vocabulary.itos[predicted.item()] == "<EOS>":
break
return [vocabulary.itos[idx] for idx in result]