-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_visual_concept.py
230 lines (194 loc) · 8.72 KB
/
preprocess_visual_concept.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
#将json文件中的图片去重,分别提取
import sys
import os
import clip
import torch
import argparse
import numpy as np
import json
import time
from PIL import Image
from tqdm import tqdm
from torch.utils.data import Dataset
from torchvision.datasets.utils import download_url
from torch.utils.data import DataLoader
from data.utils import pre_caption
from torchvision import transforms
from torchvision.transforms.functional import InterpolationMode
import time
class coco_karpathy(Dataset):
# def __init__(self, transform, image_root, ann_root, split, max_words=30):
def __init__(self, preprocess, image_root, ann_root, split, max_words=30):
'''
image_root (string): Root directory of images (e.g. coco/images/)
ann_root (string): directory to store the annotation file
split (string): train or val or test
'''
urls = {'train': 'https://storage.googleapis.com/sfr-vision-language-research/datasets/coco_karpathy_train.json',
'val': 'https://storage.googleapis.com/sfr-vision-language-research/datasets/coco_karpathy_val.json',
'test': 'https://storage.googleapis.com/sfr-vision-language-research/datasets/coco_karpathy_test.json'}
filenames = {'train':'coco_karpathy_train.json', 'val': 'coco_karpathy_val.json', 'test': 'coco_karpathy_test.json'}
# download_url(urls[split], ann_root)
self.annotation = json.load(open(os.path.join(ann_root, filenames[split]), 'r'))
# self.transform = transforms.Compose([
# transforms.Resize(32, interpolation=InterpolationMode.BICUBIC),
# transforms.CenterCrop(32),
# transforms.ToTensor(),
# transforms.Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)),
# ])
self.transform = preprocess
# self.transform = transforms.ToTensor()
self.image_root = image_root
self.text = []
self.image = []
self.txt2img = {}
self.img2txt = {}
txt_id = 0
for img_id, ann in enumerate(self.annotation):
self.image.append(ann['image'])
self.img2txt[img_id] = []
for i, caption in enumerate(ann['caption']):
self.text.append(pre_caption(caption, max_words))
self.img2txt[img_id].append(txt_id)
self.txt2img[txt_id] = img_id
txt_id += 1
def __len__(self):
return len(self.annotation)
def __getitem__(self, index):
image_path = os.path.join(self.image_root, self.annotation[index]['image'])
# image = Image.open(image_path).convert('RGB')
image = Image.open(image_path)
image = self.transform(image)
return image, index, self.annotation[index]['image']
class flickr30k(Dataset):
# def __init__(self, transform, image_root, ann_root, split, max_words=30):
def __init__(self, image_root, ann_root, split, max_words=30):
'''
image_root (string): Root directory of images (e.g. flickr30k/)
ann_root (string): directory to store the annotation file
split (string): train or val or test
'''
urls = {'train': 'https://storage.googleapis.com/sfr-vision-language-research/datasets/flickr30k_train.json',
'val': 'https://storage.googleapis.com/sfr-vision-language-research/datasets/flickr30k_val.json',
'test': 'https://storage.googleapis.com/sfr-vision-language-research/datasets/flickr30k_test.json'}
filenames = {'train': 'flickr30k_train.json','val': 'flickr30k_val.json', 'test': 'flickr30k_test.json'}
# download_url(urls[split], ann_root)
self.annotation = json.load(open(os.path.join(ann_root, filenames[split]), 'r'))
self.transform = transforms.Compose([
transforms.Resize((360,640),interpolation=InterpolationMode.BICUBIC),
transforms.ToTensor(),
])
# self.transform = transforms.ToTensor()
self.image_root = image_root
self.text = []
self.image = []
self.txt2img = {}
self.img2txt = {}
txt_id = 0
for img_id, ann in enumerate(self.annotation):
self.image.append(ann['image'])
self.img2txt[img_id] = []
for i, caption in enumerate(ann['caption']):
self.text.append(pre_caption(caption, max_words))
self.img2txt[img_id].append(txt_id)
self.txt2img[txt_id] = img_id
txt_id += 1
def __len__(self):
return len(self.annotation)
def __getitem__(self, index):
image_path = os.path.join(self.image_root, self.annotation[index]['image'])
# image = Image.open(image_path).convert('RGB')
image = Image.open(image_path)
image = self.transform(image)
return image, self.annotation[index]['image']
ann_root = '/data1/yangzhenbang_new/blip/BLIP/annotation'
#nlvr
with open(os.path.join(ann_root, 'nlvr_train.json'),'r',encoding='utf8')as fp:
nlvr_train = json.load(fp)
nlvr_train_pictures=[]
for i in tqdm(nlvr_train):
nlvr_train_pictures.extend(i['images'])
nlvr_train_pictures = list(set(nlvr_train_pictures))
with open("/data1/yangzhenbang_new/blip/BLIP/annotation/nlvr_train_pictures.json", 'w', encoding='utf-8') as fw:
json.dump(nlvr_train_pictures, fw, indent=4)
with open(os.path.join(ann_root, 'nlvr_test.json'),'r',encoding='utf8')as fp:
nlvr_test = json.load(fp)
nlvr_test_pictures=[]
for i in tqdm(nlvr_test):
nlvr_test_pictures.extend(i['images'])
nlvr_test_pictures = list(set(nlvr_test_pictures))
with open("/data1/yangzhenbang_new/blip/BLIP/annotation/nlvr_test_pictures.json", 'w', encoding='utf-8') as fw:
json.dump(nlvr_test_pictures, fw, indent=4)
with open(os.path.join(ann_root, 'nlvr_dev.json'),'r',encoding='utf8')as fp:
nlvr_dev = json.load(fp)
nlvr_dev_pictures=[]
for i in tqdm(nlvr_dev):
nlvr_dev_pictures.extend(i['images'])
nlvr_dev_pictures = list(set(nlvr_dev_pictures))
with open("/data1/yangzhenbang_new/blip/BLIP/annotation/nlvr_dev_pictures.json", 'w', encoding='utf-8') as fw:
json.dump(nlvr_dev_pictures, fw, indent=4)
# #coco
# with open(os.path.join(ann_root, 'coco_karpathy_train.json'),'r',encoding='utf8')as fp:
# coco_train = json.load(fp)
# coco_train_pictures=[]
# for i in tqdm(coco_train):
# coco_train_pictures.append(i['image'])
#
# coco_train_pictures = list(set(coco_train_pictures))
# with open("/data1/yangzhenbang_new/blip/BLIP/annotation/coco_train_pictures.json", 'w', encoding='utf-8') as fw:
# json.dump(coco_train_pictures, fw, indent=4)
#
# with open(os.path.join(ann_root, 'coco_karpathy_test.json'),'r',encoding='utf8')as fp:
# coco_test = json.load(fp)
# coco_test_pictures = []
# for i in tqdm(coco_test):
# coco_test_pictures.append(i['image'])
#
# coco_test_pictures = list(set(coco_test_pictures))
# with open("/data1/yangzhenbang_new/blip/BLIP/annotation/coco_test_pictures.json", 'w', encoding='utf-8') as fw:
# json.dump(coco_test_pictures, fw, indent=4)
#
# with open(os.path.join(ann_root, 'coco_karpathy_val.json'),'r',encoding='utf8')as fp:
# coco_val = json.load(fp)
# coco_val_pictures = []
# for i in tqdm(coco_val):
# coco_val_pictures.append(i['image'])
#
# coco_val_pictures = list(set(coco_val_pictures))
# with open("/data1/yangzhenbang_new/blip/BLIP/annotation/coco_val_pictures.json", 'w', encoding='utf-8') as fw:
# json.dump(coco_val_pictures, fw, indent=4)
#
# #flickr
# with open(os.path.join(ann_root, 'flickr30k_train.json'),'r',encoding='utf8')as fp:
# flickr_train = json.load(fp)
#
# flickr_train_pictures=[]
# for i in tqdm(flickr_train):
# flickr_train_pictures.append(i['image'])
#
# flickr_train_pictures = list(set(flickr_train_pictures))
# with open("/data1/yangzhenbang_new/blip/BLIP/annotation/flickr_train_pictures.json", 'w', encoding='utf-8') as fw:
# json.dump(flickr_train_pictures, fw, indent=4)
#
# with open(os.path.join(ann_root, 'flickr30k_test.json'),'r',encoding='utf8')as fp:
# flickr_test = json.load(fp)
#
# flickr_test_pictures=[]
# for i in tqdm(flickr_test):
# flickr_test_pictures.append(i['image'])
#
# flickr_test_pictures = list(set(flickr_test_pictures))
# with open("/data1/yangzhenbang_new/blip/BLIP/annotation/flickr_test_pictures.json", 'w', encoding='utf-8') as fw:
# json.dump(flickr_test_pictures, fw, indent=4)
#
#
# with open(os.path.join(ann_root, 'flickr30k_val.json'),'r',encoding='utf8')as fp:
# flickr_val = json.load(fp)
#
# flickr_val_pictures=[]
# for i in tqdm(flickr_val):
# flickr_val_pictures.append(i['image'])
#
# flickr_val_pictures = list(set(flickr_val_pictures))
# with open("/data1/yangzhenbang_new/blip/BLIP/annotation/flickr_val_pictures.json", 'w', encoding='utf-8') as fw:
# json.dump(flickr_val_pictures, fw, indent=4)