double NNClassifier::computeScore(const Example& example) {
  NRVec<double> mid_layer1out(_layer1OutDim);
  mat layer1out;
  mat layer2out;

  int offset;
  static double temp;
  static int tmpI, tmpJ, tmpK;
  //forward propagation

  const Feature& feature = example.m_feature;
  const vector<int>& wneural_features = feature.wneural_features;
  const vector<int>& aneural_features = feature.aneural_features;

  assert(wneural_features.size() == _wordcontext && aneural_features.size() == _atomcontext);

  offset = 0;

  mid_layer1out = 0.0;
  for (int i = 0; i < _wordcontext; i++) {
    int curFeatId = wneural_features[i] * wneural_features.size() + i;
    tmpJ = wneural_features[i] * _wordDim;
    tmpI = offset + i * _wordDim;
    for (int idk = 0; idk < _layer1OutDim; idk++) {
      temp = 0.0;
      for (int j = 0; j < _wordDim; j++) {
        temp += _layer1W[tmpI + j] * _wordEmb[tmpJ + j];
      }
      mid_layer1out[idk] += temp;
      tmpI += _lay1InputDim;
    }

  }

  offset = _wordDim * _wordcontext;
  for (int i = 0; i < _atomcontext; i++) {
    int curFeatId = aneural_features[i] * aneural_features.size() + i;
    tmpJ = aneural_features[i] * _atomDim;

    tmpI = offset + i * _atomDim;
    for (int idk = 0; idk < _layer1OutDim; idk++) {
      temp = 0.0;
      for (int j = 0; j < _atomDim; j++) {
        temp += _layer1W[tmpI + j] * _atomEmb[tmpJ + j];
      }
      mid_layer1out[idk] += temp;
      tmpI += _lay1InputDim;
    }
  }

  layer1out.resize(_layer1OutDim, 1);
  for (int idx = 0; idx < _layer1OutDim; idx++) {
    mid_layer1out[idx] = mid_layer1out[idx] + _layer1b(idx, 0);
    layer1out(idx, 0) = mid_layer1out[idx] * mid_layer1out[idx] * mid_layer1out[idx];
  }

  _layer2.ComputeForwardScore(layer1out, layer2out);

  // get delta for each output

  // Feed forward to softmax layer (no activation yet)

  int optLabel = -1;
  const vector<int>& labels = example.m_labels;
  for (int i = 0; i < _actionSize; ++i) {
    if (labels[i] >= 0) {
      if (optLabel < 0 || layer2out(i, 0) > layer2out(optLabel, 0))
        optLabel = i;
    }
  }

  NRVec<double> scores(_actionSize);
  double sum1 = 0.0;
  double sum2 = 0.0;
  double maxScore = layer2out(optLabel, 0);
  for (int i = 0; i < _actionSize; ++i) {
    scores[i] = -1e10;
    if (labels[i] >= 0) {
      scores[i] = exp(layer2out(i, 0) - maxScore);
      if (labels[i] == 1)
        sum1 += scores[i];
      sum2 += scores[i];
    }
  }

  return log(sum2) - log(sum1);
}
void GenerationDictionary::Load()
{
  FactorCollection &factorCollection = FactorCollection::Instance();

  const size_t numFeatureValuesInConfig = this->GetNumScoreComponents();


  // data from file
  InputFileStream inFile(m_filePath);
  UTIL_THROW_IF2(!inFile.good(), "Couldn't read " << m_filePath);

  string line;
  size_t lineNum = 0;
  while(getline(inFile, line)) {
    ++lineNum;
    vector<string> token = Tokenize( line );

    // add each line in generation file into class
    Word *inputWord = new Word();  // deleted in destructor
    Word outputWord;

    // create word with certain factors filled out

    // inputs
    vector<string> factorString = Tokenize( token[0], "|" );
    for (size_t i = 0 ; i < GetInput().size() ; i++) {
      FactorType factorType = GetInput()[i];
      const Factor *factor = factorCollection.AddFactor( Output, factorType, factorString[i]);
      inputWord->SetFactor(factorType, factor);
    }

    factorString = Tokenize( token[1], "|" );
    for (size_t i = 0 ; i < GetOutput().size() ; i++) {
      FactorType factorType = GetOutput()[i];

      const Factor *factor = factorCollection.AddFactor( Output, factorType, factorString[i]);
      outputWord.SetFactor(factorType, factor);
    }

    size_t numFeaturesInFile = token.size() - 2;
    if (numFeaturesInFile < numFeatureValuesInConfig) {
      stringstream strme;
      strme << m_filePath << ":" << lineNum << ": expected " << numFeatureValuesInConfig
            << " feature values, but found " << numFeaturesInFile << std::endl;
      throw strme.str();
    }
    std::vector<float> scores(numFeatureValuesInConfig, 0.0f);
    for (size_t i = 0; i < numFeatureValuesInConfig; i++)
      scores[i] = FloorScore(TransformScore(Scan<float>(token[2+i])));

    Collection::iterator iterWord = m_collection.find(inputWord);
    if (iterWord == m_collection.end()) {
      m_collection[inputWord][outputWord].Assign(this, scores);
    } else {
      // source word already in there. delete input word to avoid mem leak
      (iterWord->second)[outputWord].Assign(this, scores);
      delete inputWord;
    }
  }

  inFile.Close();
}
double NNClassifier::process(const vector<Example>& examples, int iter) {
  _eval.reset();

  _curWordPreComputed.clear();
  _curAtomPreComputed.clear();

  static hash_set<int>::iterator it;
  static int count, tmpI, tmpJ, tmpK;
  static double temp;
  int example_num = examples.size();
  for (count = 0; count < example_num; count++) {
    const Feature& feature = examples[count].m_feature;
    const vector<int>& wneural_features = feature.wneural_features;
    const vector<int>& aneural_features = feature.aneural_features;

    for (int idk = 0; idk < wneural_features.size(); idk++) {
      int curFeatId = wneural_features[idk] * wneural_features.size() + idk;
      if (_wordPreComputed.find(curFeatId) != _wordPreComputed.end()) {
        _curWordPreComputed.insert(curFeatId);
      }
    }

    for (int idk = 0; idk < aneural_features.size(); idk++) {
      int curFeatId = aneural_features[idk] * aneural_features.size() + idk;
      if (_atomPreComputed.find(curFeatId) != _atomPreComputed.end()) {
        _curAtomPreComputed.insert(curFeatId);
      }
    }
  }

  _curWordPreComputedId.clear();
  _curAtomPreComputedId.clear();
  Free(&_wordPreComputedForward);
  Free(&_atomPreComputedForward);
  Free(&_wordPreComputedBackward);
  Free(&_atomPreComputedBackward);
  //initial
  _curWordPreComputedNum = _curWordPreComputed.size();
  _wordPreComputedForward = (double *) calloc(_layer1OutDim * _curWordPreComputedNum, sizeof(double));
  count = 0;
  for (it = _curWordPreComputed.begin(); it != _curWordPreComputed.end(); ++it) {
    _curWordPreComputedId[*it] = count;
    int offset = (*it) % _wordcontext;
    int wordId = (*it) / _wordcontext;
    tmpJ = wordId * _wordDim;
    tmpI = offset * _wordDim;
    tmpK = count;
    for (int idk = 0; idk < _layer1OutDim; idk++) {
      temp = 0.0;
      for (int idy = 0; idy < _wordDim; idy++) {
        temp += _layer1W[tmpI + idy] * _wordEmb[tmpJ + idy];
      }
      _wordPreComputedForward[tmpK] = temp;
      tmpI += _lay1InputDim;
      tmpK += _curWordPreComputedNum;
    }
    count++;
  }

  _curAtomPreComputedNum = _curAtomPreComputed.size();
  _atomPreComputedForward = (double *) calloc(_layer1OutDim * _curAtomPreComputedNum, sizeof(double));
  count = 0;
  for (it = _curAtomPreComputed.begin(); it != _curAtomPreComputed.end(); ++it) {
    _curAtomPreComputedId[*it] = count;
    int offset = (*it) % _atomcontext;
    int atomId = (*it) / _atomcontext;
    tmpJ = atomId * _atomDim;
    tmpI = _wordDim * _wordcontext + offset * _atomDim;
    tmpK = count;
    for (int idk = 0; idk < _layer1OutDim; idk++) {
      temp = 0.0;
      for (int idy = 0; idy < _atomDim; idy++) {
        temp += _layer1W[tmpI + idy] * _atomEmb[tmpJ + idy];
      }
      _atomPreComputedForward[tmpK] = temp;
      tmpI += _lay1InputDim;
      tmpK += _curAtomPreComputedNum;
    }
    count++;
  }

  _wordPreComputedBackward = (double *) calloc(_layer1OutDim * _curWordPreComputedNum, sizeof(double));
  _atomPreComputedBackward = (double *) calloc(_layer1OutDim * _curAtomPreComputedNum, sizeof(double));

  double cost = 0.0;
  for (count = 0; count < example_num; count++) {
    const Example& example = examples[count];



    NRVec<double> mid_layer1out(_layer1OutDim), mid_layer1outLoss(_layer1OutDim);
    mat layer1out, layer1outLoss;
    mat layer2out, layer2outLoss;

    int offset;
    //forward propagation
    const Feature& feature = example.m_feature;
    const vector<int>& wneural_features = feature.wneural_features;
    const vector<int>& aneural_features = feature.aneural_features;

    assert(wneural_features.size() == _wordcontext && aneural_features.size() == _atomcontext);

    srand(iter*example_num + count);
    NRVec<bool> indexes_layer1(_layer1OutDim);
    for (int i = 0; i < _layer1OutDim; ++i)
    {
      if(1.0*rand()/RAND_MAX >= _dropOut)
      {
        indexes_layer1[i] = true;
      }
      else
      {
        indexes_layer1[i] = false;
      }
    }

    offset = 0;

    mid_layer1out = 0.0;
    for (int i = 0; i < _wordcontext; i++) {
      int curFeatId = wneural_features[i] * wneural_features.size() + i;
      tmpJ = wneural_features[i] * _wordDim;
      if (_curWordPreComputed.find(curFeatId) == _curWordPreComputed.end())
      {
        tmpI = offset + i * _wordDim;
        for (int idk = 0; idk < _layer1OutDim; idk++) {
          if(indexes_layer1[idk])
          {
            temp = 0.0;
            for (int j = 0; j < _wordDim; j++) {
              temp += _layer1W[tmpI + j] * _wordEmb[tmpJ + j];
            }
            mid_layer1out[idk] += temp;
          }
          tmpI += _lay1InputDim;
        }
      }
      else {
        tmpI = _curWordPreComputedId[curFeatId];
        for (int idk = 0; idk < _layer1OutDim; idk++) {
          if(indexes_layer1[idk])
          {
            mid_layer1out[idk] += _wordPreComputedForward[tmpI];
          }
          tmpI += _curWordPreComputedNum;
        }
      }
    }

    offset = _wordDim * _wordcontext;
    for (int i = 0; i < _atomcontext; i++) {
      int curFeatId = aneural_features[i] * aneural_features.size() + i;
      tmpJ = aneural_features[i] * _atomDim;
      if (_curAtomPreComputed.find(curFeatId) == _curAtomPreComputed.end())
      {
        tmpI = offset + i * _atomDim;
        for (int idk = 0; idk < _layer1OutDim; idk++) {
          if(indexes_layer1[idk])
          {
            temp = 0.0;
            for (int j = 0; j < _atomDim; j++) {
              temp += _layer1W[tmpI + j] * _atomEmb[tmpJ + j];
            }
            mid_layer1out[idk] += temp;
          }
          tmpI += _lay1InputDim;
        }
      }
      else {
        tmpI = _curAtomPreComputedId[curFeatId];
        for (int idk = 0; idk < _layer1OutDim; idk++) {
          if(indexes_layer1[idk])
          {
            mid_layer1out[idk] += _atomPreComputedForward[tmpI];
          }
          tmpI += _curAtomPreComputedNum;
        }
      }
    }

    layer1out.zeros(_layer1OutDim, 1);
    for (int idx = 0; idx < _layer1OutDim; idx++) {
      if(indexes_layer1[idx])
      {
        mid_layer1out[idx] = mid_layer1out[idx] + _layer1b(idx, 0);
        layer1out(idx, 0) = mid_layer1out[idx] * mid_layer1out[idx] * mid_layer1out[idx];
      }
    }

    _layer2.ComputeForwardScore(layer1out, layer2out);

    // get delta for each output

    // Feed forward to softmax layer (no activation yet)

    const vector<int>& labels = example.m_labels;

    if (_lossFunc == 1) {
      NRVec<double> scores(_actionSize);
      double sum1 = -1e10, sum2 = -1e10;
      int optLabel1 = -1, optLabel2 = -1;
      for (int i = 0; i < _actionSize; ++i) {
        scores[i] = -1e10;
        if (labels[i] >= 0) {
          scores[i] = layer2out(i, 0);
          if (labels[i] == 1) {
            if (optLabel1 == -1 || sum1 < scores[i]) {
              sum1 = scores[i];
              optLabel1 = i;
            }
          }
          if (optLabel2 == -1 || sum2 < scores[i]) {
            sum2 = scores[i];
            optLabel2 = i;
          }
        }
      }

      double loss = sum2 - sum1 + 1;

      cost += (sum2 - sum1) / example_num;

      _eval.overall_label_count++;
      if (optLabel1 == optLabel2) {
        _eval.correct_label_count++;
        continue; // need no update
      }

      layer2outLoss.zeros(_actionSize, 1);

      if (optLabel1 != optLabel2) {
        layer2outLoss(optLabel1, 0) = -loss / example_num;
        layer2outLoss(optLabel2, 0) = loss / example_num;
      }
    } else {
      int optLabel = -1;
      for (int i = 0; i < _actionSize; ++i) {
        //std::cout << layer2out(i, 0) << " ";
        if (labels[i] >= 0) {
          if (optLabel < 0 || layer2out(i, 0) > layer2out(optLabel, 0))
            optLabel = i;
        }
      }

      NRVec<double> scores(_actionSize);
      double sum1 = 0.0;
      double sum2 = 0.0;
      double maxScore = layer2out(optLabel, 0);
      for (int i = 0; i < _actionSize; ++i) {
        scores[i] = -1e10;
        if (labels[i] >= 0) {
          scores[i] = exp(layer2out(i, 0) - maxScore);
          if (labels[i] == 1)
            sum1 += scores[i];
          sum2 += scores[i];
        }
      }

      cost += (log(sum2) - log(sum1)) / example_num;
      if (labels[optLabel] == 1)
        _eval.correct_label_count++;
      _eval.overall_label_count++;

      layer2outLoss.resize(_actionSize, 1);
      for (int i = 0; i < _actionSize; ++i) {
        layer2outLoss(i, 0) = 0.0;
        if (labels[i] >= 0) {
          layer2outLoss(i, 0) = (scores[i] / sum2 - labels[i]) / example_num;
        }
      }
    }

    // loss backward propagation

    _layer2.ComputeBackwardLoss(layer1out, layer2out, layer2outLoss, layer1outLoss);

    mid_layer1outLoss = 0.0;
    for (int idx = 0; idx < _layer1OutDim; idx++) {
      if(indexes_layer1[idx])
      {
        mid_layer1outLoss[idx] = 3 * layer1outLoss(idx, 0) * mid_layer1out[idx] * mid_layer1out[idx];
        _gradlayer1b(idx, 0) = _gradlayer1b(idx, 0) + mid_layer1outLoss[idx];
      }
    }

    offset = 0;
    for (int i = 0; i < _wordcontext; i++) {
      int curFeatId = wneural_features[i] * wneural_features.size() + i;
      tmpJ = wneural_features[i] * _wordDim;
      if (_curWordPreComputed.find(curFeatId) == _curWordPreComputed.end())
      {
        tmpI = offset + i * _wordDim;
        for (int idk = 0; idk < _layer1OutDim; idk++) {
          if(indexes_layer1[idk])
          {
            temp = mid_layer1outLoss[idk];
            for (int j = 0; j < _wordDim; j++) {
              _gradlayer1W[tmpI + j] += temp * _wordEmb[tmpJ + j];
              if (_b_wordEmb_finetune)
                _grad_wordEmb[tmpJ + j] += temp * _layer1W[tmpI + j];
            }
          }
          tmpI += _lay1InputDim;
        }
      }
      else {
        tmpI = _curWordPreComputedId[curFeatId];
        for (int idk = 0; idk < _layer1OutDim; idk++) {
          if(indexes_layer1[idk])
          {
            _wordPreComputedBackward[tmpI] += mid_layer1outLoss[idk];
          }
          tmpI += _curWordPreComputedNum;
        }
      }
    }

    offset += _wordDim * _wordcontext;
    for (int i = 0; i < _atomcontext; i++) {
      int curFeatId = aneural_features[i] * aneural_features.size() + i;
      tmpJ = aneural_features[i] * _atomDim;
      if (_curAtomPreComputed.find(curFeatId) == _curAtomPreComputed.end())
      {
        tmpI = offset + i * _atomDim;
        for (int idk = 0; idk < _layer1OutDim; idk++) {
          if(indexes_layer1[idk])
          {
            temp = mid_layer1outLoss[idk];
            for (int j = 0; j < _atomDim; j++) {
              _gradlayer1W[tmpI + j] += temp * _atomEmb[tmpJ + j];
              _grad_atomEmb[tmpJ + j] += temp * _layer1W[tmpI + j];
            }
          }
          tmpI += _lay1InputDim;
        }
      }
      else {
        tmpI = _curAtomPreComputedId[curFeatId];
        for (int idk = 0; idk < _layer1OutDim; idk++) {
          if(indexes_layer1[idk])
          {
            _atomPreComputedBackward[tmpI] += mid_layer1outLoss[idk];
          }
          tmpI += _curAtomPreComputedNum;
        }
      }
    }

  }

  //backward feed back
  for (it = _curWordPreComputed.begin(); it != _curWordPreComputed.end(); ++it) {
    count = _curWordPreComputedId[*it];
    int offset = (*it) % _wordcontext;
    int wordId = (*it) / _wordcontext;
    tmpI = offset * _wordDim;
    tmpJ = wordId * _wordDim;
    tmpK = count;
    for (int idk = 0; idk < _layer1OutDim; idk++) {
      temp = _wordPreComputedBackward[tmpK];
      for (int idy = 0; idy < _wordDim; idy++) {
        _gradlayer1W[tmpI + idy] += temp * _wordEmb[tmpJ + idy];
        if (_b_wordEmb_finetune)
          _grad_wordEmb[tmpJ + idy] += temp * _layer1W[tmpI + idy];
      }
      tmpI += _lay1InputDim;
      tmpK += _curWordPreComputedNum;
    }
  }

  for (it = _curAtomPreComputed.begin(); it != _curAtomPreComputed.end(); ++it) {
    count = _curAtomPreComputedId[*it];
    int offset = (*it) % _atomcontext;
    int atomId = (*it) / _atomcontext;
    tmpI = _wordDim * _wordcontext + offset * _atomDim;
    tmpJ = atomId * _atomDim;
    tmpK = count;
    for (int idk = 0; idk < _layer1OutDim; idk++) {
      temp = _atomPreComputedBackward[tmpK];
      for (int idy = 0; idy < _atomDim; idy++) {
        _gradlayer1W[tmpI + idy] += temp * _atomEmb[tmpJ + idy];
        _grad_atomEmb[tmpJ + idy] += temp * _layer1W[tmpI + idy];
      }
      tmpI += _lay1InputDim;
      tmpK += _curAtomPreComputedNum;
    }
  }

  Free(&_wordPreComputedBackward);
  Free(&_atomPreComputedBackward);
  return cost;
}
  /**
    * \param first Start of the range in which to search.
    * \param last One element past the last element in the range in which to search.
    * \param query The element to compare to.
    * \return The iterator to the best element in the range (best is defined as the one which would compare favorably to all
    *         the elements in the range with respect to the distance metric).
    */
  typename TIterator::value_type operator()(const TIterator first, const TIterator last, typename TIterator::value_type query)
  {
    if(this->DebugImages)
    {
      if(this->ImageToWrite == nullptr)
      {
        throw std::runtime_error("LinearSearchBestLidarTextureGradient cannot WriteTopPatches without having an ImageToWrite!");
      }
//      PatchHelpers::WriteTopPatches(this->ImageToWrite, this->PropertyMap, first, last,
//                                    "BestPatches", this->Iteration);

      unsigned int gridWidth = 10;
      unsigned int gridHeight = 10;
      PatchHelpers::WriteTopPatchesGrid(this->ImageToWrite, this->PropertyMap, first, last,
                                        "BestPatches", this->Iteration, gridWidth, gridHeight);
    }

    unsigned int numberOfBins = 30;

    // If the input element range is empty, there is nothing to do.
    if(first == last)
    {
      std::cerr << "LinearSearchBestHistogram: Nothing to do..." << std::endl;
      return *last;
    }

    // Get the region to process
    itk::ImageRegion<2> queryRegion = get(this->PropertyMap, query).GetRegion();

    typedef itk::NthElementImageAdaptor<TImage, float> ImageChannelAdaptorType;
    typename ImageChannelAdaptorType::Pointer imageChannelAdaptor = ImageChannelAdaptorType::New();
    imageChannelAdaptor->SetImage(this->Image);

    // Compute the gradient of each channel
    for(unsigned int channel = 0; channel < 3; ++channel) // 3 is the number of HSV channels
    {
      imageChannelAdaptor->SelectNthElement(channel);

      if(channel == 0) // H channel
      {
        Helpers::HSV_H_Difference hsvHDifference;
        Derivatives::MaskedGradientInRegion(imageChannelAdaptor.GetPointer(), this->MaskImage,
                                            queryRegion, this->HSVChannelGradients[channel].GetPointer(), hsvHDifference);
      }
      else
      {
        Derivatives::MaskedGradientInRegion(imageChannelAdaptor.GetPointer(), this->MaskImage,
                                            queryRegion, this->HSVChannelGradients[channel].GetPointer());
      }

      if(this->DebugImages && this->DebugLevel > 1)
      {
        // Gradient of patch
        std::stringstream ssGradientFile;
        ssGradientFile << "QueryHSVGradient_" << Helpers::ZeroPad(this->Iteration, 3) << "_" << channel << ".mha";
        ITKHelpers::WriteRegionAsImage(this->HSVChannelGradients[channel].GetPointer(), queryRegion, ssGradientFile.str());

        // Full gradient image
//        std::stringstream ss;
//        ss << "HSV_Gradient_" << Helpers::ZeroPad(this->Iteration, 3) << "_" << channel << ".mha";
//        ITKHelpers::WriteImage(this->HSVChannelGradients[channel].GetPointer(), ss.str());
      }
    }

    HistogramType targetHSVHistogram;

    // Store, for each channel (the elements of the vector), the min/max value of the valid region of the target patch
    std::vector<GradientImageType::PixelType::RealValueType> minHSVChannelGradientMagnitudes(this->Image->GetNumberOfComponentsPerPixel());
    std::vector<GradientImageType::PixelType::RealValueType> maxHSVChannelGradientMagnitudes(this->Image->GetNumberOfComponentsPerPixel());

    std::vector<itk::Index<2> > validPixels = ITKHelpers::GetPixelsWithValueInRegion(this->MaskImage, queryRegion, this->MaskImage->GetValidValue());

    typedef itk::NormImageAdaptor<GradientImageType, GradientImageType::PixelType::RealValueType> NormImageAdaptorType;
    typename NormImageAdaptorType::Pointer normImageAdaptor = NormImageAdaptorType::New();

    // Compute the gradient magnitude images for each HSV channel's gradient, and compute the histograms for the target/query region
    for(unsigned int channel = 0; channel < 3; ++channel) // 3 is the number of HSV channels
    {
      imageChannelAdaptor->SelectNthElement(channel);

      normImageAdaptor->SetImage(this->HSVChannelGradients[channel].GetPointer());

      std::vector<GradientImageType::PixelType::RealValueType> gradientMagnitudes =
          ITKHelpers::GetPixelValues(normImageAdaptor.GetPointer(), validPixels);

      minHSVChannelGradientMagnitudes[channel] = Helpers::Min(gradientMagnitudes);
      maxHSVChannelGradientMagnitudes[channel] = Helpers::Max(gradientMagnitudes);

      // Compute histograms of the gradient magnitudes (to measure texture)
      bool allowOutside = false;
      HistogramType targetHSVChannelHistogram =
        MaskedHistogramGeneratorType::ComputeMaskedScalarImageHistogram(
            normImageAdaptor.GetPointer(), queryRegion, this->MaskImage, queryRegion, numberOfBins,
            minHSVChannelGradientMagnitudes[channel], maxHSVChannelGradientMagnitudes[channel],
            allowOutside, this->MaskImage->GetValidValue());

      targetHSVChannelHistogram.Normalize();

      targetHSVHistogram.Append(targetHSVChannelHistogram);
    }

    // Compute the gradient magnitude from the depth derivatives, and compute the histogram for the target/query region
    // Extract the depth derivative channels
    std::vector<unsigned int> depthDerivativeChannels = {3,4};
    typedef itk::Image<itk::CovariantVector<float, 2> > DepthDerivativesImageType;
    DepthDerivativesImageType::Pointer depthDerivatives = DepthDerivativesImageType::New();
    ITKHelpers::ExtractChannels(this->Image, depthDerivativeChannels, depthDerivatives.GetPointer());

    // Compute the depth gradient magnitude image
    typedef itk::Image<float, 2> DepthGradientMagnitudeImageType;
    DepthGradientMagnitudeImageType::Pointer depthGradientMagnitude = DepthGradientMagnitudeImageType::New();
    ITKHelpers::MagnitudeImage(depthDerivatives.GetPointer(), depthGradientMagnitude.GetPointer());

    // Store the min/max values (histogram range)
    std::vector<DepthGradientMagnitudeImageType::PixelType> depthGradientMagnitudes =
        ITKHelpers::GetPixelValues(depthGradientMagnitude.GetPointer(), validPixels);

    float minDepthChannelGradientMagnitude = Helpers::Min(depthGradientMagnitudes);
    float maxDepthChannelGradientMagnitude = Helpers::Max(depthGradientMagnitudes);

    // Compute histogram
    bool allowOutsideForDepthHistogramCreation = false;
    HistogramType targetDepthHistogram =
      MaskedHistogramGeneratorType::ComputeMaskedScalarImageHistogram(
          depthGradientMagnitude.GetPointer(), queryRegion, this->MaskImage, queryRegion, numberOfBins,
          minDepthChannelGradientMagnitude, maxDepthChannelGradientMagnitude,
          allowOutsideForDepthHistogramCreation, this->MaskImage->GetValidValue());

    targetDepthHistogram.Normalize();

    if(this->DebugOutputFiles)
    {
      targetHSVHistogram.Write(Helpers::GetSequentialFileName("TargetHSVHistogram", this->Iteration, "txt", 3));
      targetDepthHistogram.Write(Helpers::GetSequentialFileName("TargetDepthHistogram", this->Iteration, "txt", 3));
    }

    // Initialize
    float bestDistance = std::numeric_limits<float>::max();
    TIterator bestPatch = last;

    unsigned int bestId = 0; // Keep track of which of the top SSD patches is the best by histogram score (just for information sake)
    HistogramType bestHSVHistogram;
    HistogramType bestDepthHistogram;

    // Iterate through all of the supplied source patches
    std::vector<float> scores(last - first);

    for(TIterator currentPatch = first; currentPatch != last; ++currentPatch)
    {
      itk::ImageRegion<2> currentRegion = get(this->PropertyMap, *currentPatch).GetRegion();

      // Determine if the gradient and histogram have already been computed
      typename HistogramMapType::iterator histogramMapIterator;
      histogramMapIterator = this->PreviouslyComputedHSVHistograms.find(currentRegion);

      bool alreadyComputed;

      if(histogramMapIterator == this->PreviouslyComputedHSVHistograms.end())
      {
        alreadyComputed = false;
      }
      else
      {
        alreadyComputed = true;
      }

      HistogramType testHSVHistogram;

      bool allowOutside = true;
      // Compute the HSV histograms of the source region using the queryRegion mask
      for(unsigned int channel = 0; channel < 3; ++channel) // 3 is the number of HSV channels
      {
        if(this->DebugImages && this->DebugLevel > 1)
        {
          std::stringstream ssSourceGradientFile;
          ssSourceGradientFile << "SourceGradient_" << Helpers::ZeroPad(this->Iteration, 3) << "_" << channel << "_" << Helpers::ZeroPad(currentPatch - first, 3) <<  ".mha";
          ITKHelpers::WriteRegionAsImage(this->HSVChannelGradients[channel].GetPointer(), currentRegion, ssSourceGradientFile.str());
        }

        normImageAdaptor->SetImage(this->HSVChannelGradients[channel].GetPointer());

        HistogramType testHSVChannelHistogram;

        if(!alreadyComputed)
        {
          // We don't need a masked histogram since we are using the full source patch
          testHSVChannelHistogram = HistogramGeneratorType::ComputeScalarImageHistogram(
                          normImageAdaptor.GetPointer(), currentRegion,
                          numberOfBins,
                          minHSVChannelGradientMagnitudes[channel],
                          maxHSVChannelGradientMagnitudes[channel], allowOutside);

          testHSVChannelHistogram.Normalize();

          this->PreviouslyComputedHSVHistograms[currentRegion] = testHSVChannelHistogram;
        }
        else // already computed
        {
          testHSVChannelHistogram = this->PreviouslyComputedHSVHistograms[currentRegion];
        }

        testHSVHistogram.Append(testHSVChannelHistogram);
      }

      HistogramType testDepthHistogram;

      if(!alreadyComputed)
      {
        // Compute the depth histogram of the source region using the queryRegion mask
        testDepthHistogram = HistogramGeneratorType::ComputeScalarImageHistogram(
                        depthGradientMagnitude.GetPointer(), currentRegion,
                        numberOfBins,
                        minDepthChannelGradientMagnitude,
                        maxDepthChannelGradientMagnitude, allowOutside);

        testDepthHistogram.Normalize();

        this->PreviouslyComputedDepthHistograms[currentRegion] = testDepthHistogram;
      }
      else
      {
        testDepthHistogram = this->PreviouslyComputedDepthHistograms[currentRegion];
      }

      if(this->DebugOutputFiles)
      {
        std::stringstream ssEnding;
        ssEnding << "_" << Helpers::ZeroPad(this->Iteration, 3) << "_" << Helpers::ZeroPad(currentPatch - first, 3) << ".txt";
        testHSVHistogram.Write("TestHSVHistogram" + ssEnding.str());
        testDepthHistogram.Write("TestDepthHistogram" + ssEnding.str());
      }

      // Compute the differences in the histograms
      float hsvHistogramDifference = HistogramDifferences::HistogramDifference(targetHSVHistogram, testHSVHistogram);
      float depthHistogramDifference = HistogramDifferences::HistogramDifference(targetDepthHistogram, testDepthHistogram);

      // Weight the depth histogram 3x so that it is a 1:1 weighting of HSV and depth difference
      float histogramDifference = hsvHistogramDifference + 3.0f * depthHistogramDifference;

      scores[currentPatch - first] = histogramDifference;

      if(this->DebugScreenOutputs)
      {
        std::cout << "histogramDifference " << currentPatch - first << " : " << histogramDifference << std::endl;
      }

      if(histogramDifference < bestDistance)
      {
        bestDistance = histogramDifference;
        bestPatch = currentPatch;

        // These are not needed - just for debugging
        bestId = currentPatch - first;
        bestHSVHistogram = testHSVHistogram;
        bestDepthHistogram = testDepthHistogram;
      }
    }

    std::cout << "BestId: " << bestId << std::endl;
    std::cout << "Best distance: " << bestDistance << std::endl;

    this->Iteration++;

    if(this->DebugOutputFiles)
    {
      Helpers::WriteVectorToFileLines(scores, Helpers::GetSequentialFileName("Scores", this->Iteration, "txt", 3));
    }

    return *bestPatch;
  }
bool place::reshowPlacement(const std::string &scanName,
                            const std::string &zerosFile,
                            const std::string &doorName,
                            const place::DoorDetector &d,
                            const std::string &preDone) {
  const std::string buildName = scanName.substr(scanName.find("_") - 3, 3);
  const std::string scanNumber = scanName.substr(scanName.find(".") - 3, 3);
  const std::string placementName =
      buildName + "_placement_" + scanNumber + ".dat";

  std::ifstream in(preDone + placementName, std::ios::in | std::ios::binary);
  if (!in.is_open())
    return false;
  if (!FLAGS_reshow)
    return true;

  if (!FLAGS_quietMode)
    std::cout << placementName << std::endl;

  std::vector<cv::Mat> rotatedScans, toTrim;
  std::vector<Eigen::Vector2i> zeroZero;
  place::loadInScans(scanName, zerosFile, toTrim, zeroZero);
  place::trimScans(toTrim, rotatedScans, zeroZero);

  std::vector<std::vector<place::Door>> doors = loadInDoors(doorName, zeroZero);

  int num;
  in.read(reinterpret_cast<char *>(&num), sizeof(num));
  std::vector<place::posInfo> scores(num);
  for (auto &s : scores)
    in.read(reinterpret_cast<char *>(&s), sizeof(place::posInfo));

  int cutOffNum = place::getCutoffIndex(
      placementName, scores, [](const place::posInfo &s) { return s.score; });
  cutOffNum = FLAGS_top > 0 ? FLAGS_top : cutOffNum;

  num = std::min(num, cutOffNum);

  cvNamedWindow("Preview", CV_WINDOW_NORMAL);

  if (!FLAGS_quietMode)
    std::cout << "Showing minima: " << num << std::endl;

  for (int k = 0; k < std::min(num, (int)scores.size());) {
    auto &currentScore = scores[k];

    const cv::Mat &bestScan = rotatedScans[currentScore.rotation];

    const int xOffset = currentScore.x - zeroZero[currentScore.rotation][0];
    const int yOffset = currentScore.y - zeroZero[currentScore.rotation][1];

    cv::Mat_<cv::Vec3b> output = fpColor.clone();

    auto &res = d.getResponse(0);
    for (int i = 0; i < res.outerSize(); ++i)
      for (Eigen::SparseMatrix<char>::InnerIterator it(res, i); it; ++it)
        if (it.value() > 1)
          output(it.row(), it.col()) = cv::Vec3b(0, 255, 0);

    for (int j = 0; j < bestScan.rows; ++j) {
      if (j + yOffset < 0 || j + yOffset >= fpColor.rows)
        continue;
      const uchar *src = bestScan.ptr<uchar>(j);
      for (int i = 0; i < bestScan.cols; ++i) {
        if (i + xOffset < 0 || i + xOffset >= fpColor.cols)
          continue;

        if (src[i] != 255) {
          output(j + yOffset, i + xOffset) = cv::Vec3b(0, 0, 255 - src[i]);
        }
      }
    }

    for (int j = -10; j < 10; ++j)
      for (int i = -10; i < 10; ++i)
        output(j + currentScore.y, i + currentScore.x) = cv::Vec3b(255, 0, 0);

    for (auto &d : doors[currentScore.rotation]) {
      auto color = randomColor();
      for (double x = 0; x < d.w; ++x) {
        Eigen::Vector3i index =
            (d.corner + x * d.xAxis + Eigen::Vector3d(xOffset, yOffset, 0))
                .unaryExpr([](auto v) { return std::round(v); })
                .cast<int>();

        for (int k = -2; k <= 2; ++k) {
          for (int l = -2; l <= 2; ++l) {
            output(index[1] + k, index[0] + l) = color;
          }
        }
      }
    }

    if (!FLAGS_quietMode) {
      std::cout << &currentScore << std::endl;
      std::cout << "% of scan unexplained: "
                << currentScore.scanFP / currentScore.scanPixels
                << "   Index: " << k << std::endl
                << std::endl;
    }

    const int keyCode = cv::rectshow(output);

    if (keyCode == 27) {
      cv::imwrite(preDone + buildName + "_ss_" + scanNumber + ".png", output);
      break;
    } else if (keyCode == 8)
      k = k > 0 ? k - 1 : k;
    else
      ++k;
  }
  return true;
}
Example #6
0
int main(int argc, char** argv) {

	// handle parameters
	po::variables_map cfg;
	if (!init_params(argc,argv, &cfg)) exit(1); // something is wrong

	// setup DfTable
	DfTable dft (cfg["dftable"].as<string>());
	cerr << "DF table loaded (" << dft.size() << " entries).\n";

	// setup scorer
	BM25* scorer = new BM25();
	scorer->setAvgDocLength(cfg["avg_len"].as<double>());
	if (cfg.count("N"))
		scorer->setDocCount(cfg["N"].as<double>());
	else
		scorer->setDocCount(dft.mMaxDf);

	// load queries
	vector<Query> queries;
	cerr << "reporter:status:loading queries...\n";
	CLIR::loadQueries(cfg["queries"].as<string>(), queries);
	size_t queries_size = queries.size();

	string run_id = cfg["run-id"].as<string>();

	// initialize vector of Scores objects for each query
	vector<Scores<string> > scores (queries_size, Scores<string>(cfg["K"].as<int>()));

	// for each doc
	string docid;
	int len;
	string raw;
	TermVector doc;
	int c = 0;

	cerr << "reporter:status:scanned=0\n";

	while (cin >> docid) {
		cin.ignore(1,'\t');
		cin >> len;
		cin.ignore(1,'\t');
		getline(cin, raw);

		if (docid.size() == 0 || len <= 0 || raw.size() == 0)
			continue;

		Document doc(vutils::read_vector(raw), docid, len);

		// for each query compute score between current document and query
		for ( size_t i = 0 ; i < queries_size ; ++i ) {

			Score<string> score (doc.docid_, prob_t(0));
			int overlap = 0;
			score.mS = crosslingual_bm25(queries.at(i), doc.wvec_, doc.len_, scorer, dft, overlap);
			scores[i].update(score);

		}

		c++;
		c%10==0 ? cerr << "reporter:status:scanned="<< c << "\n" : cerr ;

	}

	cerr << "\nreporter:status:outputting kbest lists..." << endl;
	for ( size_t i = 0; i < scores.size(); ++i ) {
		vector<Score<string> > topk  = scores[i].k_largest();
		reverse(topk.begin(), topk.end());
		CLIR::writeResult(std::cout, queries.at(i), topk, run_id, cfg.count("show-empty-results"));
	}

	cerr << "reporter:status:done.\n";

}
        geometry_msgs::Twist findClosestAcceptableVelocity(const geometry_msgs::Twist & desired) {
            geometry_msgs::Twist res = desired;
            // Now build Vs inter Vd
            double min_v = std::max(-max_linear_velocity_, current_velocity_.linear.x - max_linear_accel_*time_horizon_);
            double max_v = std::min(max_linear_velocity_, current_velocity_.linear.x + max_linear_accel_*time_horizon_);
            double min_w = std::max(-max_angular_velocity_, current_velocity_.angular.z - max_angular_accel_*time_horizon_);
            double max_w = std::min(max_angular_velocity_, current_velocity_.angular.z + max_angular_accel_*time_horizon_);
            unsigned int n_v = ceil((max_v-min_v)/linear_velocity_resolution_+1); 
            unsigned int n_w = ceil((max_w-min_w)/angular_velocity_resolution_+1); 
            // printf("Vds: v %d [%.2f %.2f] w %d [%.2f %.2f]\n",n_v, min_v, max_v, n_w, min_w, max_w);
            cv::Mat_<uint8_t> Vds(n_v,n_w,FREE); // Vs inter Vd
            cv::Mat_<uint8_t> scores(n_v,n_w,(uint8_t)OCCUPIED); // Vs inter Vd

            // Now build Va (inside Vs inter Vd) by iterating over the local
            // map, and find the most appropriate speed
            // FILE * fp = fopen("/tmp/V.txt","w");
            double best_score = 0;
            double best_v = 0, best_w = 0;
            for (unsigned int j=0;j<n_v;j++) {
                double v = min_v + j*linear_velocity_resolution_;
                // double th = fabs(v/max_linear_accel_);
                double d = v * time_horizon_ ;
                int i_d = round((d + max_range_) / map_resolution_);
                if (i_d < 0) i_d =0;
                if (i_d >= (signed)n_d_) i_d = n_d_-1;
                for (unsigned int i=0;i<n_w;i++) {
                    double w = min_w + i*angular_velocity_resolution_;
                    double alpha = atan2(v,w);
                    if (alpha < 0) alpha += 2*M_PI;
                    if (alpha >= M_PI) alpha -= M_PI;
                    int i_alpha = (int)(round(alpha / alpha_resolution_)) % n_alpha_;
                    uint8_t value;
                    if (fabs(v)<linear_velocity_resolution_) { 
                        // Force rotation on the spot to be always possible
                        // They should be, but alpha is not super well defined
                        // for v = 0
                        value = FREE;
                    } else {
                        value = d_alpha_(i_d, i_alpha);
                    }
                    Vds(j,i) = value;
                    double score = 0;
                    if (value == FREE) {
                        score = exp(-(k_v_ * SQR(v - desired.linear.x)
                                    + k_w_ * SQR(w - desired.angular.z)));
                        scores(j,i) = 0x80 + score*0x7F;
                        if (score > best_score) {
                            best_v = v;
                            best_w = w;
                            best_score = score;
                        }
                    }
                    // fprintf(fp,"Control %.2f %.2f, d %.2f alpha %.2f (r %.2f) value %d score %.2f\n",
                    //         v,w,d,alpha,tan(alpha), value, score);
                }
            }
            // fclose(fp);
            cv::resize(scores,scores,cv::Size(200,200));
            cv::imshow("Vr",scores);

            res.linear.x = best_v;
            res.angular.z = best_w;

            return res;
        }
Example #8
0
	void ApplyNonMaximumSuppresion(std::vector< LkTracker* >& in_out_source, float in_nms_threshold)
	{
		if (in_out_source.empty())
			return;

		unsigned int size = in_out_source.size();

		std::vector<float> area(size);
		std::vector<float> scores(size);
		std::vector<int> x1(size);
		std::vector<int> y1(size);
		std::vector<int> x2(size);
		std::vector<int> y2(size);
		std::vector<unsigned int> indices(size);
		std::vector<bool> is_suppresed(size);

		for(unsigned int i = 0; i< in_out_source.size(); i++)
		{
			ObjectDetection tmp = in_out_source[i]->GetTrackedObject();
			area[i] = tmp.rect.width * tmp.rect.height;
			if (area[i]>0)
				is_suppresed[i] = false;
			else
			{
				is_suppresed[i] = true;
				in_out_source[i]->NullifyLifespan();
			}
			indices[i] = i;
			scores[i] = tmp.score;
			x1[i] = tmp.rect.x;
			y1[i] = tmp.rect.y;
			x2[i] = tmp.rect.width + tmp.rect.x;
			y2[i] = tmp.rect.height + tmp.rect.y;
		}

		Sort(area, indices);//returns indices ordered based on scores

		for(unsigned int i=0; i< size; i++)
		{

			for(unsigned int j= i+1; j< size; j++)
			{
				if(is_suppresed[indices[i]] || is_suppresed[indices[j]])
					continue;
				int x1_max = std::max(x1[indices[i]], x1[indices[j]]);
				int x2_min = std::min(x2[indices[i]], x2[indices[j]]);
				int y1_max = std::max(y1[indices[i]], y1[indices[j]]);
				int y2_min = std::min(y2[indices[i]], y2[indices[j]]);
				int overlap_width = x2_min - x1_max + 1;
				int overlap_height = y2_min - y1_max + 1;
				if(overlap_width > 0 && overlap_height>0)
				{
					float overlap_part = (overlap_width*overlap_height)/area[indices[j]];
					if(overlap_part > in_nms_threshold)
					{
						is_suppresed[indices[j]] = true;
						in_out_source[indices[j]]->NullifyLifespan();
						if (in_out_source[indices[j]]->GetFrameCount() > in_out_source[indices[i]]->GetFrameCount())
						{
							in_out_source[indices[i]]->object_id = in_out_source[indices[j]]->object_id;
						}

					}
				}
			}
		}
		return ;
	}
/**
 * returns the accuracy
 */
float LacSupervised::predict(Trainning &trainning, char *ftest, AssociationRule*  associationRule){
	ui ntransactions = 0;
	ui nhits = 0;
	ui totalRules = 0;

	FILE* file=fopen(ftest,"r");
	if(file==NULL) {
		fprintf(stderr,"Test set %s not found.\n\n", ftest);
		exit(-1);
	}

	SymbolTable* featureTable = SymbolTable::FeaturesTable();
	SymbolTable* classesTable = SymbolTable::ClassesTable();

	vector<pair<string, ui> > classes;
	classesTable->getTableName(classes);

	unordered_set<ui> featureIds;
	const ui bufferSize = 200*1024;
	char line[bufferSize];

	ui tid, classId;

	ui nclasses = trainning.getNumberOfClasses();
	vector<float> scores(nclasses);
	ui defaultClass = trainning.getMostFrequentClass();

	while(fgets(line, bufferSize, file)){
		++ntransactions;
		classId = 10000000;
		processLineInput(line, featureTable, classesTable, tid, classId, featureIds);
		if(classId == 10000000){
			throw  string("Class Id not found: ") + line ;
		}

		Projection* projection = trainning.geProjection(featureIds);
		fill_n(scores.begin(), scores.size(), 0.0f);
		float totalScore = 0.0f;
		ui nrules = 0;
		ui prediction = defaultClass;
		AssociationRule::RulesResult result =  associationRule->induceRules(projection, minSupport, iminSupport, nclasses);
		for(ui classId = 0; classId < nclasses; ++classId){
			nrules += result.classesNRules[classId];
			float score = result.score(classId);
			totalScore += score;
			scores[classId] = score;
			if(cmp(scores[prediction], score) < 0){
				prediction = classId;
			}
		}
		totalRules += nrules;
	    nhits += prediction == classId;;
		printStatistics(trainning, tid, classesTable, classes, classId, prediction, scores, totalScore, nrules);

		delete projection;
	}

	fclose(file);

	//printf("%u\n", totalRules);

	return ntransactions == 0 ? 1.0 : static_cast<float>(nhits)/static_cast<float>(ntransactions);
}
Example #10
0
void EnsembleGenerator::output(Ensemble& ensemble,
       const Vector<Vector<saxs::WeightedFitParameters> >& fps) const {

  if(ensemble.size() == 0) return;

  // calculate z-score
  Vector<double> scores(ensemble.size());
  for(unsigned int i=0; i<ensemble.size(); i++) scores[i] = ensemble[i].get_score();
  std::pair<double, double> average_and_std = get_average_and_stdev(scores);
  for(unsigned int i=0; i<ensemble.size(); i++) {
    double zscore = (ensemble[i].get_score()-average_and_std.first) /
      average_and_std.second;
    ensemble[i].set_zscore(zscore);
  }

  // calculate frequency of each state
  Vector<double> state_prob;
  get_state_probabilities(ensemble, state_prob);

  // calculate weights average and variance
  Vector<Vector<double> > weights_average(scorers_.size()),
    weights_variance(scorers_.size());
  for(unsigned int i=0; i<scorers_.size(); i++) {
    get_weights_average_and_std(ensemble, fps[i], weights_average[i],
                                weights_variance[i]);
  }

  // output file
  unsigned int number_of_states = ensemble[0].size();
  std::string out_file_name = "ensembles_size_" +
    std::string(boost::lexical_cast<std::string>(number_of_states)) + ".txt";
  std::ofstream s(out_file_name.c_str());
  std::cout << "multi_state_model_size " << ensemble.size ()
            << " number_of_states " << number_of_states << std::endl;

  for(unsigned int i=0; i<ensemble.size(); i++) {
    // output ensemble scores
    s.setf(std::ios::fixed, std::ios::floatfield);
    s << i+1 << " | " << std::setw(5) << std::setprecision(2)
      << ensemble[i].get_score(); // << " | " << ensemble[i].get_zscore();

    // output scores for each scorer
    for(unsigned int j=0; j<scorers_.size(); j++) {
      const saxs::WeightedFitParameters& p = fps[j][i];
      s << " | x" << std::string(boost::lexical_cast<std::string>(j+1))
        //scorers_[j]->get_dataset_name() << ": "
        << " " << std::setprecision(2) << p.get_chi()
        << " (" << p.get_c1() << ", " << p.get_c2() << ")";
    }
    s << std::endl;

    // output states and their probabilities
    const Vector<unsigned int>& states = ensemble[i].get_states();
    for(unsigned int k=0; k<states.size(); k++) {
      s << std::setw(5) << states[k];

      // output weights
      for(unsigned int j=0; j<scorers_.size(); j++) {
        const saxs::WeightedFitParameters& p = fps[j][i];
        if(p.get_weights().size() > k) {
          s << std::setw(5) << std::setprecision(3) << " | "
            << p.get_weights()[k] << " ("
            << weights_average[j][states[k]] << ", "
            << weights_variance[j][states[k]] << ")";
        }
      }
      s << " | "  << scorers_[0]->get_state_name(states[k])
        << " (" << state_prob[states[k]] << ")" << std::endl;
    }

    // output fit file
    if(i<10) { // TODO: add parameter
      for(unsigned int j=0; j<scorers_.size(); j++) {
        std::string fit_file_name = "multi_state_model_" +
          std::string(boost::lexical_cast<std::string>(number_of_states)) + "_" +
          std::string(boost::lexical_cast<std::string>(i+1));
        if(scorers_.size() > 0) {
          fit_file_name +=  "_" + std::string(boost::lexical_cast<std::string>(j+1));
        }
        fit_file_name += ".dat";
        scorers_[j]->write_fit_file(ensemble[i], fps[j][i], fit_file_name);
      }
    }
  }
  s.close();
}
Example #11
0
bool isosplit1d(int N,int *labels,double &pp,double *X) {
	QTime timer; timer.start();
	int minsize=4; //hard coded to be consistent with calibration

    //qDebug() << "ELAPSED" << __FILE__ << __LINE__ << timer.elapsed(); timer.start();
	QVector<double> avg(N),stdev(N),scores(N);
	if (!read_isosplit_calibration(N,avg,stdev,scores)) {
        printf ("ERROR: problem in read_isosplit_calibration.\n");
		return false;
	}
	int curve_len=avg.count();
	if (curve_len==0) {
        printf ("ERROR: calibration not performed for N=%d\n",N);
		return false;
	}
	int num_trials=scores.count();
    //qDebug() << "ELAPSED" << __FILE__ << __LINE__ << timer.elapsed(); timer.start();

	//compute the curve and cutpoint
	Mda curve_mda; curve_mda.allocate(1,curve_len); double *curve=curve_mda.dataPtr();
	double cutpoint;
    //qDebug() << "ELAPSED" << __FILE__ << __LINE__ << timer.elapsed(); timer.start();
	if (!get_isosplit_curve(curve_len,N,curve,cutpoint,X,minsize)) {
        printf ("ERROR in get_isosplit_curve\n");
		return false;
	}
    //qDebug() << "ELAPSED" << __FILE__ << __LINE__ << timer.elapsed(); timer.start();

	//compute the score by comparing to calibration avg and stdev and then compute the pp
	double score0=0;
	for (int ii=0; ii<curve_len; ii++) {
		if (stdev[ii]) {
			double diff0=avg[ii]-curve[ii];
			//we need a change of at least 0.1 to count it
			//this is important in the case where stdev is extremely close to 0
			if (qAbs(diff0)<0.1) diff0=0;
			double tmp_score=diff0/stdev[ii];
			if (tmp_score>score0) score0=tmp_score;
		}
	}
	if (score0!=0) {
		double tmp_ct=0;
		for (int jj=0; jj<scores.count(); jj++) {
			if (scores[jj]<score0) tmp_ct++;
		}
		pp=tmp_ct/num_trials;
	}
	else {
		pp=0;
	}
    //qDebug() << "ELAPSED" << __FILE__ << __LINE__ << timer.elapsed(); timer.start();

	//set the labels
	for (int kk=0; kk<N; kk++) {
		if (X[kk]<cutpoint) labels[kk]=1;
		else labels[kk]=2;
	}
    //qDebug() << "ELAPSED" << __FILE__ << __LINE__ << timer.elapsed(); timer.start();

	return true;
}
Example #12
0
bool options_test() {
    const int alphabet_size = 6;
    const int T = 5;
    const int minibatch = 2;

    std::vector<float> activations =
            {0.633766, 0.221185, 0.0917319, 0.0129757, 0.0142857, 0.0260553,
             0.30176, 0.28562, 0.0831517, 0.0862751, 0.0816851, 0.161508,

             0.111121, 0.588392, 0.278779, 0.0055756, 0.00569609, 0.010436,
             0.24082, 0.397533, 0.0557226, 0.0546814, 0.0557528, 0.19549,

             0.0357786, 0.633813, 0.321418, 0.00249248, 0.00272882, 0.0037688,
             0.230246, 0.450868, 0.0389607, 0.038309, 0.0391602, 0.202456,

             0.0663296, 0.643849, 0.280111, 0.00283995, 0.0035545, 0.00331533,
             0.280884, 0.429522, 0.0326593, 0.0339046, 0.0326856, 0.190345,

             0.458235, 0.396634, 0.123377, 0.00648837, 0.00903441, 0.00623107,
             0.423286, 0.315517, 0.0338439, 0.0393744, 0.0339315, 0.154046};

    std::vector<float> expected_grads = // from tensorflow
            {-0.366234, 0.221185, 0.0917319, 0.0129757, 0.0142857, 0.0260553,
             -0.69824, 0.28562, 0.0831517, 0.0862751, 0.0816851, 0.161508,

             0.111121, -0.411608, 0.278779, 0.0055756, 0.00569609, 0.010436,
             0.24082, -0.602467, 0.0557226, 0.0546814, 0.0557528, 0.19549,

             0.0357786, 0.633813, -0.678582, 0.00249248, 0.00272882, 0.0037688,
             0.230246, 0.450868, 0.0389607, 0.038309, 0.0391602, -0.797544,

             0.0663296, -0.356151, 0.280111, 0.00283995, 0.0035545, 0.00331533,
             0.280884, -0.570478, 0.0326593, 0.0339046, 0.0326856, 0.190345,

             -0.541765, 0.396634, 0.123377, 0.00648837, 0.00903441, 0.00623107,
             -0.576714, 0.315517, 0.0338439, 0.0393744, 0.0339315, 0.154046};


    // Calculate the expected scores analytically
    std::vector<double> expected_scores(2);
    auto& a = activations;
    expected_scores[0] =
            -std::log(a[offset(0, 0, 0)] * a[offset(1, 0, 1)] * a[offset(2, 0, 2)]
                      * a[offset(3, 0, 1)] * a[offset(4, 0, 0)]);
    expected_scores[1] = 5.42262; // from tensorflow

    // now take the log to account for the softmax
    for (auto& a : activations) {
        a = std::log(a);
    }

    std::vector<int> labels = {0, 1, 2, 1, 0,
                               0, 1, 1, 0};

    std::vector<int> label_lengths = {5, 4};

    std::vector<int> lengths = {5, 5};

    std::vector<float> grads(alphabet_size * T * minibatch);

    std::vector<float> scores(2);

    ctcOptions options{};
    options.loc = CTC_CPU;
    options.num_threads = 1;
    options.blank_label = 5;

    size_t cpu_alloc_bytes;
    throw_on_error(get_workspace_size(label_lengths.data(), lengths.data(),
                                      alphabet_size, lengths.size(), options,
                                      &cpu_alloc_bytes),
                   "Error: get_workspace_size in options_test");

    void* ctc_cpu_workspace = malloc(cpu_alloc_bytes);

    throw_on_error(compute_ctc_loss(activations.data(), grads.data(),
                                    labels.data(), label_lengths.data(),
                                    lengths.data(),
                                    alphabet_size,
                                    lengths.size(),
                                    scores.data(),
                                    ctc_cpu_workspace,
                                    options),
                   "Error: compute_ctc_loss in options_test");

    free(ctc_cpu_workspace);

    const double eps = 1e-4;

    bool result = true;
    for (int i = 0; i < grads.size(); i++) {
        const double lb = expected_grads[i] - eps;
        const double ub = expected_grads[i] + eps;
        if (!(grads[i] > lb && grads[i] < ub)) {
            std::cerr << "grad mismatch in options_test"
                      << " expected grad: " << expected_grads[i]
                      << " calculated score: " << grads[i]
                      << " !(" << lb << " < " << grads[i]
                      << " < " << ub << ")" << std::endl;
            result = false;
        }
    }

    for (int i = 0; i < 2; i++) {
        const double lb = expected_scores[i] - eps;
        const double ub = expected_scores[i] + eps;
        if (!(scores[i] > lb && scores[i] < ub)) {
            std::cerr << "score mismatch in options_test"
                      << " expected score: " << expected_scores[i]
                      << " calculated score: " << scores[i]
                      << " !(" << lb << " < " << scores[i]
                      << " < " << ub << ")" << std::endl;
            result = false;
        }
    }
    return result;
}
Example #13
0
vector<float> hasher::get_cur_scores(){
    const rmf& hvals = rand_scores;
    vector<float> scores(tor_hashes.size()-2, 0);
    for(size_t g = 2; g < tor_hashes.size(); g++){
        vector<vector<size_t> >& hvec = tor_hashes[g];
        //preceding has table gives probability that distance is more than
        //current measure
        vector<vector<size_t> >& hvecm2 = tor_hashes[g-2];
        //gval hvec = dval[i]*2
        //gval hvecm1 = dval[i]
        //gval hvecm2 = dval[i]/2
        //if(!in hvecm1), pr bad is for 2*gval = dval*2
        //if(!in hvecm2), pr bad is for 2*gval = dval
        //if(in hvec) pr good is for gval/2 = dval
        //therefore need to be minus two hash funcs
        for(size_t i = 0; i < (size_t)hvals.rows(); i++){
            float pr_far;
            size_t num_near = 0;
            size_t num_far=0;
            pr_far = 0;
            size_t num_ave = 0;
            for(size_t j = 0; j < (size_t)hvals.cols(); j++){
                //    increment corresponding hash table bin
                //    gamma/2, 2*gamma, 1/2, 1/3 sensitive
                //    for now we treat not being near as zero probability of begin near
                //    for now, hold two counts: number of vectors for which
                //    the hash function claims the rvec is near
                //    and number of times the rvec claims it is not near a vector
                //
                //    if (n_near*((1/2)/(1/3) < n_far)) then
                //    say it is near a vector. Else, it is not
                size_t num_near_tmp = hvec[j][get_bin(hvals(i, j), hvec[j].size())];
                long num_far_tmp = (long)hvecm2[j][get_bin(hvals(i, j), hvec[j].size())];
                num_near += num_near_tmp;
                //if not zero, make zero. otherwise make 1
                num_far_tmp = (num_far_tmp == 0);
                num_far += num_far_tmp;
                pr_far += (pow(0.5, num_near_tmp) + num_far_tmp);
                num_ave += (1+num_far_tmp);
            }
            //cout << num_near*1.0/hvals.cols() << endl;
            pr_far/= num_ave;
        //    if(pr_far < 0.2){
       if(num_far < 1){ 
                scores[g-2]++;
            }
        }
        scores[g-2]/=rand_scores.rows();
    }
    size_t num_bel_min = 0;

    list<rmf>::iterator beg;
    for(size_t r = 0; r < (size_t)rand_vecs.rows(); r++){
        float mindis=mnorm;
        list<rmf>::iterator lcur;
        for(lcur=in_vecs.begin(); lcur != in_vecs.end(); lcur++){
            for(size_t i = 0; i < (size_t)lcur->rows(); i++){
                float cdist = (rand_vecs.row(r) - lcur->row(i)).norm();
                if(cdist < mindis){
                    mindis = cdist;
                }
            }
        }
        if(mindis < 0.25){
            num_bel_min++;
        }
    }
   // float act_bel_min = (1.0*num_bel_min)/(1.0*rand_vecs.rows());
    cout << scores[0]*rand_vecs.rows() << ", " << num_bel_min << endl;
    return scores;
}
Example #14
0
// parameters setup
void RealignImp::set_scores (int mat, int mis, int gip, int gep)
{
    std::vector<int> scores (4);
    scores [0] = mat, scores [1] = mis, scores [2] = gip, scores [3] = gep;
    SetScores (scores);
}
Example #15
0
/*Read log file, which summarize bleu scores by using different initial 
    phrase tables(with different max length and cutoff)*/
void Readlog(JKArgs& args){
    if(!args.count("i"))usage();
    ifstream is(args["i"]);
    string message=args["m"];

    vector<vector<double>> scores(8,vector<double>(6,0));
    for(string line; getline(is,line);){
        vector<string> words;
        split_regex(words,line,regex(" |-pt\\.|\\.lex|/"));
        int maxlen=0;
        int cutoff=0;
        for(size_t i=0;i<words.size();i++){
            if(words[i]=="evaluation"){
                maxlen=words[i+1][1]-'0';
                cutoff=words[i+1][3]-'0';
                //cerr<<maxlen<<","<<cutoff<<endl;
            }
            else if(words[i]=="BLEUr4n4[%]")
                scores[maxlen][cutoff]=stod(words[i+1]);

        }
    }
    cout<<R"(
    \begin{table}[H]
    \centering
    \begin{tabular}{ c c | c | c | c | c|c }
    & \multicolumn{5}{c}{\bf{ count cutoff}}  \\
    &  & 1 &    2 &	3 &	4 &	5\\
    \hline
    \multirow{7}{*}{\bf{max length}}
    )";
    for(int i=1;i<8;i++){
        cout<<"& "<<i<<"";
        for(int j=1;j<6;j++){
            cout<<" &\t";
            if(scores[i][j]==0)
                cout<<"\\bf{N.A.}";
            else cout<<scores[i][j];
        }
        cout<<"\\\\"<<endl;
        if(i<7)cout<<R"(\cline{2-7})"<<endl;
    }
    if(message!="")message=", "+message;
    string tail="\\hline\n\\end{tabular}\n\\caption\
    {BLEU of different maxlen and cutoff "+message+"}\n\\end{table}\n";
    cout<<tail<<endl;
}

void filter(JKArgs& args){
    if(!args.count("i"))usage();
    ifstream is(args["i"]);
    for(string line;getline(is,line);){
        vector<string> words;
        split(words,line,is_any_of(" \t"));
        if(args["block"]=="s2t"){
            words[words.size()-3]="1";
            words[words.size()-5]="1";
            words[words.size()-4]="1";
        }
        else{
            words[words.size()-3]="1";
            words[words.size()-5]="1";
            words[words.size()-2]="1";
        }
        line=join(words," ");
        cout<<line<<endl;
    }

}
// ----------------------------------------------------------------------------
// CSipAcceptContactStrategy::ApplyL
// ----------------------------------------------------------------------------
//
CSIPResponse* CSipAcceptContactStrategy::ApplyL(
    CSIPRequest& aRequest, 
    RArray<TUid>& aUids,
    TBool& aContinueSearch,
    CSIPClientResolver2& aClientResolver2 )
	{
	SIP_CR_LOG("CSipAcceptContactStrategy::ApplyL")
	CSIPResponse* response = NULL;
	// The strategy is applied only for requests that contain Accept-Contact
	if (aRequest.HasHeader(iAcceptContactHeaderName))
        {
        RArray<TSIPClientScore> scores(1);
        CleanupClosePushL(scores);
        RPointerArray<CSIPFeatureSet> requestFeatureSets = 
            CreateFeatureSetsL(aRequest);
        TCleanupItem cleanupItem(DestroyFeatureSets,&requestFeatureSets);
	    CleanupStack::PushL(cleanupItem);
	    for (TInt i=0; i < aUids.Count(); i++)
		    {
		    TUid uid(aUids[i]);
		    MSipClient* client = iSipClients.GetByUID(uid);
		    if (client)
			    {
			    TInt score = CalculateScore(*client,requestFeatureSets);
			    if (score > 0)
			        {
			        TSIPClientScore clientScore(score,uid);
			        // The score is used as a key
			        scores.InsertInSignedKeyOrderAllowRepeatsL(clientScore);
			        }
			    }	
		    }
    	CleanupStack::PopAndDestroy(1); // cleanupItem
    	TInt clientCount = scores.Count();
    	aUids.Reset(); // empties the array
	    if (clientCount > 0)
	        {
	        // The scores are in increasing order. 
	        // The last is the best match. Reverse the order.
	        for (TInt i=scores.Count()-1; i>=0; i--)
	            {
	            // In the resulting array the first is the best match
	            aUids.AppendL(scores[i].iUid);
	            } 
	        if (iNextStrategy && clientCount > 1)
	            {
	            // Apply the next strategy only if 
	            // there are still more than one matching clients.
	            response = iNextStrategy->ApplyL(aRequest,aUids,
	                                             aContinueSearch,
	                                             aClientResolver2);
	            }
	        }
	    else
		    {
		    if ( iNextStrategy2 )
		    	{
		    	response = iNextStrategy2->ApplyL(aRequest,aUids,
		    									  aContinueSearch,
		    									  aClientResolver2);
		    	}
		    else
		    	{
		    	response = CreateResponseL();
		    	}
		    
		    }
		CleanupStack::PopAndDestroy(1); // scores
        }
    else
        {
        if (iNextStrategy)
            {
	        response = iNextStrategy->ApplyL(aRequest,aUids,aContinueSearch,
	        												aClientResolver2);
	        }
        }

    return response;
	}
Example #17
0
//the massive main function
int main(int argc, char* args[]) // Gets command line input
{
	bool quit = false, playing = true, falling = true, held = false, paused = false, cheating = false; // All our miscellaneous bools to keep track of game states.
	
	int level = 0, lines = 0, score = 0, choice = 0, cheatseq = 0;
	
	Scores scores("gfx"); // Opens the uber camoflaged gfx file for getting highscores. Tricksy, eh?
	
	Uint32 start = 0; // A REALLY big int for keeping track of time
	
	srand(SDL_GetTicks()); // seeding the random... seed
	
	if( init() == false ) // Initialize SDL
	{
		cout << "Init fail" << endl;
		return 1; //ERR !!!!
	}
	
	block = load_image( "blocks.png" ); // load blocks
	
	if(block == NULL) 
	{
		cout << "Error loading blocks.png" << endl;
		return 1; //ERR!
	}
	
	back = load_image( "back.png" ); // load background
	
	if(back == NULL)
	{
		cout << "Error loading back.png" << endl;
		return 1; //ERR!
	}
	
	smallblock = load_image( "smallblocks.png" ); // small blocks for next and hold
	
	if(smallblock == NULL)
	{
		cout << "Error loading smallblocks.png" << endl;
		return 1; //ERR!
	}
	
	title = load_image( "title.png" ); // title
	
	if(title == NULL)
	{
		cout << "Error loading title.png" << endl;
		return 1; //ERR!
	}
	
	cursor = load_image( "cursor.png" ); // cursor in menu
	
	if(cursor == NULL)
	{
		cout << "Error loading cursor.png" << endl;
		return 1; //ERR!
	}
	
	font = TTF_OpenFont("ProggyClean.ttf", FONTSIZE); // our font
	
	if(font == NULL)
	{
		cout << "Error loading ProggyClean.ttf" << endl;
		return 1; //Yup. Didn't load.
	}
	
	effect = Mix_LoadWAV( "pause.wav" ); // dee doo sound
	
	if(effect == NULL)
	{
		cout << "Mix_LoadWAV: " << Mix_GetError() << endl;
	}
	
	while(playing) // while the user hasn't quit
	{
		score = 0;
		quit = false;
		Mix_FadeOutMusic(100); // fades out the music (if playing) for menu.
		
		Mix_FreeMusic(music); // gets rid of any music we might have loaded
		
		if(XM == true) // load title music
		{
			music = Mix_LoadMUS("title.xm");
		}
		else
		{
			music = Mix_LoadMUS("title.mp3");
		}
		
		if(!music)
		{
	   		cout << "Mix_LoadMUS(\"title.mp3\"): %s\n" << Mix_GetError() << endl;
	   		// music didn't load...
		}
		
		Mix_PlayMusic(music, -1); // play it til the user can't stand it no mo'.

		for(int i = 600; i>=100; i--) // slowly bring up the title
		{
			while( SDL_PollEvent( &event ) ) // gets any recent events (mouse, keyboard, whatev)
			{
				if( event.type == SDL_QUIT ) // X button
				{
					i = 100;
					playing = false;
					quit = true;
				}
				else if( event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) //escape
				{
					i = 100; // brings the title screen instantly
				}
			}
			SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) ); // fill screen black
			apply_surface( 200, i, title, screen ); // apply title
			if( SDL_Flip( screen ) == -1 ) // hand *screen to video card
			{
				return 1; //ERRRRRRR !!
			}
			SDL_Delay(2); // slows it down a wee bit.
		}
		
		apply_surface( 325, 402 + choice * 50, cursor, screen ); // display cursor
		
		numbers = TTF_RenderText_Blended(font, "START", fontColor); // start
		apply_surface( 350, 400, numbers, screen );
		
		numbers = TTF_RenderText_Blended(font, "HIGH SCORES", fontColor); // high scores
		apply_surface( 350, 450, numbers, screen );
		
		numbers = TTF_RenderText_Blended(font, "EXIT", fontColor); // exit
		apply_surface( 350, 500, numbers, screen );
		
		if( SDL_Flip( screen ) == -1 )
		{
			return 1; //ERRRRRRR !!
		}
		
		paused = true; //pause for menu
		while(paused && !quit)
		{
			while( SDL_PollEvent( &event ) ) // wait for events
			{
				if( event.type == SDL_QUIT )
				{
					paused = false;
					playing = false;
					quit = true;
				}
				else if( event.type == SDL_KEYDOWN )
				{
					switch(event.key.keysym.sym)
					{
						case SDLK_ESCAPE:
							paused = false;
							quit = true;
							playing = false;
							break;
						case SDLK_RETURN:
							switch(choice)
							{
								case 0:
									paused = false;
									break;
								case 1:
									scores.display();
									break;
								case 2:
									paused = false;
									quit = true;
									playing = false;
									break;
							}
							break;
						case SDLK_DOWN: // down, move cursor down
							Mix_PlayChannel(-1, effect, 0); // dee doo sound
							if(choice < 2)
							{
								choice++;
							}
							else
							{
								choice = 0;
							}
							break;
						case SDLK_UP: // up, move cursor up
							Mix_PlayChannel(-1, effect, 0); // see above
							if(choice > 0)
							{
								choice--;
							}
							else
							{
								choice = 2;
							}
							break;
					}
					SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) );
					
					apply_surface( 200, 100, title, screen );
					apply_surface( 325, 402 + choice * 50, cursor, screen );
							
					numbers = TTF_RenderText_Blended(font, "START", fontColor);
					apply_surface( 350, 400, numbers, screen );
					
					numbers = TTF_RenderText_Blended(font, "HIGH SCORES", fontColor);
					apply_surface( 350, 450, numbers, screen );
		
					numbers = TTF_RenderText_Blended(font, "EXIT", fontColor);
					apply_surface( 350, 500, numbers, screen );
		
					if( SDL_Flip( screen ) == -1 )
					{
						return 1; //ERRRRRRR !!
					}
				}
			}
		}
		
		if(!quit) // if the user didn't quit
		{		
			Mix_FadeOutMusic(100); //fade out title music
			
			Mix_FreeMusic(music); //free it
			
			if(XM == true) // change to game music
			{
				music = Mix_LoadMUS("music.xm");
			}
			else
			{
				music = Mix_LoadMUS("music.mp3");
			}
		
			if(!music)
			{
	   			cout << "Mix_LoadMUS(\"music.mp3\"): %s\n" << Mix_GetError() << endl;
	   			// music didn't load...
			}
			
			Mix_PlayMusic(music, -1);
		
			SDL_Delay(1500); // wait for ba-ding! to finish
		}
		
		Tile *tile = NULL, *next = NULL, *next2 = NULL, *next3 = NULL, *hold = NULL, *blank = NULL; //current tile, next tiles 1 2 3, hold, and the tile used for switching hold
		
		setclips(); // set the clip boxes for tiles
		
		clearfield(); // initialize the field

		next = new Tile(); // inits new tiles
		next2 = new Tile();
		next3 = new Tile();
		
		while(!quit)
		{
			SDL_Delay(50); // wait a bit for the next block to drop
			tile = next; // next to current
			next = next2; // move nexts
			next2 = next3;
			next3 = new Tile(); // new next 3
			if(!tile->check(0, 0)) // if the tile is colliding from the get go
			{
				tile->show();
				if( SDL_Flip( screen ) == -1 )
				{
					return 1; //ERRRRRRR !!
				}
				gameOver(); // THEN YOU LOSE! HA!
				scores.checkScore(score); // Did you get a high score?
				scores.save(); // save the high score file
				scores.display(); // display the current high scores
				quit = true;
			}
			falling = true;
			
			while(falling && !quit) // the there a lot of !quits from now on so it goes straight back to the menu
			{
				apply_surface( 0, 0, back, screen ); // background
				tile->show(); // show the current tile
				if(hold != NULL)
				{
					hold->disp(27,78); // display hold
				}
				next->disp(390, 78); // display nexts
				next2->disp(390, 158);
				next3->disp(390, 238);
				start = SDL_GetTicks(); // start the timer
				while( falling && SDL_GetTicks() - start < (1/((static_cast<double>(level)/25) + 1))*(SPEED - MINSPEED)+MINSPEED) // spiffy formula I took way to long to make for speeding up
				{
					while( SDL_PollEvent( &event ) ) // get events
					{
						if( event.type == SDL_QUIT ) // quit immediatly
						{
							clean();
							exit(0);
							playing = false;
						}
						else if( event.type == SDL_KEYDOWN ) // was a key pressed?
						{
							switch( event.key.keysym.sym ) // what key was pressed?
							{
								case SDLK_LEFT: // move left
									tile->check(-1, 0);
									break;
								case SDLK_RIGHT: // move right
									tile->check(1, 0);
									break;
								case SDLK_z: // rotate CCW
									tile->rotate(CC);
									break;
								case SDLK_x: // rotate CW
									tile->rotate(CW);
									break;
								case SDLK_DOWN: // soft drop
									if(!tile->check(0,1)) // if it hits something as a result of dropping once
									{
	                                    falling = false; // apply the tile
	                                }
	                                start = SDL_GetTicks(); // reset the timer
									break;
								case SDLK_UP: // hard drop
									while(tile->check(0,1)) // move it down til it cant any more
									{
										score += 2 * (level + 1); // you get points for being so daring!
									}
									start -= 1000; //Lock that sucka!
									break;
								case SDLK_n: // this is the cheat key. But you have to be cheating to use it ;D
									if(cheating)
									{
										delete tile; // basically just gives you new tiles
										tile = new Tile();
									}
									break;
								case SDLK_ESCAPE: // back to main menu
									quit = true;
									break;
								case SDLK_k: //SUPER SECRET KYLE-MODE!
									block = load_image( "kyles.png" );
									back = load_image( "kyleback.png" );
									smallblock = load_image("smallkyles.png");
									Mix_HaltMusic();
									music = Mix_LoadMUS("rawk.mp3");
									Mix_PlayMusic(music, -1);
									break;
								case SDLK_o: // back to original
									block = load_image( "blocks.png" );
									back = load_image( "back.png" );
									smallblock = load_image("smallblocks.png");
									Mix_HaltMusic();
									music = Mix_LoadMUS("music.mp3");
									Mix_PlayMusic(music, -1);
									break;
								case SDLK_c: // hold
									if(hold && !held) // if there is something held and you haven't held yet this turn
									{
										blank = hold; // switch current and held
										hold = tile;
										tile = blank;
									}
									else if(!hold) // if you've never held
									{
										hold = tile; // hold current and make more tiles
										tile = next;
										next = next2;
										next2 = next3;
										next3 = new Tile();
									}
									hold->reset(); // reset the x and y values of the tile
									held = true;
									break;
								case SDLK_p: // pause
									paused = true;
									Mix_PauseMusic(); // pause music
									Mix_PlayChannel(-1, effect, 0); // play the dee doo
									apply_surface( 0, 0, back, screen ); // cover the tiles
									numbers = TTF_RenderText_Solid(font, intToStr(score).c_str(), fontColor); // but keep the scores
									apply_surface( 550, 50, numbers, screen );
									numbers = TTF_RenderText_Solid(font, intToStr(level).c_str(), fontColor);
									apply_surface( 550, 120, numbers, screen );
									if( SDL_Flip( screen ) == -1 ) // display screen
									{
										return 1; //ERRRRRRR !!
									}
									while(paused)
									{
										while(SDL_PollEvent( &event )) // get events
										{
											if( event.type == SDL_QUIT ) // quit immediatly
											{
												quit = true;
												paused = false;
												playing = false;
											}
											else if( event.type == SDL_KEYDOWN ) // key down?
											{
												switch( event.key.keysym.sym ) // what key?
												{
													case SDLK_ESCAPE: // unpause
													case SDLK_p: // unpause
														paused = false;
														break;
													case SDLK_UP: // CHEATING!! WOO!!! UP UP DOWN DOWN LEFT RIGHT LEFT RIGHT A B. <3 CONTRA.
														if(cheatseq == 0 || cheatseq == 1)
														{
															cheatseq++;
														}
														else
														{
															cheatseq = 0; // if you screwed up
														}
														break;
													case SDLK_DOWN:
														if(cheatseq == 2 || cheatseq == 3)
														{
															cheatseq++;
														}
														else
														{
															cheatseq = 0;
														}
														break;
													case SDLK_LEFT:
														if(cheatseq == 4 || cheatseq == 6)
														{
															cheatseq++;
														}
														else
														{
															cheatseq = 0;
														}
														break;
													case SDLK_RIGHT:
														if(cheatseq == 5 || cheatseq == 7)
														{
															cheatseq++;
														}
														else
														{
															cheatseq = 0;
														}
														break;
													case SDLK_z:
														if(cheatseq == 8)
														{
															cheatseq++;
														}
														else
														{
															cheatseq = 0;
														}
														break;
													case SDLK_x:
														if(cheatseq == 9)
														{
															Mix_PlayChannel(-1, effect, 0);
															cheating = true;
														}
														else
														{
															cheatseq = 0;
														}
														break;
													default:
														cheatseq = 0;
														break;
												}
											}
										}
									}
									cheatseq = 0; // If you screwed up the cheat sequence
									Mix_ResumeMusic(); // play that funky music
									break;
							}
							apply_surface( 0, 0, back, screen ); // put the background up
							screen << *tile; //There. Overloading. Huzzah.
							if(hold != NULL) // display hold
							{
								hold->disp(27,78);
							}
							next->disp(390, 78); // display all the nexts
							next2->disp(390, 158);
							next3->disp(390, 238);
							numbers = TTF_RenderText_Solid(font, intToStr(score).c_str(), fontColor); //display scores
							apply_surface( 550, 50, numbers, screen );
							numbers = TTF_RenderText_Solid(font, intToStr(level).c_str(), fontColor); //Pretty sweet you can use all the sweet string functions with a function that returns a string =D
							apply_surface( 550, 120, numbers, screen );
							if( SDL_Flip( screen ) == -1 ) // show screen
							{
								return 1; //ERRRRRRR !!
							}
						}
					}
					numbers = TTF_RenderText_Solid(font, intToStr(score).c_str(), fontColor); // display scores
					apply_surface( 550, 50, numbers, screen );
					numbers = TTF_RenderText_Solid(font, intToStr(level).c_str(), fontColor);
					apply_surface( 550, 120, numbers, screen );
					if( SDL_Flip( screen ) == -1 )
					{
						return 1; //ERRRRRRR !!
					}
					SDL_Delay(1);
				}
				if(tile->check(0, 1) == 0) // if it can't fall any more
				{
					falling = false; // stop falling
				}
			}
			tile->apply(); // apply it to the pile
			held = false; // you can hold again!
			score += (level+1)*checklines(lines); // check the lines and score accordingly
			level = lines/10; // level up?
			delete tile; // delete the current tile so we don't get yelled at
		}
		delete next; // delete all the other tiles if you lost
		delete next2;
		delete next3;
		delete hold;
	}
	clean(); // uninitialize SDL and everything
	return 0; // RETURN THAT ZERO, BABY.
} // Fin
Example #18
0
int main(int argc, char* argv[]) {
   HumdrumFileSet infiles;

   majorKey = majorKeyBellman;
   minorKey = minorKeyBellman;

   // process the command-line options
   checkOptions(options, argc, argv);

   // figure out the number of input files to process
   int numinputs = options.getArgCount();

   Array<double> absbeat;
   Array<int>    pitch;
   Array<double> duration;
   Array<double> level;
   Array<double> distribution(12);
   Array<double> scores(24);
   string filename;

   Array<int> b40hist;

   int bestkey = 0;
   int i, j;

   if (numinputs < 1) {
      infiles.read(cin);
   } else {
      for (i=0; i<numinputs; i++) {
         infiles.readAppend(options.getArg(i+1));
      }
   }
  
   for (i=0; i<infiles.getCount(); i++) {
      filename = infiles[i].getFilename();

      if (continuousQ) {
         analyzeContinuously(infiles[i], windowsize, stepsize, majorKey, 
               minorKey);
         continue;
      }

      infiles[i].getNoteArray(absbeat, pitch, duration, level);
      for (j=0; j<pitch.getSize(); j++) {
         pitch[j] = Convert::base40ToMidiNoteNumber(pitch[j]);
      }

      if (rawQ) {
	 if (normalizeQ) { 
            normalizeData(majorKey, 12);
            normalizeData(minorKey, 12);
         }
         bestkey = analyzeKeyRawCorrelation(scores.getBase(), 
               distribution.getBase(), pitch.getBase(), duration.getBase(),
               pitch.getSize(), rhythmQ, majorKey, minorKey);
      } else if (euclideanQ) {
         equalizeData(majorKey, 12, 1.0);
         equalizeData(minorKey, 12, 1.0);
         bestkey = analyzeKeyEuclidean(scores.getBase(), 
               distribution.getBase(), pitch.getBase(), duration.getBase(),
               pitch.getSize(), rhythmQ, majorKey, minorKey);
      } else {
         bestkey = analyzeKeyKS2(scores.getBase(), distribution.getBase(),
               pitch.getBase(), duration.getBase(), pitch.getSize(), rhythmQ,
                     majorKey, minorKey);
      }
      getBase40Histogram(b40hist, infiles[i]);
      printAnalysis(bestkey, scores, distribution, filename, b40hist, 
            infiles[i]);
   }

   return 0;
}
Example #19
0
void ApplyNonMaximumSuppresion(std::vector< kstate >& in_source, float in_nms_threshold)
{
	std::vector< kstate > tmp_source = in_source;

	if (tmp_source.empty())
		return ;

	unsigned int size = in_source.size();

	std::vector<float> area(size);
	std::vector<float> scores(size);
	std::vector<int> x1(size);
	std::vector<int> y1(size);
	std::vector<int> x2(size);
	std::vector<int> y2(size);
	std::vector<unsigned int> indices(size);
	std::vector<bool> is_suppresed(size);

	for(unsigned int i = 0; i< in_source.size(); i++)
	{
		kstate tmp = in_source[i];
		area[i] = tmp.pos.width * tmp.pos.height;
		indices[i] = i;
		is_suppresed[i] = false;
		scores[i] = tmp.score;
		x1[i] = tmp.pos.x;
		y1[i] = tmp.pos.y;
		x2[i] = tmp.pos.width + tmp.pos.x;
		y2[i] = tmp.pos.height + tmp.pos.y;
	}

	Sort(scores, indices);//returns indices ordered based on scores

	for(unsigned int i=0; i< size; i++)
	{
		if(!is_suppresed[indices[i]])
		{
			for(unsigned int j= i+1; j< size; j++)
			{
				int x1_max = std::max(x1[indices[i]], x1[indices[j]]);
				int x2_min = std::min(x2[indices[i]], x2[indices[j]]);
				int y1_max = std::max(y1[indices[i]], y1[indices[j]]);
				int y2_min = std::min(y2[indices[i]], y2[indices[j]]);
				int overlap_width = x2_min - x1_max + 1;
				int overlap_height = y2_min - y1_max + 1;
				if(overlap_width > 0 && overlap_height>0)
				{
					float overlap_part = (overlap_width*overlap_height)/area[indices[j]];
					if(overlap_part > in_nms_threshold)
					{
						is_suppresed[indices[j]] = true;
					}
				}
			}
		}
	}

	unsigned int size_out = 0;
	for (unsigned int i = 0; i < size; i++)
	{
		if (!is_suppresed[i])
			size_out++;
	}

	std::vector< kstate > filtered_detections(size_out);

	unsigned int index = 0;
	for(unsigned int i = 0 ; i < size_out; i++)
	{
		if(!is_suppresed[indices[i]])
		{
			filtered_detections[index] = in_source[indices[i]];//x1[indices[i]];
			index++;
		}
	}
	in_source = filtered_detections;
}
Example #20
0
//player versus player battle loop
void Game::player_battle( SDL_Rect window_outline )
{
    //create score counts
    int player1_score = 0;
    int player2_score = 0;
    
    //load score textures
    std::vector<Texture> scores(4);
    std::stringstream score_count;
    //light pink color
    SDL_Color color = { 219, 112, 147 };
    for (int i = 0; i < scores.size(); ++i)
    {
        score_count.str("");
        score_count << i;
        scores[i].load_text(big_font, color, score_count.str().c_str());
    }
    
    //white color
    color = { 255, 255, 255 };
    //load player1 wins text texture
    Texture player1_wins;
    player1_wins.load_text(medium_font, color, "PLAYER 1 WINS!");
    
    //load player2 wins text texture
    Texture player2_wins;
    player2_wins.load_text(medium_font, color, "PLAYER 2 WINS!");
    
    //load try again text texture
    Texture try_again;
    try_again.load_text(medium_font, color, "TRY AGAIN?");
    
    //yes or no text texture
    Texture yes_or_no;
    yes_or_no.load_text(medium_font, color, "YES   NO");
    
    //create yes or no button outline
    SDL_Rect button_outline = { WINDOW_WIDTH/2 - yes_or_no.get_width()/2 - 14, (WINDOW_HEIGHT*7)/10 - yes_or_no.get_height()/2 - 10, yes_or_no.get_width()/2, yes_or_no.get_height() + 14 };
    //yes or no button select
    bool button_select = true;
    
    //create ball
    Ball* ball = new Ball;
    //create right paddle (player)
    PlayerPaddle* paddle1 = new PlayerPaddle;
    //create left paddle (computer)
    PlayerPaddle* paddle2 = new PlayerPaddle(PLAYER_BATTLE);
    
    //create a polymorphic vector of colliders for ball collisions
    std::vector<Collider*> paddles;
    paddles.push_back(paddle1);
    paddles.push_back(paddle2);
    
    //create player battle loop terminator
    bool terminated = false;
    //create object that reads in events
    SDL_Event event;
    
    //game loop
    while (terminated == false)
    {
        //event loop
        while ( SDL_PollEvent(&event) )
        {
            //if there's a quit event
            if ( event.type == SDL_QUIT )
            {
                terminated = true;
                level = END;
            }
            
            //check events for velocity change
            paddle1->handle(event);
            paddle2->handle(event);
            
            //if no one has won yet
            if ( player1_score != 3 && player2_score != 3)
                //check events for ball (to start movement if spawned)
                ball->handle(event);
            
            //if a player won
            if (player1_score == 3 || player2_score == 3)
            {
                //if there's a non-repeat keypress event
                if ( event.type == SDL_KEYDOWN && event.key.repeat == 0)
                {
                    //if left or right key was pressed
                    if ( event.key.keysym.sym == SDLK_LEFT || event.key.keysym.sym == SDLK_RIGHT)
                    {
                        //if button outline is over YES
                        if (button_select == true)
                            //move button outline right
                            button_outline.x += 150;
                        //if button outline is over NO
                        else if (button_select == false)
                            //move button outline left
                            button_outline.x -= 150;
                        
                        //switch to other button
                        button_select = !button_select;
                    }
                    
                    //if enter key was pressed
                    if ( event.key.keysym.sym == SDLK_RETURN)
                    {
                        //if YES button is selected
                        if (button_select == true)
                            //allow player battle to end so it can restart
                            terminated = true;
                        //if NO button is selected
                        else if (button_select == false)
                        {
                            //change level to title
                            level = TITLE;
                            //allow player battle to end
                            terminated = true;
                        }
                    }
                }
            }
        }
        
        //move objects and modify score counts
        ball->move(paddles, player1_score, player2_score);
        paddle1->move();
        paddle2->move();
        
        //clear window black
        SDL_SetRenderDrawColor( renderer, 0, 0, 0, 255 );
        SDL_RenderClear(renderer);
        
        //set renderer color to light pink
        SDL_SetRenderDrawColor( renderer, 219, 112, 147, 255 );
        
        //render (2-pixel width) dotted line in center
        for (int i = 0; i <= 1; ++i)
            for (int j = 0; j <= WINDOW_HEIGHT; ++j)
            {
                SDL_RenderDrawPoint(renderer, WINDOW_WIDTH/2 + i, j);
                if (j % 20 == 0)
                    j += 10;
            }
        
        //render player1 score
        if (player1_score == 0) scores[0].render( (WINDOW_WIDTH*4)/10, (WINDOW_HEIGHT*3)/20 );
        else if (player1_score == 1) scores[1].render( (WINDOW_WIDTH*4)/10, (WINDOW_HEIGHT*3)/20 );
        else if (player1_score == 2) scores[2].render( (WINDOW_WIDTH*4)/10, (WINDOW_HEIGHT*3)/20 );
        else if (player1_score == 3) scores[3].render( (WINDOW_WIDTH*4)/10, (WINDOW_HEIGHT*3)/20 );
        
        //render player2 score
        if (player2_score == 0) scores[0].render( (WINDOW_WIDTH*6)/10, (WINDOW_HEIGHT*3)/20 );
        else if (player2_score == 1) scores[1].render( (WINDOW_WIDTH*6)/10, (WINDOW_HEIGHT*3)/20 );
        else if (player2_score == 2) scores[2].render( (WINDOW_WIDTH*6)/10, (WINDOW_HEIGHT*3)/20 );
        else if (player2_score == 3) scores[3].render( (WINDOW_WIDTH*6)/10, (WINDOW_HEIGHT*3)/20 );
        
        //render paddles
        paddle1->render();
        paddle2->render();
        
        //set renderer color to white
        SDL_SetRenderDrawColor( renderer, 255, 255, 255, 255 );
        
        //if a player won
        if (player1_score == 3 || player2_score == 3 )
        {
            //if player1 wins, render player1 wins text
            if (player1_score == 3)
                player1_wins.render(WINDOW_WIDTH/2, (WINDOW_HEIGHT*3)/10);
            
            //if player2 wins, render player2 wins text
            else if (player2_score == 3)
                player2_wins.render(WINDOW_WIDTH/2, (WINDOW_HEIGHT*3)/10);
            
            //render try again text
            try_again.render(WINDOW_WIDTH/2, (WINDOW_HEIGHT*6)/10);
            
            //render yes or no text
            yes_or_no.render(WINDOW_WIDTH/2, (WINDOW_HEIGHT*7)/10);
            
            //render yes or no button outline
            SDL_RenderDrawRect( renderer, &button_outline );
        }
        
        //render ball
        ball->render();
        
        //render window outline
        SDL_RenderDrawRect( renderer, &window_outline );
        
        //update window with new renders
        SDL_RenderPresent( renderer );
    }
    
    //cleanup
    delete paddle1;
    delete paddle2;
    delete ball;
}
Example #21
0
int main(int argc, char **argv) {

    // Google logging needed for parts of caffe that were extracted

    // Network structure
    // data - conv - reLU - pool - fc - softmax

    std::vector<Layer*> network;

    // Description of the neural network

    int N = 100; // number of samples/batch_size
    int d_w = 32; // data width
    int d_h = 32; // data height
    int ch = 3; // number of channels

    Image<float> data(32, 32, 3, 100);
    DataLayer * d_layer = new DataLayer(d_h, d_w, ch, N, data);
    network.push_back(d_layer);
    printf("data out size %d x %d x %d x %d\n", d_layer->out_dim_size(0),
                                                d_layer->out_dim_size(1),
                                                d_layer->out_dim_size(2),
                                                d_layer->out_dim_size(3));
    int n_f = 32; // number of filters
    int f_w = 7;  // filter width
    int f_h = 7;  // filter height
    int pad = (f_w-1)/2; // padding required to handle boundaries
    int stride = 1; // stride at which the filter evaluated

    Convolutional * conv  = new Convolutional(n_f, f_w, f_h, pad,
                                              stride, d_layer);
    network.push_back(conv);
    printf("conv out size %d x %d x %d x %d\n", conv->out_dim_size(0),
                                                conv->out_dim_size(1),
                                                conv->out_dim_size(2),
                                                conv->out_dim_size(3));

    ReLU * relu = new ReLU(conv);
    network.push_back(relu);

    int p_w = 2; // pooling width
    int p_h = 2; // pooling height
    int p_stride = 2; // pooling stride

    MaxPooling * pool = new MaxPooling(p_w, p_h, p_stride, relu);
    network.push_back(pool);
    printf("pool out size %d x %d x %d x %d\n", pool->out_dim_size(0),
                                                pool->out_dim_size(1),
                                                pool->out_dim_size(2),
                                                pool->out_dim_size(3));

    Flatten * flatten = new Flatten(pool);
    network.push_back(flatten);
    printf("flatten out size %d x %d\n", flatten->out_dim_size(0),
                                         flatten->out_dim_size(1));

    int C = 10; // number of classes

    Affine * fc = new Affine(C, flatten);
    network.push_back(fc);
    printf("fc out size %d x %d\n", fc->out_dim_size(0),
                                    fc->out_dim_size(1));

    SoftMax * softm = new SoftMax(fc);
    network.push_back(softm);
    printf("softm out size %d x %d\n", softm->out_dim_size(0),
                                       softm->out_dim_size(1));

    Image<float> scores(C, N);
    Image<int> labels(N);


    softm->back_propagate(Func(labels));

    // Schedule
    conv->forward.compute_root();
    pool->forward.compute_root();
    fc->forward.compute_root();
    softm->forward.compute_root();
    conv->f_param_grads[0].compute_root();
    conv->f_param_grads[1].compute_root();
    conv->f_in_grad.compute_root();
    fc->f_param_grads[0].compute_root();
    fc->f_param_grads[1].compute_root();
    fc->f_in_grad.compute_root();
    pool->f_in_grad.compute_root();

    conv->f_param_grads[0].print_loop_nest();

    // Build
    std::vector<Func> outs;
    outs.push_back(softm->forward);
    outs.push_back(conv->f_param_grads[0]);
    Pipeline p(outs);

    timeval t1, t2;
    gettimeofday(&t1, NULL);
    p.realize({scores, conv->param_grads[0]});
    gettimeofday(&t2, NULL);

    float time = (t2.tv_sec - t1.tv_sec) +
        (t2.tv_usec - t1.tv_usec) / 1000000.0f;
    printf("First JIT time: %f\n", time);


    gettimeofday(&t1, NULL);
    p.realize({scores, conv->param_grads[0]});
    gettimeofday(&t2, NULL);

    time = (t2.tv_sec - t1.tv_sec) +
        (t2.tv_usec - t1.tv_usec) / 1000000.0f;
    printf("Second JIT time: %f\n", time);

    for (Layer* l: network)
        delete l;

    return 0;
}
Example #22
0
    void SoftCascadeLearner::run(const nor_utils::Args& args)
    {
        // load the arguments
        this->getArgs(args);
        
        //print cascade properties
        if (_verbose > 0) {
            cout    << "[+] Softcascade parameters :" << endl
                    << "\t --> target detection rate = " << _targetDetectionRate << endl
                    << "\t --> alpha (exp param) = " << _alphaExponentialParameter << endl
                    << "\t --> bootstrap rate = " << _bootstrapRate << endl
                    << endl;
        }
        

        // get the registered weak learner (type from name)
        BaseLearner* pWeakHypothesisSource = 
            BaseLearner::RegisteredLearners().getLearner(_baseLearnerName);
        // initialize learning options; normally it's done in the strong loop
        // also, here we do it for Product learners, so input data can be created
        pWeakHypothesisSource->initLearningOptions(args);

        // get the training input data, and load it

        InputData* pTrainingData = pWeakHypothesisSource->createInputData();
        pTrainingData->initOptions(args);
        pTrainingData->load(_trainFileName, IT_TRAIN, 5);

        InputData* pBootstrapData = NULL;
        if (!_bootstrapFileName.empty()) {
            pBootstrapData = pWeakHypothesisSource->createInputData();
            pBootstrapData->initOptions(args);
            pBootstrapData->load(_bootstrapFileName, IT_TRAIN, 5);
        }
        
        // get the testing input data, and load it
        InputData* pTestData = NULL;
        if ( !_testFileName.empty() )
        {
            pTestData = pWeakHypothesisSource->createInputData();
            pTestData->initOptions(args);
            pTestData->load(_testFileName, IT_TEST, 5);
        }

        Serialization ss(_shypFileName, false );
        ss.writeHeader(_baseLearnerName);
        
        
//        outputHeader();
        // The output information object
        OutputInfo* pOutInfo = NULL;

        if ( !_outputInfoFile.empty() ) 
        {
            pOutInfo = new OutputInfo(args, true);
            pOutInfo->setOutputList("sca", &args);
            
            pOutInfo->initialize(pTrainingData);
            
            if (pTestData)
                pOutInfo->initialize(pTestData);
            pOutInfo->outputHeader(pTrainingData->getClassMap(), true, true, false);
            pOutInfo->outputUserHeader("thresh");
            pOutInfo->headerEndLine();
        }
        
        
//        ofstream trainPosteriorsFile;
//        ofstream testPosteriorsFile;
        
        
        const NameMap& namemap = pTrainingData->getClassMap();
        _positiveLabelIndex = namemap.getIdxFromName(_positiveLabelName);

        // FIXME: output posteriors

//        OutputInfo* pTrainPosteriorsOut = NULL;
//        OutputInfo* pTestPosteriorsOut = NULL;
        
//        if (! _trainPosteriorsFileName.empty()) {
//            pTrainPosteriorsOut = new OutputInfo(_trainPosteriorsFileName, "pos", true);
//            pTrainPosteriorsOut->initialize(pTrainingData);
//            dynamic_cast<PosteriorsOutput*>( pTrainPosteriorsOut->getOutputInfoObject("pos") )->addClassIndex(_positiveLabelIndex );
//        }
        
//        if (! _testPosteriorsFileName.empty() && !_testFileName.empty() ) {
//            pTestPosteriorsOut = new OutputInfo(_testPosteriorsFileName, "pos", true);
//            pTestPosteriorsOut->initialize(pTestData);
//            dynamic_cast<PosteriorsOutput*>( pTestPosteriorsOut->getOutputInfoObject("pos") )->addClassIndex(_positiveLabelIndex );            
//        }
        
        const int numExamples = pTrainingData->getNumExamples();

        vector<BaseLearner*> inWeakHypotheses;
        
        if (_fullRun) {            
            // TODO : the full training is implementet, testing is needed
            AdaBoostMHLearner* sHypothesis = new AdaBoostMHLearner();
            sHypothesis->run(args, pTrainingData, _baseLearnerName, _numIterations, inWeakHypotheses );
            delete sHypothesis;
        }
        else { 
            
            cout << "[+] Loading uncalibrated shyp file... ";
            //read the shyp file of the trained classifier
            UnSerialization us;
            us.loadHypotheses(_unCalibratedShypFileName, inWeakHypotheses, pTrainingData);  
            if (_inShypLimit > 0 && _inShypLimit < inWeakHypotheses.size() ) {
                inWeakHypotheses.resize(_inShypLimit);
            }
            if (_numIterations > inWeakHypotheses.size()) {
                _numIterations = inWeakHypotheses.size();
            }
            cout << "weak hypotheses loaded, " << inWeakHypotheses.size() << " retained.\n";
        }
        
        // some initializations
        _foundHypotheses.resize(0);
        double faceRejectionFraction = 0.;
        double estimatedExecutionTime = 0.;
        vector<double> rejectionDistributionVector;

        _rejectionThresholds.resize(0);
        
        
        set<int> trainingIndices;
        for (int i = 0; i < numExamples; i++) {
            trainingIndices.insert(pTrainingData->getRawIndex(i) );
        }
        
        // init v_t (see the paper)
        initializeRejectionDistributionVector(_numIterations, rejectionDistributionVector);

        if (_verbose == 1)
            cout << "Learning in progress..." << endl;

        ///////////////////////////////////////////////////////////////////////
        // Starting the SoftCascade main loop
        ///////////////////////////////////////////////////////////////////////
        for (int t = 0; t < _numIterations; ++t)
        {
            if (_verbose > 0)
                cout << "--------------[ iteration " << (t+1) << " ]--------------" << endl;

            faceRejectionFraction += rejectionDistributionVector[t];
            
            cout << "[+] Face rejection tolerated : " << faceRejectionFraction << " | v[t] = " << rejectionDistributionVector[t] << endl;
            
            int numberOfNegatives = pTrainingData->getNumExamplesPerClass(1 - _positiveLabelIndex);
            
            //vector<BaseLearner*>::const_iterator whyIt;
            int selectedIndex = 0;
            AlphaReal bestGap = 0;
            vector<AlphaReal> posteriors;
            computePosteriors(pTrainingData, _foundHypotheses, posteriors, _positiveLabelIndex);
            
            //should use an iterator instead of i
            
            vector<BaseLearner*>::iterator whyIt;
            int i;
            for (i = 0, whyIt = inWeakHypotheses.begin(); whyIt != inWeakHypotheses.end(); ++whyIt, ++i) {
            
                vector<AlphaReal> temporaryPosteriors = posteriors;
                vector<BaseLearner*> temporaryWeakHyp = _foundHypotheses;
                temporaryWeakHyp.push_back(*whyIt);
                updatePosteriors(pTrainingData, *whyIt, temporaryPosteriors, _positiveLabelIndex);
                
                AlphaReal gap = computeSeparationSpan(pTrainingData, temporaryPosteriors, _positiveLabelIndex );

                if (gap > bestGap) {
                    bestGap = gap;
                    selectedIndex = i;
                }
            }
            
            BaseLearner* selectedWeakHypothesis = inWeakHypotheses[selectedIndex];
            
            cout << "[+] Rank of the selected weak hypothesis : " << selectedIndex << endl
                 << "\t ---> edge gap = " << bestGap << endl
                 << "\t ---> alpha = " << selectedWeakHypothesis->getAlpha() << endl;

            //update the stages
            _foundHypotheses.push_back(selectedWeakHypothesis);
            updatePosteriors(pTrainingData, selectedWeakHypothesis, posteriors, _positiveLabelIndex);
            
            double missesFraction;
            AlphaReal r = findBestRejectionThreshold(pTrainingData, posteriors, faceRejectionFraction, missesFraction);
            _rejectionThresholds.push_back(r);
            
            
            // update the output info object
            dynamic_cast<SoftCascadeOutput*>( pOutInfo->getOutputInfoObject("sca") )->appendRejectionThreshold(r);
            
            cout << "[+] Rejection threshold = " << r << endl;
            
            //some updates
            ss.appendHypothesisWithThreshold(t, selectedWeakHypothesis, r);
            faceRejectionFraction -= missesFraction;
            
            inWeakHypotheses.erase(inWeakHypotheses.begin() + selectedIndex);
            double whypCost = 1; //just in case there are different costs for each whyp
            estimatedExecutionTime += whypCost * numberOfNegatives;
            
            // output perf in file
            vector< vector< AlphaReal> > scores(0);
            _output << t + 1 << setw(_sepWidth + 1) << r << setw(_sepWidth);
            
            // update OutputInfo with the new whyp
//            updateOutputInfo(pOutInfo, pTrainingData, selectedWeakHypothesis);
//            if (pTestData) {
//                updateOutputInfo(pOutInfo, pTestData, selectedWeakHypothesis);
//            }
            

            // output the iteration results
            printOutputInfo(pOutInfo, t, pTrainingData, pTestData, selectedWeakHypothesis, r);
                        
//            if (pTrainPosteriorsOut) {
//                pTrainPosteriorsOut->setTable(pTrainingData, pOutInfo->getTable(pTrainingData));
//                pTrainPosteriorsOut->outputCustom(pTrainingData);
//            }
//
//            if (pTestPosteriorsOut) {
//                pTestPosteriorsOut->setTable(pTestData, pOutInfo->getTable(pTestData));
//                pTestPosteriorsOut->outputCustom(pTestData);
//            }
            
            
            int leftNegatives = filterDataset(pTrainingData, posteriors, r, trainingIndices);
            if (leftNegatives == 0) {
                cout << endl << "[+] No more negatives.\n";
                break;
            }
            
            if (_bootstrapRate != 0) {
                bootstrapTrainingSet(pTrainingData, pBootstrapData, trainingIndices);
            }

        }  // loop on iterations
        /////////////////////////////////////////////////////////

        // write the footer of the strong hypothesis file
        ss.writeFooter();

        // Free the two input data objects
        if (pTrainingData)
            delete pTrainingData;
        if (pBootstrapData) {
            delete pBootstrapData;
        }
        if (pTestData)
            delete pTestData;

        if (_verbose > 0)
            cout << "Learning completed." << endl;
    }