示例#1
0
文件: nerUtils.cpp 项目: aymara/lima
LIMA_TENSORFLOWSPECIFICENTITIES_EXPORT Eigen::MatrixXi viterbiDecode(
  const Eigen::MatrixXf& score,
  const Eigen::MatrixXf& transitionParams){
  if(score.size()==0){
    std::cerr<<"The output is empty. Check the inputs.";
    return Eigen::MatrixXi();
  }
  if(transitionParams.size()==0){
    std::cerr<<"The transition matrix is empty. Check it.";
    return Eigen::MatrixXi();
  }
  //1. Initialization of matrices
  Eigen::MatrixXf trellis=  Eigen::MatrixXf::Zero(score.rows(),score.cols());
  Eigen::MatrixXi backpointers= Eigen::MatrixXi::Zero(score.rows(),score.cols());
  trellis.row(0)=score.row(0);
  Eigen::MatrixXf v(transitionParams.rows(),transitionParams.cols());

  //2.Viterbi algorithm
  for(auto k=1;k<score.rows();++k)
  {
    for(auto i=0;i<transitionParams.rows();++i){
      for(auto j=0;j<transitionParams.cols();++j){
        v(i,j)=trellis(k-1,i)+transitionParams(i,j);
      }
    }
    trellis.row(k)=score.row(k)+v.colwise().maxCoeff();//equivalent to np.max() function
    for(auto i=0;i<backpointers.cols();++i)
    {
      v.col(i).maxCoeff(&backpointers(k,i));//equivalent to np.argmax() function
    }
  }
  //In Eigen, vector is just a particular matrix with one row or one column
  Eigen::VectorXi viterbi(score.rows());
  trellis.row(trellis.rows()-1).maxCoeff(&viterbi(0));
  Eigen::MatrixXi bp = backpointers.colwise().reverse();
  for(auto i=0;i<backpointers.rows()-1;++i)
  {
    viterbi(i+1)=bp(i,viterbi(i));
  }

  float viterbi_score=trellis.row(trellis.rows()-1).maxCoeff();
  LIMA_UNUSED(viterbi_score);
  //std::cout<<"Viterbi score of the sentence: "<<viterbi_score<<std::endl;

  return viterbi.colwise().reverse();
}
示例#2
0
void VertexBufferObject::update(const Eigen::MatrixXf& M)
{
  assert(id != 0);
  glBindBuffer(GL_ARRAY_BUFFER, id);
  glBufferData(GL_ARRAY_BUFFER, sizeof(float)*M.size(), M.data(), GL_DYNAMIC_DRAW);
  rows = M.rows();
  cols = M.cols();
  check_gl_error();
}
void run ()
{

  const double tol = argument[2];

#ifdef MRTRIX_UPDATED_API

  const Eigen::MatrixXf in1 = load_matrix<float> (argument[0]);
  const Eigen::MatrixXf in2 = load_matrix<float> (argument[1]);
  
  if (in1.rows() != in2.rows() || in1.cols() != in2.cols())
    throw Exception ("matrices \"" + Path::basename (argument[0]) + "\" and \"" + Path::basename (argument[1]) + "\" do not have matching sizes"
                     " (" + str(in1.rows()) + "x" + str(in1.cols()) + " vs " + str(in2.rows()) + "x" + str(in2.cols()) + ")");
  
  const size_t numel = in1.size();
  for (size_t i = 0; i != numel; ++i) {
    if (std::abs (*(in1.data()+i) - *(in2.data()+i)) > tol)
      throw Exception ("matrices \"" + Path::basename (argument[0]) + "\" and \"" + Path::basename (argument[1]) + " do not match within specified precision of " + str(tol)
                       + " (" + str(*(in1.data()+i)) + " vs " + str(*(in2.data()+i)) + ")");
  }
  
#else

  Math::Matrix<float> in1, in2;
  in1.load (argument[0]);
  in2.load (argument[1]);
  
  if (in1.rows() != in2.rows() || in1.columns() != in2.columns())
    throw Exception ("matrices \"" + Path::basename (argument[0]) + "\" and \"" + Path::basename (argument[1]) + "\" do not have matching sizes"
                     " (" + str(in1.rows()) + "x" + str(in1.columns()) + " vs " + str(in2.rows()) + "x" + str(in2.columns()) + ")");
            
  const size_t numel = in1.rows() * in1.columns();
  for (size_t i = 0; i != numel; ++i) {
    if (std::abs (*(in1.ptr()+i) - *(in2.ptr()+i)) > tol)
      throw Exception ("matrices \"" + Path::basename (argument[0]) + "\" and \"" + Path::basename (argument[1]) + " do not match within specified precision of " + str(tol)
                       + " (" + str(*(in1.ptr()+i)) + " vs " + str(*(in2.ptr()+i)) + ")");
  }               

#endif

  CONSOLE ("data checked OK");
}
void run ()
{

  const Eigen::MatrixXf in1 = load_matrix<float> (argument[0]);
  const Eigen::MatrixXf in2 = load_matrix<float> (argument[1]);
  
  if (in1.rows() != in2.rows() || in1.cols() != in2.cols())
    throw Exception ("matrices \"" + Path::basename (argument[0]) + "\" and \"" + Path::basename (argument[1]) + "\" do not have matching sizes"
                     " (" + str(in1.rows()) + "x" + str(in1.cols()) + " vs " + str(in2.rows()) + "x" + str(in2.cols()) + ")");

  const size_t numel = in1.size();

  auto opt = get_options ("frac");
  if (opt.size()) {
  
    const double tol = opt[0][0];
    
    for (size_t i = 0; i != numel; ++i) {
      if (std::abs ((*(in1.data()+i) - *(in2.data()+i)) / (0.5 * (*(in1.data()+i) + *(in2.data()+i)))) > tol)
        throw Exception ("matrices \"" + Path::basename (argument[0]) + "\" and \"" + Path::basename (argument[1]) + " do not match within fractional precision of " + str(tol)
                         + " (" + str(*(in1.data()+i)) + " vs " + str(*(in2.data()+i)) + ")");
    }
  
  } else {
  
    double tol = 0.0;
    opt = get_options ("abs");
    if (opt.size())
      tol = opt[0][0];

    for (size_t i = 0; i != numel; ++i) {
      if (std::abs (*(in1.data()+i) - *(in2.data()+i)) > tol)
        throw Exception ("matrices \"" + Path::basename (argument[0]) + "\" and \"" + Path::basename (argument[1]) + " do not match within absolute precision of " + str(tol)
                         + " (" + str(*(in1.data()+i)) + " vs " + str(*(in2.data()+i)) + ")");
    }
        
  }
  
  CONSOLE ("data checked OK");
}
void CloudAnalyzer2D::examinePointEvidence() {
  pointEvidence.clear();

  for (int r = 0; r < R->size(); ++r) {
    Eigen::MatrixXf total = Eigen::MatrixXf::Zero(newRows, newCols);

#pragma omp parallel for schedule(static)
    for (int i = 0; i < total.cols(); ++i) {
      for (int j = 0; j < total.rows(); ++j) {
        for (int k = 0; k < numZ; ++k) {
          const Eigen::Vector3d coord(i, j, k);
          const Eigen::Vector3i src =
              (R->at(r) * (coord - newZZ) + zeroZero)
                  .unaryExpr([](auto v) { return std::round(v); })
                  .cast<int>();

          if (src[0] < 0 || src[0] >= numX || src[1] < 0 || src[1] >= numY ||
              src[2] < 0 || src[2] >= numZ)
            continue;

          if (pointInVoxel->at(src))
            ++total(j, i);
        }
      }
    }

    double average, sigma;
    const float *dataPtr = total.data();
    std::tie(average, sigma) = place::aveAndStdev(
        dataPtr, dataPtr + total.size(), [](auto v) { return v; },
        [](auto v) -> bool { return v; });

    cv::Mat heatMap(newRows, newCols, CV_8UC1, cv::Scalar::all(255));
    for (int j = 0; j < heatMap.rows; ++j) {
      uchar *dst = heatMap.ptr<uchar>(j);
      for (int i = 0; i < heatMap.cols; ++i) {
        const double count = total(j, i);
        if (count > 0) {
          const int gray = cv::saturate_cast<uchar>(
              255.0 *
              ((count - average - sigma) / (3.0 * sigma) - 0.0 * sigma));
          dst[i] = 255 - gray;
        }
      }
    }

    if (FLAGS_preview && doors->size()) {
      std::cout << "Number of doors: " << doors->size() << std::endl;
      cv::Mat out;
      cv::cvtColor(heatMap, out, CV_GRAY2BGR);
      cv::Mat_<cv::Vec3b> _out = out;
      for (auto &d : *doors) {
        Eigen::Vector3d bl = d.corner * FLAGS_scale;
        bl[1] *= -1.0;
        bl = R->at(r).inverse() * bl;

        Eigen::Vector3d xAxis = d.xAxis;
        xAxis[1] *= -1.0;
        xAxis = R->at(r).inverse() * xAxis;

        Eigen::Vector3d zAxis = d.zAxis;
        zAxis[1] *= -1.0;
        zAxis = R->at(r).inverse() * zAxis;

        double w = d.w * FLAGS_scale;

        auto color = randomColor();
        for (int i = 0; i < w; ++i) {
          Eigen::Vector3d pix =
              (bl + i * xAxis + i * zAxis + newZZ).unaryExpr([](auto v) {
                return std::round(v);
              });

          if (pix[0] < 0 || pix[0] >= out.cols || pix[1] < 0 ||
              pix[1] >= out.rows)
            continue;

          _out(pix[1], pix[0]) = color;
        }
      }
      cv::rectshow(out);
    }

    pointEvidence.push_back(heatMap);

    if (FLAGS_preview)
      cv::rectshow(heatMap);
  }
}