-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodel.py
More file actions
101 lines (67 loc) · 3.35 KB
/
model.py
File metadata and controls
101 lines (67 loc) · 3.35 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
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
import torch
import torch.nn as nn
import torchvision.models as models
class EncoderCNN(nn.Module):
def __init__(self, embed_size):
super(EncoderCNN, self).__init__()
resnet = models.resnet50(pretrained=True)
for param in resnet.parameters():
param.requires_grad_(False)
modules = list(resnet.children())[:-1]
self.resnet = nn.Sequential(*modules)
self.embed = nn.Linear(resnet.fc.in_features, embed_size)
def forward(self, images):
features = self.resnet(images)
features = features.view(features.size(0), -1)
features = self.embed(features)
return features
class DecoderRNN(nn.Module):
def __init__(self, embed_size, hidden_size, vocab_size, num_layers=1):
super(DecoderRNN, self).__init__()
self.embed_size = embed_size
self.hidden_size = hidden_size
self.vocab_size = vocab_size
self.num_layers = num_layers
self.embed = nn.Embedding(num_embeddings = self.vocab_size,
embedding_dim = self.embed_size)
self.lstm = nn.LSTM(input_size = self.embed_size,
hidden_size = self.hidden_size,
num_layers = self.num_layers,
batch_first = True)
self.fc1 = nn.Linear(in_features = self.hidden_size,
out_features = self.vocab_size)
def forward(self, features, captions):
captions = captions[:,:-1]
embeddings = self.embed(captions)
embeddings = torch.cat((features.unsqueeze(1), embeddings),dim=1)
batch_size = features.shape[0]
device = "cpu"
if torch.cuda.is_available():
device = "cuda"
self.hidden = (torch.zeros((self.num_layers, batch_size, self.hidden_size), device=device),torch.zeros((self.num_layers, batch_size, self.hidden_size), device=device))
lstm_out, self.hidden = self.lstm(embeddings, self.hidden)
output = self.fc1(lstm_out)
return output
def sample(self, inputs, states=None, max_len=20):
" accepts pre-processed image tensor (inputs) and returns predicted sentence (list of tensor ids of length max_len) "
output = []
batch_size = inputs.shape[0]
#print("Batch Size: ",batch_size)
device = "cpu"
if torch.cuda.is_available():
device = "cuda"
self.hidden = (torch.zeros((self.num_layers, batch_size, self.hidden_size), device=device),torch.zeros((self.num_layers, batch_size, self.hidden_size), device=device))
while True:
lstm_out, self.hidden = self.lstm(inputs, self.hidden)
linear_out = self.fc1(lstm_out.squeeze(1))
top_score = linear_out.max(1)[1]
output.append(top_score.cpu().numpy()[0].item())
if top_score == 1:
break
inputs = self.embed(top_score)
inputs = inputs.unsqueeze(1)
return output
def save_checkpoint(model,fileLocation):
torch.save(model.state_dict(),fileLocation)
def load_checkpoint(model,fileLocation):
return model.load_state_dict(torch.load(fileLocation))