Esempio n. 1
0
void BrennerPotential::calc_many_body(
	       const struct AtomPairInfoState *apis,
               /* i_side is really a boolean, true the first time and false the
                  second time. */
               int i_side, int index1, int index2,
               /* What does indexj mean now that things are reorganized?  It
                used to be the index of the neighbor pair in the entire
                neighbor array the second time, and -1 the first time.  We
                ignore it the first time, because i_side is true.  The second
                time, we use it to skip repeated examination of something we
                already looked at if k is equal to indexj.

                After the reorganization, it is a position in the list of
                neighbors of index1.
               */
               int indexj,
               Float vdbdi,
               /* xsik is a per-neighbor input. */
               Float *xsik,
               Float *dexni,
               Float vdrdi, Float vdrdc, Float *cfuni, Float sdalik,
               Float *xsjk, bren_vector *xk, Float *dcfuni, Float conk)
{
  /* The code that increments nk here has to match up with the code that
   increments nk in calc1side, since we use nk to index into cfuni which is
   computed in calc1side. */
  int nk = 0;
  int n; /* nl */
  int k; /* l */
  int m;
  Float dwr;
  //  XXX The following neighbor list access should be optimized away
  int nmaxnb = nblist->MaxNeighborListLength();
  vector<int> kNeighbors(nmaxnb);
  vector<Vec> distances(nmaxnb);
  vector<double> sq_distances(nmaxnb);
  /* stop_index is one past the index of the last neighbor. */
  int sizemax = nmaxnb;
  const int stop_index =
    nblist->GetFullNeighbors(index1, &kNeighbors[0], &distances[0],
			     &sq_distances[0], sizemax);
  const struct AtomPairInfo *const kPairs = pairsconst (index1, apis);
  for(k = 0; k < stop_index; ++k) {
    Float rp, rp1, rp2, rep, ddr;
    int kn = kNeighbors [k];
    int kk = getKtype (kn);
    if(i_side && k == indexj) continue;
    if(!i_side && kn == index2) continue;
    if(kPairs[k].lcheck != 1) continue;
    dwr = kPairs[k].dww/kPairs[k].rcor;
    ++nk; /*nk only increases when we get past the continue's above. */
    /*
     * First Neighbors
     */
    rp1 = vdbdi*(xsik[nk]+dwr*dexni[kk-1]) + dwr*(vdrdi+vdrdc*cfuni[nk])
      + vdbdi*dwr*sdalik;
    rp2 = vdbdi*xsjk[nk];
    transForce(index1, kn, rp1*kPairs[k].cor);
    /*
     * Angular Forces
     */
    transForce(index2, kn, rp2*xk[nk]);
    /*
     * Second Neighbors via RADIC
     */
    ddr = vdrdc*dcfuni[nk]*2.0*conk;
    if(ddr == 0.0) continue;
    {
      // This second neighborlist access is harder to optimize away.
      vector<int> mNeighbors(nmaxnb);
      vector<Vec> distances2(nmaxnb);
      vector<double> sq_distances2(nmaxnb);
      /* stop_index is one past the index of the last neighbor. */
      int sizemax = nmaxnb;
      const int stop_index2 =
	nblist->GetFullNeighbors(kn, &mNeighbors[0], &distances2[0],
				 &sq_distances2[0], sizemax);
      const struct AtomPairInfo *const mPairs = pairsconst (kn, apis);
      for(m = 0; m < stop_index2; ++m) {
        int mn;
        if(mPairs[m].lcheck != 1) continue;
        mn = mNeighbors[m];
        if(mn == kn) continue;
        rp = ddr*mPairs[m].dww/mPairs[m].rcor;
        transForce(kn, mn, rp*mPairs[m].cor);
      }
    }
  }
}
Esempio n. 2
0
File: cli.cpp Progetto: damellis/ESP
int main(int argc, char* argv[]) {
    bool draw_sample = false;
    bool load_pipeline = false;
    char c;
    opterr = 0;
    while ((c = getopt(argc, argv, "dl")) != -1) {
        switch (c) {
            case 'd': draw_sample = true; break;
            case 'l': load_pipeline = true; break;
            default: abort();
        }
    }

    // Setup Pipeline
    // TODO(benzh) Convert this to user code loading
    GRT::GestureRecognitionPipeline pipeline;

    pipeline.addFeatureExtractionModule(
        GRT::FFT(kFFT_WindowSize, kFFT_HopSize,
                 DIM, GRT::FFT::HAMMING_WINDOW, true, false));

    GRT::MFCC::Options options;
    options.sample_rate = sample_rate;
    options.fft_size = kFFT_WindowSize / 2;
    options.start_freq = 300;
    options.end_freq = 8000;
    options.num_tri_filter = 26;
    options.num_cepstral_coeff = 12;
    options.lifter_param = 22;
    options.use_vad = true;
    options.noise_level = 5;

    pipeline.addFeatureExtractionModule(GRT::MFCC(options));
    pipeline.setClassifier(GRT::GMM(16, true, false, 1, 100, 0.001));
    pipeline.addPostProcessingModule(GRT::ClassLabelFilter(25, 40));

    TrainingDataManager training_data_manager(kNumMaxLabels);

    if (!load_pipeline) {
        // We load the training data and train the model
        if (training_data_manager.load(kTrainingDataFilename)) {
            auto d = training_data_manager.getSample(1, 2);

            if (draw_sample) {
                plt::plot(training_data_manager.getSample(1, 2).getColVector(0));
                plt::save("./sample.png");
            }

            if (pipeline.train(training_data_manager.getAllData())) {
                std::cout << "Training Successful" << std::endl;;
                pipeline.save(kPipelineFilename);
            } else {
                std::cout << "Failed to train the model" << std::endl;
                return -1;
            }
        }
    } else {
        pipeline.load(kPipelineFilename);
    }

    GRT::MatrixDouble test_data;
    if (!test_data.load(kTestDataFilename)) {
        std::cout << "Failed to load test data from " << kTestDataFilename
                  << std::endl;
        return -1;
    }

    size_t vec_size = test_data.getNumRows();
    std::vector<double> x(vec_size, 0);
    std::vector<double> preds(vec_size, 0);
    std::vector<double> audio(vec_size, 0);
    std::vector<double> distances1(vec_size, 0);
    std::vector<double> distances2(vec_size, 0);

    std::cout << "Running testing" << std::endl;

    // Run test data through
    for (uint32_t i = 0; i < test_data.getNumRows(); i++) {
        auto td = test_data.getRowVector(i);
        pipeline.predict(td);
        uint32_t predicted_label = pipeline.getPredictedClassLabel();
        auto ds = pipeline.getClassDistances();

        audio[i] = td[0];
        x[i] = i;
        preds[i] = static_cast<double>(predicted_label);
        distances1[i] = ds[0];
        distances2[i] = ds[1];
    }

    // std::cout << "Test finished, plotting ... ";
    // plt::plot(x, audio, "b-");
    // plt::save("./audio.png");

    plt::plot(x, distances1, "r.", x, distances2, "g.");
    plt::save("./prediction.png");
    std::cout << "Done" << std::endl;

    return 0;
}