Spaces:
Paused
Paused
File size: 13,601 Bytes
f04c9cc 098fc8a f04c9cc 098fc8a f04c9cc 06ebfb2 475978d 9efd988 f04c9cc 098fc8a |
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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
from typing import Any
import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange
from collections import defaultdict
import torch as th
import numpy as np
import math
str_to_act = defaultdict(lambda: nn.SiLU())
str_to_act.update({
"relu": nn.ReLU(),
"silu": nn.SiLU(),
"gelu": nn.GELU(),
})
class SinusoidalPositionalEncoding(nn.Module):
def __init__(self, dim):
super().__init__()
self.dim = dim
def forward(self, t):
device = t.device
t = t.unsqueeze(-1)
inv_freq = 1.0 / (10000 ** (torch.arange(0, self.dim, 2, device=device).float() / self.dim))
sin_enc = torch.sin(t.repeat(1, self.dim // 2) * inv_freq)
cos_enc = torch.cos(t.repeat(1, self.dim // 2) * inv_freq)
pos_enc = torch.cat([sin_enc, cos_enc], dim=-1)
return pos_enc
class TimeEmbedding(nn.Module):
def __init__(self, model_dim: int, emb_dim: int, act="silu"):
super().__init__()
self.lin = nn.Linear(model_dim, emb_dim)
self.act = str_to_act[act]
self.lin2 = nn.Linear(emb_dim, emb_dim)
def forward(self, x):
x = self.lin(x)
x = self.act(x)
x = self.lin2(x)
return x
class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, act="silu", dropout=None, zero=False):
super().__init__()
self.norm = nn.GroupNorm(
num_groups=32,
num_channels=in_channels,
)
self.act = str_to_act[act]
if dropout is not None:
self.dropout = nn.Dropout(dropout)
self.conv = nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=3,
padding=1,
)
if zero:
self.conv.weight.data.zero_()
def forward(self, x):
x = self.norm(x)
x = self.act(x)
if hasattr(self, "dropout"):
x = self.dropout(x)
x = self.conv(x)
return x
class EmbeddingBlock(nn.Module):
def __init__(self, channels: int, emb_dim: int, act="silu"):
super().__init__()
self.act = str_to_act[act]
self.lin = nn.Linear(emb_dim, channels)
def forward(self, x):
x = self.act(x)
x = self.lin(x)
return x
class ResBlock(nn.Module):
def __init__(self, channels: int, emb_dim: int, dropout: float = 0, out_channels=None):
"""A resblock with a time embedding and an optional change in channel count
"""
if out_channels is None:
out_channels = channels
super().__init__()
self.conv1 = ConvBlock(channels, out_channels)
self.emb = EmbeddingBlock(out_channels, emb_dim)
self.conv2 = ConvBlock(out_channels, out_channels, dropout=dropout, zero=True)
if channels != out_channels:
self.skip_connection = nn.Conv2d(channels, out_channels, kernel_size=1)
else:
self.skip_connection = nn.Identity()
def forward(self, x, t):
original = x
x = self.conv1(x)
t = self.emb(t)
# t: (batch_size, time_embedding_dim) = (batch_size, out_channels)
# x: (batch_size, out_channels, height, width)
# we repeat the time embedding to match the shape of x
t = t.unsqueeze(-1).unsqueeze(-1).repeat(1, 1, x.shape[2], x.shape[3])
x = x + t
x = self.conv2(x)
x = x + self.skip_connection(original)
return x
class SelfAttentionBlock(nn.Module):
def __init__(self, channels, num_heads=1):
super().__init__()
self.channels = channels
self.num_heads = num_heads
self.norm = nn.GroupNorm(32, channels)
self.attention = nn.MultiheadAttention(
embed_dim=channels,
num_heads=num_heads,
dropout=0,
batch_first=True,
bias=True,
)
def forward(self, x):
h, w = x.shape[-2:]
original = x
x = self.norm(x)
x = rearrange(x, "b c h w -> b (h w) c")
x = self.attention(x, x, x)[0]
x = rearrange(x, "b (h w) c -> b c h w", h=h, w=w)
return x + original
class Downsample(nn.Module):
def __init__(self, channels):
super().__init__()
# ddpm uses maxpool
# self.down = nn.MaxPool2d
# iddpm uses strided conv
self.down = nn.Conv2d(
in_channels=channels,
out_channels=channels,
kernel_size=3,
stride=2,
padding=1,
)
def forward(self, x):
return self.down(x)
class DownBlock(nn.Module):
"""According to U-Net paper
'The contracting path follows the typical architecture of a convolutional network.
It consists of the repeated application of two 3x3 convolutions (unpadded convolutions),
each followed by a rectified linear unit (ReLU) and a 2x2 max pooling operation with stride 2
for downsampling. At each downsampling step we double the number of feature channels.'
"""
def __init__(self, in_channels, out_channels, time_embedding_dim, use_attn=False, dropout=0, downsample=True, width=1):
"""in_channels will typically be half of out_channels"""
super().__init__()
self.width = width
self.use_attn = use_attn
self.do_downsample = downsample
self.blocks = nn.ModuleList()
for _ in range(width):
self.blocks.append(ResBlock(
channels=in_channels,
out_channels=out_channels,
emb_dim=time_embedding_dim,
dropout=dropout,
))
if self.use_attn:
self.blocks.append(SelfAttentionBlock(
channels=out_channels,
))
in_channels = out_channels
if self.do_downsample:
self.downsample = Downsample(out_channels)
def forward(self, x, t):
for block in self.blocks:
if isinstance(block, ResBlock):
x = block(x, t)
elif isinstance(block, SelfAttentionBlock):
x = block(x)
residual = x
if self.do_downsample:
x = self.downsample(x)
return x, residual
class Upsample(nn.Module):
def __init__(self, channels):
super().__init__()
self.upsample = nn.Upsample(scale_factor=2)
self.conv = nn.Conv2d(
in_channels=channels,
out_channels=channels,
kernel_size=3,
padding=1,
)
def forward(self, x):
x = self.upsample(x)
x = self.conv(x)
return x
class UpBlock(nn.Module):
"""According to U-Net paper
Every step in the expansive path consists of an upsampling of the feature map followed by a 2x2
convolution (“up-convolution”) that halves the number of feature channels, a concatenation with
the correspondingly cropped feature map from the contracting path, and two 3x3 convolutions,
each followed by a ReLU.
"""
def __init__(self, in_channels, out_channels, time_embedding_dim, use_attn=False, dropout=0, upsample=True, width=1):
"""in_channels will typically be double of out_channels
"""
super().__init__()
self.use_attn = use_attn
self.do_upsample = upsample
self.blocks = nn.ModuleList()
for _ in range(width):
self.blocks.append(ResBlock(
channels=in_channels,
out_channels=out_channels,
emb_dim=time_embedding_dim,
dropout=dropout,
))
if self.use_attn:
self.blocks.append(SelfAttentionBlock(
channels=out_channels,
))
in_channels = out_channels
if self.do_upsample:
self.upsample = Upsample(out_channels)
def forward(self, x, t):
for block in self.blocks:
if isinstance(block, ResBlock):
x = block(x, t)
elif isinstance(block, SelfAttentionBlock):
x = block(x)
if self.do_upsample:
x = self.upsample(x)
return x
class Bottleneck(nn.Module):
def __init__(self, channels, dropout, time_embedding_dim):
super().__init__()
in_channels = channels
out_channels = channels
self.resblock_1 = ResBlock(
channels=in_channels,
out_channels=out_channels,
dropout=dropout,
emb_dim=time_embedding_dim
)
self.attention_block = SelfAttentionBlock(
channels=out_channels,
)
self.resblock_2 = ResBlock(
channels=out_channels,
out_channels=out_channels,
dropout=dropout,
emb_dim=time_embedding_dim
)
def forward(self, x, t):
x = self.resblock_1(x, t)
x = self.attention_block(x)
x = self.resblock_2(x, t)
return x
class Unet(nn.Module):
def __init__(
self,
image_channels=3,
res_block_width=2,
starting_channels=128,
dropout=0,
channel_mults=(1, 2, 2, 4, 4),
attention_layers=(False, False, False, True, False)
):
super().__init__()
self.is_conditional = False
#channel_mults = (1, 2, 2, 2)
#attention_layers = (False, False, True, False)
#res_block_width=3
self.image_channels = image_channels
self.starting_channels = starting_channels
time_embedding_dim = 4 * starting_channels
self.time_encoding = SinusoidalPositionalEncoding(dim=starting_channels)
self.time_embedding = TimeEmbedding(model_dim=starting_channels, emb_dim=time_embedding_dim)
self.input = nn.Conv2d(3, starting_channels, kernel_size=3, padding=1)
current_channel_count = starting_channels
input_channel_counts = []
self.contracting_path = nn.ModuleList([])
for i, channel_multiplier in enumerate(channel_mults):
is_last_layer = i == len(channel_mults) - 1
next_channel_count = channel_multiplier * starting_channels
self.contracting_path.append(DownBlock(
in_channels=current_channel_count,
out_channels=next_channel_count,
time_embedding_dim=time_embedding_dim,
use_attn=attention_layers[i],
dropout=dropout,
downsample=not is_last_layer,
width=res_block_width,
))
current_channel_count = next_channel_count
input_channel_counts.append(current_channel_count)
self.bottleneck = Bottleneck(channels=current_channel_count, time_embedding_dim=time_embedding_dim, dropout=dropout)
self.expansive_path = nn.ModuleList([])
for i, channel_multiplier in enumerate(reversed(channel_mults)):
next_channel_count = channel_multiplier * starting_channels
self.expansive_path.append(UpBlock(
in_channels=current_channel_count + input_channel_counts.pop(),
out_channels=next_channel_count,
time_embedding_dim=time_embedding_dim,
use_attn=list(reversed(attention_layers))[i],
dropout=dropout,
upsample=i != len(channel_mults) - 1,
width=res_block_width,
))
current_channel_count = next_channel_count
last_conv = nn.Conv2d(
in_channels=starting_channels,
out_channels=image_channels,
kernel_size=3,
padding=1,
)
last_conv.weight.data.zero_()
self.head = nn.Sequential(
nn.GroupNorm(32, starting_channels),
nn.SiLU(),
last_conv,
)
def forward(self, x, t):
t = self.time_encoding(t)
return self._forward(x, t)
def _forward(self, x, t):
t = self.time_embedding(t)
x = self.input(x)
residuals = []
for contracting_block in self.contracting_path:
x, residual = contracting_block(x, t)
residuals.append(residual)
x = self.bottleneck(x, t)
for expansive_block in self.expansive_path:
# Add the residual
residual = residuals.pop()
x = torch.cat([x, residual], dim=1)
x = expansive_block(x, t)
x = self.head(x)
return x
class ConditionalUnet(nn.Module):
def __init__(self, unet, num_classes):
super().__init__()
self.is_conditional = True
self.unet = unet
self.num_classes = num_classes
self.class_embedding = nn.Embedding(num_classes + 1, unet.starting_channels, padding_idx=0)
def to(self, device):
self.device = device
return super().to(device)
def forward(self, x, t, cond=None):
cond = torch.tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
cond = cond.unsqueeze(0)
cond = cond.to(self.device)
# cond: (batch_size, n), where n is the number of classes that we are conditioning on
t = self.unet.time_encoding(t)
if cond is not None:
cond = self.class_embedding(cond)
# sum across the classes so we get a single vector representing the set of classes
cond = cond.sum(dim=1)
t += cond
return self.unet._forward(x, t)
|