vikashaws's picture
Add ivus_ddpm_generation: v-prediction + zero-terminal-SNR IVUS DDPM (Apache-2.0 weights)
ef22e4c verified
Raw History Blame Contribute Delete
2.55 kB
# Copyright (c) 2026 Amazon.com, Inc. or its affiliates
# SPDX-License-Identifier: MIT
"""MONAI-inferer-compatible wrapper around the diffusers ``UNet2DModel`` the bundle was trained with.
The weights were trained with diffusers, so the network is kept as-is rather than re-mapped onto
``monai.networks.nets.DiffusionModelUNet`` (different block layout and parameter names). The wrapper only
adapts the call signature to the one ``monai.inferers.DiffusionInferer`` uses:
``network(x, timesteps=t, context=None) -> Tensor``.
"""
from __future__ import annotations
from collections.abc import Sequence
import torch
from torch import nn
class IVUSDiffusionUNet(nn.Module):
"""Unconditional 2D pixel-space DDPM U-Net. The released weights predict the velocity ``v`` (v-prediction).
Args mirror ``diffusers.UNet2DModel``; the defaults are the trained ``base`` preset (~113.7M parameters).
"""
def __init__(
self,
sample_size: int = 256,
in_channels: int = 1,
out_channels: int = 1,
layers_per_block: int = 2,
block_out_channels: Sequence[int] = (128, 128, 256, 256, 512, 512),
down_block_types: Sequence[str] = (
"DownBlock2D",
"DownBlock2D",
"DownBlock2D",
"DownBlock2D",
"AttnDownBlock2D",
"DownBlock2D",
),
up_block_types: Sequence[str] = (
"UpBlock2D",
"AttnUpBlock2D",
"UpBlock2D",
"UpBlock2D",
"UpBlock2D",
"UpBlock2D",
),
attention_head_dim: int = 8,
norm_num_groups: int = 32,
dropout: float = 0.0,
) -> None:
super().__init__()
from diffusers import UNet2DModel
self.unet = UNet2DModel(
sample_size=sample_size,
in_channels=in_channels,
out_channels=out_channels,
layers_per_block=layers_per_block,
block_out_channels=tuple(block_out_channels),
down_block_types=tuple(down_block_types),
up_block_types=tuple(up_block_types),
attention_head_dim=attention_head_dim,
norm_num_groups=norm_num_groups,
dropout=dropout,
)
def forward(self, x: torch.Tensor, timesteps: torch.Tensor, context: torch.Tensor | None = None) -> torch.Tensor:
if context is not None:
raise ValueError("IVUSDiffusionUNet is unconditional; context must be None.")
return self.unet(x, timesteps, return_dict=False)[0]