GrabCut::GrabCut( Image<Color>* image ) { m_image = image; m_w = m_image->width(); m_h = m_image->height(); m_trimap = new Image<TrimapValue>( m_w, m_h ); m_trimap->fill(TrimapUnknown); m_GMMcomponent = new Image<unsigned int>( m_w, m_h ); m_hardSegmentation = new Image<SegmentationValue>( m_w, m_h ); m_hardSegmentation->fill(SegmentationBackground); m_softSegmentation = 0; // Not yet implemented m_TLinksImage = new Image<Color>(m_w, m_h); m_TLinksImage->fill(Color(0,0,0)); m_NLinksImage = new Image<Real>(m_w, m_h); m_NLinksImage->fill(0); m_GMMImage = new Image<Color>(m_w, m_h); m_GMMImage->fill(Color(0,0,0)); m_AlphaImage = new Image<Real>(m_w, m_h); m_AlphaImage->fill(0); m_foregroundGMM = new GMM(5); m_backgroundGMM = new GMM(5); //set some constants m_lambda = 50; computeL(); computeBeta(); m_NLinks = new Image<NLinks>( m_w, m_h ); computeNLinks(); m_graph = 0; m_nodes = new Image<Graph::node_id>( m_w, m_h ); }
void computeFSPAI(MatrixType const & A, MatrixType const & PatternA, MatrixType & L, MatrixType & L_trans, fspai_tag) { typedef typename MatrixType::value_type ScalarType; typedef boost::numeric::ublas::matrix<ScalarType> DenseMatrixType; typedef std::vector<std::map<unsigned int, ScalarType> > SparseMatrixType; // // preprocessing: Store A in a STL container: // //std::cout << "Transferring to STL container:" << std::endl; std::vector<std::vector<ScalarType> > y_k(A.size1()); SparseMatrixType STL_A(A.size1()); sym_sparse_matrix_to_stl(A, STL_A); // // Step 1: Generate pattern indices // //std::cout << "computeFSPAI(): Generating pattern..." << std::endl; std::vector<std::vector<vcl_size_t> > J(A.size1()); generateJ(PatternA, J); // // Step 2: Set up matrix blocks // //std::cout << "computeFSPAI(): Setting up matrix blocks..." << std::endl; std::vector<DenseMatrixType> subblocks_A(A.size1()); fill_blocks(STL_A, subblocks_A, J, y_k); STL_A.clear(); //not needed anymore // // Step 3: Cholesky-factor blocks // //std::cout << "computeFSPAI(): Cholesky-factorization..." << std::endl; for (vcl_size_t i=0; i<subblocks_A.size(); ++i) { //std::cout << "Block before: " << subblocks_A[i] << std::endl; cholesky_decompose(subblocks_A[i]); //std::cout << "Block after: " << subblocks_A[i] << std::endl; } /*vcl_size_t num_bytes = 0; for (vcl_size_t i=0; i<subblocks_A.size(); ++i) num_bytes += 8*subblocks_A[i].size1()*subblocks_A[i].size2();*/ //std::cout << "Memory for FSPAI matrix: " << num_bytes / (1024.0 * 1024.0) << " MB" << std::endl; // // Step 4: Solve for y_k // //std::cout << "computeFSPAI(): Cholesky-solve..." << std::endl; for (vcl_size_t i=0; i<y_k.size(); ++i) { if (subblocks_A[i].size1() > 0) //block might be empty... { //y_k[i].resize(subblocks_A[i].size1()); //std::cout << "y_k[" << i << "]: "; //for (vcl_size_t j=0; j<y_k[i].size(); ++j) // std::cout << y_k[i][j] << " "; //std::cout << std::endl; cholesky_solve(subblocks_A[i], y_k[i]); } } // // Step 5: Set up Cholesky factors L and L_trans // //std::cout << "computeFSPAI(): Computing L..." << std::endl; L.resize(A.size1(), A.size2(), false); L.reserve(A.nnz(), false); L_trans.resize(A.size1(), A.size2(), false); L_trans.reserve(A.nnz(), false); computeL(A, L, L_trans, y_k, J); //std::cout << "L: " << L << std::endl; }
int main(int argc, char**) { { long long t1=0; float s1=0; long long t2=0; float s2=0; long long t3=0; float s3=0; fillR(); computeV(); for (int i=0; i!=10000; ++i) { fillO(); t1 -= rdtsc(); computeV(); t1 += rdtsc(); s1+=sum(); t2 -= rdtsc(); computeA(); t2 += rdtsc(); s2+=sum(); t3 -= rdtsc(); computeB(); t3 += rdtsc(); s3+=sum(); } std::cout << "native vector " << s1 << " " << double(t1)/10000 << std::endl; std::cout << "vector by elements " << s2 << " " << double(t2)/10000 << std::endl; std::cout << "scalar " << s3 << " " << double(t3)/10000 << std::endl << std::endl; } { long long t1=0; float s1=0; long long t2=0; float s2=0; long long t3=0; float s3=0; fillO(); computeV(); for (int i=0; i!=10000; ++i) { fillR(); t1 -= rdtsc(); computeV(); t1 += rdtsc(); s1+=sum(); t2 -= rdtsc(); computeA(); t2 += rdtsc(); s2+=sum(); memcpy(a,va,sizeof(float)*1024*vfl); t3 -= rdtsc(); computeL(); t3 += rdtsc(); memcpy(vb,b,sizeof(float)*1024*vfl); s3+=sum(); } std::cout << "native vector " << s1 << " " << double(t1)/10000 << std::endl; std::cout << "vector by elements " << s2 << " " << double(t2)/10000 << std::endl; std::cout << "scalar " << s3 << " " << double(t3)/10000 << std::endl << std::endl; } { long long t1=0; float s1=0; long long t2=0; float s2=0; long long t3=0; float s3=0; fillO(); computeV(); for (int i=0; i!=10000; ++i) { fillW(); t1 -= rdtsc(); computeV(); t1 += rdtsc(); s1+=sum(); t2 -= rdtsc(); computeA(); t2 += rdtsc(); s2+=sum(); t3 -= rdtsc(); computeB(); t3 += rdtsc(); s3+=sum(); } std::cout << "native vector " << s1 << " " << double(t1)/10000 << std::endl; std::cout << "vector by elements " << s2 << " " << double(t2)/10000 << std::endl; std::cout << "scalar " << s3 << " " << double(t3)/10000 << std::endl << std::endl; } return 0; }