-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathclip_transform.py
More file actions
416 lines (364 loc) · 15.5 KB
/
clip_transform.py
File metadata and controls
416 lines (364 loc) · 15.5 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
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
import html
from functools import lru_cache
from typing import Callable, Dict, List, Optional, Set, Tuple, Union
import ftfy
import regex as re
import torch
from PIL.Image import Image
from torch import nn, Tensor
from torchmultimodal import _PATH_MANAGER
from torchmultimodal.transforms import text_transforms
from torchvision import transforms as image_transforms
from torchvision.transforms import InterpolationMode
CLIP_DEFAULT_MEAN = (0.48145466, 0.4578275, 0.40821073)
CLIP_DEFAULT_STD = (0.26862954, 0.26130258, 0.27577711)
CLIP_DEFAULT_VOCAB_BPE_PATH = "http://download.pytorch.org/models/text/clip_merges.bpe"
def convert_to_rgb(img: Image) -> Image:
return img.convert("RGB")
@lru_cache()
def bytes_to_unicode() -> Dict[int, str]:
"""
Returns list of utf-8 byte and a corresponding list of unicode strings.
The reversible bpe codes work on unicode strings.
This means you need a large # of unicode characters in your vocab if you want to avoid UNKs.
When you're at something like a 10B token dataset you end up needing around 5K for decent coverage.
This is a signficant percentage of your normal, say, 32K bpe vocab.
To avoid that, we want lookup tables between utf-8 bytes and unicode strings.
And avoids mapping to whitespace/control characters the bpe code barfs on.
"""
bs = (
list(range(ord("!"), ord("~") + 1))
+ list(range(ord("¡"), ord("¬") + 1))
+ list(range(ord("®"), ord("ÿ") + 1))
)
cs = bs[:]
n = 0
for b in range(2**8):
if b not in bs:
bs.append(b)
cs.append(2**8 + n)
n += 1
cs = [chr(n) for n in cs]
return dict(zip(bs, cs)) # noqa
def get_pairs(word: Tuple[str, ...]) -> Set[Tuple[str, str]]:
"""
Return set of symbol pairs in a word.
Word is represented as tuple of symbols (symbols being variable-length strings).
"""
pairs = set()
prev_char = word[0]
for char in word[1:]:
pairs.add((prev_char, char))
prev_char = char
return pairs
def basic_clean(text: str) -> str:
text = ftfy.fix_text(text)
text = html.unescape(html.unescape(text))
return text.strip()
def whitespace_clean(text: str) -> str:
text = re.sub(r"\s+", " ", text)
text = text.strip()
return text
class CLIPBPETokenizer:
"""
Construct a CLIP tokenizer. Based on byte-level Byte-Pair-Encoding.
This implementation is adapted from https://git.io/JDTuJ.
Example usage:
tokenizer = CLIPBPETokenizer()
sentence = "Hello I am using CLIP tokenizer."
tokens = tokenizer.encode(sentence)
tokens -> [3306, 328, 687, 1996, 9289, 32634, 23895, 269]
decoded_sentence = tokenizer.decode(tokens)
decoded_sentence -> "hello i am using clip tokenizer ."
Args:
bpe_path (str): path to the BPE file
bos_token (str): beginning of sentence token. Defaults to "<|startoftext|>".
eos_token (str): end of sentence token. Defaults to "<|endoftext|>".
num_merges (Optional[int]): number of merges.
If None, it will load all merges from the BPE file.
"""
def __init__(
self,
bpe_path: str = CLIP_DEFAULT_VOCAB_BPE_PATH,
bos_token: str = "<|startoftext|>",
eos_token: str = "<|endoftext|>",
num_merges: Optional[int] = None, # TODO: add docstring
):
self._separator = "\u0001"
self.byte_encoder = bytes_to_unicode()
self.byte_decoder = {v: k for k, v in self.byte_encoder.items()}
with _PATH_MANAGER.open(bpe_path, "r", encoding="utf-8") as f:
bpe_merges = f.read().split("\n")[1:]
num_merges = num_merges or len(bpe_merges)
bpe_merges = bpe_merges[:num_merges]
self.bpe_merges = bpe_merges[:num_merges]
bpe_merges = [tuple(merge.split()) for merge in bpe_merges]
self.num_merges = num_merges
self.bpe_ranks = dict(zip(bpe_merges, range(len(bpe_merges)))) # noqa
bpe_vocab = list(bytes_to_unicode().values())
bpe_vocab = bpe_vocab + [v + "</w>" for v in bpe_vocab]
bpe_vocab.extend(
["".join(merge_pair) for merge_pair in bpe_merges[:num_merges]]
)
special_tokens = [bos_token, eos_token]
bpe_vocab.extend(special_tokens)
self.bpe_vocab = bpe_vocab
self.encoder = {v: i for i, v in enumerate(bpe_vocab)}
self.decoder = {v: k for k, v in self.encoder.items()}
self.cache = {tok: tok for tok in special_tokens}
self.pat = re.compile(
r"""<\|startoftext\|>|<\|endoftext\|>|'s|'t|'re|'ve|'m|'ll|'d|[\p{L}]+|[\p{N}]|[^\s\p{L}\p{N}]+""",
re.IGNORECASE,
)
@property
def vocab_size(self) -> int:
return len(self.encoder)
def bpe(self, token: str) -> str:
if token in self.cache:
return self.cache[token]
word = tuple(token[:-1]) + (token[-1] + "</w>",)
pairs = get_pairs(word)
if not pairs:
return token + "</w>"
while True:
bigram = min(pairs, key=lambda pair: self.bpe_ranks.get(pair, float("inf")))
if bigram not in self.bpe_ranks:
break
first, second = bigram
new_word: List[str] = []
i = 0
while i < len(word):
try:
j = word.index(first, i)
except ValueError:
new_word.extend(word[i:])
break
else:
new_word.extend(word[i:j])
i = j
if word[i] == first and i < len(word) - 1 and word[i + 1] == second:
new_word.append(first + second)
i += 2
else:
new_word.append(word[i])
i += 1
new_word = tuple(new_word)
word = new_word
if len(word) == 1:
break
else:
pairs = get_pairs(word)
word = " ".join(word)
self.cache[token] = word
return word
def encode(self, text: str) -> List[int]:
bpe_tokens: List[int] = []
text = text.lower().strip()
for token in re.findall(self.pat, text):
token = "".join(self.byte_encoder[b] for b in token.encode("utf-8"))
bpe_tokens.extend(
self.encoder[bpe_token] for bpe_token in self.bpe(token).split(" ")
)
return bpe_tokens
def decode(self, tokens: List[int]) -> str:
text = "".join([self.decoder[token] for token in tokens])
text = (
bytearray([self.byte_decoder[c] for c in text])
.decode("utf-8", errors="replace")
.replace("</w>", " ")
)
return text
class CLIPBPETransform(nn.Module):
"""
nn.Module wrapper around CLIPBPETokenizer. Supports either a single string
or list of strings for tokenization.
Args:
bpe_path (Optional[str]): path to the BPE file.
Defaults to CLIP_DEFAULT_VOCAB_BPE_PATH
bos_token (Optional[str]): beginning of sentence token.
Defaults to "<|startoftext|>".
eos_token (Optional[str]): end of sentence token.
Defaults to "<|endoftext|>".
num_merges (Optional[int]): number of merges.
If None, it will load all merges from the BPE file.
"""
def __init__(
self,
bpe_path: Optional[str] = CLIP_DEFAULT_VOCAB_BPE_PATH,
bos_token: Optional[str] = "<|startoftext|>",
eos_token: Optional[str] = "<|endoftext|>",
num_merges: Optional[int] = None,
):
super().__init__()
self.bpe = CLIPBPETokenizer(
bpe_path=bpe_path,
bos_token=bos_token,
eos_token=eos_token,
num_merges=num_merges,
)
def forward(self, text: Union[str, List[str]]) -> Union[List[int], List[List[int]]]:
if isinstance(text, str):
return self.bpe.encode(text)
else:
return [self.bpe.encode(t) for t in text]
class CLIPTextTransform(nn.Module):
"""CLIP text transform
CLIP BPE tokenizer transform, adds start and end tokens, then pads/truncates to text_max_length as necessary.
This transform is torch scriptable.
Args:
text_max_length (int): Maximum length of text token sequences.
text_start_token (str): Special start token passed to BPE tokenizer.
text_end_token (str): Special end token passed to BPE tokenizer.
text_pad_token (str): Special pad token to insert pad the sequence to text_max_length.
text_bpe_merges_path (str): Location of BPE merges file for text transform.
num_merges (int, optional): Number of merges to use from BPE merges file.
Default: 48894 = 49152 (vocab size) - 256 (# bytes) - 2 (bos/eos tokens)
Inputs:
text (Union[List[str],str]): Text or batch of texts upon which to apply
the transform.
"""
def __init__(
self,
text_max_length: int = 77,
text_start_token: str = "<|startoftext|>",
text_end_token: str = "<|endoftext|>",
text_pad_token: str = None,
text_bpe_merges_path: str = CLIP_DEFAULT_VOCAB_BPE_PATH,
num_merges: Optional[int] = 48894,
) -> None:
super().__init__()
local_merges_path = _PATH_MANAGER.get_local_path(text_bpe_merges_path)
tokenizer = CLIPBPETransform(
local_merges_path, text_start_token, text_end_token, num_merges
)
text_start_token = tokenizer([text_start_token])[0][0]
text_end_token = tokenizer([text_end_token])[0][0]
text_pad_token_id = 0
if text_pad_token is not None:
text_pad_token_id = tokenizer([text_pad_token])[0][0]
text_max_length = text_max_length
self.text_transform = nn.Sequential(
*[
tokenizer,
text_transforms.Truncate(text_max_length - 2),
text_transforms.AddToken(text_start_token, begin=True),
text_transforms.AddToken(text_end_token, begin=False),
text_transforms.ToTensor(padding_value=0),
text_transforms.PadTransform(
max_length=text_max_length, pad_value=text_pad_token_id
),
]
)
def forward(self, text: Union[List[str], str]) -> Tensor:
text_result = self.text_transform(text)
assert torch.jit.isinstance(text_result, Tensor)
return text_result
class CLIPImageTransform(nn.Module):
"""CLIP image transform
random resized crop (train mode) or resize and center crop, followed by RGB conversion, tensor conversion, and normalization.
Args:
image_size (Union[int, Tuple[int]): desired output image size.
image_interpolation (torchvision.transforms.InterpolationMode):
Torchvision interpolation mode used during resizing. Defaults to bicubic.
image_mean (Tuple[float]): mean of images, used for normalization.
image_std (Tuple[float]): std of images, used for normalization.
is_train (bool): Whether transform is run in train mode.
Inputs:
image (Union[List[Image], Image]): Image or batch of images upon which
to apply the transform.
"""
def __init__(
self,
image_size: Union[int, Tuple[int, int]] = 224,
image_interpolation: InterpolationMode = InterpolationMode.BICUBIC,
image_mean: Tuple[float, float, float] = CLIP_DEFAULT_MEAN,
image_std: Tuple[float, float, float] = CLIP_DEFAULT_STD,
is_train: bool = True,
) -> None:
super().__init__()
joint_transforms: List[Callable] = [
convert_to_rgb,
image_transforms.ToTensor(),
image_transforms.Normalize(image_mean, image_std),
]
base_transform: List[Callable]
if is_train:
base_transform = [
image_transforms.RandomResizedCrop(
image_size, interpolation=image_interpolation
)
]
else:
base_transform = [
image_transforms.Resize(image_size, interpolation=image_interpolation),
image_transforms.CenterCrop(image_size),
]
self.image_transform = image_transforms.Compose(
base_transform + joint_transforms
)
def forward(self, image: Union[List[Image], Image]) -> Tensor:
if isinstance(image, Image):
return self.image_transform(image)
image_result = torch.stack([self.image_transform(x) for x in image])
return image_result
class CLIPTransform(nn.Module):
"""Image and text transform for CLIP model.
Image transform: either random resized crop (train mode) or resize and center
crop, followed by RGB conversion, tensor conversion, and normalization.
Text transform: applies CLIP's BPE tokenizer transform, adds start and end
tokens, then pads/truncates to text_max_length as necessary.
Args:
image_size (Union[int, Tuple[int]): desired output image size.
image_interpolation (torchvision.transforms.InterpolationMode):
Torchvision interpolation mode used during resizing. Defaults to bicubic.
image_mean (Tuple[float]): mean of images, used for normalization.
image_std (Tuple[float]): std of images, used for normalization.
text_max_length (int): Maximum length of text token sequences.
is_train (bool): Whether transform is run in train mode.
text_start_token (str): Special start token passed to BPE tokenizer.
text_end_token (str): Special end token passed to BPE tokenizer.
text_pad_token (str): Special pad token to insert pad the sequence to text_max_length.
text_bpe_merges_path (str): Location of BPE merges file for text transform.
num_merges (int, optional): Number of merges to use from BPE merges file.
Default: 48894 = 49152 (vocab size) - 256 (# bytes) - 2 (bos/eos tokens)
Inputs:
image (Union[List[Image], Image]): Image or batch of images upon which
to apply the transform.
text (Union[List[str],str]): Text or batch of texts upon which to apply
the transform.
"""
def __init__(
self,
image_size: Union[int, Tuple[int, int]] = 224,
image_interpolation: InterpolationMode = InterpolationMode.BICUBIC,
image_mean: Tuple[float, float, float] = CLIP_DEFAULT_MEAN,
image_std: Tuple[float, float, float] = CLIP_DEFAULT_STD,
text_max_length: int = 77,
is_train: bool = True,
text_start_token: str = "<|startoftext|>",
text_end_token: str = "<|endoftext|>",
text_pad_token: str = None,
text_bpe_merges_path: str = CLIP_DEFAULT_VOCAB_BPE_PATH,
num_merges: Optional[int] = 48894,
) -> None:
super().__init__()
self.image_transform = CLIPImageTransform(
image_size, image_interpolation, image_mean, image_std, is_train
)
self.text_transform = CLIPTextTransform(
text_max_length,
text_start_token,
text_end_token,
text_pad_token,
text_bpe_merges_path,
num_merges,
)
def forward(
self, image: Union[List[Image], Image], text: Union[List[str], str]
) -> Tuple[torch.Tensor, torch.Tensor]:
return self.image_transform(image), self.text_transform(text)