-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader_val.py
More file actions
65 lines (60 loc) · 2.61 KB
/
Copy pathdata_loader_val.py
File metadata and controls
65 lines (60 loc) · 2.61 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
import nltk
import os
import torch
import torch.utils.data as data
from vocabulary import Vocabulary
from PIL import Image
from pycocotools.coco import COCO
import numpy as np
from tqdm import tqdm
import random
import json
from data_loader import CoCoDataset
def get_loader(transform,
mode='val',
batch_size=1,
vocab_threshold=None,
vocab_file='./vocab.pkl',
start_word="<start>",
end_word="<end>",
unk_word="<unk>",
vocab_from_file=True,
num_workers=0,
cocoapi_loc='/opt'):
"""Returns the data loader.
Args:
transform: Image transform.
mode: One of 'train' or 'test'.
batch_size: Batch size (if in testing mode, must have batch_size=1).
vocab_threshold: Minimum word count threshold.
vocab_file: File containing the vocabulary.
start_word: Special word denoting sentence start.
end_word: Special word denoting sentence end.
unk_word: Special word denoting unknown words.
vocab_from_file: If False, create vocab from scratch & override any existing vocab_file.
If True, load vocab from from existing vocab_file, if it exists.
num_workers: Number of subprocesses to use for data loading
cocoapi_loc: The location of the folder containing the COCO API: https://github.com/cocodataset/cocoapi
"""
assert mode in ['val'], "mode must be one of 'train', 'test' or 'val'."
assert os.path.exists(vocab_file), "Must first generate vocab.pkl from training data."
assert vocab_from_file==True, "Change vocab_from_file to True."
img_folder = os.path.join(cocoapi_loc, 'cocoapi/images/val2014/')
annotations_file = os.path.join(cocoapi_loc, 'cocoapi/annotations/captions_val2014.json')
# COCO caption dataset.S
dataset = CoCoDataset(transform=transform,
mode=mode,
batch_size=batch_size,
vocab_threshold=vocab_threshold,
vocab_file=vocab_file,
start_word=start_word,
end_word=end_word,
unk_word=unk_word,
annotations_file=annotations_file,
vocab_from_file=True,
img_folder=img_folder)
data_loader = data.DataLoader(dataset=dataset,
batch_size=dataset.batch_size,
shuffle=True,
num_workers=num_workers)
return data_loader