-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunet.py
227 lines (197 loc) · 6.75 KB
/
unet.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
import jax
import jax.numpy as jnp
import math
from einops import rearrange, repeat
from typing import NamedTuple, Optional, Any
from functools import partial
from flax import linen as nn
class SinusoidalPositionEmbeddings(nn.Module):
dim: int
@nn.compact
def __call__(self, time: jax.Array) -> jax.Array:
half_dim = self.dim // 2
embeddings_scale = math.log(10000) / (half_dim - 1)
embeddings = jnp.exp(jnp.arange(half_dim) * -embeddings_scale)
embeddings = time[:, None] * embeddings[None, :]
embeddings = jnp.concatenate((jnp.sin(embeddings), jnp.cos(embeddings)), axis=1)
return embeddings
class Block(nn.Module):
dim_out: int
dtype: Any = jnp.float32
@nn.compact
def __call__(
self, x: jax.Array, scale_shift: Optional[list[jax.Array]] = None
) -> jax.Array:
x = nn.Conv(
features=self.dim_out,
kernel_size=(3, 3),
strides=1,
padding=1,
kernel_init=nn.initializers.glorot_uniform(),
bias_init=nn.initializers.zeros,
dtype=self.dtype,
)(x)
x = nn.GroupNorm(32)(x)
if scale_shift is not None:
scale, shift = scale_shift
x = x * (scale + 1) + shift
return nn.gelu(x)
class ResNetBlock(nn.Module):
dim: int
dim_out: int
time_emb_dim: Optional[int] = None
dtype: Any = jnp.float32
@nn.compact
def __call__(self, x: jax.Array, time_emb: Optional[jax.Array] = None) -> jax.Array:
scale_shift = None
if time_emb is not None:
t = nn.Sequential(
[
nn.gelu,
nn.Dense(
features=self.dim_out * 2,
kernel_init=nn.initializers.glorot_uniform(),
bias_init=nn.initializers.zeros,
dtype=self.dtype,
),
]
)(time_emb)
t = rearrange(t, "b c -> b 1 1 c")
scale_shift = jnp.split(t, 2, axis=-1)
h = Block(dim_out=self.dim_out, dtype=self.dtype)(x, scale_shift=scale_shift)
h = Block(dim_out=self.dim_out, dtype=self.dtype)(h)
if self.dim != self.dim_out:
h = h + nn.Dense(
features=self.dim_out,
kernel_init=nn.initializers.glorot_uniform(),
bias_init=nn.initializers.zeros,
dtype=self.dtype,
)(x)
return h
class PixelShuffleUpsample(nn.Module):
dim: int
dtype: Any = jnp.float32
@nn.compact
def __call__(self, x: jax.Array) -> jax.Array:
x = nn.Sequential(
[
nn.Dense(
features=self.dim * 4,
kernel_init=nn.initializers.glorot_uniform(),
bias_init=nn.initializers.zeros,
dtype=self.dtype,
),
nn.gelu,
]
)(x)
x = rearrange(x, "b h w (c s1 s2) -> b (h s1) (w s2) c", s1=2, s2=2)
return x
class PixelShuffleDownsample(nn.Module):
dim: int
dtype: Any = jnp.float32
@nn.compact
def __call__(self, x: jax.Array) -> jax.Array:
x = rearrange(x, "b (h s1) (w s2) c -> b h w (c s1 s2)", s1=2, s2=2)
x = nn.Dense(
features=self.dim,
kernel_init=nn.initializers.glorot_uniform(),
bias_init=nn.initializers.zeros,
dtype=self.dtype,
)(x)
return x
class UNet(nn.Module):
dim: int
dim_mults: tuple[int, ...] = (1, 2, 4)
channels: int = 3
dtype: Any = jnp.float32
@nn.compact
def __call__(
self,
x: jax.Array,
time: Optional[jax.Array] = None,
condition: Optional[jax.Array] = None,
) -> jax.Array:
dims = [self.dim, *map(lambda m: self.dim * m, self.dim_mults)]
in_out = list(zip(dims[:-1], dims[1:]))
num_resolutions = len(in_out)
if time is not None:
time_emb_dim = self.dim * 4
time_embed = nn.Sequential(
[
SinusoidalPositionEmbeddings(self.dim),
nn.Dense(
features=time_emb_dim,
kernel_init=nn.initializers.glorot_uniform(),
bias_init=nn.initializers.zeros,
dtype=self.dtype,
),
nn.gelu,
nn.Dense(
features=time_emb_dim,
kernel_init=nn.initializers.glorot_uniform(),
bias_init=nn.initializers.zeros,
dtype=self.dtype,
),
]
)(time)
else:
time_emb_dim = None
time_embed = None
x = nn.Conv(
features=self.dim,
kernel_size=(7, 7),
padding=3,
kernel_init=nn.initializers.glorot_uniform(),
bias_init=nn.initializers.zeros,
dtype=self.dtype,
)(x)
residual = x
h = []
for ind, (dim_in, dim_out) in enumerate(in_out):
is_last = ind >= (num_resolutions - 1)
x = ResNetBlock(
dim=dim_in,
dim_out=dim_out,
time_emb_dim=time_emb_dim,
dtype=self.dtype,
)(x, time_embed)
h.append(x)
if not is_last:
x = PixelShuffleDownsample(dim=dim_out, dtype=self.dtype)(x)
# Middle block + attn + block
mid_dim = dims[-1]
x = ResNetBlock(
dim=mid_dim,
dim_out=mid_dim,
time_emb_dim=time_emb_dim,
dtype=self.dtype,
)(x, time_embed)
for ind, (dim_in, dim_out) in enumerate(reversed(in_out[1:])):
h_l = h.pop()
x = jnp.concatenate((x, h_l), axis=-1)
is_last = ind >= (num_resolutions - 1)
x = ResNetBlock(
dim=dim_out * 2,
dim_out=dim_in,
time_emb_dim=time_emb_dim,
dtype=self.dtype,
)(x, time_embed)
if not is_last:
x = PixelShuffleUpsample(dim=dim_in, dtype=self.dtype)(x)
x = jnp.concatenate((x, residual), axis=-1)
x = nn.Sequential(
[
ResNetBlock(
dim=self.dim,
dim_out=self.dim,
dtype=self.dtype,
),
nn.Dense(
features=self.channels,
kernel_init=nn.initializers.glorot_uniform(),
bias_init=nn.initializers.zeros,
dtype=self.dtype,
),
]
)(x)
return x