medical
File size: 11,754 Bytes
5ceacbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
from io import BytesIO
import json
import logging
import base64
import random
from typing import Callable, List, Tuple, Union, NamedTuple
from PIL import Image
from PIL import ImageFile
import torch.utils.data as data
from .languages.prompt_engineering import prompt_engineering
from .tsv_file import TSVFile, CompositeTSVFile

ImageFile.LOAD_TRUNCATED_IMAGES = True

logger = logging.getLogger(__name__)


class TSVDataset(data.Dataset):

    def __init__(self,
                 tsv_file: Union[str, List[str]],
                 transform: Callable = None,
                 map_file: str = None,
                 token_file: str = None,
                 is_train: bool = True,
                 azcopy_path: str = None):
        self.transform = transform
        self._chunk_sizes = None
        self.label2idx = self._load_map(map_file)
        self.class_selector = list(self.label2idx.keys()) if self.label2idx else None

        if isinstance(tsv_file, str):
            if os.path.splitext(tsv_file)[1] == '.tsv':
                self.tsv_file = TSVFile(
                    tsv_file, class_selector=self.class_selector
                )
            else:
                self.tsv_file = CompositeTSVFile(
                    tsv_file,
                    class_selector=self.class_selector,
                    is_train=is_train,
                    sas_token_path=token_file,
                    azcopy_path=azcopy_path
                )
                self._chunk_sizes = self.tsv_file.get_chunk_size()
        elif isinstance(tsv_file, list):
            self.tsv_file = CompositeTSVFile(
                tsv_file,
                class_selector=self.class_selector,
                is_train=is_train,
                sas_token_path=token_file,
                azcopy_path=azcopy_path
            )
            self._chunk_sizes = self.tsv_file.get_chunk_size()
        else:
            raise ValueError("Invalid input! Please check the tsv filenames")

        logger.debug('=> {}\titems: {}'.format(tsv_file, len(self.tsv_file)))

    def fetch_blob(self, idx):
        image_tsv = self.tsv_file.file_list[idx]
        self.tsv_file.blob_storage.fetch_blob(image_tsv)

    def num_classes(self):
        return len(self.class_selector)

    def get_chunk_sizes(self):
        return self._chunk_sizes

    def get_class_boundaries(self):
        # The samples of each class are organized class-by-class.
        # _class_boundaries stores the lower- and upper-bound of each class.
        return self.tsv_file.get_class_boundaries()

    def get_filenames(self):
        filenames = [
            self.tsv_file.get_key(i)
            for i in range(self.tsv_file.num_rows())
        ]

        return filenames

    def _load_map(self, map_file: str):
        if not map_file:
            return None

        label2idx = {}
        with open(map_file) as f:
            for line in f:
                items = line.strip().split('\t')
                label2idx[items[0]] = int(items[1])

        return label2idx

    def __getitem__(self, index: Union[int, Tuple[int, int]]):
        items = self.tsv_file[index]
        _, target, img = self._decode_data(items)

        if self.transform:
            img = self.transform(img)

        return img, target

    def _decode_data(self, items: Tuple[str, str, str]):
        key = items[0]
        label = self._get_label(items[1])
        image = Image.open(BytesIO(base64.b64decode(items[2]))).convert('RGB')

        return key, label, image

    def _get_label(self, item: str):
        if not self.label2idx:
            return int(item)

        js = json.loads(item)
        return self.label2idx[js[0]['class']]

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


class TSVMeta(NamedTuple):
    source: str
    num_classes: int
    task: str


class TSVImageTextDatasetV2(data.Dataset):
    """
        This class is intended for encapsulating Image/Text pair data for contrastive learning described in
        the following paper,
        "Learning Transferable Visual Models From Natural Language Supervision" (a.k.a CLIP)
        V2: support image text pairs and supervised classification data
    """

    def __init__(self,
                 image_tsv_file: Union[str, List[str]],
                 text_tsv_file: Union[str, List[str]],
                 transform: Callable = None,
                 tokenize: Callable = None,
                 context_length: int = 77,
                 num_captions: int = 1,
                 text_format: str = 'txt',
                 is_train: bool = True,
                 sas_token_path: str = None,
                 azcopy_path: str = None,
                 metas: List[NamedTuple] = None,
                 prompt_engineering=True,
                 concat_queries=False):
        self.transform = transform
        self.tokenize = tokenize
        self._chunk_sizes = None
        self.context_length = context_length
        self.num_captions = num_captions
        self.text_format = text_format
        self.tsv_file_list = []
        self.metas = metas
        self.label_offsets = self.build_label_offsets()
        self.prompt_engineering = prompt_engineering
        self.concat_queries = concat_queries

        if isinstance(image_tsv_file, str) and isinstance(text_tsv_file, str):
            # single tsv file
            if (
                    os.path.splitext(image_tsv_file)[1].lower() == '.tsv'
                    and os.path.splitext(text_tsv_file)[1].lower() == '.tsv'
            ):
                self.tsv_file_list.append((image_tsv_file, text_tsv_file))
                self.image_tsv_file = TSVFile(
                    image_tsv_file, if_generate_lineidx=True
                )
                self.text_tsv_file = TSVFile(
                    text_tsv_file, if_generate_lineidx=True
                )
            else:
                raise ValueError("Invalid input! Please check the tsv filenames.")
        # multiple tsv files specified in a list
        elif (
                isinstance(image_tsv_file, list)
                and isinstance(text_tsv_file, list)
        ):
            assert len(image_tsv_file) == len(text_tsv_file), \
                "Inconsistent number of Image/Text tsv files!"
            self.tsv_file_list = [
                (txt, img)
                for img, txt in zip(image_tsv_file, text_tsv_file)
            ]
            self.image_tsv_file = CompositeTSVFile(
                image_tsv_file,
                is_train=is_train,
                sas_token_path=sas_token_path,
                azcopy_path=azcopy_path
            )
            self.text_tsv_file = CompositeTSVFile(
                text_tsv_file,
                is_train=is_train,
                sas_token_path=sas_token_path,
                azcopy_path=azcopy_path
            )
            self._chunk_sizes = self.image_tsv_file.get_chunk_size()
        else:
            raise ValueError("Invalid input! Please check the tsv filenames.")

        assert len(self.image_tsv_file) == len(self.text_tsv_file), \
            "Inconsistent size of Image/Text ({}/{}) data!".format(
                len(self.image_tsv_file), len(self.text_tsv_file)
            )

    def build_label_offsets(self):
        if self.metas is None:
            return None

        label_offsets = {}
        offset = 1
        for meta in self.metas:
            print(meta)
            print(label_offsets)
            label_offsets[meta.source] = offset
            offset += meta.num_classes

        return label_offsets

    def fetch_blob(self, idx):
        # image_tsv, text_tsv = self.tsv_file_list[idx]
        image_tsv = self.image_tsv_file.file_list[idx]
        text_tsv = self.text_tsv_file.file_list[idx]
        self.image_tsv_file.blob_storage.fetch_blob(image_tsv)
        self.text_tsv_file.blob_storage.fetch_blob(text_tsv)

    def get_chunk_sizes(self):
        return self._chunk_sizes

    def __getitem__(self, index: Union[int, Tuple[int, int]]):
        if index is None:
            import torch
            return torch.tensor([], dtype=torch.float32), \
                torch.tensor([], dtype=torch.int64), \
                torch.tensor([], dtype=torch.int64)

        items_image = self.image_tsv_file[index]
        items_text = self.text_tsv_file[index]

        assert items_text[0] == items_image[0], \
            'keys do not match for image and text {} vs {}'.format(
                items_text[0], items_image[0]
            )

        _, img = self._decode_image(items_image)
        _, txt, label = self._decode_text(items_text)

        if self.transform:
            img = self.transform(img)

        tokens = self.tokenize(
            txt, padding='max_length', truncation=True, max_length=self.context_length,
            return_tensors='pt'
        ) if self.tokenize else txt

        tokens['input_ids'].squeeze_()
        tokens['attention_mask'].squeeze_()

        return img, tokens, label

    def _decode_image(self, items: Tuple[str, str]):
        key = items[0]
        image = Image.open(BytesIO(base64.b64decode(items[1]))).convert('RGB')

        return key, image

    def _decode_text(self, items: Tuple[str, Union[str, dict]]):
        key = items[0]
        text = ''

        if self.text_format != 'json':
            raise ValueError('Only support json format')

        # Do some reasonable handing of occasionally bad data.
        try:
            js = json.loads(items[1])
        except Exception as e:

            # empty dictionary
            js = {}

            # Record the data error in the log.
            logger.info("JSON parsing error on: " + items[1])
            logger.info(str(e))

            # do not raise the exception
            # raise e

            # put some text in and continue processing data (do not kill job)
            sstr = items[1].find("\"")
            if (sstr < 0):
                sstr = 0

            estr = items[1][sstr:].find("\"")
            if (estr < 0):
                estr = len(items[1])

            text = items[1][sstr:estr]
            if (len(text) < 2):
                text = "A picture showing some content."

            label = 0

        if 'captions' in js:
            captions = js['captions']
            if isinstance(captions, list):
                if self.num_captions == 1:
                    text = random.choice(captions)
                else:
                    text = captions
                    if len(captions) > self.num_captions:
                        text = captions[:self.num_captions]
            elif isinstance(captions, str):
                text = captions
            else:
                raise ValueError('captions should be str or list')
            label = 0
        elif 'tags' in js:
            text = prompt_engineering(js['tags'])
            label = 0
        elif 'task' in js and js['task'] == 'classification':
            if (self.prompt_engineering):
                text = prompt_engineering(js['class_name'])
            else:
                text = js['class_name']
            label = js['class_id']

            if (self.label_offsets is not None):
                if (js['source'] in self.label_offsets):
                    label += self.label_offsets[js['source']]

        if (self.concat_queries):
            if ('queries' in js) and (len(js['queries']) > 0):
                q = ''
                for item in js['queries']:
                    q = q + item + ' '

                text = q + ', ' + text

        return key, text, label

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