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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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
from torch.utils.data import DataLoader, Dataset, Sampler
from pathlib import Path
import json
from multiprocessing import Pool
from tqdm import tqdm
from PIL import Image
import random
import numpy as np
import torch
import torchvision
import torchvision.transforms as T

from torch.utils.data.distributed import DistributedSampler

from transformers import T5Tokenizer, BertTokenizer, BertTokenizerFast, CLIPTokenizer

import text_utils

project_dir = Path(__file__).parent.resolve()
workspace_dir = project_dir.parent.parent
dataset_dir = workspace_dir.joinpath('datasets/').resolve()
# coco_dir = dataset_dir.joinpath('COCO')
# vg_dir = dataset_dir.joinpath('VG')
coco_img_dir = dataset_dir.joinpath('COCO/images/')
coco_data_dir = project_dir.parent.joinpath('CLIP-ViL/CLIP-ViL-Direct/caption/data/')
# coco_feature_dir = coco_dir.joinpath('features')


class COCORetrievalDataset(Dataset):
    def __init__(self, split='karpathy_train', rank=-1, topk=-1, verbose=True, args=None, mode='train'):
        super().__init__()

        self.topk = topk
        self.verbose = verbose
        self.args = args
        self.rank = rank
        self.mode = mode

        # Loading datasets to data
        self.source = split
        if self.verbose:
            print('Data source: ', self.source)

        # if self.args.tokenizer is None:
        #     self.args.tokenizer = self.args.decoder_backbone

        # if 'bert' in self.args.tokenizer:
        #     self.tokenizer = BertTokenizerFast.from_pretrained(
        #         self.args.tokenizer,
        #         # max_length=self.args.max_text_length,
        #         # do_lower_case=self.args.do_lower_case
        #         )
        # elif 'clip' in self.args.tokenizer:
        #     self.tokenizer = CLIPTokenizer.from_pretrained(
        #         self.args.tokenizer,
        #         # max_length=self.args.max_text_length,
        #         # do_lower_case=self.args.do_lower_case
        #         )

        self.tokenizer = CLIPTokenizer.from_pretrained(
                self.args.tokenizer,
                # max_length=self.args.max_text_length,
                # do_lower_case=self.args.do_lower_case
                )

        with open(coco_data_dir.joinpath('cocotalk.json')) as f:
            self.vocab = list(json.load(f)['ix_to_word'].values())
            popped = self.vocab.pop(-1)
            assert popped == 'UNK'
            if self.verbose:
                print('vocab size: ', len(self.vocab))


        data_info_path = coco_data_dir.joinpath('dataset_coco.json')
        with open(data_info_path) as f:
            karpathy_data = json.load(f)

        split_rename = {
            'train': 'train',
            'restval': 'train',
            'val': 'val',
            'test': 'test'
        }

        n_images = 0

        data = []
        # self.vocab = set()
        for datum in karpathy_data['images']:
            re_split = split_rename[datum['split']]

            # if re_split == 'train':
            #     for d in datum['sentences']:
            #         self.vocab = self.vocab.union(set(d['tokens']))

            if re_split != self.source.split('_')[-1]:
                continue

            if re_split == 'train':
                # for d in datum['sentences']:
                #     img_id = datum['filename'].split('.')[0]
                #     new_datum = {
                #         'filename': datum['filename'],
                #         'img_id': img_id,
                #         'sent': d['raw'].strip(),
                #         'targets': [d['raw'].strip() for d in datum['sentences']],
                #         'is_train': True,
                #         'cocoid': datum['cocoid']
                #     }
                #     data.append(new_datum)
                img_id = datum['filename'].split('.')[0]
                new_datum = {
                    'filename': datum['filename'],
                    'img_id': img_id,
                    # 'sent': d['raw'],
                    # 'targets': [d['raw'].strip() for d in datum['sentences']],
                    'targets': [" ".join(d['tokens']) for d in datum['sentences']],
                    'is_train': True,
                    'cocoid': datum['cocoid']
                }
                data.append(new_datum)

            else:
                img_id = datum['filename'].split('.')[0]
                new_datum = {
                    'filename': datum['filename'],
                    'img_id': img_id,
                    # 'sent': d['raw'],
                    # 'targets': [d['raw'].strip() for d in datum['sentences']],
                    'targets': [" ".join(d['tokens']) for d in datum['sentences']],
                    'is_train': False,
                    'cocoid': datum['cocoid']
                }
                data.append(new_datum)

            n_images += 1

        if self.verbose:
            print(f"{self.source} has {n_images} images")
            # print(f"Loaded {len(data)} data from", split)

        self.n_gpus = torch.cuda.device_count()

        if self.topk > 0:
            data = data[:self.topk]
            if self.verbose:
                print(f"Use only {self.topk} data")

        self.data = data

        # if self.verbose:
        #     print("# all sentences:", len(self.data))

        if self.args.load_feat:
            # feat_dir = coco_dir.joinpath(''
            # self.feat_loader = HybridLoader('/scratch-space/CLIP-ViL/CLIP-ViL-Direct/caption/data/cocotalk_clipscore_vis', ext='.npy', in_memory=False)
            self.feat_loader = HybridLoader(
                coco_data_dir.joinpath('cocotalk_clipscore_vis'),
                ext='.npy', in_memory=False)
        else:
            if 'openai/clip' in self.args.encoder_backbone:
                # from transformers import CLIPProcessor
                # self.processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32",
                #     size=args.image_size,
                #     do_resize=True,
                #     do_center_crop=False,
                # )
                # self.img_transform = lambda image: self.processor.feature_extractor(
                #     image,
                #     return_tensors='pt')['pixel_values'][0]

                self.image_mean = [0.48145466, 0.4578275, 0.40821073]
                self.image_std = [0.26862954, 0.26130258, 0.27577711]

                # captioning
                # self.img_transform = T.Compose([
                #     T.Resize((self.args.image_size, self.args.image_size))
                # ])

                # retrieval
                self.img_transform = T.Compose([
                    T.Resize(self.args.image_size, interpolation=T.functional.InterpolationMode.BICUBIC),
                    T.CenterCrop(self.args.image_size)
                ])

                self.img_tensor_transform = T.Compose([
                    # T.RandomCrop(224),
                    # T.RandomHorizontalFlip(p=0.3),
                    T.ConvertImageDtype(torch.float),
                    T.Normalize(self.image_mean, self.image_std)
                ]
                )
            # elif 'google/vit' in self.args.encoder_backbone:
            #     self.image_mean = [0.5, 0.5, 0.5]
            #     self.image_std = [0.5, 0.5, 0.5]

            #     self.img_transform = T.Compose([
            #         # T.PILToTensor(),
            #         T.Resize((self.args.image_size, self.args.image_size))
            #     ])

            #     self.img_tensor_transform = T.Compose([
            #         # T.RandomCrop(224),
            #         # T.RandomHorizontalFlip(p=0.3),
            #         T.ConvertImageDtype(torch.float),
            #         T.Normalize(self.image_mean, self.image_std)
            #     ]
            #     )

    def get_negative_text(self, text):
        neg_type = random.choice(['repeat', 'remove', 'insert', 'swap', 'shuffle'])

        if neg_type == 'repeat':
            text = text_utils.repeat(text)
        elif neg_type == 'remove':
            text = text_utils.remove(text)
        elif neg_type == 'insert':
            text = text_utils.insert(text, self.vocab)
        elif neg_type == 'swap':
            text = text_utils.swap(text, self.vocab)
        elif neg_type == 'shuffle':
            text = text_utils.shuffle(text)

        return text, neg_type

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        datum = self.data[idx]
        return self.process_datum(datum)

    def process_datum(self, datum):
        out_dict = {}

        ###### Image ######

        if self.args.load_feat:
            cocoid = datum['cocoid']
            out_dict['cocoid'] = str(cocoid)
            img_feat = self.feat_loader.get(str(cocoid))
            out_dict['img_feat'] = torch.from_numpy(img_feat)

        else:
            img_id = datum['img_id']
            out_dict['img_id'] = img_id

            if 'train' in datum['filename']:
                img_split = 'train2014'
            elif 'val' in datum['filename']:
                img_split = 'val2014'
            img_path = coco_img_dir.joinpath(img_split).joinpath(datum['filename']).with_suffix('.jpg')
            assert img_path.exists()
            img_path = str(img_path)
            out_dict['img_path'] = img_path

            img_tensor = torchvision.io.read_image(img_path)
            # out_dict['img_tensor'] = img

            # img = Image.open(img_path).convert('RGB')
            # img_tensor = torch.as_tensor(np.asarray(img))
            out_dict['img_tensor'] = self.img_transform(img_tensor)
            # self.img_transform(img_tensor)
            # out_dict['img_tensor'] = self.img_transform(img)

        ###### Text #####
        # if datum['is_train']:
        # sent = datum['sent'].strip()

        sent = random.choice(datum['targets'])
        
        # target_ids = self.tokenizer.encode(
        #     sent, max_length=self.args.gen_max_length, truncation=True)

        # assert len(target_ids) <= self.args.gen_max_length, len(target_ids)
        out_dict['sent'] = sent
        # out_dict['target_ids'] = torch.LongTensor(target_ids)
        # out_dict['target_length'] = len(target_ids)


        # negative sample
        neg_sent, neg_type = self.get_negative_text(sent)

        # neg_target_ids = self.tokenizer.encode(
        #     neg_sent, max_length=self.args.gen_max_length, truncation=True)

        # assert len(neg_target_ids) <= self.args.gen_max_length, len(neg_target_ids)
        out_dict['neg_sent'] = neg_sent
        out_dict['neg_type'] = neg_type
        # out_dict['neg_target_ids'] = torch.LongTensor(neg_target_ids)
        # out_dict['neg_target_length'] = len(neg_target_ids)


        if 'targets' in datum:
            out_dict['targets'] = datum['targets']

        return out_dict

    def collate_fn(self, batch):
        batch_entry = {}

        B = len(batch)

        # if 'target_ids' in batch[0]:
        #     T_W_L = max(entry['target_length'] for entry in batch)
        #     target_ids = torch.ones(
        #         B, T_W_L, dtype=torch.long) * self.tokenizer.pad_token_id

        # if 'target_ids' in batch[0]:
        #     T_W_L = max(entry['target_length'] for entry in batch)
        #     target_ids = torch.ones(
        #         B, T_W_L, dtype=torch.long) * self.tokenizer.pad_token_id



        targets = []
        img_ids = []
        img_paths = []

        coco_ids = []

        if self.args.load_feat:
            img_feats = torch.zeros(B, 512, dtype=torch.float)
        else:
            # imgs = []
            img_tensor = torch.zeros(B, 3, self.args.image_size, self.args.image_size, dtype=torch.uint8)

        for i, entry in enumerate(batch):

            if self.args.load_feat:
                coco_ids.append(entry['cocoid'])
                img_feats[i] = entry['img_feat']

            else:

                img_ids.append(entry['img_id'])
                img_paths.append(entry['img_path'])
                img_tensor[i] = entry['img_tensor']

            # if 'target_ids' in entry:
            #     target_ids[i, :entry['target_length']] = entry['target_ids']

            if 'targets' in entry:
                targets.append(entry['targets'])

        if 'sent' in batch[0]:
            # word_mask = target_ids != self.tokenizer.pad_token_id
            # target_ids[~word_mask] = -100
            # batch_entry['target_ids'] = target_ids

            tokenized = self.tokenizer([entry['sent'] for entry in batch], truncation=True, padding=True, return_tensors='pt')
            neg_tokenized = self.tokenizer([entry['neg_sent'] for entry in batch], truncation=True, padding=True, return_tensors='pt')
                #     sent, max_length=self.args.gen_max_length, truncation=True)

            batch_entry['text'] = (tokenized.input_ids, tokenized.attention_mask)
            batch_entry['neg_text'] = (neg_tokenized.input_ids, neg_tokenized.attention_mask)


        if self.args.load_feat:
            batch_entry['coco_ids'] = coco_ids
            batch_entry['img_feats'] = img_feats

        else:

            img_tensor = self.img_tensor_transform(img_tensor)

            batch_entry['img_id'] = img_ids
            batch_entry['img_paths'] = img_paths
            batch_entry['img_tensor'] = img_tensor

        batch_entry['targets'] = targets

        # print('batch created')

        # batch_entry['task'] = 'caption'

        return batch_entry


# def get_loader(args, split='karpathy_train', mode='train',
#                batch_size=32, workers=4, distributed=False, gpu=0,
#                topk=-1):

#     verbose = (gpu == 0)

#     dataset = COCORetrievalDataset(
#         split,
#         rank=gpu,
#         topk=topk,
#         verbose=verbose,
#         args=args,
#         mode=mode)

#     # if distributed:
#     #     sampler = DistributedSampler(dataset)
#     # else:
#     #     sampler = None

#     if mode == 'train':
#         loader = DataLoader(
#             dataset, batch_size=batch_size, shuffle=(sampler is None),
#             num_workers=workers, pin_memory=True, sampler=sampler,
#             collate_fn=dataset.collate_fn)
#     else:
#         loader = DataLoader(
#             dataset,
#             batch_size=batch_size, shuffle=False,
#             num_workers=workers, pin_memory=True,
#             sampler=sampler,
#             collate_fn=dataset.collate_fn,
#             drop_last=False)

#     # if verbose:
#         # loader.evaluator = COCOCaptionEvaluator()

#     # loader.task = 'caption'

#     return loader


# class COCOCaptionEvaluator:
#     def __init__(self):
#         import language_evaluation
#         self.evaluator = language_evaluation.CocoEvaluator(verbose=False)

#     def evaluate(self, predicts, answers):

#         results = self.evaluator.run_evaluation(predicts, answers)

#         return results

import six
import os
import h5py

class HybridLoader:
    """
    If db_path is a director, then use normal file loading
    If lmdb, then load from lmdb
    The loading method depend on extention.

    in_memory: if in_memory is True, we save all the features in memory
               For individual np(y|z)s, we don't need to do that because the system will do this for us.
               Should be useful for lmdb or h5.
               (Copied this idea from vilbert)
    """

    def __init__(self, db_path, ext='.npy', in_memory=False):
        self.db_path = db_path
        self.ext = ext
        if self.ext == '.npy':
            self.loader = lambda x: np.load(six.BytesIO(x))
        else:
            self.loader = lambda x: np.load(six.BytesIO(x))['feat']
        # if db_path.endswith('.lmdb'):
        #     self.db_type = 'lmdb'
        #     self.lmdb = lmdbdict(db_path, unsafe=True)
        #     self.lmdb._key_dumps = DUMPS_FUNC['ascii']
        #     self.lmdb._value_loads = LOADS_FUNC['identity']
        # elif db_path.endswith('.pth'):  # Assume a key,value dictionary
        #     self.db_type = 'pth'
        #     self.feat_file = torch.load(db_path)
        #     self.loader = lambda x: x
        #     print('HybridLoader: ext is ignored')
        # elif db_path.endswith('h5'):
        #     self.db_type = 'h5'
        #     self.loader = lambda x: np.array(x).astype('float32')
        # else:
        #     self.db_type = 'dir'

        self.in_memory = in_memory
        if self.in_memory:
            self.features = {}

    def get(self, key):

        # if self.in_memory and key in self.features:
        #     # We save f_input because we want to save the
        #     # compressed bytes to save memory
        #     f_input = self.features[key]
        # elif self.db_type == 'lmdb':
        #     f_input = self.lmdb[key]
        # elif self.db_type == 'pth':
        #     f_input = self.feat_file[key]
        # elif self.db_type == 'h5':
        #     f_input = h5py.File(self.db_path, 'r')[key]
        # else:
            # f_input = open(os.path.join(
            #     self.db_path, key + self.ext), 'rb').read()

        f_input = open(os.path.join(
            self.db_path, key + self.ext), 'rb').read()

        if self.in_memory and key not in self.features:
            self.features[key] = f_input

        # load image
        feat = self.loader(f_input)

        return feat