ImFusion C++ SDK 4.5.0
ImFusion::ML::OrientedROISampler Class Reference

#include <ImFusion/ML/Samplers/OrientedROISampler.h>

The OrientedROISampler samples m_numSamples samples of size m_roiSize from each dataset with an efficient GPU sampler. More...

Inheritance diagram for ImFusion::ML::OrientedROISampler:

Detailed Description

The OrientedROISampler samples m_numSamples samples of size m_roiSize from each dataset with an efficient GPU sampler.

All samples are gathered together in one SharedImageSet in an ImageElement. In most usecases, the images should be split into individual data items, with the SplitDataLoader (typically a simple '- split: {}' in your training pipeline).

The OrientedROISampler supports random sampling and target-guided sampling from label maps or keypoints. For target-guided sampling make sure to set the sample target with a MarkAsTargetOperation. sample_from_labels_proportion controls the expected fraction of ROI centers drawn from the target instead of uniformly from the full image; the actual number of target-guided samples is binomially distributed around that proportion. For targets of type Modality::LABEL, centers are drawn with class-agnostic foreground sampling by default: every non-zero voxel has equal probability regardless of label value, so larger structures dominate the draw. Setting equalize_label_classes to true switches to class-balanced sampling: a present label value is first picked uniformly at random and then a voxel of that class is picked uniformly at random, so small and large structures are centered with equal probability. For other target image modalities, target values act as relative heatmap weights: zero-valued voxels are never selected, larger values are sampled more often, values do not need to sum to 1, and negative values are not supported. If no valid target voxels are present, target-guided slots fall back to uniform random sampling.

Various augmentations can be used, including random rotation, flipping, shearing, scaling and center position jitter. These operations directly alter the sampling matrix, such that they can then be efficiently sampled. The matrices are retained in the resulting crops, and are not guaranteed to be affine and can be in left- or right-handed orientation systems. Use the SetMatrixToIdentityOperation to set the matrix to identity if required.

Note
For legacy reasons, the default padding mode of this sampler is set to Zero.

Public Types

enum class  Distribution { Normal = 0 , Uniform = 1 }
 Probability distribution used for random augmentation of additive parameters. More...
Public Types inherited from ImFusion::ML::Operation
enum  ProcessingPolicy { EverythingExceptLabels = 0 , Everything , OnlyLabels }
 Policy used by default when selecting on the fields on which the operation will be applied Note that "Labels" here refers to the target tag, not to be confused with Data::Modality::LABEL. More...
using PreProcessHook = std::function<void(Operation&, DataElement*)>
using PostProcessHook = std::function<void(Operation&, DataElement*)>

Public Member Functions

 OrientedROISampler (vec3i roiSize=vec3i(1, 1, 1), vec3 roiSpacing=vec3(1, 1, 1), int numSamples=1, vec3 randomRotationRange=vec3::Zero(), vec3 randomFlippingProbability=vec3::Zero(), vec3 randomShearingRange=vec3::Zero(), vec3 randomScalingRange=vec3::Zero(), vec3 randomJitterRange=vec3::Zero(), double sampleFromLabelsProportion=0.0, bool avoidBorders=false, bool alignCrop=false, std::optional< std::vector< vec3 > > centers={}, bool randomScalingLogarithmic=false, float randomRotationProbability=1.f, float randomShearingProbability=1.f, float randomScalingProbability=1.f, bool yAxisDown=false, bool squeeze=false, Distribution randomRotationDistribution=Distribution::Normal, Distribution randomShearingDistribution=Distribution::Normal, Distribution randomJitterDistribution=Distribution::Normal, bool equalizeLabelClasses=false)
void setCenters (std::vector< vec3 > centers)
std::optional< RegionOfInterestcomputeROI (const SharedImageSet *image) const override
std::unique_ptr< SharedImageSetextractROI (const SharedImageSet *image, std::optional< RegionOfInterest > precomputedRoi={}) const override
std::shared_ptr< SharedImagesample (SharedImageSet const &source, mat4 sourceMatix, mat4 targetMatrix, vec3i targetSize, vec3 targetSpacing, InterpolationMode interpolationMode, PaddingMode paddingMode) const
bool requiresLabel () const override
std::vector< mat4 > & sampleMatrices ()
void process (DataItem &item) override
 Main function that processes a DataItem in-place, may throw OperationException The data to be processed is determined by the active fields (if set) and the processing policy otherwise (if set), otherwise all fields are processed.
Public Member Functions inherited from ImFusion::ML::ImageROISampler
 ImageROISampler (std::string name="", PaddingMode paddingMode=PaddingMode::Clamp, std::optional< PaddingMode > labelPaddingMode=PaddingMode::Clamp)
virtual std::shared_ptr< SharedImageSetprocessImages (std::shared_ptr< SharedImageSet > input) const override
 Virtual function that every subclass should override.
virtual std::shared_ptr< BoundingBoxSetprocessBoxes (std::shared_ptr< BoundingBoxSet > input) const override
 Virtual function that every subclass should override.
virtual std::shared_ptr< KeypointSetprocessPoints (std::shared_ptr< KeypointSet > input) const override
 Virtual function that every subclass should override.
virtual std::shared_ptr< SharedImageSetprocessVectors (std::shared_ptr< SharedImageSet > input) const override
 Virtual function that every subclass should override.
bool doesNotModifyInput () const override
 Returns whether the operation is guaranteed to not modify its input element, either because it computes its output out-of-place or because it is a no-op.
void setPaddingMode (PaddingMode paddingMode)
PaddingMode paddingMode () const
void setLabelPaddingMode (PaddingMode labelPaddingMode)
PaddingMode labelPaddingMode () const
PaddingMode paddingModeForImage (const SharedImageSet &image) const
Public Member Functions inherited from ImFusion::ML::Operation
 Operation (std::string name, ProcessingPolicy processingPolicy)
 Derived classes must specify a name and whether, by default, the operation should also be applied to label maps.
virtual std::shared_ptr< SharedImageSetprocess (std::shared_ptr< SharedImageSet > input)
 Utility function to directly apply an operation on a SharedImageSet irrespective of active fields and processing policy.
virtual std::shared_ptr< KeypointSetprocess (std::shared_ptr< KeypointSet > input)
 Utility function to directly apply an operation on a set of KeypointSet irrespective of active fields and processing policy.
virtual std::shared_ptr< BoundingBoxSetprocess (std::shared_ptr< BoundingBoxSet > input)
 Utility function to directly apply an operation on a set of BoundingBoxSet irrespective of active fields and processing policy.
virtual std::unique_ptr< SharedImageSetprocess (std::unique_ptr< SharedImageSet > input) final
 Utility function with unique_ptr as input and output to avoid breaking changes (must not be overridden).
void addTemporaryPreProcessHook (PreProcessHook hook)
 Add a temporary hook that will only be active for the next process call and then automatically removed after the DataItem is processed.
void addTemporaryPostProcessHook (PostProcessHook hook)
 Add a temporary hook that will only be active for the next process call and then automatically removed after the DataItem is processed.
virtual bool checkRequiredItemsTypes (const DataItem &item) const
 Checks if the data item holds fields with types required by the operation.
const std::stringname () const
 Returns the name of the operation.
virtual bool configure (const Properties &properties)
 Configure an Operation with the given properties and return whether the configuration was successful.
virtual Properties configuration () const
 Return the Operation configuration.
void setActiveFields (std::optional< std::unordered_set< std::string > > activeFields)
std::optional< std::unordered_set< std::string > > activeFields () const
virtual void setProcessingPolicy (ProcessingPolicy policy)
ProcessingPolicy processingPolicy () const
std::optional< uint32_t > seed () const
 Return the current seed (if there is one).
virtual void seedRandomEngine (uint32_t seed)
 Set a seed for the random generator.
ML::ComputingDevice computingDevice () const
 Get the computing device.
virtual void setComputingDevice (ML::ComputingDevice device)
 Set the computing device selection strategy.
void configFailed (const std::string &missingParam) const
 Helper function to show an error message due to a bad configuration.
void logDeprecatedParam (const std::string &oldName, const std::string &newName) const
 Helper function to print a warning because of a deprecated parameter has been specified.
bool errorOnUnexpectedBehaviour () const
 Get the policy whether to treat unexpected behavior warnings as errors.
virtual void setErrorOnUnexpectedBehaviour (bool error)
 Treat unexpected behavior warnings as errors.
void registerParameter (ParameterBase *param)
 Register a parameter so that the operation knows about it, and can automatically configure it (unless it has been marked as manually configured parameter via the Operation::setManuallyConfiguredParameters method).
std::vector< ParameterBase * > parameters () const
 Return the list of registered parameters.
virtual bool supportsInversion () const
 Whether the operation supports inversion. Returns False unless the operation derives from InvertibleOperation.
const std::stringrecordIdentifier () const
 Get the operation's record identifier.
virtual void setRecordIdentifier (const std::string &recordIdentifier)
 Set the operation's record identifier.
const std::stringlogDomain () const
 Log domain for AdvancedParameter.
std::unordered_set< std::stringselectedFields (const DataItem &item) const
 Get the fields on which the operation will be applied.

Static Public Member Functions

static std::string distributionToString (Distribution d)
static Distribution stringToDistribution (const std::string &s)
Static Public Member Functions inherited from ImFusion::ML::ImageROISampler
static std::unique_ptr< SharedImageSetpadImageToDimensions (const SharedImageSet *image, const vec3i &newDims, ImFusion::PaddingMode paddingMode)
static std::unique_ptr< SharedImageSetcropImage (const SharedImageSet *image, const RegionOfInterest &roi, ML::ComputingDevice device)
static vec3i preprocessRoiSize (vec3i roiSize, const vec3i &imageDims)
 Replace -1s with the corresponding image dimension.
static void checkRoiSize (const SharedImageSet *image, const vec3i &roiSize)
Static Public Member Functions inherited from ImFusion::ML::Operation
static std::string processingPolicyToString (const ProcessingPolicy policy)
static ProcessingPolicy stringToProcessingPolicy (const std::string &s)
static std::unique_ptr< OperationcreateFromFactories (const Operation::Specs &specs)
 Helper function to create an operation from any factory (both C++ and Python factories are tested).

Public Attributes

OpParam< vec3i > m_roiSize = {"roi_size", vec3i::Ones(), this, ParamRequired::Yes}
 Fixed size of the ROI to sample.
OpParam< vec3 > m_roiSpacing = {"roi_spacing", vec3::Ones(), this, ParamRequired::Yes}
 Voxel spacing for resampling of the ROI to sample.
OpParam< int > m_numSamples = {"num_samples", 1, this, ParamRequired::No}
 Number of crops to generate from an image.
OpParam< float > m_randomRotationProbability = {"random_rotation_probability", 1.f, this, ParamRequired::No, "random_rotation_chance"}
 Probability to apply a random rotation.
OpParam< vec3 > m_randomRotationRange = {"random_rotation_range", vec3::Zero(), this, ParamRequired::No}
 Per-axis rotation magnitude. Standard deviation in degrees for "normal"; half-range in degrees for "uniform".
OpParam< Distributionm_randomRotationDistribution
 Sampling distribution for random_rotation_range.
OpParam< vec3 > m_randomFlippingProbability
 Probability to flip image over corresponding dimension.
OpParam< float > m_randomShearingProbability = {"random_shearing_probability", 1.f, this, ParamRequired::No, "random_shearing_chance"}
 Probability to apply a random shearing.
OpParam< vec3 > m_randomShearingRange = {"random_shearing_range", vec3::Zero(), this, ParamRequired::No}
 Per-axis shearing magnitude. Standard deviation for "normal"; half-range for "uniform".
OpParam< Distributionm_randomShearingDistribution
 Sampling distribution for random_shearing_range.
OpParam< float > m_randomScalingProbability = {"random_scaling_probability", 1.f, this, ParamRequired::No, "random_scaling_chance"}
 Probability to apply a random scaling.
OpParam< vec3 > m_randomScalingRange = {"random_scaling_range", vec3::Zero(), this, ParamRequired::No}
 Defines the range for random scaling along each dimension.
OpParam< bool > m_randomScalingLogarithmic = {"random_scaling_logarithmic", false, this, ParamRequired::No}
 Enables logarithmic sampling for scale factors.
OpParam< vec3 > m_randomJitterRange = {"random_jitter_range", vec3::Zero(), this, ParamRequired::No}
 Per-axis jitter added to label-guided sample centers. Standard deviation in mm for "normal"; half-range in mm for "uniform".
OpParam< Distributionm_randomJitterDistribution
 Sampling distribution for random_jitter_range.
OpParam< double > m_sampleFromLabelsProportion = {"sample_from_labels_proportion", 0.0, this, ParamRequired::No}
 Fraction of ROI centers sampled from the target instead of uniformly from the full image.
OpParam< bool > m_equalizeLabelClasses = {"equalize_label_classes", false, this, ParamRequired::No}
 Switch to class-balanced sampling when drawing target-guided centers from a Modality::LABEL target.
OpParam< bool > m_avoidBorders = {"avoid_borders", false, this, ParamRequired::No}
 Try to create crops that do not sample outside the border of the image.
OpParam< bool > m_yAxisDown = {"y_axis_down", false, this, ParamRequired::No}
 Flip Y-axis of the crop (used for certain legacy training pipelines).
OpParam< bool > m_squeeze = {"squeeze", false, this, ParamRequired::No}
 Squeeze crops to a 2D representation; requires that one of the dimensions in roi_size is 1. Used for 2D training pipelines to get rid of the 'flat' 3rd dimension.
OpParam< bool > m_alignCrop = {"align_crop", false, this, ParamRequired::No}
 Align crops with voxel grid (before applying random operations).
Public Attributes inherited from ImFusion::ML::ImageROISampler
OpParam< PaddingModep_paddingMode = {"padding_mode", PaddingMode::Clamp, this, ParamRequired::No}
OpParam< PaddingModep_labelPaddingMode = {"label_padding_mode", PaddingMode::Clamp, this, ParamRequired::No}

Additional Inherited Members

Protected Member Functions inherited from ImFusion::ML::Operation
virtual std::shared_ptr< TensorSetprocessTensors (std::shared_ptr< TensorSet > input) const
 Virtual function that every subclass should override.
void throwOperationError (const std::string &msg) const
 Helper function to throw an exception due to a runtime error.
void warnOperationUnexpectedBehaviour (const std::string &msg) const
 Helper function to warn about a behavior different than asked.
bool inputIsEmptyOrNull (const SharedImageSet *input) const
 Helper function to check if the input is empty or null and print a warning if so.
virtual bool useGPU (const SharedImageSet *input) const
 Helper function that returns which device the operation should choose based on its configuration and a given input.
void prepareInputForDevice (SharedImageSet &input) const
 Make sure the input is on the correct device for the current device strategy.
virtual bool allowChannelBatchOnGPU () const
 Whether the operation implements channel-batching so that it can still be run on input with more than 4 channels.
bool configureOnly (const Properties &properties, const std::vector< ParameterBase * > &paramSelection) noexcept
 Helper method to configure only a subset of parameters.
void setManuallyConfiguredParameters (const std::vector< ParameterBase * > &manuallyConfiguredParams)
 Disable the auto-configuration of some of the parameters so that they can be parsed manually (in the configure function of the derived class).
void applyPreProcessHooks (DataElement *element)
 Apply the current pre-process hooks to an element.
void applyPostProcessHooks (DataElement *element)
 Apply the current post-process hooks to an element.
Protected Member Functions inherited from ImFusion::Utils::NotCopyable
 NotCopyable (NotCopyable &&) noexcept=default
NotCopyable & operator= (NotCopyable &&) noexcept=default
 NotCopyable (const NotCopyable &)=delete
NotCopyable & operator= (const NotCopyable &)=delete
Protected Attributes inherited from ImFusion::ML::Operation
std::string m_name
ProcessingPolicy m_processingFieldsPolicy
 Flag specifying whether labels should be processed by default (when no active field has been specified).
std::optional< std::unordered_set< std::string > > m_activeFields
 Run the Operation only on those fields (if empty, will select suitable fields based on the current processing policy) when calling process().
std::unordered_set< std::stringm_alreadyWarned
 Set of warnings about unexpected behavior that have already been printed.
bool m_errorOnUnexpectedBehaviour = false
 Whether to throw an exception instead of warning about unexpected behavior.
std::optional< uint32_t > m_seed
Random::Generator m_randGenerator
ML::ComputingDevice m_device = ML::ComputingDevice::GPUIfOpenGl
std::vector< ParameterBase * > m_params
 All registered parameters.
std::vector< ParameterBase * > m_manuallyConfiguredParams
 All registered parameters that should not be configured automatically.
std::unordered_map< std::string, ElementTypem_requiredFieldsTypes
 List of all required field types used by checkRequiredItemsTypes.
std::string m_recordIdentifier
 User-provided unique identifier for this operation, used for recording processing history and inversion.
std::vector< PreProcessHook > m_preProcessHooks
std::vector< PostProcessHook > m_postProcessHooks
std::vector< PreProcessHook > m_temporaryPreProcessHooks
 Pre-process hooks that will be used for the next processing call only.
std::vector< PostProcessHook > m_temporaryPostProcessHooks
 Post-process hooks that will be used for the next processing call only.

Member Enumeration Documentation

◆ Distribution

Probability distribution used for random augmentation of additive parameters.

Enumerator
Normal 

Sample from N(0, range); range is the standard deviation.

Uniform 

Sample uniformly in [-range, +range].

Member Function Documentation

◆ computeROI()

std::optional< RegionOfInterest > ImFusion::ML::OrientedROISampler::computeROI ( const SharedImageSet * image) const
overridevirtual

◆ extractROI()

std::unique_ptr< SharedImageSet > ImFusion::ML::OrientedROISampler::extractROI ( const SharedImageSet * image,
std::optional< RegionOfInterest > precomputedRoi = {} ) const
overridevirtual

Reimplemented from ImFusion::ML::ImageROISampler.

◆ requiresLabel()

bool ImFusion::ML::OrientedROISampler::requiresLabel ( ) const
inlineoverridevirtual

Reimplemented from ImFusion::ML::ImageROISampler.

◆ process()

void ImFusion::ML::OrientedROISampler::process ( DataItem & item)
overridevirtual

Main function that processes a DataItem in-place, may throw OperationException The data to be processed is determined by the active fields (if set) and the processing policy otherwise (if set), otherwise all fields are processed.

Reimplemented from ImFusion::ML::ImageROISampler.

Member Data Documentation

◆ m_randomRotationDistribution

OpParam<Distribution> ImFusion::ML::OrientedROISampler::m_randomRotationDistribution
Initial value:
= {"random_rotation_distribution",
this,
ParamRequired::No,
"",
distributionToString,
stringToDistribution,
{"normal", "uniform"}}
@ Normal
Sample from N(0, range); range is the standard deviation.
Definition OrientedROISampler.h:49

Sampling distribution for random_rotation_range.

◆ m_randomFlippingProbability

OpParam<vec3> ImFusion::ML::OrientedROISampler::m_randomFlippingProbability
Initial value:
= {"random_flipping_probability",
vec3::Zero(),
this,
ParamRequired::No,
"random_flipping_chance"}

Probability to flip image over corresponding dimension.

◆ m_randomShearingDistribution

OpParam<Distribution> ImFusion::ML::OrientedROISampler::m_randomShearingDistribution
Initial value:
= {"random_shearing_distribution",
this,
ParamRequired::No,
"",
distributionToString,
stringToDistribution,
{"normal", "uniform"}}

Sampling distribution for random_shearing_range.

◆ m_randomScalingRange

OpParam<vec3> ImFusion::ML::OrientedROISampler::m_randomScalingRange = {"random_scaling_range", vec3::Zero(), this, ParamRequired::No}

Defines the range for random scaling along each dimension.

  • Standard mode (m_randomScalingLogarithmic == false): If randomScalingRange > 0, scale is sampled as: \( \max(|1.0 + \mathcal{N}(0, \text{randomScalingRange})|, 0.001) \).
  • Logarithmic mode (m_randomScalingLogarithmic == true): If randomScalingRange > 1, scale is uniformly sampled in \( [1/x, x] \) with \( x \in [1-\text{randomScalingRange}, 1+\text{randomScalingRange}] \).
Note
All non-positive components of randomScalingRange are skipped.

◆ m_randomScalingLogarithmic

OpParam<bool> ImFusion::ML::OrientedROISampler::m_randomScalingLogarithmic = {"random_scaling_logarithmic", false, this, ParamRequired::No}

Enables logarithmic sampling for scale factors.

  • false: Uses normal distribution-based scaling.
  • true: Uses uniform sampling in log space for multiplicative scaling.

◆ m_randomJitterDistribution

OpParam<Distribution> ImFusion::ML::OrientedROISampler::m_randomJitterDistribution
Initial value:
= {"random_jitter_distribution",
this,
ParamRequired::No,
"",
distributionToString,
stringToDistribution,
{"normal", "uniform"}}

Sampling distribution for random_jitter_range.

◆ m_sampleFromLabelsProportion

OpParam<double> ImFusion::ML::OrientedROISampler::m_sampleFromLabelsProportion = {"sample_from_labels_proportion", 0.0, this, ParamRequired::No}

Fraction of ROI centers sampled from the target instead of uniformly from the full image.

For Modality::LABEL targets, centers are sampled uniformly from all non-zero voxels. For other target image modalities, centers are sampled from the target as an unnormalized heatmap. For KeypointElement targets, a random keypoint is selected as the crop location. If no valid target voxels are present, target-guided slots fall back to uniform random sampling.

◆ m_equalizeLabelClasses

OpParam<bool> ImFusion::ML::OrientedROISampler::m_equalizeLabelClasses = {"equalize_label_classes", false, this, ParamRequired::No}

Switch to class-balanced sampling when drawing target-guided centers from a Modality::LABEL target.

When enabled, a present label value is first picked uniformly at random and then a voxel of that class is picked uniformly at random, so small and large structures are sampled with equal probability. The default (class-agnostic foreground sampling) treats every non-zero voxel equally, which lets the largest structures dominate. Has no effect for non-LABEL targets or keypoint targets.


The documentation for this class was generated from the following file:
  • ImFusion/ML/Samplers/OrientedROISampler.h
Search Tab / S to search, Esc to close