-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
214 lines (171 loc) · 8.37 KB
/
training.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
# coding: utf-8
# In[5]:
import importlib
import preprocessing
import torch
importlib.reload(preprocessing)
# DEVICE= torch.device('cuda:0' if torch.cuda.is_available() else "cpu")
DEVICE=torch.device('cpu')
loaders = preprocessing.TrainLoader(DEVICE)
trainloader = loaders.trainloader
valloader = loaders.valloader
dic = preprocessing.dic
# In[6]:
import encoder
import decoder
# In[34]:
import importlib
importlib.reload(decoder)
importlib.reload(encoder)
import os
import pickle
import time
import numpy as np
import torch.nn as nn
from torch.autograd import Variable
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torchvision import transforms
from torch.nn.utils.rnn import pack_padded_sequence
EncoderCNN = encoder.EncoderCNN
SentenceRNN = decoder.SentenceRNN
WordRNN = decoder.WordRNN
EPOCHS = 2
LEARNING_RATE = 0.1
BATCH_SIZE=2
class Im2pGenerator(object):
def __init__(self):
print('in constructor')
self.train_data_loader = trainloader
self.val_data_loader = trainloader
self.encoderCNN = EncoderCNN()
self.sentenceRNN = SentenceRNN()
self.wordRNN = WordRNN(hidden_size=256, vocab_size=len(dic), att_dim=256, embed_size=256, encoded_dim=256, device=DEVICE)
self.criterionSentence = nn.BCELoss(size_average=False, reduce=False)
self.criterionWord = nn.CrossEntropyLoss().to(DEVICE)
self.optimizer = torch.optim.Adam(params=(
list(self.encoderCNN.parameters()) + list(self.sentenceRNN.parameters()) + list(self.wordRNN.parameters())
), lr=LEARNING_RATE)
def train(self):
for epoch in range(EPOCHS):
train_loss = self.__epoch_train()
# val_loss = self.__epoch_val()
val_loss = 0
print("[{}] Epoch-{} - train loss:{} - val loss:{} - lr:{}".format(self.__get_now(),
epoch + 1,
train_loss,
val_loss,
# self.optimizer.param_groups[0]['lr']
0
))
self.__save_model(epoch)
def __epoch_train(self):
print('in epoch train')
train_loss = 0
self.encoderCNN.train()
self.wordRNN.train()
self.sentenceRNN.train()
self.encoderCNN.to(DEVICE)
self.wordRNN.to(DEVICE)
self.sentenceRNN.to(DEVICE)
for i, (images, findings, sentenceVectors, word2d, wordsLengths) in enumerate(self.train_data_loader):
images = images.to(DEVICE)
word2d = word2d.to(DEVICE)
featureMap, globalFeatures = self.encoderCNN.forward(images)
sentence_states = None
loss = 0
sentenceLoss = 0
wordLoss = 0
word2d = word2d.permute(1, 0, 2) #(sentenceIndex, batchSize, maxWordsInSentence)
for sentenceIndex, sentence_value in enumerate(sentenceVectors):
endToken, topic_vec, sentence_states = self.sentenceRNN.forward(globalFeatures, sentence_states)
endToken = endToken.squeeze(1).squeeze(1)
"""***TODO*** Should stop calculating loss for sentences once they're done."""
sentenceLoss = sentenceLoss + self.criterionSentence(endToken, sentence_value.type(torch.float).to(DEVICE)).sum()
captions=word2d[sentenceIndex]
captionLengths=wordsLengths[sentenceIndex]
if(any(captionLengths)):
predictions, alphas, betas, encoded_captions, decode_lengths, sort_ind = self.wordRNN.forward(
enc_image=featureMap,
global_features=globalFeatures,
encoded_captions=captions,
caption_lengths=captionLengths
)
# predictions: (batch_size, largest_sentence_in_batch_size, vocab_size)
targets = captions
# Remove timesteps that we didn't decode at, or are pads
# pack_padded_sequence is an easy trick to do this
greaterThan0LengthIndeces = list() #remove length 0 sentences
greaterThan0Lengths = list()
for i, length in enumerate(decode_lengths):
if(length > 0):
greaterThan0LengthIndeces.append(i)
greaterThan0Lengths.append(length)
targets = targets[greaterThan0LengthIndeces]
predictions = predictions[greaterThan0LengthIndeces]
targets = pack_padded_sequence(targets, greaterThan0Lengths, batch_first=True).data
scores = pack_padded_sequence(predictions, greaterThan0Lengths, batch_first=True).data
# Calculate loss
wordLoss = wordLoss + self.criterionWord(scores, targets)
loss = wordLoss + sentenceLoss
self.optimizer.zero_grad()
# Update weights
loss.backward()
self.optimizer.step()
break
return train_loss
def __epoch_val(self):
print('in epoch val')
val_loss = 0
for i, (images, findings, sentenceVectors, word2d, wordsLengths) in enumerate(self.train_data_loader):
images = images.to(DEVICE)
word2d = word2d.to(DEVICE)
featureMap, globalFeatures = self.encoderCNN.forward(images)
sentence_states = None
loss = 0
sentenceLoss = 0
wordLoss = 0
for sentenceIndex, sentence_value in enumerate(sentenceVectors):
endToken, topic_vec, sentence_states = self.sentenceRNN.forward(globalFeatures, sentence_states)
endToken = endToken.squeeze(1).squeeze(1)
captions=word2d[sentenceIndex]
captionLengths=wordsLengths[sentenceIndex]
if(any(captionLengths)):
predictions, alphas, betas, encoded_captions, decode_lengths, sort_ind = self.wordRNN.forward(
enc_image=featureMap,
global_features=globalFeatures,
encoded_captions=captions,
caption_lengths=captionLengths
)
# predictions: (batch_size, largest_sentence_in_batch_size, vocab_size)
targets = captions
# Remove timesteps that we didn't decode at, or are pads
# pack_padded_sequence is an easy trick to do this
greaterThan0LengthIndeces = list() #remove length 0 sentences
greaterThan0Lengths = list()
for i, length in enumerate(decode_lengths):
if(length > 0):
greaterThan0LengthIndeces.append(i)
greaterThan0Lengths.append(length)
targets = targets[greaterThan0LengthIndeces]
predictions = predictions[greaterThan0LengthIndeces]
# Need to softmax predictions to generate vocabular index
# Back convert targets and predictions to the proper sentence
# Add to references and hypotheses
break
# compare references and hypotheses to generate bleu
return val_loss
def __get_date(self):
return str(time.strftime('%Y%m%d', time.gmtime()))
def __get_now(self):
return str(time.strftime('%y%m%d-%H:%M:%S', time.gmtime()))
def __save_model(self, epoch):
state = {'epoch': epoch,
'sentenceRNN': self.sentenceRNN,
'wordRNN': self.wordRNN,
'encoderCNN': self.encoderCNN,
'optimizer': self.optimizer}
filename = 'checkpoints/checkpoint_' + str(epoch) + '.pth.tar'
torch.save(state, filename)
im2p = Im2pGenerator()
im2p.train()