/* Copyright (c) 2013, Philipp Krähenbühl All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the Stanford University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY Philipp Krähenbühl ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Philipp Krähenbühl BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "densecrf.h" #include "permutohedral.h" #include "util.h" #include "pairwise.h" #include #include #include ///////////////////////////// ///// Alloc / Dealloc ///// ///////////////////////////// DenseCRF::DenseCRF(int N, int M) : N_(N), M_(M), unary_(0) { } DenseCRF::~DenseCRF() { if (unary_) delete unary_; for( unsigned int i=0; iget(); expAndNormalize( Q, -unary ); for( int it=0; itapply( tmp2, Q ); tmp1 -= tmp2; } expAndNormalize( Q, tmp1 ); } return Q; } VectorXs DenseCRF::map ( int n_iterations ) const { // Run inference MatrixXf Q = inference( n_iterations ); // Find the map return currentMap( Q ); } /////////////////// ///// Debug ///// /////////////////// VectorXf DenseCRF::unaryEnergy(const VectorXs & l) { assert( l.cols() == N_ ); VectorXf r( N_ ); r.fill(0.f); if( unary_ ) { MatrixXf unary = unary_->get(); for( int i=0; iapply( Q, Q ); for( int i=0; iget() ); return Q; } void DenseCRF::stepInference( MatrixXf & Q, MatrixXf & tmp1, MatrixXf & tmp2 ) const{ tmp1.resize( Q.rows(), Q.cols() ); tmp1.fill(0); if( unary_ ) tmp1 -= unary_->get(); // Add up all pairwise potentials for( unsigned int k=0; kapply( tmp2, Q ); tmp1 -= tmp2; } // Exponentiate and normalize expAndNormalize( Q, tmp1 ); } VectorXs DenseCRF::currentMap( const MatrixXf & Q ) const{ VectorXs r(Q.cols()); // Find the map for( int i=0; iget(); for( int i=0; iapply( tmp, Q ); kl += (Q.array()*tmp.array()).sum(); } return kl; } // Gradient computations double DenseCRF::gradient( int n_iterations, const ObjectiveFunction & objective, VectorXf * unary_grad, VectorXf * lbl_cmp_grad, VectorXf * kernel_grad) const { // Run inference std::vector< MatrixXf > Q(n_iterations+1); MatrixXf tmp1, unary( M_, N_ ), tmp2; unary.fill(0); if( unary_ ) unary = unary_->get(); expAndNormalize( Q[0], -unary ); for( int it=0; itapply( tmp2, Q[it] ); tmp1 -= tmp2; } expAndNormalize( Q[it+1], tmp1 ); } // Compute the objective value MatrixXf b( M_, N_ ); double r = objective.evaluate( b, Q[n_iterations] ); sumAndNormalize( b, b, Q[n_iterations] ); // Compute the gradient if(unary_grad && unary_) *unary_grad = unary_->gradient( b ); if( lbl_cmp_grad ) *lbl_cmp_grad = 0*labelCompatibilityParameters(); if( kernel_grad ) *kernel_grad = 0*kernelParameters(); for( int it=n_iterations-1; it>=0; it-- ) { // Do the inverse message passing tmp1.fill(0); int ip = 0, ik = 0; // Add up all pairwise potentials for( unsigned int k=0; kgradient( b, Q[it] ); lbl_cmp_grad->segment( ip, pg.rows() ) += pg; ip += pg.rows(); } // Compute the kernel gradient expression if( kernel_grad ) { VectorXf pg = pairwise_[k]->kernelGradient( b, Q[it] ); kernel_grad->segment( ik, pg.rows() ) += pg; ik += pg.rows(); } // Compute the new b pairwise_[k]->applyTranspose( tmp2, b ); tmp1 += tmp2; } sumAndNormalize( b, tmp1.array()*Q[it].array(), Q[it] ); // Add the gradient if(unary_grad && unary_) *unary_grad += unary_->gradient( b ); } return r; } VectorXf DenseCRF::unaryParameters() const { if( unary_ ) return unary_->parameters(); return VectorXf(); } void DenseCRF::setUnaryParameters( const VectorXf & v ) { if( unary_ ) unary_->setParameters( v ); } VectorXf DenseCRF::labelCompatibilityParameters() const { std::vector< VectorXf > terms; for( unsigned int k=0; kparameters() ); int np=0; for( unsigned int k=0; k n; for( unsigned int k=0; kparameters().rows() ); int np=0; for( unsigned int k=0; ksetParameters( v.segment( i, n[k] ) ); i += n[k]; } } VectorXf DenseCRF::kernelParameters() const { std::vector< VectorXf > terms; for( unsigned int k=0; kkernelParameters() ); int np=0; for( unsigned int k=0; k n; for( unsigned int k=0; kkernelParameters().rows() ); int np=0; for( unsigned int k=0; ksetKernelParameters( v.segment( i, n[k] ) ); i += n[k]; } }