示例#1
0
int main( int argc, char **argv )
{

  if ( argc < 2 ) {
    Error( "Missing configuration file in options. (testing.conf)" );
    exit( -1 );
  }

  srand( 345645631 );


  env.parse( argv[1] );
  env.Summary();


  std::vector<std::string> imgList = std::move( readlines( strf( "%s/%s", env["dataset"].c_str(),
                                                                 env["list-file"].c_str())) );
  imgList = std::move( path::FFFL( env["dataset"], imgList, ".png" ) );
  
  for ( auto& ele : imgList ) {
    printf( "%s\n", ele.c_str() );
  }


  Info( "Loading Learning Album ..." );
  Album<float> album;
  for ( auto& ele : imgList ) {
    album.push( std::move( cvFeat<HOG>::gen( ele ) ) );
  }
  Done( "Learning Album Loaded" );
  
  
  Info( "Loading Forest ..." );
  Forest<SimpleKernel<float> > forest( env["forest-name"] );
  
  Info( "Learning ..." );
  
  forest.PrepareWeitghts();
  
  float feat[album(0).GetPatchDim()];

  for ( int k=0; k<album.size(); k++ ) {
    for ( int i=0; i<album(k).rows; i++ ) {
      for ( int j=0; j<album(k).cols; j++ ) {
        album(k).FetchPatch( i, j, feat );
        forest.learn( feat );
      }
    }
    Info( "%d / %d learned.", k + 1, album.size() );
  }
  
  forest.writeWeights( env["forest-name"] );
  
  return 0;
}
示例#2
0
int main( int argc, char **argv )
{

  if ( argc < 2 ) {
    Error( "Missing configuration file in options. (training.conf)" );
    exit( -1 );
  }
  
  srand( 345645631 );

  env.parse( argv[1] );
  env.Summary();
  
  std::vector<std::string> imgList = std::move( readlines( strf( "%s/%s", env["dataset"].c_str(),
                                                                 env["list-file"].c_str())) );
  imgList =  std::move( path::FFFL( env["dataset"], imgList, ".png" ) );

  for ( auto& ele : imgList ) {
    printf( "%s\n", ele.c_str() );
  }


  Info( "Loading Training Album ..." );

  Album<float> album;
  for ( auto& ele : imgList ) {
    album.push( std::move( cvFeat<HOG>::gen( ele ) ) );
  }
  Done( "Training Album Loaded" );
  
  std::vector<FeatImage<float>::PatchProxy> l;
  
  for ( int k=0; k<album.size(); k++ ) {
    auto& ref = album(k);
    for ( int i=7; i<ref.rows-7; i++ ) {
      for ( int j=7; j<ref.cols-7; j++ ) {
        l.push_back( ref.Spawn( i, j ) );
      }
    }
  }


  timer::tic();
  Forest<SimpleKernel<float> > forest( env["forest-size"], l, env["proportion-of-data-per-tree"].toDouble() );
  Done( "Tree built within %.5lf sec.", timer::utoc() );

  forest.write( env["forest-name"] );

  return 0;
}
示例#3
0
int main( int argc, char **argv )
{

  if ( argc < 2 ) {
    Error( "Missing configuration file in options. (testing.conf)" );
    exit( -1 );
  }

  srand( 345645631 );
  

  env.parse( argv[1] );
  env.Summary();

  LabelSet::initialize( env["color-map"] );


  std::vector<std::string> nameList = std::move( readlines( strf( "%s/%s", env["dataset"].c_str(),
                                                                  env["list-file"].c_str())) );
  std::vector<std::string> imgList = std::move( path::FFFL( env["dataset"], nameList, ".png" ) );
  std::vector<std::string> labelList = std::move( path::FFFL( env["dataset"], nameList, "_L.png" ) );  
  
  for ( auto& ele : imgList ) {
    printf( "%s\n", ele.c_str() );
  }


  Info( "Loading Learning Album ..." );
  Album<float> album;
  for ( auto& ele : imgList ) {
    album.push( std::move( cvFeat<HOG>::gen( ele ) ) );
  }


  Album<uchar> labelAlbum;
  for ( auto& ele : labelList ) {
    labelAlbum.push( std::move( cvFeat<BGR>::gen( ele ) ) );
  }
  Done( "Learning Album Loaded" );
  
  
  Info( "Loading Forest ..." );
  Forest<SimpleKernel<float> > forest( env["forest-name"] );
  Done( "Forest Loaded." );
  
  Info( "Start Label Training ..." );



  /// Construct Bipartite Graph between l and m
  /// and also the ground truth P

  Info( "Initializing Optimization ... " );

  // calculate M
  int M = 0;
  for ( auto& img :album ) {
    M += ( img.rows - 14 ) * ( img.cols - 14 );
  }

  Bipartite m_to_l( M, forest.centers() );
  
  double *P = new double[M * LabelSet::classes];
  double *pP = P;

  int m = 0;
  for ( auto& img : album ) {
    printf( "working on Image %d ...\n", img.id );
    float feat[img.GetPatchDim()];
    for ( int i=7; i<img.rows-7; i++ ) {
      for ( int j=7; j<img.cols-7; j++ ) {
        img.FetchPatch( i, j, feat );
        auto res = std::move( forest.query_with_coef( feat ) );

        int count = 0;
        for ( auto& ele : res ) {
          if ( count++ > 100 ) break;
          m_to_l.add( m, ele.first, ele.second );
        }

        const uchar* color = labelAlbum(img.id)( i, j );
        int classID = LabelSet::GetClass( color[0],
                                          color[1],
                                          color[2] );
        for ( int k=0; k<LabelSet::classes; k++ ) {
          if ( k == classID ) {
            *(pP++) = 1.0;
          } else {
            *(pP++) = 0.0;
          }
        }
        m++;
      }
    }
  }
  Done( "Initialized." ); 
  
  double *q = new double[ forest.centers() * LabelSet::classes ];
  double *qp = q;
  for ( int i=0; i<forest.centers() * LabelSet::classes; i++ ) *(qp++) = LabelSet::inv;


  
  Solver solver;
  solver.options.beta = 0.0;
  solver.options.maxIter = 20;
  Info( "Solving ..." );
  solver.solve( M, forest.centers(), &forest, &m_to_l, P, q );
  Done( "Solved." );


  // debugging:
  // double *q1 = new double[ forest.centers() * LabelSet::classes ];
  // solver.solve1( M, forest.centers(), &m_to_l, P, q1 );

  // qp = q;
  // double *qp1 = q1;
  // for ( int l=0; l<forest.centers(); l++ ) {
  //   printf( "q[%d] = ", l );
  //   printVec( qp, LabelSet::classes );
  //   printf( "q1[%d] = ", l );
  //   printVec( qp1, LabelSet::classes );
  //   char ch;
  //   scanf( "%c", &ch );
  //   qp += LabelSet::classes;
  //   qp1 += LabelSet::classes;
  // }
  

  

  
  // update center label maps
  qp = q;
  for ( int l=0; l<forest.centers(); l++ ) {
    forest.updateLabelMap( l, qp );
    qp += LabelSet::classes;
  }
  forest.writeLeaves( env["forest-name"] );
  
  Done( "Write to forest." );


  DeleteToNullWithTestArray( q );
  DeleteToNullWithTestArray( P );
  return 0;
}
示例#4
0
int main( int argc, char **argv )
{

  if ( argc < 2 ) {
    Error( "Missing configuration file in arguments. (treeQ.conf)" );
    exit( -1 );
  }

  // srand( 7325273 );
  srand(time(NULL));

  env.parse( argv[1] );
  env.Summary();

  LabelSet::initialize( env["color-map"] );

  /* ---------- Build/Load Forest ---------- */
  std::vector<std::string> imgList = std::move( readlines( strf( "%s/%s", env["dataset"].c_str(),
                                                                 env["list-file"].c_str())) );
  auto lblList = std::move( path::FFFL( env["dataset"], imgList, "_L.png" ) );
  imgList =  std::move( path::FFFL( env["dataset"], imgList, ".png" ) );

  Album<float> album;
  {
    int i = 0;
    int n = static_cast<int>( imgList.size() );
    for ( auto& ele : imgList ) {
      album.push( std::move( cvFeat<HOG>::gen( ele ) ) );
      progress( ++i, n, "Loading Album" );
    }
  }
  printf( "\n" );



  
  Album<int> lblAlbum;
  {
    int i = 0;
    int n = static_cast<int>( lblList.size() );
    for ( auto& ele : lblList ) {
      lblAlbum.push( std::move( cvFeat<HARD_LABEL_MAP>::gen( ele ) ) );
      progress( ++i, n, "Loading Label Album" );
    }
  }
  printf( "\n" );
  lblAlbum.SetPatchSize( env["lbl-size"] );
  lblAlbum.SetPatchStride( 1 );


  

  
  /* ---------- Load Forest ---------- */
  Info( "Loading Forest .." );
  timer::tic();
  Forest<EntropyKernel<float> > forest( env["forest-dir"] );
  printf( "tree loaded: %.3lf sec\n", timer::utoc() );
  printf( "maxDepth: %d\n", forest.maxDepth() );

  
  /* ---------- Collective Entropy ---------- */
  if ( env.find( "entropy-output" ) ) {
    WITH_OPEN( out, env["entropy-output"].c_str(), "w" );
    int label[lblAlbum(0).GetPatchDim()];
    int count[LabelSet::classes];
    for ( int i=0; i<forest.centers(); i++ ) {
      memset( count, 0, sizeof(int) * LabelSet::classes );
      for ( auto& ele : forest(i).store ) {
        lblAlbum(ele.id).FetchPatch( ele.y, ele.x, label );
        for ( int j=0; j<lblAlbum(0).GetPatchDim(); j++ ) {
          count[label[j]]++;
        }
      }
      double ent = entropy( count, LabelSet::classes );
      fprintf( out, "%.8lf\n", ent );
      if ( 0 == i % 100 ) progress( i+1, forest.centers(), "Calculating Entropy" );
    }
    printf( "\n" );
    END_WITH( out );
  }


  /* ---------- Center Entropy ---------- */
  if ( env.find( "center-entropy-output" ) ) {
    WITH_OPEN( out, env["center-entropy-output"].c_str(), "w" );
    int count[LabelSet::classes];
    for ( int i=0; i<forest.centers(); i++ ) {
      memset( count, 0, sizeof(int) * LabelSet::classes );
      for ( auto& ele : forest(i).store ) {
        count[*lblAlbum(ele.id)(ele.y, ele.x)]++;
      }
      double ent = entropy( count, LabelSet::classes );
      fprintf( out, "%.8lf\n", ent );
      if ( 0 == i % 100 ) progress( i+1, forest.centers(), "Calculating Entropy" );
    }
    printf( "\n" );
    END_WITH( out );
  }


  /* ---------- Voting Test ---------- */
  if ( env.find( "reconstruct-output" ) ) {

    int label[lblAlbum(0).GetPatchDim()];

    // Class Weight
    double classWeight[LabelSet::classes];
    GetClassInvDistribution( lblAlbum, classWeight );


    printf( "---------- Class Weight ----------\n" );
    for ( int i=0; i<LabelSet::classes; i++ ) {
      printf( "%20s: %.6lf\n", LabelSet::GetClassName(i).c_str(), classWeight[i] );
    }
    Done( "Calculating Inverse Class Weight." );


    // Build voters
    std::vector<std::vector<float> > voters;
    
    int voterSize = env["lbl-size"];
    
    voters.resize( forest.centers() );
    for ( int leafID=0; leafID<forest.centers(); leafID++ ) {
      voters[leafID].resize( LabelSet::classes * voterSize * voterSize );
      for ( auto& loc : forest(leafID).store ) {
        lblAlbum(loc.id).FetchPatch( loc.y, loc.x, label );
        for ( int i=0; i<voterSize*voterSize; i++ ) {
          int k = label[i];
          voters[leafID][ i * LabelSet::classes + k ] += classWeight[k];
        }
      }
      for ( int i=0; i<voterSize*voterSize*LabelSet::classes; i+=LabelSet::classes ) {
        float s = sum_vec( &voters[leafID][i], LabelSet::classes );
        scale( &voters[leafID][i], LabelSet::classes, 1.0f / s );
      }
      if ( 0 == leafID % 20000 ) {
        progress( leafID + 1, forest.centers(), "constructing voters" );
      }
    }
    printf( "\n" );
    
    
    int voterRadius = voterSize >> 1;
    float feat[album(0).GetPatchDim()];
    for ( int i=0; i<album.size(); i++ ) {
      VoteMap voteMap( album(i).rows, album(i).cols );
      for ( int y=0; y<album(i).rows; y++ ) {
        for ( int x=0; x<album(i).cols; x++ ) {
          album(i).FetchPatch( y, x, feat );
          std::vector<int> res = std::move( forest.query( feat ) );
          for ( auto& leafID : res ) {
            int j = 0;
            for ( int dy=-voterRadius; dy<=voterRadius; dy++ ) {
              int y1 = y + dy;
              if ( 0 > y1 || album(i).rows <= y1 ) continue;
              for ( int dx=-voterRadius; dx<=voterRadius; dx++, j += LabelSet::classes ) {
                int x1 = x + dx;
                if ( 0 > x1 || album(i).cols <= x1 ) continue;
                voteMap.vote( y1, x1, &voters[leafID][j] );
              } // for dx
            } // for dy
          } // for leafID
        } // for x
      } // for y

      WITH_OPEN( out, strf( "%s/%s.txt", env["reconstruct-output"].c_str(), imgList[i].c_str() ).c_str(), "w" );
      fprintf( out, "%.6lf\n", voteMap.compare( lblAlbum(i) ) );
      END_WITH( out );

      cv::Mat syn = voteMap.synthesis();
      cv::imwrite( strf( "%s/%s", env["reconstruct-output"].c_str(), imgList[i].c_str() ), syn );
      progress( i+1, album.size(), "Reconstructing" );
    } // for i
    printf( "\n" );
  }