-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_load.py
More file actions
271 lines (233 loc) · 10.9 KB
/
Copy pathdata_load.py
File metadata and controls
271 lines (233 loc) · 10.9 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
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
# 用于训练的dataloader
# 不同的模型进行不同的预处理
import torch
import numpy as np
import json
import os
import pickle
from torch.utils.data import Dataset, DataLoader
from PIL import Image
from torchvision.transforms import InterpolationMode
import torchvision.transforms as transforms
from utils.vocab import Vocabulary
from transformers import OFATokenizer, OFAModel
from transformers import AutoProcessor
from transformers import BertTokenizer
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class IC_data(Dataset):
"""作为val和test时的dataset"""
def __init__(self, config, dir, mode):
super(IC_data, self).__init__()
self.config = config
self.data = json.load(open(dir, 'r'))
self.model = config.model
# 根据不同的model选择不同的transforms
self.patch_resize_transform = self.get_transforms(self.model)
if self.model == 'OFA':
self.ofa_ckpt = config.ofa_ckpts
self.tokenizer = OFATokenizer.from_pretrained("OFA-Sys/ofa-large")
self.mode = mode
def get_transforms(self, model):
if model == 'OFA':
self.resolution = 480
self.mean, self.std = [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]
patch_resize_transform = transforms.Compose([
lambda image: image.convert("RGB"),
transforms.Resize((self.resolution, self.resolution), interpolation=Image.BICUBIC),
transforms.ToTensor(),
transforms.Normalize(mean=self.mean, std=self.std)])
return patch_resize_transform
def __getitem__(self, item):
if self.mode == 'train':
""""""
else:
#print('1. self.data is ', self.data)
image_path = self.data[item]['filename']
#print('1. image_path')
#print('The image path is ', image_path)
img = Image.open(image_path)
patch_img = self.patch_resize_transform(img)
image_id = self.data[item]['image_id']
return image_id, patch_img
def collate_fn_train(self, batch_data):
""""""
def collate_fn_eval(self, batch_data):
image_id, image = zip(*batch_data)
image = torch.stack(image, dim=0)
image_feature = {'patch_image': image}
return image_id, image_feature
def __len__(self):
return len(self.data)
class RWConcept_data(Dataset):
def __init__(self, config, dir, mode):
super(RWConcept_data, self).__init__()
self.config = config
self.data = json.load(open(dir, 'r'))
#print('dir is ', dir)
self.model = config.model
# 根据不同的model选择不同的transforms
#Choose different transforms according to different models
self.patch_resize_transform = self.get_transforms(config.model)
if self.model == 'OFA':
self.ofa_ckpt = config.ofa_ckpts
self.tokenizer = OFATokenizer.from_pretrained("OFA-Sys/ofa-large")
self.mode = mode
def get_transforms(self, model):
if model == 'OFA':
self.resolution = 480
self.mean, self.std = [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]
patch_resize_transform = transforms.Compose([
lambda image: image.convert("RGB"),
transforms.Resize((self.resolution, self.resolution), interpolation=Image.BICUBIC),
transforms.ToTensor(),
transforms.Normalize(mean=self.mean, std=self.std)])
return patch_resize_transform
def __getitem__(self, item):
#print('I am insode __getitem__')
#print('item is ', item)
if self.mode == 'train':
caption = self.data[item]['caption']
# 不同的模型加载不同的前缀
if self.model == 'OFA':
caption = ' '+caption
elif self.model == "BLIP":
caption = ' a picture of ' + caption
elif self.model == 'GIT':
caption = ' '+caption
# 不同的模型tokenize的方式不同
if self.model == 'OFA':
cap_id = self.tokenizer([caption], return_tensors="pt").input_ids[0]
cap_len = cap_id.shape[0]
if cap_len < self.config.fixed_len:
if self.model == 'OFA':
cap_id = torch.cat([cap_id, torch.ones([self.config.fixed_len-cap_len])], dim=0)
att_mask = torch.cat([torch.ones([cap_len]), torch.zeros([self.config.fixed_len-cap_len])], dim=0)
else:
cap_id = cap_id[:self.config.fixed_len]
cap_len = self.config.fixed_len
att_mask = torch.ones(cap_id.shape)
#print('2. self.data is ', self.data)
image_path = self.data[item]['filename']
#print('2. image_path')
#print('The image path is ', image_path)
img = Image.open(image_path)
patch_img = self.patch_resize_transform(img)
label = 0 if self.data[item]['data'] == 'coco' else 1
return patch_img, cap_id, att_mask, cap_len, label, self.data[item]
else:
#print('3. self.data is ', self.data)
image_path = self.data[item]['filename']
#print('3. image_path')
#print('The image path is ', image_path)
img = Image.open(image_path)
patch_img = self.patch_resize_transform(img)
image_id = self.data[item]['image_id']
return image_id, patch_img
def collate_fn_train(self, batch_data):
image, cap_id, att_mask, cap_len, label, data_item = zip(*batch_data)
image = torch.stack(image, dim=0)
image_feature = {'patch_image': image}
cap_id = torch.stack(cap_id, dim=0)
att_mask = torch.stack(att_mask, dim=0)
cap_len = torch.Tensor(cap_len).int()
label = torch.Tensor(label).int()
return image_feature, cap_id.long(), att_mask.long(), cap_len, label, list(data_item)
def collate_fn_eval(self, batch_data):
image_id, image = zip(*batch_data)
image = torch.stack(image, dim=0)
image_feature = {'patch_image': image}
return image_id, image_feature
def __len__(self):
return len(self.data)
class RWConcept_data_EWC(Dataset):
def __init__(self, config, dir, mode='train'):
super(RWConcept_data_EWC, self).__init__()
self.config = config
self.data = json.load(open(dir, 'r'))
self.mean, self.std = [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]
self.resolution = 480
self.patch_resize_transform = transforms.Compose([
lambda image: image.convert("RGB"),
transforms.Resize((self.resolution, self.resolution), interpolation=Image.BICUBIC),
transforms.ToTensor(),
transforms.Normalize(mean=self.mean, std=self.std)])
self.ofa_ckpt = config.ofa_ckpts
self.tokenizer = OFATokenizer.from_pretrained("OFA-Sys/ofa-large")
self.mode = mode
def __getitem__(self, item):
if self.mode == 'train':
caption = ' '+self.data[item]['caption']
cap_id = self.tokenizer([caption], return_tensors="pt").input_ids[0]
keyword = ' '+self.data[item]['keyword']
keyword_id = self.tokenizer([keyword], return_tensors="pt").input_ids[0]
keyword_id = keyword_id[keyword_id > 2]
cap_len = cap_id.shape[0]
if cap_len < self. config.fixed_len:
cap_id = torch.cat([cap_id, torch.ones([self.config.fixed_len-cap_len])], dim=0)
att_mask = torch.cat([torch.ones([cap_len]), torch.zeros([self.config.fixed_len-cap_len])], dim=0)
else:
cap_id = cap_id[:self.config.fixed_len]
cap_len = self.config.fixed_len
att_mask = torch.ones(cap_id.shape)
if_keyword = torch.Tensor([True if (item in keyword_id) else False for item in cap_id])
#print('4. self.data is ', self.data)
image_path = self.data[item]['filename']
#print('4. image_path')
#print('The image path is ', image_path)
img = Image.open(image_path)
patch_img = self.patch_resize_transform(img)
label = 0 if self.data[item]['data'] == 'coco' else 1
return patch_img, cap_id, att_mask, cap_len, label, if_keyword
def collate_fn_train(self, batch_data):
image, cap_id, att_mask, cap_len, label, if_keyword = zip(*batch_data)
image = torch.stack(image, dim=0)
image_feature = {'patch_image': image}
cap_id = torch.stack(cap_id, dim=0)
if_keyword = torch.stack(if_keyword, dim=0)
att_mask = torch.stack(att_mask, dim=0)
cap_len = torch.Tensor(cap_len).int()
label = torch.Tensor(label).int()
return image_feature, cap_id.long(), att_mask.long(), cap_len, label, if_keyword
def __len__(self):
return len(self.data)
def data_load_rwc_EWC(config, dir, mode):
print('dir is', dir)
dataset = RWConcept_data_EWC(config, dir, mode)
#print('Line 212')
config.num_workers = 0 # Disable multiprocessing
data_loader = DataLoader(dataset=dataset,
batch_size=config.batch_size,
shuffle=True,
collate_fn=dataset.collate_fn_train,
num_workers=config.num_workers,
pin_memory=True,
)
return data_loader
def data_load(config, dir, mode):
print('dir in data_load', dir)
if mode == 'train':
print("warning: the train_loader is not exist")
dataset = IC_data(config, dir, mode)
print('Line 227')
data_loader = DataLoader(dataset=dataset,
batch_size=config.batch_size if mode == 'train' else config.val_batch_size,
shuffle=True if mode == 'train' else False,
collate_fn=dataset.collate_fn_train if mode == 'train' else dataset.collate_fn_eval,
num_workers=config.num_workers,
pin_memory=True,
)
return data_loader
def data_load_rwc(config, dir, mode):
dataset = RWConcept_data(config, dir, mode)
#print('Line 239')
#print('dataset is ', dataset)
#print('dataset.collate_fn_train is ', dataset.collate_fn_train)
data_loader = DataLoader(dataset=dataset,
batch_size=config.batch_size if mode == 'train' else config.val_batch_size,
shuffle=False,
collate_fn=dataset.collate_fn_train if mode == 'train' else dataset.collate_fn_eval,
num_workers=config.num_workers,
pin_memory=True,
)
#print('return data_load_rwc')
return data_loader