-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimage.py
144 lines (106 loc) · 3.19 KB
/
image.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
r"""Image helpers"""
import dm_pix as pix
import jax
import jax.numpy as jnp
import numpy as np
from einops import rearrange
from jax import Array
from pathlib import Path
from PIL import Image
from typing import *
def flatten(x: Array) -> Array:
return rearrange(x, '... H W C -> ... (H W C)')
def unflatten(x: Array, height: int, width: int) -> Array:
return rearrange(x, '... (H W C) -> ... H W C', H=height, W=width)
def from_pil(img: Image.Image) -> Array:
x = np.asarray(img)
x = x * (4 / 256) - 2
return x
def to_pil(
x: Array,
pad: int = 0,
background: int = 255,
zoom: int = 1,
file: Union[str, Path] = None,
) -> Image.Image:
x = np.asarray(x)
x = np.clip((x + 2) * (256 / 4), 0, 255)
x = np.rint(x).astype(np.uint8)
x = np.tile(x, (1, 1, 1, 1, 1))
x = np.pad(x, pad_width=((0, 0), (0, 0), (pad, pad), (pad, pad), (0, 0)), constant_values=background)
x = rearrange(x, 'M N H W C -> (M H) (N W) C')
if x.shape[-1] == 1:
x = Image.fromarray(x.squeeze(-1), mode='L')
else:
x = Image.fromarray(x, mode='RGB')
if zoom > 1:
x = x.resize((zoom * x.width, zoom * x.height), Image.NEAREST)
if file is not None:
x.save(file)
return x
def collate(
images: List[List[Image.Image]],
pad: int = 0,
background: int = 255,
file: Union[str, Path] = None,
) -> Image.Image:
M, N = len(images), max(map(len, images))
for i in range(M):
for j in range(N):
try:
W, H = images[i][j].size
except IndexError:
continue
canvas = Image.new(
'RGB',
size=(
N * (W + pad) + pad,
M * (H + pad) + pad,
),
color=background,
)
for i in range(M):
for j in range(N):
offset = (
j * (W + pad) + pad,
i * (H + pad) + pad,
)
try:
canvas.paste(images[i][j], offset)
except IndexError:
continue
if file is not None:
canvas.save(file)
return canvas
def random_flip(x: Array, key: Array, axis: int = -2) -> Array:
return jnp.where(
jax.random.bernoulli(key),
x,
jnp.flip(x, axis=axis),
)
def random_hue(x: Array, key: Array, delta: float = 1e-2) -> Array:
x = (x + 2) / 4
x = pix.random_hue(key, x, delta)
x = x * 4 - 2
return x
def random_saturation(x: Array, key: Array, lower: float = 0.95, upper: float = 1.05) -> Array:
x = (x + 2) / 4
x = pix.random_saturation(key, x, lower, upper)
x = x * 4 - 2
return x
def random_shake(x: Array, key: Array, delta: int = 1, mode: str = 'reflect') -> Array:
i = jax.random.randint(key, shape=(3,), minval=0, maxval=2 * delta + 1)
i = i.at[-1].set(0)
return jax.lax.dynamic_slice(
jnp.pad(
x,
pad_width=((delta, delta), (delta, delta), (0, 0)),
mode=mode,
),
start_indices=i,
slice_sizes=x.shape,
)
def psnr(a: Array, b: Array) -> Array:
return pix.psnr((a + 2) / 4, (b + 2) / 4)
def ssim(a: Array, b: Array) -> Array:
return pix.ssim((a + 2) / 4, (b + 2) / 4)