่ฟฝ่จ๏ผๅ
ฅ้โ ๏ฝโขใๅฎ่ณชใพใจใใใใใช่จไบใๆธใใพใใ
[ๆกๆฃใขใใซๅ
ฅ้] ใผใญใใ็่งฃใใๆกๆฃใขใใซใฎๆๆฐ็่ซ๏ผๅณ่งฃไปใ๏ผ
ๅ
ฅ้โ ๏ผDDPMใฎ็่ซใจMNISTใฎๅฎ่ฃ
ๅ
ฅ้โก๏ผSDE/ODEใฎๅบ็ค็่ซ๏ผTensorflowๅฎ่ฃ
ไปใ๏ผ
ๅ
ฅ้โข๏ผEDMใฎ่งฃ่ชฌใจMNISTใฎๅฎ่ฃ
ๅ
ฅ้โฃ๏ผใใ
ๅฟ็จ็ทจ๏ผๆกๆฃใขใใซใซGRPOใไฝฟใฃใฆใใกใคใณใใฅใผใใณใฐใใฆใฟใ
ๆๅพใซU-Netใฎ่ฉณ็ดฐใจๆกไปถใฎๅใ่พผใฟๆนใซใคใใฆ่ฆใฆใใใพใใ
ๅ่
ใปhttps://huggingface.co/blog/annotated-diffusion
ใปhttps://github.com/eloialonso/diamond/
DDPMใใผในใฎU-Net
DDPMใฎๅฎ่ฃ
ใงใฏU-Netใซ Wide ResNet block ใฎไฝฟ็จใAttentionๅฑคใฎ่ฟฝๅ ใใใใๆญฃ่ฆๅใใใฐใซใผใๆญฃ่ฆๅใธใฎๅคๆดใใใใฆใใพใใ
EDMใงใU-Netใฎๅฎ่ฃ
่ชไฝใฏๅคใใใฏใชใใใใงใใ
ๆฌ่จไบใงใฏใ็ดฐใใ้ใใฏใใใพใใๅ
้จใงEDMใไฝฟใใใฆใใDIAMONDใฎๅฎ่ฃ
(GitHub)ใใใผในใซ่ฆใฆใใใพใใ
ๅ จไฝใใๅใขใธใฅใผใซใซๆใไธใใๅฝขใง่ฆใฆใใใพใใ
U-Net
U-Netใฏ็ปๅใๅ
ฅๅใจใใฆ็ปๅใๅบๅใใใขใใซใจใชใใพใใ
่งฃๅๅบฆใไธใใDownsampleใจ่งฃๅๅบฆใไธใใUpsampleใใใใใ็ดๆฅใคใชใใในใญใใๆฅ็ถ๏ผSkip Connection๏ผใใใใฎใ็นๅพดใงใใ
็ปๅใซๆ
ๅ ฑใไปไธใใๅ ดๅใฏใๆกไปถไปใใจใใฆ Condition ใๅ
ฅๅใใพใใ
Conditionใฏๅ
จ้จใฎๅฑคใซๅฏพใใฆๅ
ฅๅใใใพใใ
ใพใใใใญใใฏใฎ้ ๅบใงใใใใใใฎๅฎ่ฃ
ใ่ฆใฆใฟใใจ "Down/UpโResBlock" ใจ "ResBockโDown/Up" ใฎไธกๆนใฎๅฎ่ฃ
ใ่ฆใใใพใใใ
้ใใๅใใใชใใฃใใฎใงChatGPTใซ่ใใใจใใไปฅไธใฎใใใงใใ
| ้ ๅบ | ็นๅพด | ใกใชใใ/ใใกใชใใ | ไพ |
|---|---|---|---|
| ResBlock โ Downsample | ็นๅพดใๅ ใซๆฝๅบใใฆใใ็ธฎๅฐ | ่จ็ฎใณในใใ้ซใใใ้ซ่งฃๅๅบฆใฎๆ ๅ ฑใไฟๆ | DDPM,็ปๅ็ๆ |
| Downsample โ ResBlock | ็ธฎๅฐใใฆใใ็นๅพดๆฝๅบ | ่จ็ฎใณในใใไฝใใใๆ ๅ ฑใฎๆๅคฑใๅคงใใ | ๅ้กใขใใซ,ๅฐ่ฆๆจกใช็นๅพดๆฝๅบ |
| ้ ๅบ | ็นๅพด | ใกใชใใ | ใใกใชใใ | ไพ |
|---|---|---|---|---|
| ResBlock โ Upsample | ็นๅพดใๅ ใซๆฝๅบใใๆกๅคงใใ | ้ซ่งฃๅๅบฆใฎๆ ๅ ฑใไฟๆใใใใ | ใขใใใตใณใใชใณใฐๅพใฎ่ชฟๆดใๅฐใชใ | DDPM,็ปๅ็ๆ |
| Upsample โ ResBlock | ๅ ใซๆกๅคงใใ็นๅพดใๅ่ชฟๆด | ้ซ่งฃๅๅบฆใฎ็นๅพดใๅญฆ็ฟใงใใ | ๆกๅคงๆใฎๆ ๅ ฑๆๅคฑใๅคงใใใชใๅฏ่ฝๆง | ่ถ ่งฃๅ, ้ซ่งฃๅๅบฆใฎ็ปๅๅฆ็ |
Condition
Condition ใฏๅ็ปๅใฎ่ฟฝๅ ๆ
ๅ ฑใงใๆกๆฃใขใใซใฎใฟใคใ ในใใใใฎๆ
ๅ ฑใๅซใพใใพใใ
ไปๅใฏMNISTใฎใซใใดใชๆ
ๅ ฑใ่ฟฝๅ ใใฆใฟใพใใใ
ใฟใคใ ในใใใใฏDIAMONDใฎๅฎ่ฃ
ใงใฏใใผใชใจๅคๆใใใ็นๅพด้ใไฝฟใฃใฆใใพใใใใใใฎ่จไบใงใฏDDPMใซๅฃใฃใฆใตใคใณใณใตใคใณใจใณใณใผใใฃใณใฐใซใใฆใใพใใ
MNISTใฎใซใใดใชๆ
ๅ ฑใฏTFใฎEmbbedingใฌใคใคใผใงใใฏใใซๅใใพใใใ
Downsample/Upsample
DIAMONDใฎๅฎ่ฃ ใงใฏใDownsampleใฏConv2Dใฎstrides=2ใงใตใคใบใๅๅใซใใUpsampleใฏๆ่ฟๅ่ฃ้ใง2ๅใซใใฆใใพใใใฎใงใใใใซๅฃใฃใฆใใพใใ
class Downsample(keras.layers.Layer):
def build(self, input_shape):
self.conv = kl.Conv2D(
filters=input_shape[-1],
kernel_size=3,
strides=2,
padding="same",
kernel_initializer=keras.initializers.Orthogonal(),
)
def call(self, x, training=False):
return self.conv(x, training=training)
class Upsample(keras.layers.Layer):
def build(self, input_shape):
self.conv = kl.Conv2D(
filters=input_shape[-1],
kernel_size=3,
strides=1,
padding="same",
)
def call(self, x, training=False):
# ๆ่ฟๅ่ฃ้ใง2ๅใซใชใตใคใบ
input_shape = tf.shape(x)
x = tf.image.resize(x, size=(input_shape[1] * 2, input_shape[2] * 2), method="nearest")
return self.conv(x, training=training)
ResBLocks & ResBlock๏ผResidual Block๏ผ
ResBLocksใงใใใ่คๆฐใฎResBlockใใๆใใพใใ
Skip Connection ใฏ Upsample ใซใฎใฟใใ Downsample ใฎๅบๅใซๅฏพๅฟใใพใใ
ๆฌกใซResBlockใงใใ
ๆฎ้ใฎResBlockใจ้ใ็นใฏๆๅพใซAttentionใฌใคใคใผ๏ผSelfAttention๏ผใๅ
ฅใฃใฆใใ็นใงใใ
ใพใConditionใงใใใDDPMใงใฏConv3x3ๅฑคใซ็ดๆฅ่ถณใใฆใใพใใใใDIAMONDใงใฏ AdaGN ใจใใฆNormๅฑคใซ่ฟฝๅ ใใฆใใพใใ
Adaptive Group Normalization๏ผAdaGN๏ผ
Normใฌใคใคใผใงใใใไธ่ฌ็ใซใฏ Group Normalization๏ผGN๏ผใไฝฟใใใพใใ
ๅณใฏGroup Normalization (่ซๆ)ใใใไปใฎๆญฃ่ฆๅๆๆณใจGNใฎ้ใใ่กจใใฆใใใNใใใใใCใใใฃใณใใซใ(H,W)ใ็ปๅใ่กจใ่ปธใจใชใใพใใ
็ฐกๅใซใพใจใใใจไปฅไธใงใใ
| Norm | ใใใ่ปธ | ใใฃใณใใซ่ปธ | |
|---|---|---|---|
| Batch Norm | All | 1 | ใใใใซๅฏพใใฆๆญฃ่ฆๅ |
| Layer Norm | 1 | All | ใใฃใณใใซใซๅฏพใใฆๆญฃ่ฆๅ |
| Instance Norm | 1 | 1ใใใใใ | ็ปๅใใฃใณใใซๆฏใซๆญฃ่ฆๅ |
| Group Norm | 1 | ใฐใซใผใใงๅๅฒ | ใใฃใณใใซ่ปธใๅๅฒใใใใใใใฎใฐใซใผใใงๆญฃ่ฆๅ |
DIAMONDใฎๅฎ่ฃ
ใงใฏ GN ใงใฏใชใใAdaptive Group Normalization๏ผAdaGN๏ผใไฝฟใใใฆใใพใใใ
ใใใฏ GN ใซๆกไปถ๏ผไพใใฐในใฟใคใซๆ
ๅ ฑใชใฉ๏ผใ้ฉ็จใงใใใใใซๆกๅผตใใๆๆณใงใใ
GN ใฏไปฅไธใฎใใใซๆญฃ่ฆๅใใใพใใ
$$
\hat{x}_i = \frac{x_i - \mu_g}{\sigma_g}
$$
$\mu_g$ใจ$\sigma_g$ใใฐใซใผใ$g$ใฎๅนณๅใจๆจๆบๅๅทฎใ่กจใใพใใ
AdaGNใงใฏใใใซๆกไปถ $s$ ใซๅฏพใใในใฑใผใซ $\gamma$ ใจใใคใขใน $\beta$ ใ่ฟฝๅ ใใใพใใ
$$
\hat{x}_i = \gamma(s)\cdot \frac{x_i - \mu_g}{\sigma_g} + \beta(s)
$$
ไธปใซในใฟใคใซๅคๆ๏ผStyle Transfer๏ผใไปๅใฟใใใชๆกไปถไป็ปๅ็ๆใงไฝฟใใใใใจใๅคใใใใงใใ
ๅคๅใใใ่ซๆ๏ผArbitrary Style Transfer in Real-time with Adaptive Instance Normalization
SelfAttention
TransformerใงๆๅใซใชใฃใAttentionใซใคใใฆ็ฐกๅใซ่ชฌๆใใพใใ
่ฉณ็ดฐใซ่ฆใใๅ ดๅใฏไปฅไธใฎๅ็ปใๅ่ใซใใฆใใ ใใใ
ใๆทฑๅฑคๅญฆ็ฟใAttention - ๅ
จ้ ๅใซๅฟ็จใใๆ้ซ็ฒพๅบฆใๅฉใๅบใๆณจๆๆฉๆงใฎไป็ตใฟใใใฃใผใใฉใผใใณใฐใฎไธ็ vol. 24ใ(youtube; AIcia Solid Project)
โปใใใใ่ชฟในใใจใใใฎ็งใฎ่งฃ้ใจใชใใพใใ
AttentionๆฉๆงใฏQuery/Key/Valueใฎ3ใคใฎๅคใๅ
ฅๅใใฆใใใฎไธญใงๆณจ็ฎๅบฆใฎ้ซใ่ฆ็ด ใๅบๅใใไป็ตใฟใจใชใใพใใ
Attentionๆฉๆงใฎไปฃ่กจ็ใช Source-Targetๅ Attention ใฎไพใ ใจไปฅไธใฎใใใซใชใใพใใ๏ผQโ K=Vใฎๅฝข๏ผ
็ธฆใฎๅใงใใใ็ฟป่จณใฎไพใ ใจใใผใฏใณๅใจใชใใใฎใพใพ๏ผๆฌกๅ
ใง่กจ็พใงใใพใใใ็ปๅใฏ๏ผๆฌกๅ
ใชใฎใงw*hใฎ๏ผๆฌกๅ
ใซไธฆในๆฟใใฆ่กจ็พใใใพใใ
๏ผๅใใใใซๅฏพใใฆๅฅใฎใใใใซๅฏพใใๆณจ็ฎๅบฆใ่ฆใใคใกใผใธ๏ผ
SelfAttentionใฏ Q=K=V ใจใ3ใคใจใๅใๅคใๅ ฅๅใใใใฎใซใชใใใใฎ็ปๅๅ ใง้ขไฟๆงใฎ้ซใใใใใๅผท่ชฟใใๅฝนๅฒใซใชใใพใใ
่ชฌๆใฏไปฅไธใงใๆฎใใฏๅฎ่ฃ ็ตๆใงใใ
ๅฎ่ก็ตๆ
ๅๆฐๅญใๅบๅใใใฆใฟใพใใใ
ใกใใใจใใใฃใฝใๆฐๅญใๅบๅใใใฆใใพใใญใ
ใใ ใขใใซใฎๅคงใใใจlossใฎไธใใๅ
ทๅ็ใซใพใ ๅญฆ็ฟใ่ถณใใฆใใชใๆฐใใใพใใ
- 0
0ใฎใฟhisotryใใฎใใฆใใใพใใ
ๅ จไฝใณใผใ
version
- Windows11
- WSL2: Ubuntu24.04
- Python3.12.3
- Tensorflow 2.18.0
- CUDA 12.5.1
- cuDNN 9.3.0
- GeForce RTX 3060 12GB
import math
from functools import partial
from pathlib import Path
import numpy as np
import tensorflow as tf
from matplotlib import pyplot as plt
from tensorflow import keras
from tqdm import tqdm
kl = keras.layers
# define
img_size = 32
img_shape = (img_size, img_size, 1)
def create_dataset():
(x_train, y_train), (_, _) = keras.datasets.mnist.load_data()
# x_train, y_train = x_train[y_train == 1], y_train[y_train == 1]
x_train = tf.image.resize(tf.expand_dims(x_train, -1), [img_size, img_size]).numpy() # 28 -> 32
x_train = (x_train / 255.0) * 2 - 1 # [0,255] -> [-1,1]
return x_train.astype(np.float32), y_train.astype(np.float32)
def decode_image(x):
img = np.clip(x, -1.0, 1.0)
# [-1,1] -> [0,255]
img = (((img + 1) / 2) * 255).astype(np.uint8)
return img
# ----------------------------------
# model
# ----------------------------------
# alias
Conv2D1x1 = partial(kl.Conv2D, kernel_size=1, strides=1, padding="valid")
Conv2D3x3 = partial(kl.Conv2D, kernel_size=3, strides=1, padding="same")
IdentityLayer = partial(kl.Lambda, function=lambda x: x) # ไฝใใใชใใฌใคใคใผ
class PositionalEmbedding(keras.layers.Layer):
def __init__(self, dim: int, **kwargs):
super().__init__(**kwargs)
self.dim = dim
def call(self, time: tf.Tensor) -> tf.Tensor:
half_dim = self.dim // 2
embeddings = math.log(10000) / (half_dim - 1)
embeddings = tf.exp(tf.range(half_dim, dtype=tf.float32) * -embeddings)
embeddings = tf.expand_dims(time, axis=-1) * tf.expand_dims(embeddings, axis=0)
embeddings = tf.concat([tf.math.sin(embeddings), tf.math.cos(embeddings)], axis=-1)
return embeddings
@staticmethod
def plot(embedding_dim: int = 500, N: int = 100): # for debug
model = PositionalEmbedding(embedding_dim)
timestep = EDM.create_timesptes(N)
emb = model(tf.constant(timestep)).numpy()
plt.pcolormesh(emb.T, cmap="RdBu")
plt.ylabel("dimension")
plt.xlabel("time step")
plt.colorbar()
plt.show()
class AdaGroupNorm(keras.layers.Layer):
def __init__(self, group_size: int = 32, eps: float = 1e-5, **kwargs) -> None:
super().__init__(**kwargs)
self.group_size = group_size
self.eps = eps
def build(self, input_shape):
in_channels = input_shape[-1]
# group_sizeใฏๅฒใๅใใๅ ดๅใฎใฟๆๅฎใ-1: LayerNorm, 1: InstanceNorm
groups = self.group_size if in_channels % self.group_size == 0 else -1
self.norm = kl.GroupNormalization(groups=groups, epsilon=self.eps)
self.gamma = kl.Dense(in_channels, use_bias=False, kernel_initializer="zeros")
self.beta = kl.Dense(in_channels, use_bias=False, kernel_initializer="zeros")
def call(self, x, condition, training=False):
x = self.norm(x, training=training)
condition = tf.expand_dims(tf.expand_dims(condition, axis=1), axis=1) # (b,c)->(b,1,1,c)
gamma = self.gamma(condition, training=training)
beta = self.beta(condition, training=training)
return x * (1 + gamma) + beta
class SelfAttention2D(keras.layers.Layer):
def __init__(self, head_dim: int = 8, **kwargs) -> None:
super().__init__(**kwargs)
self.head_dim = head_dim
def build(self, input_shape):
in_channels = input_shape[-1]
self.n_head = max(1, in_channels // self.head_dim)
assert in_channels % self.n_head == 0, f"ๅ
ฅๅใใฃใณใใซๆฐใฏheadๆฐใงๅๅฒใงใใๆฐ๏ผhead={self.n_head}๏ผ"
self.norm = kl.GroupNormalization()
self.qkv_proj = Conv2D1x1(in_channels * 3)
self.out_proj = Conv2D1x1(in_channels, kernel_initializer="zeros", bias_initializer="zeros")
self.softmax = kl.Softmax(axis=-1)
def call(self, x, training=False):
n, h, w, c = x.shape
x = self.norm(x, training=training)
qkv = self.qkv_proj(x)
# chใใใใๆฐใงๅๅฒใhใจwใฏใพใจใใฆseq_lenใจใใ
# [batch, h, w, ch*3] -> [batch, h*w, ch//head, head*3]
qkv = tf.reshape(qkv, (n, h * w, c // self.n_head, self.n_head * 3))
# [batch, seq_len, head, d*3] -> [batch, head, seq_len, d*3]
qkv = tf.transpose(qkv, perm=[0, 2, 1, 3])
# -> [batch, head, seq_len, d] * 3
q, k, v = tf.split(qkv, num_or_size_splits=3, axis=-1)
attn = tf.matmul(q, k, transpose_b=True) # q@k.T
attn = attn / tf.math.sqrt(tf.cast(k.shape[-1], tf.float32))
attn = tf.matmul(self.softmax(attn), v)
# ใใใใ็ตๅใใseq_len->h,wใซๅๅฒ
# [batch, head, seq_len, d] -> [batch, seq_len, head, d] -> [batch, h, w, head*d]
y = tf.transpose(attn, perm=[0, 2, 1, 3])
y = tf.reshape(y, (n, h, w, c))
return x + self.out_proj(y)
class Downsample(keras.layers.Layer):
def build(self, input_shape):
self.conv = kl.Conv2D(
filters=input_shape[-1],
kernel_size=3,
strides=2,
padding="same",
kernel_initializer=keras.initializers.Orthogonal(),
)
def call(self, x, training=False):
return self.conv(x, training=training)
class Upsample(keras.layers.Layer):
def build(self, input_shape):
self.conv = kl.Conv2D(
filters=input_shape[-1],
kernel_size=3,
strides=1,
padding="same",
)
def call(self, x, training=False):
# ๆ่ฟๅ่ฃ้ใง2ๅใซใชใตใคใบ
input_shape = tf.shape(x)
x = tf.image.resize(x, size=(input_shape[1] * 2, input_shape[2] * 2), method="nearest")
return self.conv(x, training=training)
class ResBlock(keras.Model):
def __init__(self, channels: int, use_attention: bool, **kwargs) -> None:
super().__init__(**kwargs)
self.channels = channels
self.use_attention = use_attention
def build(self, input_shape):
use_projection = input_shape[-1] != self.channels
self.proj = Conv2D1x1(self.channels) if use_projection else IdentityLayer()
self.norm1 = AdaGroupNorm()
self.act1 = kl.Activation("silu")
self.conv1 = Conv2D3x3(self.channels)
self.norm2 = AdaGroupNorm()
self.act2 = kl.Activation("silu")
self.conv2 = Conv2D3x3(self.channels)
self.attn = SelfAttention2D() if self.use_attention else IdentityLayer()
def call(self, x, condition, training=False):
r = self.proj(x, training=training)
x = self.norm1(x, condition, training=training)
x = self.act1(x, training=training)
x = self.conv1(x, training=training)
x = self.norm2(x, condition, training=training)
x = self.act2(x, training=training)
x = self.conv2(x, training=training)
x = x + r
x = self.attn(x, training=training)
return x
class ResBlocks(keras.Model):
def __init__(self, channels_list: list[int], use_attention: bool, **kwargs) -> None:
super().__init__(**kwargs)
self.resblocks = [ResBlock(c, use_attention) for c in channels_list]
def call(self, x, condition=None, shortcut=None, training=False):
outputs = []
for i, resblock in enumerate(self.resblocks):
if shortcut is not None:
x = tf.concat([x, shortcut[i]], axis=-1)
x = resblock(x, condition, training=training)
outputs.append(x)
return x, outputs
class UNet(keras.Model):
def __init__(self, img_shape: tuple, category_num: int, **kwargs) -> None:
super().__init__(**kwargs)
# condition
embedding_dim = 128
self.time_embedding1 = PositionalEmbedding(embedding_dim)
self.time_embedding2 = kl.Dense(embedding_dim, activation="gelu")
self.time_embedding3 = kl.Dense(embedding_dim)
self.category_embedding1 = kl.Embedding(category_num, embedding_dim)
self.category_embedding2 = kl.Dense(embedding_dim, activation="gelu")
self.category_embedding3 = kl.Dense(embedding_dim)
# Note: SelfAttentionใฏ็ปๅใตใคใบใๅคงใใๅ ดๅใซ้ฉ็จใใใจใกใขใชไธ่ถณใซใชใฃใ
# downsample
self.down_block11 = ResBlocks([32, 32], use_attention=False)
self.down_block12 = ResBlocks([32, 32], use_attention=False)
self.down_block13 = ResBlocks([32, 32], use_attention=False)
self.downsample1 = Downsample()
self.down_block21 = ResBlocks([32, 32], use_attention=False)
self.down_block22 = ResBlocks([32, 32], use_attention=False)
self.down_block23 = ResBlocks([32, 32], use_attention=False)
self.downsample2 = Downsample()
# middle
self.middle_block = ResBlocks([256], use_attention=True)
# upsample
self.up_block23 = ResBlocks([32, 32], use_attention=False)
self.up_block22 = ResBlocks([32, 32], use_attention=False)
self.up_block21 = ResBlocks([32, 32], use_attention=False)
self.upsample2 = Upsample()
self.up_block13 = ResBlocks([32, 32], use_attention=False)
self.up_block12 = ResBlocks([32, 32], use_attention=False)
self.up_block11 = ResBlocks([32, 32], use_attention=False)
self.upsample1 = Upsample()
# ๅบๅๅฑค๏ผๆญฃ่ฆๅๅธใฎใใคใบ็ปๅ๏ผ
self.out_layer = kl.Conv2D(1, (1, 1), padding="same")
# build & init weight
self(
[
np.zeros((1,) + img_shape),
np.zeros((1,)),
np.zeros((1,)),
]
)
@tf.function
def call(self, inputs, training=False):
# ๅ
ฅๅ: (ใใคใบ็ปๅใๆ้ในใใใใใซใใดใช)
x, t, category = inputs
# ๆ้ๅใ่พผใฟ
t_emb = self.time_embedding1(t, training=training)
t_emb = self.time_embedding2(t_emb, training=training)
t_emb = self.time_embedding3(t_emb, training=training)
# ใซใใดใชใฎๅใ่พผใฟ
c_emb = self.category_embedding1(category, training=training)
c_emb = self.category_embedding2(c_emb, training=training)
c_emb = self.category_embedding3(c_emb, training=training)
# ๆกไปถไปใใฏtimesteps+catetory
condition = t_emb + c_emb
# --- U-Net
x, o11 = self.down_block11(x, condition, training=training)
x, o12 = self.down_block12(x, condition, training=training)
x, o13 = self.down_block13(x, condition, training=training)
x = self.downsample1(x, training=training) # 28x28 -> 14x14
x, o21 = self.down_block21(x, condition, training=training)
x, o22 = self.down_block22(x, condition, training=training)
x, o23 = self.down_block23(x, condition, training=training)
x = self.downsample2(x, training=training) # 14x14 -> 7x7
x, _ = self.middle_block(x, condition, training=training)
x = self.upsample2(x, training=training) # 7x7 -> 14x14
x, _ = self.up_block23(x, condition, o23[::-1], training=training)
x, _ = self.up_block22(x, condition, o22[::-1], training=training)
x, _ = self.up_block21(x, condition, o21[::-1], training=training)
x = self.upsample1(x, training=training) # 14x14 -> 28x28
x, _ = self.up_block13(x, condition, o13[::-1], training=training)
x, _ = self.up_block12(x, condition, o12[::-1], training=training)
x, _ = self.up_block11(x, condition, o11[::-1], training=training)
x = self.out_layer(x, training=training)
return x
class EDM:
def __init__(self, img_shape, category_num, lr: float = 0.0001, sigma_data=0.5):
self.sigma_data = sigma_data
self.model = UNet(img_shape, category_num)
self.optimizer = keras.optimizers.Adam(learning_rate=lr)
def calc_c(self, sigma):
c_skip = self.sigma_data**2 / (sigma**2 + self.sigma_data**2)
c_out = sigma * self.sigma_data / tf.sqrt(sigma**2 + self.sigma_data**2)
c_in = 1 / tf.sqrt(self.sigma_data**2 + sigma**2)
c_noise = tf.math.log(sigma) / 4
return c_skip, c_out, c_in, c_noise
def call(self, x, sigma, category, training=False):
c_skip, c_out, c_in, c_noise = self.calc_c(sigma)
c_noise = tf.reshape(c_noise, (-1,))
fx = self.model([c_in * x, c_noise, category], training=training)
dx = c_skip * x + c_out * fx
return dx
def train(self, img, category, p_mean=-1.2, p_std=1.2):
# Noise distibution: ln(ฯ) ~ N(Pmena, Pstd^2)
r = np.random.randn(img.shape[0], 1, 1, 1).astype(np.float32)
sigma = np.exp(p_mean + r * p_std)
c_skip, c_out, c_in, c_noise = self.calc_c(sigma)
c_noise = tf.reshape(c_noise, (-1,))
# ฮป(ฯ): (ฯ^2+ฯd^2) / (ฯ*ฯd)^2
lambda_w = (sigma**2 + self.sigma_data**2) / ((sigma * self.sigma_data) ** 2)
# ใใคใบ
n = np.random.randn(*img.shape) * sigma
# tensorflowใซๅใ่พผใใๅฝขใซๅคๆ
n = tf.convert_to_tensor(n, dtype=tf.float32)
sigma = tf.convert_to_tensor(sigma.reshape((-1, 1, 1, 1)), dtype=tf.float32)
with tf.GradientTape() as tape:
output = self.model([c_in * (img + n), c_noise, category])
target = (img - c_skip * (img + n)) / c_out
loss = lambda_w * (c_out**2) * ((output - target) ** 2)
loss = tf.reduce_mean(loss)
grad = tape.gradient(loss, self.model.trainable_variables)
self.optimizer.apply_gradients(zip(grad, self.model.trainable_variables))
return loss.numpy()
def generate(
self,
size: int,
N: int,
category: int,
sigma_min=0.002,
sigma_max=80,
rho=7,
s_churn=0,
s_min=0,
s_max=float("inf"),
s_noise=1,
):
# Time steps
timesteps = self.create_timesptes(N, sigma_min, sigma_max, rho)
tf_category = tf.convert_to_tensor([category] * size, dtype=tf.float32)
samples_history = []
# ๅๆใตใณใใซ
var = timesteps[0]
x = np.random.randn(size, *img_shape).astype(np.float32) * var
x = tf.convert_to_tensor(x, dtype=tf.float32)
for t, next_t in tqdm(
zip(timesteps[:-1], timesteps[1:]),
total=N,
desc="sampling loop",
):
gamma = min(s_churn / N, np.sqrt(2) - 1) if s_min <= t <= s_max else 0
t_hat = t + gamma * t
e = np.random.randn(size, *img_shape).astype(np.float32) * s_noise
x_hat = x + np.sqrt(t_hat**2 - t**2) * e
# tf
t_hat = tf.convert_to_tensor([t_hat] * size, dtype=tf.float32)
t_hat = tf.reshape(t_hat, (-1, 1, 1, 1))
denoise = self.call(x_hat, t_hat, tf_category)
d = (x_hat - denoise) / t_hat
x_next = x_hat + (next_t - t_hat) * d
if next_t != 0:
# tf
t_next = tf.convert_to_tensor([next_t] * size, dtype=tf.float32)
t_next = tf.reshape(t_next, (-1, 1, 1, 1))
denoise = self.call(x_next, t_next, tf_category)
d_dash = (x_next - denoise) / next_t
x_next = x_hat + (next_t - t_hat) * (d / 2 + d_dash / 2)
samples_history.append(x_next)
x = x_next
return x, samples_history
@staticmethod
def create_timesptes(N: int, sigma_min=0.002, sigma_max=80, rho=7):
timesteps = []
for i in range(N):
t = (sigma_max ** (1 / rho) + i / (N - 1) * (sigma_min ** (1 / rho) - sigma_max ** (1 / rho))) ** rho
timesteps.append(t)
timesteps += [0] # N=0
return timesteps
# ----------------------------------
# main
# ----------------------------------
def plot_summary():
# U-Net
model = UNet(img_shape, category_num=10)
model.summary()
# Time steps
timesptes = EDM.create_timesptes(20)
plt.figure(figsize=(8, 5))
plt.plot(range(len(timesptes)), timesptes, marker=".")
plt.xlabel("i")
plt.ylabel("Time step")
plt.title("Visualization of Time steps")
plt.grid()
plt.show()
# ไฝ็ฝฎใจใณใณใผใใฃใณใฐใฎๅฏ่ฆๅ
PositionalEmbedding.plot()
def train(epochs: int, batch_size: int, lr: float):
x_img, x_category = create_dataset()
# ใขใใซ
edm = EDM(img_shape, category_num=10, lr=lr)
# ๅญฆ็ฟ็จใซใใผใฟใใใใๅ
train_dataset = tf.data.Dataset.from_tensor_slices((x_img, x_category)).shuffle(len(x_img)).batch(batch_size)
for epoch in range(epochs):
with tqdm(train_dataset, desc=f"Epoch {epoch + 1}/{epochs}") as pbar:
for img, category in pbar:
loss = edm.train(img, category)
pbar.set_postfix(loss=loss) # ๆๅคฑใ้ฒๆใใผใซ่กจ็คบ
edm.model.save_weights(Path(__file__).parent / "edm.weights.h5")
def generate(steps: int, category: int, **gen_kwargs):
edm = EDM(img_shape, category_num=10)
edm.model.load_weights(Path(__file__).parent / "edm.weights.h5")
# ็ๆ
num_w = 8
num_h = 2
num_samples = num_w * num_h
samples, samples_history = edm.generate(num_samples, steps, category, **gen_kwargs)
samples = decode_image(samples)
samples_history = decode_image(samples_history)
# ็ตๆ
plt.figure(figsize=(10, 4))
for i in range(num_samples):
plt.subplot(num_h, num_w, i + 1)
plt.imshow(samples[i, :, :, 0], cmap="gray")
plt.axis("off")
plt.savefig(Path(__file__).parent / f"plot_{category}.png")
# plt.show()
# ไฝๆ้็จ
index = 4
img_list = np.array(samples_history)[:, index, :, :, 0]
plt.figure(figsize=(20, 5))
step_idxs = list(range(0, len(img_list), int(steps / 12))) # ๅคใใฎใงไธๅฎ้้ใงๆใๅบใ
step_idxs += [len(img_list) - 1] # ๆๅพใ่ฟฝๅ
for i, idx in enumerate(step_idxs):
plt.subplot(1, len(step_idxs), i + 1)
plt.imshow(img_list[idx], cmap="gray")
plt.xticks([])
plt.yticks([])
plt.xlabel(f"step={idx}")
plt.savefig(Path(__file__).parent / f"plot_{category}_history.png")
# plt.show()
if __name__ == "__main__":
# plot_summary()
train(epochs=10, batch_size=128, lr=0.0002)
for i in range(10):
generate(steps=20, category=i)
ใขใใซใฎsummary
TFใฏ๏ผ้ๅฑคไปฅไธใฎใฏใฉในใฎshapeใใใใฉใซใใง่กจ็คบใใใพใใใ๏ผๆใใ่งฃๆฑบ็ญใ่ชฟในใฆใใใงใใโฆใๅใใๆนๆใใฆใใ ใใ๏ผ
Model: "u_net"
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโณโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโณโโโโโโโโโโโโโโโโโโ
โ Layer (type) โ Output Shape โ Param # โ
โกโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฉ
โ positional_embedding โ ? โ 0 โ
โ (PositionalEmbedding) โ โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ dense (Dense) โ (1, 128) โ 16,512 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ dense_1 (Dense) โ (1, 128) โ 16,512 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ embedding (Embedding) โ (1, 128) โ 1,280 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ res_blocks (ResBlocks) โ ? โ 53,154 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ res_blocks_1 (ResBlocks) โ ? โ 70,016 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ downsample (Downsample) โ ? โ 9,248 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ res_blocks_2 (ResBlocks) โ ? โ 70,016 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ res_blocks_3 (ResBlocks) โ ? โ 70,016 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ downsample_1 (Downsample) โ ? โ 9,248 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ res_blocks_4 (ResBlocks) โ ? โ 296,384 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ res_blocks_5 (ResBlocks) โ ? โ 164,608 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ res_blocks_6 (ResBlocks) โ ? โ 109,120 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ upsample (Upsample) โ ? โ 147,584 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ res_blocks_7 (ResBlocks) โ ? โ 109,120 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ res_blocks_8 (ResBlocks) โ ? โ 109,120 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ upsample_1 (Upsample) โ ? โ 9,248 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ conv2d (Conv2D) โ (1, 32, 32, 1) โ 33 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโ
Total params: 1,261,219 (4.81 MB)
Trainable params: 1,261,219 (4.81 MB)
Non-trainable params: 0 (0.00 B)
ๅญฆ็ฟ้ไธญใฎloss
Epoch 1/10: 100%|โโโโโโโโโโโโโโโโโโโ| 469/469 [09:29<00:00, 4.22s/it, loss=0.178]
Epoch 1/10: 100%|โโโโโโโโโโโโโโโโโโโ| 469/469 [09:29<00:00, 1.21s/it, loss=0.178]
Epoch 2/10: 100%|โโโโโโโโโโโโโโโโโโโ| 469/469 [08:44<00:00, 1.10s/it, loss=0.153]
Epoch 2/10: 100%|โโโโโโโโโโโโโโโโโโโ| 469/469 [08:44<00:00, 1.12s/it, loss=0.153]
Epoch 3/10: 100%|โโโโโโโโโโโโโโโโโโโ| 469/469 [08:15<00:00, 1.06s/it, loss=0.147]
Epoch 4/10: 100%|โโโโโโโโโโโโโโโโโโโ| 469/469 [08:02<00:00, 1.03s/it, loss=0.119]
Epoch 4/10: 100%|โโโโโโโโโโโโโโโโโโโ| 469/469 [08:02<00:00, 1.03s/it, loss=0.119]
Epoch 5/10: 100%|โโโโโโโโโโโโโโโโโโโ| 469/469 [07:27<00:00, 1.05it/s, loss=0.128]
Epoch 6/10: 100%|โโโโโโโโโโโโโโโโโโโ| 469/469 [07:15<00:00, 1.08it/s, loss=0.113]
Epoch 7/10: 100%|โโโโโโโโโโโโโโโโโโโ| 469/469 [07:13<00:00, 1.08it/s, loss=0.104]
Epoch 8/10: 100%|โโโโโโโโโโโโโโโโโโโ| 469/469 [07:17<00:00, 1.01it/s, loss=0.113]
Epoch 8/10: 100%|โโโโโโโโโโโโโโโโโโโ| 469/469 [07:17<00:00, 1.07it/s, loss=0.113]
Epoch 9/10: 100%|โโโโโโโโโโโโโโโโโโโ| 469/469 [07:07<00:00, 1.10it/s, loss=0.115]
Epoch 10/10: 100%|โโโโโโโโโโโโโโโโโโ| 469/469 [07:12<00:00, 1.08it/s, loss=0.12]
ใใใใซ
ใใใงๆกๆฃใขใใซใฏ็ตใใใชๆฐใใใ๏ผๅคๅโใค๏ฝ๏ผ
ๅญฆ็ฟใๅฐใชใใจๅใฎๅฐใใใขใใซใฎๆนใใใๅญฆ็ฟใงใใฆๅช็งใซๆใใพใใใ
ใใฎใขใใซใใใฉใกใผใฟๆฐใใใฃใจๅขใใใฆ้ทๆ้ๅญฆ็ฟใใใใฐ็ฒพๅบฆใไธใใใใใชๆใใฏใใฆใใพใใ
ๅพใๅญฆ็ฟ็ใๅพใ
ใซไธใใฆใใฃใๆนใใใใใใ
่ชฐใใฎๅ่ใซใชใใฐๅนธใใงใใ
















