File size: 5,780 Bytes
e12111a | 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 | """Standard diffusion training."""
from typing import Callable
import chex
import jax
import jax.numpy as jnp
from omegaconf import DictConfig
from imgx.datasets.constant import IMAGE, LABEL
from imgx.datasets.dataset_info import DatasetInfo
from imgx.diffusion.time_sampler import TimeSampler
from imgx.loss.segmentation import segmentation_loss
from imgx.metric.util import aggregate_metrics, aggregate_metrics_for_diffusion
from imgx.task.diffusion_segmentation.diffusion import DiffusionSegmentation
from imgx.task.diffusion_segmentation.train_state import TrainState
def get_loss_logits_metrics(
batch: dict[str, jnp.ndarray],
x_start: jnp.ndarray,
x_t: jnp.ndarray,
t_index: jnp.ndarray,
probs_t: jnp.ndarray,
noise: jnp.ndarray,
model_out: jnp.ndarray,
dataset_info: DatasetInfo,
loss_config: DictConfig,
diffusion_model: DiffusionSegmentation,
) -> tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray, dict[str, jnp.ndarray]]:
"""Get loss, logits, and metrics.
Args:
batch: batch of data.
x_start: x_start, shape (batch, ..., num_classes).
x_t: x_t, shape (batch, ..., num_classes).
t_index: t_index, shape (batch, ).
probs_t: probs_t, shape (batch, ).
noise: noise, shape (batch, ..., num_classes).
model_out: model_out, shape (batch, ..., num_classes).
dataset_info: dataset info to transform label to mask.
loss_config: have weights of diff losses.
diffusion_model: segmentation diffusion model.
Returns:
loss, loss_batch, logits, metrics.
"""
# diffusion loss, VLB, MSE, etc.
metrics_batch_diff, model_out = diffusion_model.diffusion_loss(
x_start=x_start,
x_t=x_t,
t_index=t_index,
noise=noise,
model_out=model_out,
)
# segmentation loss
logits = diffusion_model.model_out_to_logits_start(
model_out=model_out,
x_t=x_t,
t_index=t_index,
)
loss_batch, metrics_batch_seg = segmentation_loss(
logits=logits,
label=batch[LABEL],
dataset_info=dataset_info,
loss_config=loss_config,
)
# add aux loss
if loss_config["mse"] > 0:
loss_batch += loss_config["mse"] * metrics_batch_diff["mse_loss"]
if loss_config["vlb"] > 0:
loss_batch += loss_config["vlb"] * metrics_batch_diff["vlb_loss"]
# importance sampling
weights_t = probs_t * diffusion_model.num_timesteps # so that mean(weights_t) ~ 1
loss = jnp.mean(loss_batch * weights_t)
# record metrics
# each value is of shape (batch_size, )
metrics_batch = {
"t_index": t_index,
"probs_t": probs_t,
**metrics_batch_diff,
**metrics_batch_seg,
}
metrics = aggregate_metrics(metrics_batch)
metrics_diff = aggregate_metrics_for_diffusion(
metrics={
k: v
for k, v in metrics_batch_seg.items()
if ("class" not in k) and k.startswith("mean_")
},
t_index=t_index,
)
metrics = {"total_loss": loss, **metrics, **metrics_diff}
return loss, loss_batch, logits, metrics
def get_diffusion_loss_step(
train_state: TrainState,
dataset_info: DatasetInfo,
loss_config: DictConfig,
diffusion_model: DiffusionSegmentation,
time_sampler: TimeSampler,
) -> Callable[
[chex.ArrayTree, chex.ArrayTree, jax.Array],
tuple[jnp.ndarray, tuple[jnp.ndarray, chex.ArrayTree, jnp.ndarray, jnp.ndarray]],
]:
"""Return loss_step for vanilla diffusion.
Args:
train_state: train state.
dataset_info: dataset info to transform label to mask.
loss_config: have weights of diff losses.
diffusion_model: segmentation diffusion model.
time_sampler: time sampler for training.
Returns:
loss_step: loss step function.
"""
def loss_step(
params: chex.ArrayTree,
batch: dict[str, jnp.ndarray],
key: jax.Array,
) -> tuple[jnp.ndarray, tuple[jnp.ndarray, chex.ArrayTree, jnp.ndarray, jnp.ndarray]]:
"""Apply forward and calculate loss."""
key_dropout, key_t, key_noise = jax.random.split(key=key, num=3)
image, label = batch[IMAGE], batch[LABEL]
mask_true = dataset_info.label_to_mask(label, axis=-1, dtype=image.dtype)
x_start = diffusion_model.mask_to_x(mask=mask_true)
batch_size = image.shape[0]
# t, t_index, probs_t
t, t_index, probs_t = time_sampler.sample(
key=key_t,
batch_size=batch_size,
t_index_min=0, # inclusive
t_index_max=time_sampler.num_timesteps, # exclusive
loss_count_hist=train_state.loss_count_hist,
loss_sq_hist=train_state.loss_sq_hist,
)
# x_t
noise = diffusion_model.sample_noise(
key=key_noise, shape=x_start.shape, dtype=x_start.dtype
)
x_t = diffusion_model.q_sample(x_start=x_start, noise=noise, t_index=t_index)
mask_t = diffusion_model.x_to_mask(x_t)
# forward
model_out = train_state.apply_fn(
{"params": params},
True, # is_train
image,
mask_t,
t,
rngs={"dropout": key_dropout},
)
# loss
loss, loss_batch, logits, metrics = get_loss_logits_metrics(
batch=batch,
x_start=x_start,
x_t=x_t,
t_index=t_index,
probs_t=probs_t,
noise=noise,
model_out=model_out,
dataset_info=dataset_info,
loss_config=loss_config,
diffusion_model=diffusion_model,
)
return loss, (logits, metrics, loss_batch, t_index)
return loss_step
|