Exemplo n.º 1
0
static int fmaKMeans(void)
{
    CvTermCriteria crit;
    float** vectors;
    int*    output;
    int*    etalon_output;

    int lErrors = 0;
    int lNumVect = 0;
    int lVectSize = 0;
    int lNumClust = 0;
    int lMaxNumIter = 0;
    float flEpsilon = 0;

    int i,j;
    static int  read_param = 0;

    /* Initialization global parameters */
    if( !read_param )
    {
        read_param = 1;
        /* Read test-parameters */
        trsiRead( &lNumVect, "1000", "Number of vectors" );
        trsiRead( &lVectSize, "10", "Number of vectors" );
        trsiRead( &lNumClust, "20", "Number of clusters" );
        trsiRead( &lMaxNumIter,"100","Maximal number of iterations");
        trssRead( &flEpsilon, "0.5", "Accuracy" );
    }

    crit = cvTermCriteria( CV_TERMCRIT_EPS|CV_TERMCRIT_ITER, lMaxNumIter, flEpsilon );
    
    //allocate vectors
    vectors = (float**)cvAlloc( lNumVect * sizeof(float*) );
    for( i = 0; i < lNumVect; i++ )
    {
        vectors[i] = (float*)cvAlloc( lVectSize * sizeof( float ) );
    }

    output = (int*)cvAlloc( lNumVect * sizeof(int) );
    etalon_output = (int*)cvAlloc( lNumVect * sizeof(int) );
    
    //fill input vectors
    for( i = 0; i < lNumVect; i++ )
    {
        ats1flInitRandom( -2000, 2000, vectors[i], lVectSize );
    }
    
    /* run etalon kmeans */
    /* actually it is the simpliest realization of kmeans */

    int ni = _real_kmeans( lNumClust, vectors, lNumVect, lVectSize, etalon_output, crit.epsilon, crit.max_iter );

    trsWrite(  ATS_CON, "%d iterations done\n",  ni );
                  
    /* Run OpenCV function */
#define _KMEANS_TIME 0

#if _KMEANS_TIME
    //timing section 
    trsTimerStart(0);
    __int64 tics = atsGetTickCount();  
#endif  

    cvKMeans( lNumClust, vectors, lNumVect, lVectSize, 
              crit, output );

#if _KMEANS_TIME
    tics = atsGetTickCount() - tics;     
    trsTimerStop(0);
    //output result
    //double dbUsecs =ATS_TICS_TO_USECS((double)tics);
    trsWrite( ATS_CON, "Tics per iteration %d\n", tics/ni );    

#endif

    //compare results
    for( j = 0; j < lNumVect; j++ )
    {
        if ( output[j] != etalon_output[j] )
        {
            lErrors++;
        }
    }

    //free memory
    for( i = 0; i < lNumVect; i++ )
    {
        cvFree( &(vectors[i]) );
    }
    cvFree(&vectors);
    cvFree(&output);
    cvFree(&etalon_output);      
   
   if( lErrors == 0 ) return trsResult( TRS_OK, "No errors fixed for this text" );
    else return trsResult( TRS_FAIL, "Detected %d errors", lErrors );

}
Exemplo n.º 2
0
int testGraph()
{
    int i, j;

    int ret = TRS_OK;

    const int nnodes = 7;

    int numOfNeigh[] = { 3, 3, 4, 5, 2, 3, 2 };

    int neigh1[] = { 3, 1, 2 };
    int neigh2[] = { 2, 0, 3 };
    int neigh3[] = { 0, 1, 3, 6 };
    int neigh4[] = { 0, 1, 2, 4, 5 };
    int neigh5[] = { 3, 5 };
    int neigh6[] = { 3, 4, 6 };
    int neigh7[] = { 5, 2 };

    ENeighborType orient1[] = { ntChild, ntChild, ntChild };
    ENeighborType orient2[] = { ntChild, ntParent, ntChild };
    ENeighborType orient3[] = { ntParent, ntParent, ntChild, ntChild };
    ENeighborType orient4[] = { ntParent, ntParent, ntParent, ntNeighbor, ntNeighbor };
    ENeighborType orient5[] = { ntNeighbor, ntNeighbor };
    ENeighborType orient6[] = { ntNeighbor, ntNeighbor, ntChild };
    ENeighborType orient7[] = { ntParent, ntParent };

    int *neigh[] = { neigh1, neigh2, neigh3, neigh4, neigh5, neigh6,
                     neigh7
                   };
    ENeighborType *orient[] = { orient1, orient2, orient3, orient4, orient5,
                                orient6, orient7
                              };

    const int *neighbors;
    const ENeighborType *orientation;

    double time1;

    TestGraph *graph;

    /* The graph I used to have a test is described in a document named
    graph_structure.doc which is stored in VSS base in docs folder */

    /* to test graph the graph creation you can either: */

    /* 1. create it from the list of neighbors */

    trsTimerStart(0);
    graph = new TestGraph(nnodes, numOfNeigh, neigh, orient);
    graph->Dump();

    /* look at the graph */

    for( i = 0; i < nnodes; i++ )
    {
        graph->GetNeighbors( i, &numOfNeigh[i], &neighbors, &orientation );

        intVector neighToComp( neigh[i], neigh[i] + numOfNeigh[i] );
        std::sort( neighToComp.begin(), neighToComp.end() );

        for( j = 0; j < numOfNeigh[i]; j++ )
        {
            if(neighbors[j] != neighToComp[j])
            {
                ret = TRS_FAIL;
            }
        }
    }

    delete(graph);

    time1 = trsTimerClock(0);
    std::cout<<"timing of graph creation with all neighbors preset "
             <<time1<<std::endl;

    /* 2. create a graph with a blank list of neighbors
    and then set neighbors */

    trsTimerStart(0);
    graph = new TestGraph(nnodes, NULL, NULL, NULL);

    graph->SetNeighbors( 0, numOfNeigh[0], neigh1, orient1 );
    graph->SetNeighbors( 1, numOfNeigh[1], neigh2, orient2 );
    graph->SetNeighbors( 2, numOfNeigh[2], neigh3, orient3 );
    graph->SetNeighbors( 3, numOfNeigh[3], neigh4, orient4 );
    graph->SetNeighbors( 4, numOfNeigh[4], neigh5, orient5 );
    graph->SetNeighbors( 5, numOfNeigh[5], neigh6, orient6 );
    graph->SetNeighbors( 6, numOfNeigh[6], neigh7, orient7 );
    graph->Dump();

    for( i = 0; i < nnodes; i++ )
    {
        graph->GetNeighbors( i, &numOfNeigh[i], &neighbors, &orientation );

        intVector neighToComp( neigh[i], neigh[i] + numOfNeigh[i] );
        std::sort( neighToComp.begin(), neighToComp.end() );

        for( j = 0; j < numOfNeigh[i]; j++ )
        {
            if(neighbors[j] != neighToComp[j])
            {
                ret = TRS_FAIL;
            }
        }
    }

    delete(graph);

    time1 = trsTimerClock(0);
    std::cout<<"graph creation with setting the each neighbor separatly "
             <<time1<<std::endl;

    /* 3. create a blank graph and add edges to it. take the edges from the
    list of neighbors above */

    trsTimerStart(0);
    graph = new TestGraph(nnodes, NULL, NULL, NULL);

    graph->AddEdge( 0, 1, 1 );
    graph->AddEdge( 0, 2, 1 );
    graph->AddEdge( 0, 3, 1 );
    graph->AddEdge( 1, 2, 1 );
    graph->AddEdge( 1, 3, 1 );
    graph->AddEdge( 2, 3, 1 );
    graph->AddEdge( 2, 6, 1 );
    graph->AddEdge( 3, 4, 0 );
    graph->AddEdge( 3, 5, 0 );
    graph->AddEdge( 4, 5, 0 );
    graph->AddEdge( 5, 6, 1 );
    graph->Dump();

    for( i = 0; i < nnodes; i++ )
    {
        graph->GetNeighbors( i, &numOfNeigh[i], &neighbors, &orientation );
        intVector neighToComp( neigh[i], neigh[i] + numOfNeigh[i] );
        std::sort( neighToComp.begin(), neighToComp.end() );

        for( j = 0; j < numOfNeigh[i]; j++ )
        {
            if(neighbors[j] != neighToComp[j])
            {
                ret = TRS_FAIL;
            }
        }
    }

    delete(graph);

    time1 = trsTimerClock(0);
    std::cout<<"timing graph creation with edges added "<<time1<<std::endl;

    /* all three graphs that are shown, should look the same */

    return trsResult( ret, ret == TRS_OK ? "No errors" : "Graph FAILED");
}
Exemplo n.º 3
0
int timeJTreeInfEngine()
{
    int ret = TRS_OK;

    char filename[120];

    trstRead(filename, sizeof(filename), DSL_NETWORK_NAME, "Model name");

    trsTimerStart(0);

    CBNet* pBNet = ConvertFromDSLNet(filename);

    trsTimerStop(0);

    double timeOfDSL2PNLConversion = trsTimerSec(0);

    if( pBNet == NULL )
    {
        ret = TRS_FAIL;

        return trsResult( ret, ret == TRS_OK ? "No errors" : "JTree timing FAILED");
    }

    const CModelDomain* pModelDomain = pBNet->GetModelDomain();

    const int numOfNds               = pBNet->GetNumberOfNodes();

    const int numOfObsNds            = NUM_OF_OBS_NDS;

    assert( numOfObsNds <= numOfNds );


    intVector   obsNds(numOfObsNds);

    valueVector obsNdsVals(numOfObsNds);

    SetRndObsNdsAndVals( pModelDomain, &obsNds, &obsNdsVals );

    CEvidence* pEvidence = CEvidence::Create( pModelDomain, obsNds,
        obsNdsVals );

    trsTimerStart(1);

    CJtreeInfEngine* pJTreeInfEngine = CJtreeInfEngine::Create(pBNet);

    trsTimerStop(1);

    double timeOfInfCreation = trsTimerSec(1);


    const int numOfEnterEvidenceLoops = TIMES_TO_RUN_ENTER_EVIDENCE;

    assert( numOfEnterEvidenceLoops > 0 );

    trsTimerStart(2);

    int i = 0;

    for( ; i < numOfEnterEvidenceLoops; ++i )
    {
        pJTreeInfEngine->EnterEvidence(pEvidence);
    }

    trsTimerStop(2);

    double timeOfEnterEvidence = trsTimerSec(2);

    double averageTimeOfEnterEvidence = timeOfEnterEvidence
        /numOfEnterEvidenceLoops;

    double freqCPU = trsClocksPerSec();

    trsCSVString8( "d", func_name,
        trsDouble(timeOfInfCreation),
        trsDouble(averageTimeOfEnterEvidence),
        trsDouble(freqCPU),
        "\n JTree inference creation ",
        "\n average time for entering evidence ",
        "\n CPU frequency " );

    trsWrite( TW_RUN | TW_CON,
        " %s performance measurement:\n\n", func_name );

    trsWrite( TW_RUN | TW_CON,
        " Conversion from DSL to PNL network took    %g seconds\n"
        " JTree inference engine creation took       %g seconds\n"
        " Average entering evidence time is          %g seconds\n",
        timeOfDSL2PNLConversion,
        timeOfInfCreation,
        averageTimeOfEnterEvidence );

    delete pEvidence;

    //CJtreeInfEngine::Release(&pJTreeInfEngine);
    delete pJTreeInfEngine;

    delete pBNet;

    return trsResult( ret, ret == TRS_OK ? "No errors" : "JTree timing FAILED");
}