-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimg_caption_Farsi.py
360 lines (275 loc) · 11.3 KB
/
img_caption_Farsi.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""
Image Captioning
Input: image.
Output: A sequence of words in a natural language, which hopefully describes the contents of the input Image.
"""
import os
import sys
import json
import pickle
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from glob import glob
from IPython.display import display
import torch
import torch.nn as nn
import torch.optim as optim
from torch.nn.utils.rnn import pack_padded_sequence
import torchvision.transforms as transforms
import torchvision.models as models
from utils import *
from build_vocab import build_vocab
from data_loader import get_loader
# setup
use_gpu = torch.cuda.is_available()
def load_cnn_model(model_name, pretrained=True):
"Load and return a convolutional neural network."
assert model_name in ['resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152']
return models.__dict__[model_name](pretrained)
def load_image(image_path, transform=None):
"Load an image and perform given transformations."
image = Image.open(image_path)
if transform is not None:
image = transform(image).unsqueeze(0)
return image
"""
Data
https://cocodataset.org/#download
More than 80k training images and 40k validation images.
At leat 5 captions for every image.
"""
dataset = load_json('data/images_captions_train.json')
print(dataset['images'][0])
print(dataset.keys())
print(dataset['annotations'][0])
show_random_image_with_caption(dataset)
# building vocab
DATA_DIR = 'data'
captions_filename = f'{DATA_DIR}/fa_captions.txt'
vocab_filename = f'{DATA_DIR}/vocab.pkl'
if os.path.exists(vocab_filename):
vocab = pickle.load(open(vocab_filename, 'rb'))
else:
vocab = build_vocab(captions_filename, min_count=3)
pickle.dump(vocab, open(vocab_filename, 'wb'))
for i in range(20):
print("%s --> %d" %(vocab.idx2word[i], i))
images_dir = f'{DATA_DIR}/images'
captions_json = f'{DATA_DIR}/fa_images_captions_train.json'
image_size = 256
crop_size = 224
batch_size = 16
transform = transforms.Compose([
transforms.Resize(image_size),
transforms.RandomCrop(crop_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225))
])
data_loader = get_loader(images_dir, captions_json, vocab,
transform, batch_size,
shuffle=True, num_workers=0)
imgs, caps, lengths = next(iter(data_loader))
print(" ".join([str(id) for id in caps[0][1:-1]]))
print(" ".join([vocab.idx2word[id] for id in caps[0][1:-1]]))
print(caps.size())
#-----------------------------------------------------
#Encoder (CNN)
#----------------------------------------------------
class EncoderCNN(nn.Module):
def __init__(self, model_name, embed_size):
super(EncoderCNN, self).__init__()
# load cnn and remove last layer
cnn = load_cnn_model(model_name)
modules = list(cnn.children())[:-1] # remove last layer
self.cnn = nn.Sequential(*modules)
self.linear = nn.Linear(cnn.fc.in_features, embed_size)
self.bn = nn.BatchNorm1d(embed_size, momentum=0.01)
self.init_weights()
def init_weights(self):
self.linear.weight.data.normal_(0, 0.02)
self.linear.bias.data.fill_(0)
def forward(self, x):
x = self.cnn(x) # extract features from input image
x = Variable(x.data)
x = x.view(x.size(0), -1)
x = self.linear(x)
x = self.bn(x)
return x
def fine_tune(self, requires_grad=True):
for param in self.cnn.layer4.parameters():
param.requires_grad = requires_grad
#-------------------------------------------------------------
#Decoder (LSTM)
#-------------------------------------------------------------
class DecoderLSTM(nn.Module):
def __init__(self, vocab_size, embed_size, hidden_size, num_layers, dropout, tie_weights):
super(DecoderLSTM, self).__init__()
if tie_weights:
embed_size = hidden_size
self.embedding = nn.Embedding(vocab_size, embed_size)
self.lstm = nn.LSTM(embed_size, hidden_size, num_layers, batch_first=True, dropout=0.35)
self.fc = nn.Linear(hidden_size, vocab_size)
self.dropout = nn.Dropout(p=dropout)
if tie_weights:
# share weights between embedding and classification layer
self.fc.weight = self.embedding.weight
self.init_weights()
def init_weights(self):
self.embedding.weight.data.uniform_(-0.1, 0.1)
self.fc.weight.data.uniform_(-0.1, 0.1)
self.fc.bias.data.fill_(0)
def forward(self, features, captions, lengths):
x = self.embedding(captions)
x = torch.cat([features.unsqueeze(1), x], dim=1)
x = self.dropout(x)
x = pack_padded_sequence(x, lengths, batch_first=True)
x, _ = self.lstm(x)
x = self.dropout(x[0])
x = self.fc(x)
return x
def sample(self, features, states=None):
"""Samples captions for given image features (Greedy search)."""
sampled_ids = []
inputs = features.unsqueeze(1)
for i in range(20): # maximum sampling length
hiddens, states = self.lstm(inputs, states) # (batch_size, 1, hidden_size),
outputs = self.fc(hiddens.squeeze(1)) # (batch_size, vocab_size)
token_id = outputs.max(1)[1]
sampled_ids += [token_id]
inputs = self.embedding(token_id)
inputs = inputs.unsqueeze(1) # (batch_size, 1, embed_size)
sampled_ids = torch.cat(sampled_ids, 0) # (batch_size, 20)
return sampled_ids.squeeze()
#-----------------------------------------------
# Encoder-Decoder
#-----------------------------------------------
class EncoderDecoder(nn.Module):
def __init__(self, cnn_name, vocab_size, embed_size, hidden_size, num_layers, dropout, tie_weights):
super(EncoderDecoder, self).__init__()
if tie_weights:
embed_size = hidden_size
self.encoder = EncoderCNN(cnn_name, embed_size)
self.decoder = DecoderLSTM(vocab_size, embed_size, hidden_size, num_layers, dropout, tie_weights)
# create output folder to save weights
self.save_path = f'{cnn_name}-{embed_size}-{hidden_size}-{num_layers}'
if not os.path.exists(self.save_path):
os.mkdir(self.save_path)
def forward(self, images, captions, lengths):
features = self.encoder(images)
outputs = self.decoder(features, captions, lengths)
return outputs
def save(self, epoch, loss):
torch.save({'encoder': self.encoder.state_dict(),
'decoder': self.decoder.state_dict()}, f'{self.save_path}/{epoch}-{loss:.2f}.pth')
def load(self, epoch):
model_path = glob(f'{self.save_path}/{epoch}-*.pth')[-1]
try:
d = torch.load(model_path)
self.encoder.load_state_dict(d['encoder'])
self.decoder.load_state_dict(d['decoder'])
except:
print('Invalid epoch number <{}>, the model does not exist!'.format(epoch))
# model hyper-parameters
cnn_name = 'resnet50'
embed_size = 512
hidden_size = 512
num_layers = 2
tie_weights = True
# training hyper-parameters
start_epoch = 0
num_epochs = 20
learning_rate = 0.001
# training
def train_epoch(model, train_dl, criterion, optimizer, scheduler, epoch, last_epoch):
model.encoder.train()
model.decoder.train()
scheduler.step()
total_steps = len(train_dl)
epoch_loss = 0.0
for i, (images, captions, lengths) in enumerate(train_dl):
images, captions = to_var(images), to_var(captions)
targets = pack_padded_sequence(captions, lengths, batch_first=True)[0]
# forward step
outputs = model(images, captions, lengths)
loss = criterion(outputs, targets)
epoch_loss = (epoch_loss * i + loss.data[0]) / (i + 1)
# backward step
model.encoder.zero_grad()
model.decoder.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_norm(model.decoder.parameters(), 5.0)
optimizer.step()
# report log info
sys.stdout.flush()
sys.stdout.write('\rEpoch [%2d/%2d], Step [%3d/%3d], Loss = %.4f, Perplexity = %.4f '
% (epoch+1, last_epoch, i+1, total_steps, epoch_loss, np.exp(epoch_loss)))
print()
return epoch_loss
def train(model, train_dl, criterion, optimizer, scheduler, start_epoch=0, num_epochs=10):
last_epoch = start_epoch + num_epochs
for epoch in range(start_epoch, last_epoch):
# train step
trn_loss = train_epoch(model, data_loader, criterion, optimizer, scheduler, epoch, last_epoch)
# save model
model.save(epoch, trn_loss)
#------------------------------------------------------
#Encoder-Decoder
#------------------------------------------------------
model = EncoderDecoder(cnn_name, len(vocab), embed_size, hidden_size, num_layers, 0.3, tie_weights)
if use_gpu:
model = model.cuda()
# Loss and optimizer
# loss function
criterion = nn.CrossEntropyLoss()
if use_gpu:
criterion = criterion.cuda()
# list of parameters which will be updated
params = list(model.decoder.parameters())
params += list(model.encoder.linear.parameters())
params += list(model.encoder.bn.parameters())
# optimizer
optimizer = torch.optim.RMSprop(params, lr=learning_rate)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.97)
# Training
train(model, data_loader, criterion, optimizer, scheduler, start_epoch, num_epochs)
from PIL import Image
val_transform = transforms.Compose([
transforms.Resize(image_size),
transforms.CenterCrop(crop_size),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225))
])
def generate_caption(model, img_filenames):
model.encoder.eval()
model.decoder.eval()
captions = []
for img_filename in img_filenames:
# prepare test image
image = load_image(img_filename, val_transform)
image_tensor = to_var(image, volatile=True)
# Generate features from image
feature = model.encoder(image_tensor)
# Generate caption from image
sampled_ids = model.decoder.sample(feature)
sampled_ids = sampled_ids.cpu().data.numpy()
# decode word ids to words
sampled_caption = []
for word_id in sampled_ids:
word = vocab.idx2word[word_id]
if word == '<EOS>': break
sampled_caption.append(word)
caption = " ".join(sampled_caption[1:])
captions.append((img_filename, caption))
return captions
img_filenames = glob('data/images/*.jpg')[:10]
captions = generate_caption(model, img_filenames)
for img, caption in captions:
display(show_persian_image_and_caption(caption, img))
img_filenames = glob('./data/im2txt/*.jpg')[:10]
captions = generate_caption(model, img_filenames)
for img, caption in captions:
display(show_persian_image_and_caption(caption, img))