-
Notifications
You must be signed in to change notification settings - Fork 1
/
load_data.py
145 lines (115 loc) · 5.34 KB
/
load_data.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
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from __future__ import print_function
from model.clip_model.simple_tokenizer import SimpleTokenizer
import os
import numpy as np
import scipy.io as scio
from torch.utils.data import Dataset
import torch
import random
from PIL import Image
from torchvision.transforms import Compose, Resize, CenterCrop, ToTensor, Normalize
class BaseDataset(Dataset):
def __init__(self,
captions: dict,
indexs: dict,
labels: dict,
is_train=True,
tokenizer=SimpleTokenizer(),
maxWords=32,
imageResolution=224,
):
self.captions = captions
self.indexs = indexs
self.labels = labels
self.maxWords = maxWords
self.tokenizer = tokenizer
self.transform = Compose([
Resize(imageResolution, interpolation=Image.BICUBIC),
CenterCrop(imageResolution),
ToTensor(),
Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)),
]) if is_train else Compose([
Resize((imageResolution, imageResolution), interpolation=Image.BICUBIC),
ToTensor(),
Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)),
])
self.SPECIAL_TOKEN = {"CLS_TOKEN": "<|startoftext|>", "SEP_TOKEN": "<|endoftext|>",
"MASK_TOKEN": "[MASK]", "UNK_TOKEN": "[UNK]", "PAD_TOKEN": "[PAD]"}
self.__length = len(self.indexs)
def __len__(self):
return self.__length
def _load_image(self, index: int) -> torch.Tensor:
image_path = self.indexs[index].strip()
image = Image.open(image_path).convert("RGB")
image = self.transform(image)
return image
def _load_text(self, index: int):
captions = self.captions[index]
words = self.tokenizer.tokenize(captions)
words = [self.SPECIAL_TOKEN["CLS_TOKEN"]] + words
total_length_with_CLS = self.maxWords - 1
if len(words) > total_length_with_CLS:
words = words[:total_length_with_CLS]
words = words + [self.SPECIAL_TOKEN["SEP_TOKEN"]]
caption = self.tokenizer.convert_tokens_to_ids(words)
while len(caption) < self.maxWords:
caption.append(0)
caption = torch.tensor(caption)
key_padding_mask = (caption == 0)
return caption, key_padding_mask
def _load_label(self, index: int) -> torch.Tensor:
label = self.labels[index]
label = torch.from_numpy(label)
return label
def get_all_label(self):
labels = torch.zeros([self.__length, len(self.labels[0])], dtype=torch.int64)
for i, item in enumerate(self.labels):
labels[i] = torch.from_numpy(item)
return labels
def __getitem__(self, index):
image = self._load_image(index)
caption, key_padding_mask = self._load_text(index)
label = self._load_label(index)
return image, caption, key_padding_mask, label, index
def split_data(captions, indexs, labels, query_num, train_num, seed=None):
np.random.seed(seed=1) # fixed to 1 for all experiments.
random_index = np.random.permutation(range(len(indexs)))
query_index = random_index[: query_num]
train_index = random_index[query_num: query_num + train_num]
retrieval_index = random_index[query_num:]
query_indexs = indexs[query_index]
query_captions = captions[query_index]
query_labels = labels[query_index]
train_indexs = indexs[train_index]
train_captions = captions[train_index]
train_labels = labels[train_index]
retrieval_indexs = indexs[retrieval_index]
retrieval_captions = captions[retrieval_index]
retrieval_labels = labels[retrieval_index]
split_indexs = (query_indexs, train_indexs, retrieval_indexs)
split_captions = (query_captions, train_captions, retrieval_captions)
split_labels = (query_labels, train_labels, retrieval_labels)
return split_indexs, split_captions, split_labels
def generate_dataset(captionFile: str,
indexFile: str,
labelFile: str,
maxWords=32,
imageResolution=224,
query_num=2000,
train_num=10000,
seed=None,
):
captions = scio.loadmat(captionFile)["caption"]
indexs = scio.loadmat(indexFile)["index"]
labels = scio.loadmat(labelFile)["label"]
split_indexs, split_captions, split_labels = split_data(captions, indexs, labels, query_num=query_num, train_num=train_num, seed=seed)
query_data = BaseDataset(captions=split_captions[0], indexs=split_indexs[0], labels=split_labels[0],
maxWords=maxWords, imageResolution=imageResolution, is_train=False)
train_data = BaseDataset(captions=split_captions[1], indexs=split_indexs[1], labels=split_labels[1],
maxWords=maxWords, imageResolution=imageResolution)
retrieval_data = BaseDataset(captions=split_captions[2], indexs=split_indexs[2], labels=split_labels[2],
maxWords=maxWords, imageResolution=imageResolution, is_train=False)
return train_data, query_data, retrieval_data