Fusion¶
generator.materialize_hybrid_samples() executes the stored
hybrid plan: every placement is fused into its target original
and the resulting image and label mask are written to the study. Two backends
are available:
| Backend | Method | Data |
|---|---|---|
classical (default) |
intensity matching and alpha blending with a distance-based falloff | 2D and 3D |
poisson |
gradient-domain blending | 2D and 3D; considerably slower in 3D |
config.fusion.parameters is the selected backend's parameter dataclass.
config.fusion.set_backend(name) selects a backend and resets the parameters
to its defaults. The registry creates the matching dataclass and validates parameter types,
ranges and backend compatibility. Validation also runs when saving/loading a
configuration and creating a backend. JSON stores backend parameters directly
under fusion.parameters; unknown parameter names are rejected.
Both backends share the intensity normalization, anomaly/context relation and
background settings (fusion_normalization_border_width, fusion_relation_*,
fusion_keep_bg and related parameters) described below.
Materialization processes every planned hybrid. A hybrid whose fusion fails is
stored with status failed and its error message. After all hybrids are
processed, a RuntimeError lists the failures; pass
materialize_hybrid_samples(raise_on_error=False) to get the successfully
generated hybrids back without the exception.
An anomaly that is larger than its target image after restoring the extraction
scale cannot be fused: the hybrid fails with an error that names both shapes.
This usually indicates a target mask that covers far more of the cutout than
the mask of the real anomaly, for example a mask derived with a
generation.background_threshold that is too low.
Classical fusion¶
config.fusion.set_backend("classical") # default backend
config.fusion.parameters.sq = 0.1
config.fusion.parameters.steepness_factor = 5.0
config.fusion.parameters.upsampling_factor = 2
config.fusion.parameters.dilation_size = 1
config.fusion.parameters.shave_pixels = 0
config.fusion.parameters.max_alpha = 0.9
config.fusion.parameters.fusion_variation = False
# Optional final pretrained harmonization step:
config.fusion.parameters.image_harmonization = "PCTNet" # None, "PCTNet", or "LBM"
config.validate()
The classical backend crops the generated anomaly to its target mask, restores its saved extraction scale, matches its intensity to the target context and alpha-blends it at the planned normalized center. It returns the fused image, a label mask in control coordinates and optional placement ROI artifacts. Multiple placements are materialized in their stored order and their label masks are combined.
Important classical parameters include:
max_alpha,sq,steepness_factorandupsampling_factor, which control the maximum anomaly contribution and the distance-transform alpha falloff. The blending weight rises from the mask border inward and reachesmax_alphaat1 / steepness_factorof the largest border distance inside the anomaly. A lowsteepness_factorgives a soft transition but fades out thin parts of an anomaly that also has a thick part; the default of12keeps them visible. Amax_alphabelow the default of1.0reduces the contrast of the whole anomaly by the same share.segmentation_min_alpha, which removes target-mask pixels whose anomaly contribution is below the configured threshold;0.0preserves the full target mask.image_harmonization, which optionally runs pretrained PCTNet or LBM after classical blending. It supports 2D samples with one or three channels and requires the separately installedlibcomdependency and a CUDA GPU. One-channel samples are converted through RGB and restored to grayscale. Output outside the current placement mask is discarded, preserving the control image and previously fused anomalies.Nonekeeps harmonization disabled.fusion_use_sobel_for_alpha_mask,sobel_threshold,dilation_sizeandshave_pixels, which enable and tune the optional edge-refined alpha path.fusion_variationplusalpha_variation,sq_variation,steepness_variationandselected_confidence, which sample blending parameters per placement.fusion_normalization_border_width:Nonedisables fusion-time intensity normalization,-1uses the whole control,0uses the available fallback context, and a positive value uses a local ring of that width around the target mask. The default is8: a ring of only a few pixels mostly covers the rim of the anomaly and underestimates its contrast to the surroundings.fusion_restore_anomaly_bg_relation,fusion_relation_mode,fusion_relation_norm_classes_separatelyandfusion_relation_min_context_size, which control whether the original anomaly/context relation is restored and how multiclass context is estimated.fusion_keep_bg,fusion_bg_value,fusion_relative_bg_thresholdandfusion_bg_exterior_only, which can preserve detected control-background pixels unchanged.
Local normalization uses robust median/IQR context statistics. Relation mode
delta preserves the original median difference; ratio preserves the median
ratio and is intended for strictly positive intensities away from zero. If a
local or class-specific ring contains too few values, the backend falls back to
available target-mask-outside context; if that is still insufficient, the scope
is left unnormalized.
The complete parameter list is available in the classical fusion reference.
Poisson fusion¶
Select gradient-domain blending as a separate backend:
config.fusion.set_backend("poisson")
config.fusion.parameters.guidance_mode = "source" # or "mixed"
config.fusion.parameters.solver_rtol = 1e-5
config.fusion.parameters.solver_max_iterations = 2000
config.validate()
Poisson fusion supports channel-first 2D (C,H,W) and true volumetric 3D
(C,D,H,W) data. It solves over a 4-neighbor grid in 2D and a 6-neighbor grid
in 3D. source guidance preserves anomaly gradients. mixed guidance selects
the stronger source or control gradient independently for each channel and
edge, which can retain important control-image boundaries.
Generated pixels outside the target mask are removed before placement but are not used as Poisson boundary guidance. The corresponding control-image values provide the exterior source baseline, preventing artificial gradients from the cleaned anomaly background. A one-pixel or one-voxel control halo supplies Dirichlet boundary values around the placed mask.
3D runtime and memory
True 3D Poisson blending constructs a sparse system over all target-mask
voxels and solves it once per image channel. It can require substantially
more computation time and memory than classical alpha blending. The first
3D fusion call on each backend instance emits a RuntimeWarning containing
the mask voxel and channel counts.
The complete parameter list is available in the Poisson fusion reference.