From 3aa4bf2cdda11561fc4d279310dec890efc0a9bf Mon Sep 17 00:00:00 2001 From: Sebastian Herholz Date: Mon, 26 Oct 2026 11:30:28 +0100 Subject: [PATCH 1/2] [sam] Refactoring and documenting the Sam VMM fitting code --- .../vmm/AdaptiveSplitandMergeFactory.h | 85 ++-- .../vmm/ParallaxAwareVonMisesFisherMixture.h | 88 +++- ...llaxAwareVonMisesFisherWeightedEMFactory.h | 454 ++++++++++-------- .../vmm/VMMChiSquareComponentMerger.h | 68 +-- .../vmm/VMMChiSquareComponentSplitter.h | 15 +- 5 files changed, 422 insertions(+), 288 deletions(-) diff --git a/openpgl/directional/vmm/AdaptiveSplitandMergeFactory.h b/openpgl/directional/vmm/AdaptiveSplitandMergeFactory.h index 05f8acb..4b71678 100644 --- a/openpgl/directional/vmm/AdaptiveSplitandMergeFactory.h +++ b/openpgl/directional/vmm/AdaptiveSplitandMergeFactory.h @@ -29,13 +29,15 @@ struct AdaptiveSplitAndMergeFactory // typedef WeightedEMVonMisesFisherFactory WeightedEMFactory; typedef ParallaxAwareVonMisesFisherWeightedEMFactory WeightedEMFactory; typedef VonMisesFisherChiSquareComponentSplitter Splitter; - typedef VonMisesFisherChiSquareComponentMerger Merger; + typedef VonMisesFisherChiSquareComponentMerger Merger; struct Configuration { typename WeightedEMFactory::Configuration weightedEMCfg; + // The min. Chi^2 threshold for splitting a mixture component float splittingThreshold{0.5f}; + // The max. Chi^2 threshold for merging a two mixture components float mergingThreshold{0.025f}; bool useSplitAndMerge{true}; @@ -43,8 +45,11 @@ struct AdaptiveSplitAndMergeFactory bool partialReFit{false}; uint32_t maxSplitItr{1}; + // The min. number of samples processed after the last merge step required to trigger a splitting step uint32_t minSamplesForSplitting{0}; + // The min. number of samples processed after the last merge step required to trigger partial refitting after splitting uint32_t minSamplesForPartialRefitting{0}; + // The min. number of samples processed after the last merge step required to trigger a merge step uint32_t minSamplesForMerging{0}; void serialize(std::ostream &stream) const; @@ -71,8 +76,6 @@ struct AdaptiveSplitAndMergeFactory typename WeightedEMFactory::SufficientStatistics sufficientStatistics; typename Splitter::ComponentSplitStatistics splittingStatistics; - // size_t numComponents {0}; - size_t numSamplesAfterLastSplit{0}; size_t numSamplesAfterLastMerge{0}; @@ -308,7 +311,7 @@ void AdaptiveSplitAndMergeFactory::fit(VMM &vmm, Statistics &s { const size_t numComponents = cfg.weightedEMCfg.initK; stats.clear(numComponents); - // intial fit + // Initial fitting of the mixture using standard weighted EM WeightedEMFactory factory = WeightedEMFactory(); typename WeightedEMFactory::FittingStatistics wemFitStats; factory.fitMixture(vmm, stats.sufficientStatistics, samples, numSamples, cfg.weightedEMCfg, wemFitStats); @@ -316,28 +319,29 @@ void AdaptiveSplitAndMergeFactory::fit(VMM &vmm, Statistics &s OPENPGL_ASSERT(vmm.isValid()); OPENPGL_ASSERT(vmm.getNumComponents() == stats.sufficientStatistics.getNumComponents()); OPENPGL_ASSERT(stats.isValid()); - /* */ + // We use split and merge to optimze the fitting result of the standard weighted EM algorithm which can get stuck in local + // maximas (e.g., one component trying to represent a multi-modal distribution or a distribtion containing a firely signal). if (cfg.useSplitAndMerge) { - // calculate the estimate of the integral of the function (e.g. radiance or importance) fitted by the VMM + // Calculate the estimate of the integral of the function (e.g. radiance or importance) represented by the VMM float mcEstimate = stats.sufficientStatistics.getSumWeights() / stats.sufficientStatistics.getNumSamples(); - // split the fitted components of the inital fit to match - // the observed samples -#ifdef OPENPGL_SHOW_PRINT_OUTS - std::cout << stats.sufficientStatistics.toString() << std::endl; -#endif + // Perfroming recursive splitting so that the mixture components best represent the observed data Splitter splitter = Splitter(); splitter.PerformRecursiveSplitting(vmm, stats.sufficientStatistics, cfg.splittingThreshold, mcEstimate, samples, numSamples, cfg.weightedEMCfg); + // After the recursive splitting process is finished we recaclulate the split statistics for current mixture from scratch splitter.CalculateSplitStatistics(vmm, stats.splittingStatistics, mcEstimate, samples, numSamples); OPENPGL_ASSERT(vmm.getNumComponents() == stats.getNumComponents()); OPENPGL_ASSERT(vmm.isValid()); + ////////////////////////////////////////////////////// + // Merging + ////////////////////////////////////////////////////// Merger merger = Merger(); - merger.PerformMerging(vmm, cfg.mergingThreshold, stats.sufficientStatistics, stats.splittingStatistics); + merger.PerformMerging(vmm, cfg.mergingThreshold, cfg.splittingThreshold, false, stats.sufficientStatistics, stats.splittingStatistics); OPENPGL_ASSERT(vmm.isValid()); } @@ -357,24 +361,25 @@ void AdaptiveSplitAndMergeFactory::update(VMM &vmm, Statistics OPENPGL_ASSERT(vmm.getNumComponents() == stats.getNumComponents()); OPENPGL_ASSERT(stats.isValid()); - // first update the mixture + // Update the mixture using standard weighted EM WeightedEMFactory factory = WeightedEMFactory(); typename WeightedEMFactory::FittingStatistics wemFitStats; - // stats.sufficientStatistics.clear(vmm._numComponents); const size_t prevNumberOfComponents = vmm._numComponents; factory.updateMixture(vmm, stats.sufficientStatistics, samples, numSamples, cfg.weightedEMCfg, wemFitStats); OPENPGL_ASSERT(vmm.isValid()); - // check if the update step added a new component. - // This happnes if samples are not covered by any existing component - // and we need to extend the splittingStats. + + // Check if the update step added a new component. + // This happens if samples are not covered by any existing component and we need to extend the splittingStats. if (prevNumberOfComponents < vmm._numComponents) { stats.splittingStatistics.setNumComponents(vmm._numComponents); } OPENPGL_ASSERT(stats.sufficientStatistics.isValid()); + // We use split and merge to optimize the fitting/update of the mixture to better reflect the observed data. if (cfg.useSplitAndMerge) { + // Calculate the estimate of the integral of the function (e.g. radiance or importance) represented by the VMM float mcEstimate = stats.sufficientStatistics.getSumWeights() / stats.sufficientStatistics.getNumSamples(); fitStats.numSamples = numSamples; @@ -383,28 +388,42 @@ void AdaptiveSplitAndMergeFactory::update(VMM &vmm, Statistics stats.numSamplesAfterLastSplit += numSamples; stats.numSamplesAfterLastMerge += numSamples; + ////////////////////////////////////////////////////// + // Splitting + ////////////////////////////////////////////////////// Splitter splitter = Splitter(); - // OPENPGL_ASSERT(stats.splittingStatistics.isValid()); + // Updating split statistics splitter.UpdateSplitStatistics(vmm, stats.splittingStatistics, mcEstimate, samples, numSamples); OPENPGL_ASSERT(stats.splittingStatistics.isValid()); + // We only perform splitting if we have observed enough samples after the last splitting round. if (stats.numSamplesAfterLastSplit >= cfg.minSamplesForSplitting) { + // The binary mask tagging the split components which need to be refitted typename WeightedEMFactory::PartialFittingMask mask; mask.resetToFalse(); + // The binary mask identifying if the previous components stats of the split components should be used as prior or not. + // In this version it is alsways set to false. + typename WeightedEMFactory::PartialFittingMask previousAsPriorMask; + previousAsPriorMask.resetToFalse(); + // Getting the list of split candidates sorted by their chi^2 values std::vector splitComps = stats.splittingStatistics.getSplitCandidates(); int totalSplitCount = 0; - // const size_t numComp = vmm._numComponents; + // For each split cadidate we check if its chi^2 value is above our split threshold and if we still have free components in our mixture. for (size_t k = 0; k < splitComps.size(); k++) { if (splitComps[k].chiSquareEst > cfg.splittingThreshold && vmm._numComponents < VMM::MaxComponents) { - splitter.SplitComponent(vmm, stats.splittingStatistics, stats.sufficientStatistics, splitComps[k].componentIndex); - mask.setToTrue(splitComps[k].componentIndex); - mask.setToTrue(vmm._numComponents - 1); - // std::cout << "split[" << totalSplitCount << "]: " << "\tidx0: " << splitComps[k].componentIndex << "\tidx1: " << vmm._numComponents-1 << std::endl; - totalSplitCount++; + // We split the component. + bool success = splitter.SplitComponent(vmm, stats.splittingStatistics, stats.sufficientStatistics, splitComps[k].componentIndex); + // If the split was successfull mark the new components for partial refitting. + if (success) + { + mask.setToTrue(splitComps[k].componentIndex); + mask.setToTrue(vmm._numComponents - 1); + totalSplitCount++; + } } } @@ -412,15 +431,12 @@ void AdaptiveSplitAndMergeFactory::update(VMM &vmm, Statistics OPENPGL_ASSERT(vmm.getNumComponents() == stats.getNumComponents()); OPENPGL_ASSERT(stats.isValid()); + // We perform a partial refit, if we performed any split and the number of samples is above our minimal sample threshold if (totalSplitCount > 0 && cfg.partialReFit && numSamples >= cfg.minSamplesForPartialRefitting) { - typename WeightedEMFactory::SufficientStatistics tempSuffStatistics = stats.sufficientStatistics; - tempSuffStatistics.clear(vmm._numComponents); - factory.partialUpdateMixture(vmm, mask, tempSuffStatistics, samples, numSamples, cfg.weightedEMCfg, wemFitStats); - stats.sufficientStatistics.setNumComponents(vmm._numComponents); - stats.sufficientStatistics.maskedReplace(mask, tempSuffStatistics); - // update number of components for the splitStats to - // account for additionaly added componetes based on not covered samples. + // For this partial refitting we are NOT using the previous component stats as prior + factory.partialUpdateMixture(vmm, mask, false, previousAsPriorMask, stats.sufficientStatistics, samples, numSamples, cfg.weightedEMCfg, wemFitStats); + // Updating the number of component in the splitting stats to account for the new split components stats.splittingStatistics.setNumComponents(vmm._numComponents); fitStats.numPartialUpdateWEMIterations = wemFitStats.numIterations; OPENPGL_ASSERT(vmm.isValid()); @@ -435,15 +451,18 @@ void AdaptiveSplitAndMergeFactory::update(VMM &vmm, Statistics std::cout << "update: totalSplitCount = " << totalSplitCount << "\t splitThreshold: " << cfg.splittingThreshold << std::endl; #endif } - + // OLD OPENPGL_ASSERT(vmm.isValid()); OPENPGL_ASSERT(vmm.getNumComponents() == stats.getNumComponents()); OPENPGL_ASSERT(stats.isValid()); + ////////////////////////////////////////////////////// + // Merging + ////////////////////////////////////////////////////// if (stats.numSamplesAfterLastMerge >= cfg.minSamplesForMerging) { Merger merger = Merger(); - size_t numMerges = merger.PerformMerging(vmm, cfg.mergingThreshold, stats.sufficientStatistics, stats.splittingStatistics); + size_t numMerges = merger.PerformMerging(vmm, cfg.mergingThreshold, cfg.splittingThreshold, false, stats.sufficientStatistics, stats.splittingStatistics); fitStats.numMerges = numMerges; stats.numSamplesAfterLastMerge = 0.0f; diff --git a/openpgl/directional/vmm/ParallaxAwareVonMisesFisherMixture.h b/openpgl/directional/vmm/ParallaxAwareVonMisesFisherMixture.h index efe2b98..18a1595 100644 --- a/openpgl/directional/vmm/ParallaxAwareVonMisesFisherMixture.h +++ b/openpgl/directional/vmm/ParallaxAwareVonMisesFisherMixture.h @@ -99,6 +99,8 @@ struct ParallaxAwareVonMisesFisherMixture float pdf(Vector3 direction) const; + float pdfComponent(Vector3 direction, const size_t idx) const; + Vector3 sample(const Vector2 sample) const; #ifdef USE_SIMD_CDF_SAMPLING @@ -111,6 +113,8 @@ struct ParallaxAwareVonMisesFisherMixture void splitComponent(const size_t &idx0, const size_t &idx1, const float &weight0, const float &weight1, const Vector3 &meanDirection0, const Vector3 &meanDirection1, const float &meanCosine0, const float &meanCosine1); + void splitFireFlyComponent(const size_t &idx0, const size_t &idx1, const float &fireFlyFrac, const Vector3 &fireFlyMeanDirection, const float &fireFlyMeanCosine); + void performRelativeParallaxShift(const Vector3 &shiftDirection); #ifdef OPENPGL_RADIANCE_CACHES @@ -135,6 +139,8 @@ struct ParallaxAwareVonMisesFisherMixture void clearComponent(const size_t &idx); + void clearComponents(); + // Getter methods for the PAVMM attributes size_t getNumComponents() const; @@ -245,8 +251,8 @@ std::string ParallaxAwareVonMisesFisherMixture_numComponents << std::endl; float sumWeights = 0.0f; - // for ( int k = 0; k < this->_numComponents; k++) - for (int k = 0; k < maxComponents; k++) + for (uint32_t k = 0; k < this->_numComponents; k++) + // for (uint32_t k = 0; k < maxComponents; k++) { const div_t tmp = div(k, static_cast(VecSize)); ss << "vmm[" << k << "]: " << "weight: " << this->_weights[tmp.quot][tmp.rem]; @@ -343,6 +349,30 @@ void ParallaxAwareVonMisesFisherMixture +void ParallaxAwareVonMisesFisherMixture::splitFireFlyComponent(const size_t &idx0, const size_t &idx1, const float &fireFlyFrac, + const Vector3 &fireFlyMeanDirection, const float &fireFlyMeanCosine) +{ + const div_t tmpIdx0 = div(idx0, static_cast(VectorSize)); + const div_t tmpIdx1 = div(idx1, static_cast(VectorSize)); + + const float weight = _weights[tmpIdx0.quot][tmpIdx0.rem]; + _weights[tmpIdx0.quot][tmpIdx0.rem] *= (1.f - fireFlyFrac); + + _weights[tmpIdx1.quot][tmpIdx1.rem] = weight * fireFlyFrac; + _meanDirections[tmpIdx1.quot].x[tmpIdx1.rem] = fireFlyMeanDirection.x; + _meanDirections[tmpIdx1.quot].y[tmpIdx1.rem] = fireFlyMeanDirection.y; + _meanDirections[tmpIdx1.quot].z[tmpIdx1.rem] = fireFlyMeanDirection.z; + _meanCosines[tmpIdx1.quot][tmpIdx1.rem] = fireFlyMeanCosine; + _kappas[tmpIdx1.quot][tmpIdx1.rem] = MeanCosineToKappa(fireFlyMeanCosine); + _distances[tmpIdx1.quot][tmpIdx1.rem] = _distances[tmpIdx0.quot][tmpIdx0.rem]; + if (idx1 == _numComponents) + { + _numComponents++; + } + _calculateNormalization(); +} + template void ParallaxAwareVonMisesFisherMixture::mergeComponents(const size_t &idx0, const size_t &idx1) { @@ -517,6 +547,39 @@ void ParallaxAwareVonMisesFisherMixture +void ParallaxAwareVonMisesFisherMixture::clearComponents() +{ + const embree::vfloat ones(1.0f); + const embree::vfloat zeros(0.0f); + const embree::vfloat zeroKappaNorm(ONE_OVER_FOUR_PI); + + for (uint32_t k = 0; k < NumVectors; k++) + { + _weights[k] = zeros; + _kappas[k] = zeros; + _eMinus2Kappa[k] = ones; + _meanCosines[k] = zeros; + _normalizations[k] = zeroKappaNorm; + + _meanDirections[k].x = zeros; + _meanDirections[k].y = zeros; + _meanDirections[k].z = ones; + + _distances[k] = zeros; + +#ifdef OPENPGL_RADIANCE_CACHES + _fluenceRGBWeightsWithMIS[k].x = zeros; + _fluenceRGBWeightsWithMIS[k].y = zeros; + _fluenceRGBWeightsWithMIS[k].z = zeros; + + _fluenceRGBWeights[k].x = zeros; + _fluenceRGBWeights[k].y = zeros; + _fluenceRGBWeights[k].z = zeros; +#endif + } +} + template void ParallaxAwareVonMisesFisherMixture::serialize(std::ostream &stream) const { @@ -879,6 +942,27 @@ float ParallaxAwareVonMisesFisherMixture +float ParallaxAwareVonMisesFisherMixture::pdfComponent(Vector3 direction, const size_t idx) const +{ + const div_t tmpIdx = div(idx, VecSize); + + embree::vfloat pdf = {0.0f}; + embree::Vec3> vec3Direction(direction[0], direction[1], direction[2]); + + const embree::vfloat ones(1.0f); + const embree::vfloat zeros(0.0f); + + size_t k = tmpIdx.quot; + + const embree::vfloat cosTheta = embree::dot(vec3Direction, _meanDirections[k]); + const embree::vfloat cosThetaMinusOne = embree::min(cosTheta - ones, zeros); + const embree::vfloat eval = _normalizations[k] * embree::fastapprox::exp>(_kappas[k] * cosThetaMinusOne); + pdf += _weights[k] * eval; + + return pdf[tmpIdx.rem]; +} + template bool ParallaxAwareVonMisesFisherMixture::softAssignment( Vector3 direction, typename ParallaxAwareVonMisesFisherMixture::SoftAssignment &softAssign) const diff --git a/openpgl/directional/vmm/ParallaxAwareVonMisesFisherWeightedEMFactory.h b/openpgl/directional/vmm/ParallaxAwareVonMisesFisherWeightedEMFactory.h index d19b5f2..77e672c 100644 --- a/openpgl/directional/vmm/ParallaxAwareVonMisesFisherWeightedEMFactory.h +++ b/openpgl/directional/vmm/ParallaxAwareVonMisesFisherWeightedEMFactory.h @@ -83,6 +83,7 @@ struct ParallaxAwareVonMisesFisherWeightedEMFactory PartialFittingMask() = default; void resetToFalse(); + void resetToTrue(); void resetToTrue(const size_t &numComponents); void setToTrue(const size_t &idx); void setToFalse(const size_t &idx); @@ -90,7 +91,7 @@ struct ParallaxAwareVonMisesFisherWeightedEMFactory std::string toString() const; }; - struct SufficientStatistics //: public WEMVMMFactory::SufficientStatistics + struct SufficientStatistics { // FittingStatistics public: @@ -103,11 +104,22 @@ struct ParallaxAwareVonMisesFisherWeightedEMFactory embree::vfloat surfaceSampleNumberWeights[VMM::NumVectors]; #endif float sumWeights{0.f}; + + // number of effective samples for the fit this number can be smaller than overallNumSamples + // due to decaying the statistics float numSamples{0.f}; + + // the total number of samples used for fitting without the decay float overallNumSamples{0.f}; + + // Number of mixture components size_t numComponents{VMM::MaxComponents}; + // If the statistics are already normalized or not bool normalized{false}; + float norm{1.f}; + float inv_norm{1.f}; + embree::vfloat sumOfDistanceWeightes[VMM::NumVectors]; SufficientStatistics() = default; @@ -154,6 +166,11 @@ struct ParallaxAwareVonMisesFisherWeightedEMFactory return sumWeights; } + inline float getMeanSamplesWeights() const + { + return sumWeights / numSamples; + } + inline void setNumComponents(const size_t &numComponents) { this->numComponents = numComponents; @@ -191,8 +208,11 @@ struct ParallaxAwareVonMisesFisherWeightedEMFactory void updateMixture(VMM &vmm, SufficientStatistics &previousStats, const SampleData *samples, const size_t numSamples, const Configuration &cfg, FittingStatistics &fitStats) const; - void partialUpdateMixture(VMM &vmm, PartialFittingMask &mask, SufficientStatistics &previousStats, const SampleData *samples, const size_t numSamples, const Configuration &cfg, - FittingStatistics &fitStats) const; + void partialUpdateMixture(VMM &vmm, PartialFittingMask &mask, const bool usePreviousStatsAsPrior, PartialFittingMask &previousAsPriorMask, SufficientStatistics &previousStats, + const SampleData *samples, const size_t numSamples, const Configuration &cfg, FittingStatistics &fitStats) const; + + void partialMergeSufficientStatisticsWithPriors(PartialFittingMask &mask, SufficientStatistics ¤tStats, const bool usePreviousStatsAsPrior, + const PartialFittingMask &previousAsPriorMask, const SufficientStatistics &previousStats) const; #ifdef OPENPGL_RADIANCE_CACHES void updateFluenceEstimate(VMM &vmm, const SampleData *samples, const size_t numSamples, const size_t numZeroValueSamples, const SampleStatistics &sampleStatistics) const; @@ -218,20 +238,11 @@ struct ParallaxAwareVonMisesFisherWeightedEMFactory float weightedExpectationStep(VMM &vmm, SufficientStatistics &stats, UnassignedSamplesStatistics &unassignedStats, const SampleData *samples, const size_t numSamples) const; - void weightedMaximumAPosteriorStep(VMM &vmm, const SufficientStatistics &previousStats, const SufficientStatistics ¤tStats, const Configuration &cfg) const; - - void estimateMAPWeights(VMM &vmm, const SufficientStatistics ¤tStats, const SufficientStatistics &previousStats, const float &_weightPrior) const; + void weightedMaximumAPosteriorStep(VMM &vmm, const SufficientStatistics ¤tStats, const Configuration &cfg) const; - void estimateMAPMeanDirectionAndConcentration(VMM &vmm, const SufficientStatistics ¤tStats, const SufficientStatistics &previousStats, const Configuration &cfg) const; + void estimateMAPWeights(VMM &vmm, const SufficientStatistics ¤tStats, const float &_weightPrior) const; - void partialWeightedMaximumAPosteriorStep(VMM &vmm, const PartialFittingMask &mask, SufficientStatistics &previousStats, SufficientStatistics ¤tStats, - const Configuration &cfg) const; - - void estimatePartialMAPWeights(VMM &vmm, const PartialFittingMask &mask, SufficientStatistics ¤tStats, SufficientStatistics &previousStats, - const float &_weightPrior) const; - - void estimatePartialMAPMeanDirectionAndConcentration(VMM &vmm, const PartialFittingMask &mask, SufficientStatistics ¤tStats, SufficientStatistics &previousStats, - const Configuration &cfg) const; + void estimateMAPMeanDirectionAndConcentration(VMM &vmm, const SufficientStatistics ¤tStats, const Configuration &cfg) const; void handleUnassignedSampleStats(UnassignedSamplesStatistics &unassignedStats, VMM &vmm, SufficientStatistics ¤tStats, SufficientStatistics &previousStats) const; @@ -403,6 +414,8 @@ void ParallaxAwareVonMisesFisherWeightedEMFactory::SufficientS stream.write(reinterpret_cast(&overallNumSamples), sizeof(float)); stream.write(reinterpret_cast(&numComponents), sizeof(size_t)); stream.write(reinterpret_cast(&normalized), sizeof(bool)); + stream.write(reinterpret_cast(&norm), sizeof(float)); + stream.write(reinterpret_cast(&inv_norm), sizeof(float)); #ifdef OPENPGL_VSP_GUIDING serializeFloatVectors(stream, volumeContributionWeights); serializeFloatVectors(stream, surfaceContributionWeights); @@ -422,6 +435,8 @@ void ParallaxAwareVonMisesFisherWeightedEMFactory::SufficientS stream.read(reinterpret_cast(&overallNumSamples), sizeof(float)); stream.read(reinterpret_cast(&numComponents), sizeof(size_t)); stream.read(reinterpret_cast(&normalized), sizeof(bool)); + stream.read(reinterpret_cast(&norm), sizeof(float)); + stream.read(reinterpret_cast(&inv_norm), sizeof(float)); #ifdef OPENPGL_VSP_GUIDING deserializeFloatVectors(stream, volumeContributionWeights); deserializeFloatVectors(stream, surfaceContributionWeights); @@ -454,8 +469,11 @@ void ParallaxAwareVonMisesFisherWeightedEMFactory::SufficientS } sumWeights = 0.0f; - numSamples = 0; + numSamples = 0.0f; + overallNumSamples = 0.0f; normalized = false; + norm = 1.0f; + inv_norm = 1.0f; } template @@ -510,6 +528,7 @@ template std::string ParallaxAwareVonMisesFisherWeightedEMFactory::SufficientStatistics::toString() const { std::stringstream ss; + ss << std::setprecision(10); ss << "SufficientStatistics:" << std::endl; ss << "\tsumWeights = " << sumWeights << std::endl; ss << "\tnumSamples = " << numSamples << std::endl; @@ -517,13 +536,21 @@ std::string ParallaxAwareVonMisesFisherWeightedEMFactory::Suff ss << "\tnumComponents = " << numComponents << std::endl; ss << "\tisNormalized = " << normalized << std::endl; // for (size_t k = 0; k < numComponents ; k++) + + ss << "\tnorm = " << norm << std::endl; + ss << "\tinv_norm = " << inv_norm << std::endl; + + float sumWeightedStats{0.0f}; for (size_t k = 0; k < VMM::MaxComponents; k++) { int i = k / VMM::VectorSize; int j = k % VMM::VectorSize; - ss << "\tstat[" << k << "]:" << "\tsumWeightedStats = " << sumOfWeightedStats[i][j] << "\tsumWeightedDirections = [" << sumOfWeightedDirections[i].x[j] << ",\t" - << sumOfWeightedDirections[i].y[j] << ",\t" << sumOfWeightedDirections[i].z[j] << "]" << "\tsumWeightedDistanceWeights = " << sumOfDistanceWeightes[i][j] << std::endl; + ss << "\tstat[" << k << "]:" << "\tsumWeightedStats = " << sumOfWeightedStats[i][j] << "\tsumWeightedStats = " << sumOfWeightedStats[i][j] * inv_norm + << "\tsumWeightedDirections = [" << sumOfWeightedDirections[i].x[j] << ",\t" << sumOfWeightedDirections[i].y[j] << ",\t" << sumOfWeightedDirections[i].z[j] << "]" + << "\tsumWeightedDistanceWeights = " << sumOfDistanceWeightes[i][j] << std::endl; + sumWeightedStats += sumOfWeightedStats[i][j]; } + ss << "\tsumWeightedStats = " << sumWeightedStats << std::endl; return ss.str(); } @@ -548,6 +575,9 @@ void ParallaxAwareVonMisesFisherWeightedEMFactory::SufficientS { sumWeights = embree::reduce_add(newSumWeights); } + + norm = numSamples / sumWeights; + inv_norm = sumWeights / numSamples; } template @@ -655,35 +685,42 @@ void ParallaxAwareVonMisesFisherWeightedEMFactory::SufficientS sumWeightedStatsVec += sumOfWeightedStats[k]; } sumWeights = reduce_add(sumWeightedStatsVec); - embree::vfloat norm(sumWeights > FLT_EPSILON ? _numSamples / sumWeights : 1.0f); + norm = sumWeights > FLT_EPSILON ? _numSamples / sumWeights : 1.0f; + inv_norm = _numSamples > FLT_EPSILON ? sumWeights / _numSamples : 1.0f; + embree::vfloat normVec(_numSamples / sumWeights); for (int k = 0; k < cnt; k++) { - sumOfWeightedDirections[k] *= norm; - sumOfWeightedStats[k] *= norm; + sumOfWeightedDirections[k] *= normVec; + sumOfWeightedStats[k] *= normVec; } normalized = true; + overallNumSamples = numSamples; } - +/* */ template typename ParallaxAwareVonMisesFisherWeightedEMFactory::SufficientStatistics & ParallaxAwareVonMisesFisherWeightedEMFactory::SufficientStatistics::operator+=( const typename ParallaxAwareVonMisesFisherWeightedEMFactory::SufficientStatistics &stats) { + OPENPGL_ASSERT(this->numComponents == stats.numComponents); // TODO: check for normalization + if ((this->overallNumSamples > 0.f && !this->normalized) || (stats.overallNumSamples > 0.f && !stats.normalized)) + std::cout << "ERROR: normalization" << std::endl; const int cnt = (numComponents + VMM::VectorSize - 1) / VMM::VectorSize; this->sumWeights += stats.sumWeights; this->numSamples += stats.numSamples; - this->overallNumSamples += stats.numSamples; + this->overallNumSamples += stats.overallNumSamples; for (int k = 0; k < cnt; k++) { this->sumOfWeightedDirections[k] += stats.sumOfWeightedDirections[k]; this->sumOfWeightedStats[k] += stats.sumOfWeightedStats[k]; this->sumOfDistanceWeightes[k] += stats.sumOfDistanceWeightes[k]; } - + this->norm = this->numSamples / this->sumWeights; + this->inv_norm = this->sumWeights / this->numSamples; return *this; } @@ -793,11 +830,10 @@ template typename ParallaxAwareVonMisesFisherWeightedEMFactory::VMM ParallaxAwareVonMisesFisherWeightedEMFactory::VMMfromSufficientStatistics( const SufficientStatistics &suffStats, const Configuration &cfg) const { - SufficientStatistics previousStats; - previousStats.clear(suffStats.numComponents); VMM vmm; + // vmm.clearComponents(); // TODO: seems to be a debug check vmm._numComponents = suffStats.numComponents; - weightedMaximumAPosteriorStep(vmm, previousStats, suffStats, cfg); + weightedMaximumAPosteriorStep(vmm, suffStats, cfg); return vmm; } @@ -807,13 +843,9 @@ void ParallaxAwareVonMisesFisherWeightedEMFactory::fitMixture( const Configuration &cfg, FittingStatistics &fitStats) const { const size_t numComponents = cfg.initK; - // VonMisesFisherFactory< TVMMDistribution>::InitUniformVMM( vmm, numComponents, 5.0f); this->InitUniformVMM(vmm, numComponents, cfg.initKappa); - // OPENPGL_ASSERT(vmm.isValid()); - // SufficientStatistics stats; stats.clear(numComponents); stats.normalized = true; - // stats.clearAll(); updateMixture(vmm, stats, samples, numSamples, cfg, fitStats); } @@ -821,6 +853,11 @@ template void ParallaxAwareVonMisesFisherWeightedEMFactory::handleUnassignedSampleStats(UnassignedSamplesStatistics &unassignedStats, VMM &vmm, SufficientStatistics ¤tStats, SufficientStatistics &previousStats) const { + OPENPGL_ASSERT(embree::isvalid(unassignedStats.sumOfUnassignedWeights)); + OPENPGL_ASSERT(embree::isvalid(unassignedStats.sumUnassignedWeightedDirections.x)); + OPENPGL_ASSERT(embree::isvalid(unassignedStats.sumUnassignedWeightedDirections.y)); + OPENPGL_ASSERT(embree::isvalid(unassignedStats.sumUnassignedWeightedDirections.z)); + const div_t tmpK = div(currentStats.numComponents, TVMMDistribution::VectorSize); currentStats.numComponents++; currentStats.sumOfWeightedStats[tmpK.quot][tmpK.rem] = unassignedStats.sumOfUnassignedWeights; @@ -850,21 +887,30 @@ void ParallaxAwareVonMisesFisherWeightedEMFactory::updateMixtu float previousLogLikelihood = 0.0f; float inv_previousLogLikelihood = 1.0f; UnassignedSamplesStatistics unassignedStats; + + // Running multiple EM iterations until the mixture is converged or a number of max iterations is reached while (!converged && currentEMIteration < cfg.maxEMIterrations) { + // Running the E-step to calculate the sufficient statistics and estimate the current log likelihood float logLikelihood = weightedExpectationStep(vmm, currentStats, unassignedStats, samples, numSamples); + // Special handling of samples which are not covered by any mixture component (i.e., adding an additional/special component) if (unassignedStats.sumOfUnassignedWeights > 0.0f && currentStats.numComponents < TVMMDistribution::MaxComponents) { handleUnassignedSampleStats(unassignedStats, vmm, currentStats, previousStats); } OPENPGL_ASSERT(!currentStats.isNormalized()); + // Normalizing sufficient statistics so that the weighted stats per component can be re-interpreded by number of samples currentStats.normalize(currentStats.numSamples); OPENPGL_ASSERT(currentStats.isValid()); - weightedMaximumAPosteriorStep(vmm, currentStats, previousStats, cfg); + // Adding/Merging the sufficient statistics of the previous training/update iteration to act as a prior to implement + // incremental EM fitting + currentStats += previousStats; + // Deriving the mixture parameter from the merged suffcient statistics using MAP and addtional global priors. + weightedMaximumAPosteriorStep(vmm, currentStats, cfg); currentEMIteration++; - // TODO: Add convergence check + // Checking of the fitting converged (i.e., the loglikelihood only changed minimally) if (currentEMIteration > 1) { float relLogLikelihoodDifference = std::fabs(logLikelihood - previousLogLikelihood) * inv_previousLogLikelihood; @@ -872,22 +918,22 @@ void ParallaxAwareVonMisesFisherWeightedEMFactory::updateMixtu { converged = true; } - // std::cout << "logLikelihood:" << logLikelihood << "\t previousLogLikelihood: "<< previousLogLikelihood << "\t relLogLikelihoodDifference: " << - // relLogLikelihoodDifference << std::endl; previousLogLikelihood = logLikelihood; inv_previousLogLikelihood = 1.0f / std::fabs(logLikelihood); } } - previousStats += currentStats; + + // The merged sufficient stats from the last iteration are now the new previous/prior stats + previousStats = currentStats; fitStats.numSamples = numSamples; fitStats.numIterations = currentEMIteration; fitStats.summedWeightedLogLikelihood = previousLogLikelihood; - // td::cout << "converged:" << currentEMIteration << std::endl; } template -void ParallaxAwareVonMisesFisherWeightedEMFactory::partialUpdateMixture(VMM &vmm, PartialFittingMask &mask, SufficientStatistics &previousStats, +void ParallaxAwareVonMisesFisherWeightedEMFactory::partialUpdateMixture(VMM &vmm, PartialFittingMask &mask, const bool usePreviousStatsAsPrior, + PartialFittingMask &previousAsPriorMask, SufficientStatistics &previousStats, const SampleData *samples, const size_t numSamples, const Configuration &cfg, FittingStatistics &fitStats) const { @@ -899,25 +945,33 @@ void ParallaxAwareVonMisesFisherWeightedEMFactory::partialUpda bool converged = false; float previousLogLikelihood = 0.0f; float inv_previousLogLikelihood = 1.0f; - UnassignedSamplesStatistics unassignedStats; + + // Running multiple EM iterations until the mixture is converged or a number of max iterations is reached. + // During these iterations only the masked mixture components are updated while (!converged && currentEMIteration < cfg.maxEMIterrations) { + // Running the E-step to calculate the sufficient statistics and estimate the current log likelihood float logLikelihood = weightedExpectationStep(vmm, currentStats, unassignedStats, samples, numSamples); + // Special handling of samples which are not covered by any mixture component (i.e., adding an additional/special component) if (unassignedStats.sumOfUnassignedWeights > 0.0f && currentStats.numComponents < TVMMDistribution::MaxComponents) { handleUnassignedSampleStats(unassignedStats, vmm, currentStats, previousStats); mask.setToTrue(vmm._numComponents - 1); } - OPENPGL_ASSERT(currentStats.isValid()); OPENPGL_ASSERT(!currentStats.isNormalized()); + // Normalizing sufficient statistics so that the weighted stats per component can be re-interpreded by number of samples currentStats.normalize(currentStats.numSamples); OPENPGL_ASSERT(currentStats.isValid()); - partialWeightedMaximumAPosteriorStep(vmm, mask, currentStats, previousStats, cfg); + // Replacing the sufficient stats of the component which should not be updated with the previous suffient stats. + partialMergeSufficientStatisticsWithPriors(mask, currentStats, usePreviousStatsAsPrior, previousAsPriorMask, previousStats); + // Deriving the mixture parameter from the merged suffcient statistics using MAP and addtional global priors. + weightedMaximumAPosteriorStep(vmm, currentStats, cfg); currentEMIteration++; - // TODO: Add convergence check + + // Checking of the fitting converged (i.e., the loglikelihood only changed minimally) if (currentEMIteration > 1) { float relLogLikelihoodDifference = std::fabs(logLikelihood - previousLogLikelihood) * inv_previousLogLikelihood; @@ -925,18 +979,120 @@ void ParallaxAwareVonMisesFisherWeightedEMFactory::partialUpda { converged = true; } - // std::cout << "logLikelihood:" << logLikelihood << "\t previousLogLikelihood: "<< previousLogLikelihood << "\t relLogLikelihoodDifference: " << - // relLogLikelihoodDifference << std::endl; previousLogLikelihood = logLikelihood; inv_previousLogLikelihood = 1.0f / std::fabs(logLikelihood); } } - previousStats += currentStats; + + // The merged sufficient stats from the last iteration are now the new previous/prior stats + previousStats = currentStats; fitStats.numSamples = numSamples; fitStats.numIterations = currentEMIteration; fitStats.summedWeightedLogLikelihood = previousLogLikelihood; - // std::cout << "converged:" << currentEMIteration << std::endl; +} + +template +void ParallaxAwareVonMisesFisherWeightedEMFactory::partialMergeSufficientStatisticsWithPriors(PartialFittingMask &mask, SufficientStatistics ¤tStats, + const bool usePreviousStatsAsPrior, + const PartialFittingMask &previousAsPriorMask, + const SufficientStatistics &previousStats) const +{ + const embree::vfloat zeros = 0.f; + + // The old (previous) and new (current) sum of weights for the partial updated components. + // In the end the weights of the partial updated components must be similar + embree::vfloat sumPreviousPartialWeightsVec = 0.f; + embree::vfloat sumCurrentPartialWeightsVec = 0.f; + + float decay = 1.f; + + const uint32_t cnt = (previousStats.numComponents + VMM::VectorSize - 1) / VMM::VectorSize; + for (uint32_t k = 0; k < cnt; k++) + { + // First, check if the previous stats should be applied as prior to the current stats + if (usePreviousStatsAsPrior) + { + // Merge the current and the previous stats if allowed by the previousAsPriorMask. + currentStats.sumOfWeightedDirections[k].x = + select(previousAsPriorMask.mask[k], currentStats.sumOfWeightedDirections[k].x + previousStats.sumOfWeightedDirections[k].x * decay, + currentStats.sumOfWeightedDirections[k].x); + currentStats.sumOfWeightedDirections[k].y = + select(previousAsPriorMask.mask[k], currentStats.sumOfWeightedDirections[k].y + previousStats.sumOfWeightedDirections[k].y * decay, + currentStats.sumOfWeightedDirections[k].y); + currentStats.sumOfWeightedDirections[k].z = + select(previousAsPriorMask.mask[k], currentStats.sumOfWeightedDirections[k].z + previousStats.sumOfWeightedDirections[k].z * decay, + currentStats.sumOfWeightedDirections[k].z); + + currentStats.sumOfWeightedStats[k] = + select(previousAsPriorMask.mask[k], currentStats.sumOfWeightedStats[k] + previousStats.sumOfWeightedStats[k] * decay, currentStats.sumOfWeightedStats[k]); + currentStats.sumOfDistanceWeightes[k] = select(previousAsPriorMask.mask[k], previousStats.sumOfDistanceWeightes[k] * decay, zeros); + } + // Second, reset the stats that should not be updated by the partial fit. + // These stats are reseted to the ones from the previous stats. + currentStats.sumOfWeightedDirections[k].x = select(mask.mask[k], currentStats.sumOfWeightedDirections[k].x, previousStats.sumOfWeightedDirections[k].x); + currentStats.sumOfWeightedDirections[k].y = select(mask.mask[k], currentStats.sumOfWeightedDirections[k].y, previousStats.sumOfWeightedDirections[k].y); + currentStats.sumOfWeightedDirections[k].z = select(mask.mask[k], currentStats.sumOfWeightedDirections[k].z, previousStats.sumOfWeightedDirections[k].z); + + // Update the sum of weights and wieghted distances for the partial updated components. + currentStats.sumOfWeightedStats[k] = select(mask.mask[k], currentStats.sumOfWeightedStats[k], previousStats.sumOfWeightedStats[k]); + currentStats.sumOfDistanceWeightes[k] = select(mask.mask[k], currentStats.sumOfDistanceWeightes[k], previousStats.sumOfDistanceWeightes[k]); + + // Calculate the previous and current sum of weights and distances for the partial updated components. + sumPreviousPartialWeightsVec += select(mask.mask[k], previousStats.sumOfWeightedStats[k], zeros); + sumCurrentPartialWeightsVec += select(mask.mask[k], currentStats.sumOfWeightedStats[k], zeros); + } + + embree::vfloat sumTmpVec = 0.f; + // Calcualting the normalization factor for the weights of the updated components + float inv_currentWeights = 1.0f; + const float sumCurrentPartialWeights = embree::reduce_add(sumCurrentPartialWeightsVec); + if (sumCurrentPartialWeights > 0.f) + { + // Correction factor so that the sum of weights for the updated components stays the same as before. + inv_currentWeights = embree::reduce_add(sumPreviousPartialWeightsVec) / sumCurrentPartialWeights; + for (uint32_t k = 0; k < cnt; k++) + { + currentStats.sumOfWeightedStats[k] = select(mask.mask[k], currentStats.sumOfWeightedStats[k] * inv_currentWeights, currentStats.sumOfWeightedStats[k]); + + currentStats.sumOfWeightedDirections[k].x = + select(mask.mask[k], currentStats.sumOfWeightedDirections[k].x * inv_currentWeights, currentStats.sumOfWeightedDirections[k].x); + currentStats.sumOfWeightedDirections[k].y = + select(mask.mask[k], currentStats.sumOfWeightedDirections[k].y * inv_currentWeights, currentStats.sumOfWeightedDirections[k].y); + currentStats.sumOfWeightedDirections[k].z = + select(mask.mask[k], currentStats.sumOfWeightedDirections[k].z * inv_currentWeights, currentStats.sumOfWeightedDirections[k].z); + sumTmpVec += currentStats.sumOfWeightedStats[k]; + } + } + OPENPGL_ASSERT(embree::isvalid(sumTmpVec)); + float sumWTmp = embree::reduce_add(sumTmpVec); + // Since only some partial components are updated the overall stats for numSamples, sumWeights, ..., + // should not change. Nevertheless, due to some numerical differences we recalculate the stats. + if (currentStats.normalized) + { + currentStats.sumWeights = previousStats.sumWeights; + currentStats.numSamples = sumWTmp; + } + else + { + currentStats.sumWeights = sumWTmp; + currentStats.numSamples = previousStats.numSamples; + } + + // The overall number of samples stays the same. + currentStats.overallNumSamples = previousStats.overallNumSamples; + + // Updating/Recalculating the weights to num samples normalization factor and its inverse. + currentStats.norm = 1.f; + currentStats.inv_norm = 1.f; + if (currentStats.sumWeights > 0.f && currentStats.numSamples > 0.f) + { + currentStats.norm = currentStats.numSamples / currentStats.sumWeights; + currentStats.inv_norm = currentStats.sumWeights / currentStats.numSamples; + } + + OPENPGL_ASSERT(embree::isvalid(currentStats.norm)); + OPENPGL_ASSERT(embree::isvalid(currentStats.inv_norm)); } template @@ -962,52 +1118,52 @@ float ParallaxAwareVonMisesFisherWeightedEMFactory::weightedEx const Vector3 sampleDirection(direction.x, direction.y, direction.z); const embree::Vec3 > sampleDirectionSIMD(sampleDirection); - // check if the samples is covered by any of the components + // Calculating the soft assignment of the current sample direction for all mixture components. + // We collect the sufficient statistics for all sample directions not covered by any mixture component. if (!vmm.softAssignment(sampleDirection, softAssign)) { unassignedStats.sumOfUnassignedWeights += sampleData.weight; unassignedStats.sumUnassignedWeightedDirections += sampleDirection * sampleData.weight; -#ifdef OPENPGL_DEBUG_MODE - std::cout << "continue" << std::endl; -#endif continue; } + // Updating the sumed loglikelihood summedWeightedLogLikelihood += sampleData.weight * embree::log(softAssign.pdf); for (uint32_t k = 0; k < cnt; k++) { stats.sumOfWeightedDirections[k] += sampleDirectionSIMD * softAssign.assignments[k] * sampleWeight; stats.sumOfWeightedStats[k] += softAssign.assignments[k] * sampleWeight; + + OPENPGL_ASSERT(embree::isvalid(stats.sumOfWeightedDirections[k].x)); + OPENPGL_ASSERT(embree::isvalid(stats.sumOfWeightedDirections[k].y)); + OPENPGL_ASSERT(embree::isvalid(stats.sumOfWeightedDirections[k].z)); + OPENPGL_ASSERT(embree::isvalid(stats.sumOfWeightedStats[k])); } } return summedWeightedLogLikelihood; } template -void ParallaxAwareVonMisesFisherWeightedEMFactory::estimateMAPWeights(VMM &vmm, const SufficientStatistics ¤tStats, - const SufficientStatistics &previousStats, const float &_weightPrior) const +void ParallaxAwareVonMisesFisherWeightedEMFactory::estimateMAPWeights(VMM &vmm, const SufficientStatistics ¤tStats, const float &_weightPrior) const { const uint32_t cnt = (vmm._numComponents + VMM::VectorSize - 1) / VMM::VectorSize; const size_t numComponents = vmm._numComponents; - const embree::vfloat weightPrior(_weightPrior); - - const embree::vfloat numSamples = currentStats.numSamples + previousStats.numSamples; - // const vfloat numSamples = currentStats.numSamples + previousStats.overallNumSamples; + const embree::vfloat numSamples = currentStats.numSamples; for (uint32_t k = 0; k < cnt; k++) { - //_sumWeights += currentStats.sumOfWeightedStats[k]; - embree::vfloat weight = (currentStats.sumOfWeightedStats[k] + previousStats.sumOfWeightedStats[k]); + OPENPGL_ASSERT(embree::isvalid(vmm._weights[k])); + embree::vfloat weight = (currentStats.sumOfWeightedStats[k]); weight = (weightPrior + (weight)) / ((weightPrior * numComponents) + numSamples); - // vfloat weight = ( currentStats.sumOfWeightedStats[k]/* + previousStats.sumOfWeightedStats[k]*/ ) / ( sumWeights ); - // weight = ( weightPrior + ( weight * numSamples ) ) / (( weightPrior * numComponents ) + numSamples ); vmm._weights[k] = weight; + OPENPGL_ASSERT(embree::isvalid(vmm._weights[k])); } // TODO: find better more efficient way + // Ensuring that the weights for unused SIMD vector entries are zero if (vmm._numComponents % VMM::VectorSize > 0) { for (size_t i = vmm._numComponents % VMM::VectorSize; i < VMM::VectorSize; i++) @@ -1019,16 +1175,9 @@ void ParallaxAwareVonMisesFisherWeightedEMFactory::estimateMAP template void ParallaxAwareVonMisesFisherWeightedEMFactory::estimateMAPMeanDirectionAndConcentration(VMM &vmm, const SufficientStatistics ¤tStats, - const SufficientStatistics &previousStats, const Configuration &cfg) const { - const embree::vfloat currentNumSamples = currentStats.numSamples; - const embree::vfloat previousNumSamples = previousStats.numSamples; - const embree::vfloat numSamples = currentNumSamples + previousNumSamples; - const embree::vfloat overallNumSamples = currentStats.numSamples + previousStats.overallNumSamples; - - const embree::vfloat currentEstimationWeight = currentNumSamples / numSamples; - const embree::vfloat previousEstimationWeight = 1.0f - currentEstimationWeight; + const embree::vfloat overallNumSamples = currentStats.overallNumSamples; const embree::vfloat meanCosinePrior = cfg.meanCosinePrior; const embree::vfloat meanCosinePriorStrength = cfg.meanCosinePriorStrength; @@ -1038,20 +1187,11 @@ void ParallaxAwareVonMisesFisherWeightedEMFactory::estimateMAP for (uint32_t k = 0; k < cnt; k++) { - // const vfloat partialNumSamples = vmm._weights[k] * numSamples; const embree::vfloat partialNumSamples = vmm._weights[k] * overallNumSamples; - embree::Vec3 > currentMeanDirection; - currentMeanDirection.x = select(currentStats.sumOfWeightedStats[k] > 0.0f, currentStats.sumOfWeightedDirections[k].x / currentStats.sumOfWeightedStats[k], 0.0f); - currentMeanDirection.y = select(currentStats.sumOfWeightedStats[k] > 0.0f, currentStats.sumOfWeightedDirections[k].y / currentStats.sumOfWeightedStats[k], 0.0f); - currentMeanDirection.z = select(currentStats.sumOfWeightedStats[k] > 0.0f, currentStats.sumOfWeightedDirections[k].z / currentStats.sumOfWeightedStats[k], 0.0f); - - // TODO: find a better design to precompute the previousMeanDirection - embree::Vec3 > previousMeanDirection; - previousMeanDirection.x = select(previousStats.sumOfWeightedStats[k] > 0.0f, previousStats.sumOfWeightedDirections[k].x / previousStats.sumOfWeightedStats[k], 0.0f); - previousMeanDirection.y = select(previousStats.sumOfWeightedStats[k] > 0.0f, previousStats.sumOfWeightedDirections[k].y / previousStats.sumOfWeightedStats[k], 0.0f); - previousMeanDirection.z = select(previousStats.sumOfWeightedStats[k] > 0.0f, previousStats.sumOfWeightedDirections[k].z / previousStats.sumOfWeightedStats[k], 0.0f); - - embree::Vec3 > meanDirection = currentMeanDirection * currentEstimationWeight + previousMeanDirection * previousEstimationWeight; + embree::Vec3 > meanDirection; + meanDirection.x = select(currentStats.sumOfWeightedStats[k] > 0.0f, currentStats.sumOfWeightedDirections[k].x / currentStats.sumOfWeightedStats[k], 0.0f); + meanDirection.y = select(currentStats.sumOfWeightedStats[k] > 0.0f, currentStats.sumOfWeightedDirections[k].y / currentStats.sumOfWeightedStats[k], 0.0f); + meanDirection.z = select(currentStats.sumOfWeightedStats[k] > 0.0f, currentStats.sumOfWeightedDirections[k].z / currentStats.sumOfWeightedStats[k], 0.0f); embree::vfloat meanCosine = length(meanDirection); @@ -1059,14 +1199,19 @@ void ParallaxAwareVonMisesFisherWeightedEMFactory::estimateMAP vmm._meanDirections[k].y = select(meanCosine > 0.0f, meanDirection.y / meanCosine, vmm._meanDirections[k].y); vmm._meanDirections[k].z = select(meanCosine > 0.0f, meanDirection.z / meanCosine, vmm._meanDirections[k].z); + // Applying a global prior to the mean cosines of the mixture components. + // The strength of the prior depends on the partial number of samples associated with each component. meanCosine = (meanCosinePrior * meanCosinePriorStrength + meanCosine * partialNumSamples) / (meanCosinePriorStrength + partialNumSamples); meanCosine = embree::min(maxMeanCosine, meanCosine); vmm._meanCosines[k] = meanCosine; vmm._kappas[k] = MeanCosineToKappa >(meanCosine); + OPENPGL_ASSERT(embree::isvalid(vmm._meanCosines[k])); + OPENPGL_ASSERT(embree::isvalid(vmm._kappas[k])); } // TODO: find better more efficient way + // Ensuring that the component parameters for unused SIMD vector entries are cleared if (rem > 0) { for (uint32_t i = rem; i < VMM::VectorSize; i++) @@ -1088,140 +1233,13 @@ void ParallaxAwareVonMisesFisherWeightedEMFactory::estimateMAP template void ParallaxAwareVonMisesFisherWeightedEMFactory::weightedMaximumAPosteriorStep(VMM &vmm, const SufficientStatistics ¤tStats, - const SufficientStatistics &previousStats, const Configuration &cfg) const -{ - // Estimating components weights - estimateMAPWeights(vmm, currentStats, previousStats, cfg.weightPrior); - - // Estimating mean and concentration - estimateMAPMeanDirectionAndConcentration(vmm, currentStats, previousStats, cfg); -} - -template -void ParallaxAwareVonMisesFisherWeightedEMFactory::partialWeightedMaximumAPosteriorStep(VMM &vmm, const PartialFittingMask &mask, - SufficientStatistics ¤tStats, SufficientStatistics &previousStats, - const Configuration &cfg) const -{ - // Estimating components weights - estimatePartialMAPWeights(vmm, mask, currentStats, previousStats, cfg.weightPrior); - - // Estimating mean and concentration - estimatePartialMAPMeanDirectionAndConcentration(vmm, mask, currentStats, previousStats, cfg); -} - -template -void ParallaxAwareVonMisesFisherWeightedEMFactory::estimatePartialMAPWeights(VMM &vmm, const PartialFittingMask &mask, SufficientStatistics ¤tStats, - SufficientStatistics &previousStats, const float &_weightPrior) const -{ - const embree::vfloat zeros(0.0f); - const uint32_t cnt = (vmm._numComponents + VMM::VectorSize - 1) / VMM::VectorSize; - - const size_t numComponents = vmm._numComponents; - - const embree::vfloat weightPrior(_weightPrior); - - const embree::vfloat numSamples = currentStats.numSamples + previousStats.numSamples; - // const vfloat numSamples = currentStats.numSamples + previousStats.overallNumSamples; - - embree::vfloat sumWeights(0.0f); - embree::vfloat sumPartialWeights(0.0f); - - for (uint32_t k = 0; k < cnt; k++) - { - //_sumWeights += currentStats.sumOfWeightedStats[k]; - embree::vfloat weight = (currentStats.sumOfWeightedStats[k] + previousStats.sumOfWeightedStats[k]); - weight = (weightPrior + (weight)) / ((weightPrior * numComponents) + numSamples); - // vfloat weight = ( currentStats.sumOfWeightedStats[k]/* + previousStats.sumOfWeightedStats[k]*/ ) / ( sumWeights ); - // weight = ( weightPrior + ( weight * numSamples ) ) / (( weightPrior * numComponents ) + numSamples ); - - sumPartialWeights += select(mask.mask[k], weight, zeros); - sumWeights += select(mask.mask[k], zeros, vmm._weights[k]); - - vmm._weights[k] = select(mask.mask[k], weight, vmm._weights[k]); - } - - embree::vfloat inv_sumPartialWeights = 1.0f / reduce_add(sumPartialWeights); - inv_sumPartialWeights *= 1.0f - reduce_add(sumWeights); - for (uint32_t k = 0; k < cnt; k++) - { - vmm._weights[k] = select(mask.mask[k], vmm._weights[k] * inv_sumPartialWeights, vmm._weights[k]); - } - - // TODO: find better more efficient way - if (vmm._numComponents % VMM::VectorSize > 0) - { - for (uint32_t i = vmm._numComponents % VMM::VectorSize; i < VMM::VectorSize; i++) - { - vmm._weights[cnt - 1][i] = 0.0f; - } - } -} - -template -void ParallaxAwareVonMisesFisherWeightedEMFactory::estimatePartialMAPMeanDirectionAndConcentration(VMM &vmm, const PartialFittingMask &mask, - SufficientStatistics ¤tStats, - SufficientStatistics &previousStats, - const Configuration &cfg) const + const Configuration &cfg) const { - const embree::vfloat currentNumSamples = currentStats.numSamples; - const embree::vfloat previousNumSamples = previousStats.numSamples; - const embree::vfloat numSamples = currentNumSamples + previousNumSamples; - const embree::vfloat overallNumSamples = currentStats.numSamples + previousStats.overallNumSamples; - - const embree::vfloat currentEstimationWeight = currentNumSamples / numSamples; - const embree::vfloat previousEstimationWeight = 1.0f - currentEstimationWeight; - - const embree::vfloat meanCosinePrior = cfg.meanCosinePrior; - const embree::vfloat meanCosinePriorStrength = cfg.meanCosinePriorStrength; - const embree::vfloat maxMeanCosine = cfg.maxMeanCosine; - const uint32_t cnt = (vmm._numComponents + VMM::VectorSize - 1) / VMM::VectorSize; - const uint32_t rem = vmm._numComponents % VMM::VectorSize; - - for (uint32_t k = 0; k < cnt; k++) - { - // const vfloat partialNumSamples = vmm._weights[k] * numSamples; - const embree::vfloat partialNumSamples = vmm._weights[k] * overallNumSamples; - embree::Vec3 > currentMeanDirection; - currentMeanDirection.x = select(currentStats.sumOfWeightedStats[k] > 0.0f, currentStats.sumOfWeightedDirections[k].x / currentStats.sumOfWeightedStats[k], 0.0f); - currentMeanDirection.y = select(currentStats.sumOfWeightedStats[k] > 0.0f, currentStats.sumOfWeightedDirections[k].y / currentStats.sumOfWeightedStats[k], 0.0f); - currentMeanDirection.z = select(currentStats.sumOfWeightedStats[k] > 0.0f, currentStats.sumOfWeightedDirections[k].z / currentStats.sumOfWeightedStats[k], 0.0f); - - // TODO: find a better design to precompute the previousMeanDirection - embree::Vec3 > previousMeanDirection; - previousMeanDirection.x = select(previousStats.sumOfWeightedStats[k] > 0.0f, previousStats.sumOfWeightedDirections[k].x / previousStats.sumOfWeightedStats[k], 0.0f); - previousMeanDirection.y = select(previousStats.sumOfWeightedStats[k] > 0.0f, previousStats.sumOfWeightedDirections[k].y / previousStats.sumOfWeightedStats[k], 0.0f); - previousMeanDirection.z = select(previousStats.sumOfWeightedStats[k] > 0.0f, previousStats.sumOfWeightedDirections[k].z / previousStats.sumOfWeightedStats[k], 0.0f); - - embree::Vec3 > meanDirection = currentMeanDirection * currentEstimationWeight + previousMeanDirection * previousEstimationWeight; - - embree::vfloat meanCosine = embree::length(meanDirection); - - vmm._meanDirections[k].x = select(mask.mask[k], select(meanCosine > 0.0f, meanDirection.x / meanCosine, vmm._meanDirections[k].x), vmm._meanDirections[k].x); - vmm._meanDirections[k].y = select(mask.mask[k], select(meanCosine > 0.0f, meanDirection.y / meanCosine, vmm._meanDirections[k].y), vmm._meanDirections[k].y); - vmm._meanDirections[k].z = select(mask.mask[k], select(meanCosine > 0.0f, meanDirection.z / meanCosine, vmm._meanDirections[k].z), vmm._meanDirections[k].z); - - meanCosine = (meanCosinePrior * meanCosinePriorStrength + meanCosine * partialNumSamples) / (meanCosinePriorStrength + partialNumSamples); - - meanCosine = embree::min(maxMeanCosine, meanCosine); - vmm._meanCosines[k] = select(mask.mask[k], meanCosine, vmm._meanCosines[k]); - vmm._kappas[k] = select(mask.mask[k], MeanCosineToKappa >(meanCosine), vmm._kappas[k]); - } - - // TODO: find better more efficient way - if (rem > 0) - { - for (uint32_t i = rem; i < VMM::VectorSize; i++) - { - vmm._meanDirections[cnt - 1].x[i] = 0.0f; - vmm._meanDirections[cnt - 1].y[i] = 0.0f; - vmm._meanDirections[cnt - 1].z[i] = 1.0f; + // MAP estimation of the components weights + estimateMAPWeights(vmm, currentStats, cfg.weightPrior); - vmm._meanCosines[cnt - 1][i] = 0.0f; - vmm._kappas[cnt - 1][i] = 0.0f; - } - } - - vmm._calculateNormalization(); + // MAP estimation of the mean and concentration + estimateMAPMeanDirectionAndConcentration(vmm, currentStats, cfg); } template @@ -1667,6 +1685,16 @@ void ParallaxAwareVonMisesFisherWeightedEMFactory::PartialFitt } } +template +void ParallaxAwareVonMisesFisherWeightedEMFactory::PartialFittingMask::resetToTrue() +{ + const embree::vbool vTrue(true); + for (size_t k = 0; k < ((VMM::MaxComponents + (VMM::VectorSize - 1)) / VMM::VectorSize); k++) + { + mask[k] = vTrue; + } +} + template void ParallaxAwareVonMisesFisherWeightedEMFactory::PartialFittingMask::resetToTrue(const size_t &numComponents) { diff --git a/openpgl/directional/vmm/VMMChiSquareComponentMerger.h b/openpgl/directional/vmm/VMMChiSquareComponentMerger.h index 6c3affe..1fcc2d3 100644 --- a/openpgl/directional/vmm/VMMChiSquareComponentMerger.h +++ b/openpgl/directional/vmm/VMMChiSquareComponentMerger.h @@ -10,26 +10,28 @@ namespace openpgl { -template +template struct VonMisesFisherChiSquareComponentMerger { public: typedef typename TVMMFactory::Distribution VMM; typedef typename TVMMFactory::SufficientStatistics SufficientStatistics; - typedef typename VonMisesFisherChiSquareComponentSplitter::ComponentSplitStatistics ComponentSplitStatistics; + typedef typename TSplitter::ComponentSplitStatistics ComponentSplitStatistics; // typedef std::integral_constant NumVectors; float MergeNext(VMM &vmm) const; bool ThresholdedMergeNext(VMM &vmm, const float &mergeThreshold, float &mergeCost) const; - bool ThresholdedMergeNext(VMM &vmm, const float &mergeThreshold, float &mergeCost, SufficientStatistics &suffStats, ComponentSplitStatistics &splitStats) const; + bool ThresholdedMergeNext(VMM &vmm, const float mergeThreshold, const float splitThreshold, const bool considerSplitThreshold, float &mergeCost, + SufficientStatistics &suffStats, ComponentSplitStatistics &splitStats) const; float CalculateMergeCost(const VMM &vmm, const size_t &idx0, const size_t &idx1) const; size_t PerformMerging(VMM &vmm, const float &mergeThreshold) const; - size_t PerformMerging(VMM &vmm, const float &mergeThreshold, SufficientStatistics &suffStats, ComponentSplitStatistics &splitStats) const; + size_t PerformMerging(VMM &vmm, const float mergeThreshold, const float splitThreshold, const bool considerSplitThreshold, SufficientStatistics &suffStats, + ComponentSplitStatistics &splitStats) const; private: inline float _IntegratedProduct(const Vector3 &meanDirection0, const float &kappa0, const float &normalization0, const Vector3 &meanDirection1, const float &kappa1, @@ -42,8 +44,8 @@ struct VonMisesFisherChiSquareComponentMerger const float &normalization1, Vector3 &meanDirection, float &kappa, float &normalization) const; }; -template -size_t VonMisesFisherChiSquareComponentMerger::PerformMerging(VMM &vmm, const float &mergeThreshold) const +template +size_t VonMisesFisherChiSquareComponentMerger::PerformMerging(VMM &vmm, const float &mergeThreshold) const { bool stopMerging = false; size_t totalMergeCount = 0; @@ -63,9 +65,10 @@ size_t VonMisesFisherChiSquareComponentMerger::PerformMerging(VMM & return totalMergeCount; } -template -size_t VonMisesFisherChiSquareComponentMerger::PerformMerging(VMM &vmm, const float &mergeThreshold, SufficientStatistics &suffStats, - ComponentSplitStatistics &splitStats) const +template +size_t VonMisesFisherChiSquareComponentMerger::PerformMerging(VMM &vmm, const float mergeThreshold, const float splitThreshold, + const bool considerSplitThreshold, SufficientStatistics &suffStats, + ComponentSplitStatistics &splitStats) const { bool stopMerging = false; size_t totalMergeCount = 0; @@ -74,7 +77,7 @@ size_t VonMisesFisherChiSquareComponentMerger::PerformMerging(VMM & { float mergeCost = 0.0f; stopMerging = true; - bool mergedComponents = ThresholdedMergeNext(vmm, mergeThreshold, mergeCost, suffStats, splitStats); + bool mergedComponents = ThresholdedMergeNext(vmm, mergeThreshold, splitThreshold, considerSplitThreshold, mergeCost, suffStats, splitStats); stopMerging = !mergedComponents; // std::cout << "merge: " << "\tmergedComponents: " << mergedComponents << "\tmergeCost: " << mergeCost << std::endl; @@ -89,8 +92,8 @@ size_t VonMisesFisherChiSquareComponentMerger::PerformMerging(VMM & return totalMergeCount; } -template -float VonMisesFisherChiSquareComponentMerger::CalculateMergeCost(const VMM &vmm, const size_t &idx0, const size_t &idx1) const +template +float VonMisesFisherChiSquareComponentMerger::CalculateMergeCost(const VMM &vmm, const size_t &idx0, const size_t &idx1) const { const div_t div0 = div(idx0, VMM::VectorSize); float weight0 = vmm._weights[div0.quot][div0.rem]; @@ -165,8 +168,8 @@ float VonMisesFisherChiSquareComponentMerger::CalculateMergeCost(co return chiSquareIJ; } -template -bool VonMisesFisherChiSquareComponentMerger::ThresholdedMergeNext(VMM &vmm, const float &mergeThreshold, float &mergeCost) const +template +bool VonMisesFisherChiSquareComponentMerger::ThresholdedMergeNext(VMM &vmm, const float &mergeThreshold, float &mergeCost) const { // std::cout << vmm.toString()<::ThresholdedMergeNext(V return foundMergeCandidates; } -template -bool VonMisesFisherChiSquareComponentMerger::ThresholdedMergeNext(VMM &vmm, const float &mergeThreshold, float &mergeCost, SufficientStatistics &suffStats, - ComponentSplitStatistics &splitStats) const +template +bool VonMisesFisherChiSquareComponentMerger::ThresholdedMergeNext(VMM &vmm, const float mergeThreshold, const float splitThreshold, + const bool considerSplitThreshold, float &mergeCost, SufficientStatistics &suffStats, + ComponentSplitStatistics &splitStats) const { OPENPGL_ASSERT(splitStats.isValid()); int K = vmm._numComponents; @@ -219,7 +223,9 @@ bool VonMisesFisherChiSquareComponentMerger::ThresholdedMergeNext(V { const div_t tmpJ = div(j, static_cast(VMM::VectorSize)); float mergeCost = CalculateMergeCost(vmm, i, j); - if (mergeCost < mergeThreshold && mergeCost < minMergeCost && splitStats.numSamples[tmpI.quot][tmpI.rem] > 0.0f && splitStats.numSamples[tmpJ.quot][tmpJ.rem] > 0.0f) + if (mergeCost < mergeThreshold && mergeCost < minMergeCost && splitStats.numSamples[tmpI.quot][tmpI.rem] > 0.0f && splitStats.numSamples[tmpJ.quot][tmpJ.rem] > 0.0f && + (!considerSplitThreshold || + (considerSplitThreshold && (splitStats.chiSquareMCEstimates[tmpI.quot][tmpI.rem] + splitStats.chiSquareMCEstimates[tmpJ.quot][tmpJ.rem] < splitThreshold)))) { mergeCandidateI = i; mergeCandidateJ = j; @@ -262,8 +268,8 @@ bool VonMisesFisherChiSquareComponentMerger::ThresholdedMergeNext(V return foundMergeCandidates; } -template -float VonMisesFisherChiSquareComponentMerger::MergeNext(VMM &vmm) const +template +float VonMisesFisherChiSquareComponentMerger::MergeNext(VMM &vmm) const { int K = vmm._numComponents; int mergeCandidateI = 0; @@ -288,9 +294,9 @@ float VonMisesFisherChiSquareComponentMerger::MergeNext(VMM &vmm) c return minMergeCost; } -template -float VonMisesFisherChiSquareComponentMerger::_IntegratedProduct(const Vector3 &meanDirection0, const float &kappa0, const float &normalization0, - const Vector3 &meanDirection1, const float &kappa1, const float &normalization1) const +template +float VonMisesFisherChiSquareComponentMerger::_IntegratedProduct(const Vector3 &meanDirection0, const float &kappa0, const float &normalization0, + const Vector3 &meanDirection1, const float &kappa1, const float &normalization1) const { Vector3 productMeanDirection = kappa0 * meanDirection0 + kappa1 * meanDirection1; @@ -318,10 +324,10 @@ float VonMisesFisherChiSquareComponentMerger::_IntegratedProduct(co return scale; } -template -float VonMisesFisherChiSquareComponentMerger::_IntegratedDivision(const Vector3 &meanDirection0, const float &kappa0, const float &normalization0, - const Vector3 &meanDirection1, const float &kappa1, const float &normalization1, - const float &eMinus2Kappa1) const +template +float VonMisesFisherChiSquareComponentMerger::_IntegratedDivision(const Vector3 &meanDirection0, const float &kappa0, const float &normalization0, + const Vector3 &meanDirection1, const float &kappa1, const float &normalization1, + const float &eMinus2Kappa1) const { Vector3 productMeanDirection = kappa0 * meanDirection0 + kappa1 * meanDirection1; @@ -350,10 +356,10 @@ float VonMisesFisherChiSquareComponentMerger::_IntegratedDivision(c return scale; } -template -float VonMisesFisherChiSquareComponentMerger::_Product(const Vector3 &meanDirection0, const float &kappa0, const float &normalization0, const Vector3 &meanDirection1, - const float &kappa1, const float &normalization1, Vector3 &productMeanDirection, float &productKappa, - float &productNormalization) const +template +float VonMisesFisherChiSquareComponentMerger::_Product(const Vector3 &meanDirection0, const float &kappa0, const float &normalization0, + const Vector3 &meanDirection1, const float &kappa1, const float &normalization1, + Vector3 &productMeanDirection, float &productKappa, float &productNormalization) const { productMeanDirection = kappa0 * meanDirection0 + kappa1 * meanDirection1; productKappa = embree::sqrt(dot(productMeanDirection, productMeanDirection)); diff --git a/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h b/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h index 1ea72c6..d449269 100644 --- a/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h +++ b/openpgl/directional/vmm/VMMChiSquareComponentSplitter.h @@ -279,7 +279,7 @@ void VonMisesFisherChiSquareComponentSplitter::PerformSplitting(VMM // std::cout << "suffStatistics: " << suffStatistics.toString() << std::endl; if (doPartialRefit) { - vmmFactory.partialUpdateMixture(vmm, mask, suffStatistics, data, numData, factoryCfg, vmmFitStats); + vmmFactory.partialUpdateMixture(vmm, mask, suffStatistics, true, data, numData, factoryCfg, vmmFitStats); // std::cout << "vmmpartialUpdate: " << vmm.toString() << std::endl; splitItr++; } @@ -301,8 +301,9 @@ void VonMisesFisherChiSquareComponentSplitter::PerformRecursiveSpli const size_t &numData, const typename VMMFactory::Configuration factoryCfg) const { PartialFittingMask mask; + PartialFittingMask previousAsPriorMask; + previousAsPriorMask.resetToFalse(); ComponentSplitStatistics splitStatistics; - SufficientStatistics tempSuffStatistics = suffStatistics; // bool stopSplitting = false; // size_t splitItr = 0; @@ -331,14 +332,14 @@ void VonMisesFisherChiSquareComponentSplitter::PerformRecursiveSpli if (splitComps[k].chiSquareEst > splitThreshold && vmm._numComponents < VMM::MaxComponents) { #ifndef OPENPGL_USE_THREE_SPLIT - bool splitSuccess = SplitComponent(vmm, splitStatistics, tempSuffStatistics, splitComps[k].componentIndex); + bool splitSuccess = SplitComponent(vmm, splitStatistics, suffStatistics, splitComps[k].componentIndex); if (splitSuccess) { mask.setToTrue(splitComps[k].componentIndex); mask.setToTrue(vmm._numComponents - 1); } #else - bool splitSuccess = SplitComponentIntoThree(vmm, splitStatistics, tempSuffStatistics, splitComps[k].componentIndex); + bool splitSuccess = SplitComponentIntoThree(vmm, splitStatistics, suffStatistics, splitComps[k].componentIndex); if (splitSuccess) { mask.setToTrue(splitComps[k].componentIndex); @@ -358,11 +359,7 @@ void VonMisesFisherChiSquareComponentSplitter::PerformRecursiveSpli } if (numSplits > 0) { - tempSuffStatistics.clear(vmm._numComponents); - vmmFactory.partialUpdateMixture(vmm, mask, tempSuffStatistics, data, numData, factoryCfg, vmmFitStats); - // std::cout << "tempSuffStatistics" << std::endl << tempSuffStatistics.toString() << std::endl; - suffStatistics.setNumComponents(vmm._numComponents); - suffStatistics.maskedReplace(mask, tempSuffStatistics); + vmmFactory.partialUpdateMixture(vmm, mask, false, previousAsPriorMask, suffStatistics, data, numData, factoryCfg, vmmFitStats); } // std::cout << "vmmpartialUpdate: " << vmm.toString() << std::endl; // splitItr++; From c4544973ca52f45bbce7073c363922bbf0a92941 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 4 Nov 2026 15:34:41 +0100 Subject: [PATCH 2/2] [sam] Adding additional documentation to the AdaptiveSplitAndMergeFactory --- openpgl/directional/dqt/DQTFactory.h | 6 +- .../vmm/AdaptiveSplitandMergeFactory.h | 149 +++++++++++++++--- 2 files changed, 128 insertions(+), 27 deletions(-) diff --git a/openpgl/directional/dqt/DQTFactory.h b/openpgl/directional/dqt/DQTFactory.h index aa545ee..e1755f5 100644 --- a/openpgl/directional/dqt/DQTFactory.h +++ b/openpgl/directional/dqt/DQTFactory.h @@ -182,15 +182,17 @@ class DirectionalQuadtreeFactory void prepareSamples([[maybe_unused]] SampleData *samples, [[maybe_unused]] const size_t numSamples, [[maybe_unused]] const SampleStatistics &sampleStatistics, [[maybe_unused]] const Configuration &cfg) const {} - +#ifdef OPENPGL_RADIANCE_CACHES void updateFluenceEstimate([[maybe_unused]] Distribution &dist, [[maybe_unused]] const SampleData *samples, [[maybe_unused]] const size_t numSamples, [[maybe_unused]] const size_t numZeroValueSamples, [[maybe_unused]] const SampleStatistics &sampleStatistics) const {} - +#endif +#ifdef OPENPGL_VSP_GUIDING void updateVolumeScatterProbability([[maybe_unused]] Distribution &dist, [[maybe_unused]] Statistics &stats, [[maybe_unused]] const SampleData *samples, [[maybe_unused]] const size_t numSamples, [[maybe_unused]] const ZeroValueSampleData *zeroValueSamples, [[maybe_unused]] const size_t numZeroValueSamples, [[maybe_unused]] const bool varianceBased) const {} +#endif void fit(Distribution &dist, Statistics &stats, const SampleData *samples, const size_t numSamples, const Configuration &cfg, FittingStatistics &fitStats) const { diff --git a/openpgl/directional/vmm/AdaptiveSplitandMergeFactory.h b/openpgl/directional/vmm/AdaptiveSplitandMergeFactory.h index 4b71678..081c4b4 100644 --- a/openpgl/directional/vmm/AdaptiveSplitandMergeFactory.h +++ b/openpgl/directional/vmm/AdaptiveSplitandMergeFactory.h @@ -16,6 +16,17 @@ namespace openpgl { +/** + * @brief Class that implements the variance-aware, Chi^2-based, split and merge fitting algorithm for parametric mixture models. + * + * This AdaptiveSplitAndMergeFactory implements the variance-aware Split-and-Merge (SaM) algorithm, for parametric mixture models + * based on the work of Ruppert et al. "Robust Fitting of Parallax-Aware Mixtures for Path Guiding". This class implements the + * basic split and merge fitting and updating structure and offloads the calculation of the splitting statistics and the actual + * splitting and merging to the VMMChiSquareComponentSplitter and VMMChiSquareComponentMerger helper classes. At the moment the + * implementation supports parallax-aware von Mises-Fisher mixtures but can in principle be extended to any parametric mixture + * model. + * + */ template struct AdaptiveSplitAndMergeFactory { @@ -26,23 +37,32 @@ struct AdaptiveSplitAndMergeFactory typedef TVMMDistribution Distribution; typedef TVMMDistribution VMM; - // typedef WeightedEMVonMisesFisherFactory WeightedEMFactory; typedef ParallaxAwareVonMisesFisherWeightedEMFactory WeightedEMFactory; typedef VonMisesFisherChiSquareComponentSplitter Splitter; typedef VonMisesFisherChiSquareComponentMerger Merger; + /** + * @brief The configuration struct containing all fitting relevant parameters. + * + * This struct contains all parameters for fitting parametric mixture models + * using the split and merge (SaM) approach. These settings include the individual + * SaM parameters such as the Chi^2 splitting and merging thresholds as well as the + * fitting parameter sets for the standard incremental weighted MAP-EM fitting of the mixtures. + */ struct Configuration { + // The fitting parameter set for the weighted MAP EM fitting. typename WeightedEMFactory::Configuration weightedEMCfg; // The min. Chi^2 threshold for splitting a mixture component float splittingThreshold{0.5f}; // The max. Chi^2 threshold for merging a two mixture components float mergingThreshold{0.025f}; - + // If SaM should be active or not. Mainly for debugging purpose. bool useSplitAndMerge{true}; bool partialReFit{false}; + // How many split iteration should be performed per update. uint32_t maxSplitItr{1}; // The min. number of samples processed after the last merge step required to trigger a splitting step @@ -71,6 +91,13 @@ struct AdaptiveSplitAndMergeFactory } }; + /** + * @brief The sufficient statistics needed to fit a parametric mixture using the split and merge approach. + * + * This struct contains all sufficient statistics needed to fit the parametric mixture using the split and merge (SaM) approach. + * These statistics include the sufficient statistics of the weighted MAP EM from previous training iterations as well as the splitting + * statistics for the variance-aware component splitting. + */ struct Statistics { typename WeightedEMFactory::SufficientStatistics sufficientStatistics; @@ -81,23 +108,28 @@ struct AdaptiveSplitAndMergeFactory Statistics() = default; + // Clears the statistics for the _numComponents of mixture components. void clear(const size_t &_numComponents); + // Clears the statistics for all mixture components. void clearAll(); - + // Decays the strength of the sufficient statistics by a constant factor. + // Through this decay the sufficient statistics have less influence on upcoming updates. void decay(const float &alpha); void serialize(std::ostream &stream) const; void deserialize(std::istream &stream); - + // Checks if all values in the sufficient statistic are valid (e.g., finite or in the correct ranges). bool isValid() const; - + // Returns the number of mixture components the sufficient statistics cover. inline size_t getNumComponents() const { OPENPGL_ASSERT(sufficientStatistics.getNumComponents() == splittingStatistics.getNumComponents()); return sufficientStatistics.getNumComponents(); } + // Returns the number of overall samples represented by the sufficient statistics. + // NOTE: Due to decay this number does not need to represent the actual/overall number of samples. inline float getNumSamples() const { return sufficientStatistics.getNumSamples(); @@ -107,6 +139,10 @@ struct AdaptiveSplitAndMergeFactory bool operator==(const Statistics &b) const; }; + /** + * @brief A set of statistics of a split and merge fitting/update pass. + * + */ struct FittingStatistics { size_t numSamples{0}; @@ -121,16 +157,64 @@ struct AdaptiveSplitAndMergeFactory std::string toString() const; }; + /************************************************************** + * Public functions of the AdaptiveSplitAndMergeFactory + ***************************************************************/ + + public: + /** + * @brief Pre-processing function that will be called to prepare the samples for the fit or update step. + * + * This pre-processing function need to be called before the fit or update function to preapare the training samples. + * Based on the used model (e.g., parallax-aware von Mises-Fisher mixtures) the function can make adjustments to the + * sample data (e.g., pivot point re-projection). + * + */ void prepareSamples(SampleData *samples, const size_t numSamples, const SampleStatistics &sampleStatistics, const Configuration &cfg) const; + /** + * @brief The function for the initial fitting of the parametric mixture from a set of weighted samples using the split and merge approach. + * + * The function fits a mixture model from a set of training samples (i.e., weighted directional samples). It starts by fitting the mixture + * using classical weighted MAP EM followed by a variance-aware split and merge step. During splitting the splitted mixture components are + * partially re-fitted to the observed data. During this initial fit the components are split and refitted until no component meets the split + * criteria or if the number of maximum components is reached. After the splitting one merge pass is performed. + */ void fit(VMM &vmm, Statistics &stats, const SampleData *samples, const size_t numSamples, const Configuration &cfg, FittingStatistics &fitStats) const; + /** + * @brief The function to update an already fit mixture using an new set of training samples. + * + * This function updates the a previously fitted mixture using newly observed samples (i.e., weighted directional sample). The update + * procedure starts by doing and incremental weighted MAP EM fit that uses the sufficient statistics form previous iterations as additional + * prior. After the incremental fitting step the split statistics are updated and a split and merge step is preformed. + * + */ void update(VMM &vmm, Statistics &stats, const SampleData *samples, const size_t numSamples, const Configuration &cfg, FittingStatistics &fitStats) const; +#ifdef OPENPGL_RADIANCE_CACHES + /** + * @brief Updates the estimates of the radiance quantities (e.g., fluence or incoming radiance) represented by the mixture. + * + * Using the radiance estimates from the training samples the radiance estimates for the fluence or incoming are updated or initially calculated. + * The fluence estimate is a Monte Carlo estimate calculated by summing the sample weights of training samples. For the incoming radiance estimate + * these weights are distributed over the mixture components based on their soft assignment. To make these estimates unbiased the zero-value samples + * are also considered while calculating/updating the estimates. + */ void updateFluenceEstimate(VMM &vmm, const SampleData *samples, const size_t numSamples, const size_t numZeroValueSamples, const SampleStatistics &sampleStatistics) const; - +#endif +#ifdef OPENPGL_VSP_GUIDING + /** + * @brief Updates the estimate of the volume scatter probability used for volume scatter probability guiding (VSP). + * + * Using the directions, radiance weights, and the info if the incoming radiance of a samples originates from a volume or + * surface scattering event, estimates of the volume scattering probability disribution are updated. These estimates are used during + * rendering to guide the probability of generating a volume or surface event next. Based on the VSPG mode this probability is either based + * on the first or second moment. Details of how to calculate these estimates can be found in the work of Xu et al. "Volume Scattering Probability Guiding". + */ void updateVolumeScatterProbability(VMM &vmm, Statistics &stats, const SampleData *samples, const size_t numSamples, const ZeroValueSampleData *zeroValueSamples, const size_t numZeroValueSamples, const bool varianceBased) const; +#endif std::string toString() const { @@ -144,13 +228,16 @@ struct AdaptiveSplitAndMergeFactory } }; +/******************************************************************************** + * Implementation of the AdaptiveSplitAndMergeFactor::Statistics functions. + ********************************************************************************/ + template void AdaptiveSplitAndMergeFactory::Statistics::serialize(std::ostream &stream) const { sufficientStatistics.serialize(stream); splittingStatistics.serialize(stream); - // stream.write(reinterpret_cast(&numComponents), sizeof(size_t)); stream.write(reinterpret_cast(&numSamplesAfterLastSplit), sizeof(size_t)); stream.write(reinterpret_cast(&numSamplesAfterLastMerge), sizeof(size_t)); } @@ -161,7 +248,6 @@ void AdaptiveSplitAndMergeFactory::Statistics::deserialize(std sufficientStatistics.deserialize(stream); splittingStatistics.deserialize(stream); - // stream.read(reinterpret_cast(&numComponents), sizeof(size_t)); stream.read(reinterpret_cast(&numSamplesAfterLastSplit), sizeof(size_t)); stream.read(reinterpret_cast(&numSamplesAfterLastMerge), sizeof(size_t)); } @@ -201,27 +287,12 @@ std::string AdaptiveSplitAndMergeFactory::Statistics::toString return ss.str(); } -template -std::string AdaptiveSplitAndMergeFactory::FittingStatistics::toString() const -{ - std::stringstream ss; - ss << "FittingStatistics:" << std::endl; - ss << "\tnumSamples:" << numSamples << std::endl; - ss << "\tnumSplits:" << numSplits << std::endl; - ss << "\tnumMerges:" << numMerges << std::endl; - ss << "\tnumComponents:" << numComponents << std::endl; - ss << "\tnumUpdateWEMIterations:" << numUpdateWEMIterations << std::endl; - ss << "\tnumPartialUpdateWEMIterations:" << numPartialUpdateWEMIterations << std::endl; - return ss.str(); -} - template void AdaptiveSplitAndMergeFactory::Statistics::clear(const size_t &_numComponents) { sufficientStatistics.clear(_numComponents); splittingStatistics.clear(_numComponents); - // numComponents = _numComponents; numSamplesAfterLastSplit = 0; numSamplesAfterLastMerge = 0; } @@ -248,6 +319,28 @@ bool AdaptiveSplitAndMergeFactory::Statistics::operator==(cons return equal; } +/******************************************************************************** + * Implementation of the AdaptiveSplitAndMergeFactor::FittingStatistics functions. + ********************************************************************************/ + +template +std::string AdaptiveSplitAndMergeFactory::FittingStatistics::toString() const +{ + std::stringstream ss; + ss << "FittingStatistics:" << std::endl; + ss << "\tnumSamples:" << numSamples << std::endl; + ss << "\tnumSplits:" << numSplits << std::endl; + ss << "\tnumMerges:" << numMerges << std::endl; + ss << "\tnumComponents:" << numComponents << std::endl; + ss << "\tnumUpdateWEMIterations:" << numUpdateWEMIterations << std::endl; + ss << "\tnumPartialUpdateWEMIterations:" << numPartialUpdateWEMIterations << std::endl; + return ss.str(); +} + +/******************************************************************************** + * Implementation of the AdaptiveSplitAndMergeFactor::Configuration functions. + ********************************************************************************/ + template void AdaptiveSplitAndMergeFactory::Configuration::serialize(std::ostream &stream) const { @@ -297,6 +390,10 @@ std::string AdaptiveSplitAndMergeFactory::Configuration::toStr return ss.str(); } +/******************************************************************************** + * Implementation of the AdaptiveSplitAndMergeFactor functions. + ********************************************************************************/ + template void AdaptiveSplitAndMergeFactory::prepareSamples(SampleData *samples, const size_t numSamples, const SampleStatistics &sampleStatistics, const Configuration &cfg) const @@ -481,6 +578,7 @@ void AdaptiveSplitAndMergeFactory::update(VMM &vmm, Statistics OPENPGL_ASSERT(stats.sufficientStatistics.isValid()); } +#ifdef OPENPGL_RADIANCE_CACHES template void AdaptiveSplitAndMergeFactory::updateFluenceEstimate(VMM &vmm, const SampleData *samples, const size_t numSamples, const size_t numZeroValueSamples, const SampleStatistics &sampleStatistics) const @@ -488,7 +586,8 @@ void AdaptiveSplitAndMergeFactory::updateFluenceEstimate(VMM & WeightedEMFactory factory = WeightedEMFactory(); factory.updateFluenceEstimate(vmm, samples, numSamples, numZeroValueSamples, sampleStatistics); } - +#endif +#ifdef OPENPGL_VSP_GUIDING template void AdaptiveSplitAndMergeFactory::updateVolumeScatterProbability(VMM &vmm, Statistics &stats, const SampleData *samples, const size_t numSamples, const ZeroValueSampleData *zeroValueSamples, const size_t numZeroValueSamples, @@ -497,5 +596,5 @@ void AdaptiveSplitAndMergeFactory::updateVolumeScatterProbabil WeightedEMFactory factory = WeightedEMFactory(); factory.updateVolumeScatterProbability(vmm, stats.sufficientStatistics, samples, numSamples, zeroValueSamples, numZeroValueSamples, varianceBased); } - +#endif } // namespace openpgl