-
Notifications
You must be signed in to change notification settings - Fork 512
/
image_bytes.py
395 lines (334 loc) · 13.9 KB
/
image_bytes.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
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
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2024 Apple Inc. All Rights Reserved.
#
import argparse
import io
from typing import Dict, Union
import numpy as np
import torch
from PIL import Image
from corenet.data.transforms import TRANSFORMATIONS_REGISTRY, BaseTransformation
def _image_to_bytes(x: torch.Tensor, **kwargs) -> io.BytesIO:
"""
Take an image in [0, 1] and save it as file bytes using PIL.
Args:
x: an image tensor in [C, H, W] order, where C is the number of channels,
and H, W are the height and width.
kwargs: any keyword arguments that can be passed to PIL's Image.save().
Returns:
The file bytes.
"""
assert x.min() >= 0
assert x.max() <= 1
x = (x * 255).byte().permute(1, 2, 0).cpu().numpy() # Bytes in H, W, C order
img = Image.fromarray(x)
byte_array = io.BytesIO()
img.save(byte_array, **kwargs)
return byte_array
def _bytes_to_int32(byte_array: io.BytesIO) -> torch.Tensor:
"""
Convert a byte array to int32 values.
Args:
byte_array: The input byte array.
Returns:
The int32 tensor.
"""
buf = np.frombuffer(byte_array.getvalue(), dtype=np.uint8)
# The copy operation is required to avoid a warning about non-writable
# tensors.
buf = torch.from_numpy(buf.copy()).to(dtype=torch.int32)
return buf
@TRANSFORMATIONS_REGISTRY.register(name="pil_save", type="image_torch")
class PILSave(BaseTransformation):
"""
Encode an image with a supported file encoding.
"""
def __init__(self, opts: argparse.Namespace, *args, **kwargs) -> None:
super().__init__(opts=opts, *args, **kwargs)
self.file_encoding = getattr(opts, "image_augmentation.pil_save.file_encoding")
self.quality = getattr(opts, "image_augmentation.pil_save.quality")
self.opts = opts
def __call__(
self, data: Dict[str, Union[torch.Tensor, int]]
) -> Dict[str, Union[torch.Tensor, int]]:
"""
Serialize an image as file bytes.
Args:
data: A dictionary containing a key called "samples", which contains
an image tensor of shape [C, H, W].
Returns:
The transformed data.
"""
x = data["samples"]
if self.file_encoding == "fCHW":
x = (x * 255).byte().to(dtype=torch.int32).reshape(-1)
elif self.file_encoding == "fHWC":
x = (x * 255).byte().to(dtype=torch.int32).permute(1, 2, 0).reshape(-1)
elif self.file_encoding == "TIFF":
x = _bytes_to_int32(_image_to_bytes(x, format="tiff"))
elif self.file_encoding == "PNG":
x = _bytes_to_int32(_image_to_bytes(x, format="png", compress_level=0))
elif self.file_encoding == "JPEG":
quality = getattr(self.opts, "image_augmentation.pil_save.quality")
x = _bytes_to_int32(_image_to_bytes(x, format="jpeg", quality=quality))
else:
raise NotImplementedError(
f"Invalid file encoding {self.file_encoding}. Expected one of 'fCHW, fHWC, TIFF, PNG, JPEG'."
)
data["samples"] = x
return data
def __repr__(self) -> str:
return f"{self.__class__.__name__}(file_encoding={self.file_encoding}, quality={self.quality})"
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
group = parser.add_argument_group(title=cls.__name__)
group.add_argument(
"--image-augmentation.pil-save.enable",
action="store_true",
help="Use {}. This flag is useful when you want to study the effect of different "
"transforms.".format(cls.__name__),
)
group.add_argument(
"--image-augmentation.pil-save.file-encoding",
choices=("fCHW", "fHWC", "TIFF", "PNG", "JPEG"),
help="The type of file encoding to use. Defaults to TIFF.",
default="TIFF",
)
group.add_argument(
"--image-augmentation.pil-save.quality",
help="JPEG quality if using JPEG encoding. Defaults to 100.",
type=int,
default=100,
)
return parser
@TRANSFORMATIONS_REGISTRY.register(name="shuffle_bytes", type="image_torch")
class ShuffleBytes(BaseTransformation):
"""
Reorder the bytes in a 1-dimensional buffer.
"""
def __init__(self, opts: argparse.Namespace, *args, **kwargs) -> None:
super().__init__(opts=opts, *args, **kwargs)
self.mode = getattr(opts, "image_augmentation.shuffle_bytes.mode")
self.stride = getattr(opts, "image_augmentation.shuffle_bytes.stride")
window_size = getattr(opts, "image_augmentation.shuffle_bytes.window_size")
self.window_shuffle = torch.randperm(window_size)
def __call__(
self, data: Dict[str, Union[torch.Tensor, int]]
) -> Dict[str, Union[torch.Tensor, int]]:
"""
Reorder the bytes of a 1-dimensional buffer.
Args:
data: A dictionary containing a key called "samples", which contains
a tensor of shape [N], where N is the number of bytes.
Returns:
The transformed data.
"""
x = data["samples"]
if not x.dim() == 1:
raise ValueError(f"Expected 1d input, got {x.shape}")
if self.mode == "reverse":
x = torch.fliplr(x.view(1, -1))[0]
elif self.mode == "random_shuffle":
x = x[torch.randperm(x.shape[0])]
elif self.mode == "cyclic_half_length":
x = torch.roll(x, x.shape[0] // 2)
elif self.mode == "stride":
l = len(x)
values = []
for i in range(self.stride):
values.append(x[i :: self.stride])
x = torch.cat(values, dim=0)
assert len(x) == l
elif self.mode == "window_shuffle":
l = len(x)
window_size = self.window_shuffle.shape[0]
num_windows = l // window_size
values = []
for i in range(num_windows):
chunk = x[i * window_size : (i + 1) * window_size]
values.append(chunk[self.window_shuffle])
# Add the last bits that fall outside the shuffling window.
values.append(x[num_windows * window_size :])
x = torch.cat(values, dim=0)
assert len(x) == l
else:
raise NotImplementedError(
f"mode={self.mode} not implemented. Expected one of 'reverse, random_shuffle, cyclic_half_length, stride, window_shuffle'."
)
data["samples"] = x
return data
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
group = parser.add_argument_group(title=cls.__name__)
group.add_argument(
"--image-augmentation.shuffle-bytes.enable",
action="store_true",
help="Use {}. This flag is useful when you want to study the effect of different "
"transforms.".format(cls.__name__),
)
group.add_argument(
"--image-augmentation.shuffle-bytes.mode",
default="reverse",
help="The mode to use when shuffling bytes. Defaults to 'reverse'.",
choices=(
"reverse",
"random_shuffle",
"cyclic_half_length",
"stride",
"window_shuffle",
),
)
group.add_argument(
"--image-augmentation.shuffle-bytes.stride",
type=int,
default=1024,
help="The stride of the window used in shuffling operations that are windowed. Defaults to 1024.",
)
group.add_argument(
"--image-augmentation.shuffle-bytes.window-size",
type=int,
default=1024,
help="The size of the window used in shuffling operations that are windowed. Defaults to 1024.",
)
return parser
@TRANSFORMATIONS_REGISTRY.register(name="mask_positions", type="image_torch")
class MaskPositions(BaseTransformation):
"""
Mask out values in a 1-dimensional buffer using a fixed masking pattern.
"""
def __init__(self, opts: argparse.Namespace, *args, **kwargs) -> None:
super().__init__(opts=opts, *args, **kwargs)
self.keep_frac = getattr(opts, "image_augmentation.mask_positions.keep_frac")
self._cached_masks = None
def _generate_masks(self, N: int) -> torch.Tensor:
if self._cached_masks is None:
g = torch.Generator()
# We want to fix the mask across all inputs, so we fix the seed.
# Choose a seed with a good balance of 0 and 1 bits. See:
# https://pytorch.org/docs/stable/generated/torch.Generator.html#torch.Generator.manual_seed
g.manual_seed(2147483647)
random_mask = torch.zeros([N], requires_grad=False, dtype=torch.bool)
random_mask[torch.randperm(N, generator=g)[: int(self.keep_frac * N)]] = 1
self._cached_masks = random_mask
return self._cached_masks
def __call__(
self, data: Dict[str, Union[torch.Tensor, int]]
) -> Dict[str, Union[torch.Tensor, int]]:
"""
Mask values in a 1-dimensional buffer with a fixed masking pattern.
Args:
data: A dictionary containing a key called "samples", which contains
a tensor of shape [N], where N is the number of bytes.
Returns:
The transformed data.
"""
x = data["samples"]
mask = self._generate_masks(x.shape[0])
x = x[mask]
data["samples"] = x
return data
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
group = parser.add_argument_group(title=cls.__name__)
group.add_argument(
"--image-augmentation.mask-positions.enable",
action="store_true",
help="Use {}. This flag is useful when you want to study the effect of different "
"transforms.".format(cls.__name__),
)
group.add_argument(
"--image-augmentation.mask-positions.keep-frac",
type=float,
default=0.5,
help="The fraction of bytes to keep. Defaults to 0.5.",
)
return parser
@TRANSFORMATIONS_REGISTRY.register(name="byte_permutation", type="image_torch")
class BytePermutation(BaseTransformation):
"""
Remap byte values in [0, 255] to new values in [0, 255] using a permutation.
"""
def __init__(self, opts: argparse.Namespace, *args, **kwargs) -> None:
super().__init__(opts=opts, *args, **kwargs)
g = torch.Generator()
g.manual_seed(2147483647)
self.mask = torch.randperm(256, generator=g)
def __call__(
self, data: Dict[str, Union[torch.Tensor, int]]
) -> Dict[str, Union[torch.Tensor, int]]:
"""
Remap byte values in [0, 255] to new values in [0, 255] using a permutation.
Args:
data: A dictionary containing a key called "samples", which contains
a tensor of shape [N], where N is the number of bytes.
Returns:
The transformed data.
"""
x = data["samples"]
if x.dim() != 1:
raise ValueError(f"Expected 1d tensor. Got {x.shape}.")
x = torch.index_select(self.mask, dim=0, index=x)
data["samples"] = x
return data
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
group = parser.add_argument_group(title=cls.__name__)
group.add_argument(
"--image-augmentation.byte-permutation.enable",
action="store_true",
help="Use {}. This flag is useful when you want to study the effect of different "
"transforms.".format(cls.__name__),
)
return parser
@TRANSFORMATIONS_REGISTRY.register(name="random_uniform", type="image_torch")
class RandomUniformNoise(BaseTransformation):
"""
Add random uniform noise to integer values.
"""
def __init__(self, opts: argparse.Namespace, *args, **kwargs) -> None:
super().__init__(opts=opts, *args, **kwargs)
self.opts = opts
self.width_range = getattr(
opts, "image_augmentation.random_uniform.width_range"
)
def __call__(
self, data: Dict[str, Union[torch.Tensor, int]]
) -> Dict[str, Union[torch.Tensor, int]]:
"""
Add random uniform noise to byte values.
Args:
data: A dict containing a tensor in its "samples" key. The tensor
contains integers representing byte values. Integers are used
because negative padding values may be added later. The shape
of the tenor is [N], where N is the number of bytes.
Returns:
The transformed data.
"""
x = data["samples"]
noise = torch.randint_like(x, self.width_range[0], self.width_range[1] + 1)
dtype = x.dtype
x = x.int()
x = x + noise
x = x % 256
x = x.to(dtype)
data["samples"] = x
return data
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
group = parser.add_argument_group(title=cls.__name__)
group.add_argument(
"--image-augmentation.random-uniform.enable",
action="store_true",
help="Use {}. This flag is useful when you want to study the effect of different "
"transforms.".format(cls.__name__),
)
group.add_argument(
"--image-augmentation.random-uniform.width-range",
type=int,
nargs=2,
default=[-5, 5],
help="The range of values from which to add noise. It is specified"
" as [low, high] (inclusive). Defaults to [-5, 5].",
)
return parser