-
Notifications
You must be signed in to change notification settings - Fork 7
/
coco_data_loader.py
90 lines (67 loc) · 2.22 KB
/
coco_data_loader.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
import torch
import os
import numpy as np
import torch.utils.data
import torchvision
import word_embedding
from PIL import Image
import json
TRAIN_DIR = '../train2014'
VALID_DIR = '../val2014'
TRAIN_JSON = '../annotations/captions_train2014.json'
VALID_JSON = '../annotations/captions_val2014.json'
def resize_and_pad(img):
img.thumbnail((224, 224))
w, h = img.size
new_img = Image.new('RGB', (224, 224), 'black')
new_img.paste(img, ((224 - w)//2, (224 - h)//2))
return new_img
class CocoData(torch.utils.data.Dataset):
"""Utility for loading COCO data in batches"""
def __init__(self, mode):
self.word_embeddings = word_embedding.WordEmbedding()
self.mode = mode
if mode == 'train':
self.json_file = TRAIN_JSON
else:
self.json_file = VALID_JSON
with open(self.json_file) as jsonf:
data = json.load(jsonf)
self.captions = data['annotations']
def __len__(self):
return len(self.captions)
def __getitem__(self, idx):
caption = self.captions[idx]
image_id = caption['image_id']
if self.mode == 'train':
image_file = '%s/COCO_train2014_%012d.jpg' % (TRAIN_DIR, image_id)
else:
image_file = '%s/COCO_val2014_%012d.jpg' % (VALID_DIR, image_id)
text = caption['caption']
# Process text
words, wordvecs = self.word_embeddings.sentence_to_embedding(text, pad = 20)
# Load image
img = Image.open(image_file)
transforms = torchvision.transforms.Compose([
torchvision.transforms.Lambda(resize_and_pad),
torchvision.transforms.ToTensor(),
])
img = transforms(img)
return img, words, wordvecs
class CocoDataValid(torch.utils.data.Dataset):
"""Dataloader for evaluation mode (generate caption from image)"""
def __init__(self):
self.images = os.listdir(VALID_DIR)
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
"""Return (ID, image tensor)"""
image_file = VALID_DIR + '/' + self.images[idx]
img = Image.open(image_file)
transforms = torchvision.transforms.Compose([
torchvision.transforms.Lambda(resize_and_pad),
torchvision.transforms.ToTensor(),
])
img = transforms(img)
img_id = int(image_file.split('.')[-2][-12:])
return img_id, img