All pastes #923069 Raw Edit

Anonymous

public text v1 · immutable
#923069 ·published 2008-02-29 06:51 UTC
rendered paste body
    void ANN::train(ImageManager *imageManager)
    {
        LogManager& mLogManager = LogManager::getSingleton();
        ImageSet *imageSet = imageManager->getImageSet(mANNParams->name);

        // Loop over each image in the set to get the total number of SIFT keypoints in the set
        int totalKeypoints = 0;
        ImageSet::iterator setIt;
        for(setIt = imageSet->begin(); setIt != imageSet->end(); setIt++)
        {
            VL::Sift *sift = setIt->second;
            int numKeypoints = 0;
            for(VL::Sift::Keypoints::const_iterator keyIt = sift->keypointsBegin(); keyIt != sift->keypointsEnd(); keyIt++)
            {
                numKeypoints++;
            }
            totalKeypoints = totalKeypoints + numKeypoints;
            #ifdef DEBUG
            mLogManager.logDebug("ANN: Image '" + setIt->first + "' has " + boost::lexical_cast<std::string>(numKeypoints) + " keypoints.");
            #endif
        }

        #ifdef DEBUG
        mLogManager.logDebug("ANN: " + boost::lexical_cast<std::string>(totalKeypoints) + " total keypoint(s) in ANN '" + mANNParams->name + "'. ");
        #endif

        int outputNeurons = mANNParams->layerLayout[mANNParams->numLayers - 1];
        float (*input)[128] = new float[totalKeypoints][128];
        //float (*output)[outputNeurons] = new float[totalKeypoints][outputNeurons];
        float **output = new float *[totalKeypoints];
        for(std::size_t i = 0; i < totalKeypoints; ++i)
        {
            output[i] = new float[outputNeurons];
        }

        // Loop over keypoints, this time assign answers to them for the training data.
        for(setIt = imageSet->begin(); setIt != imageSet->end(); setIt++)
        {
            VL::Sift *sift = setIt->second;
            int keyIndex = 0;
            for(VL::Sift::Keypoints::const_iterator keyIt = sift->keypointsBegin(); keyIt != sift->keypointsEnd(); keyIt++)
            {
                /*// detect orientations
                VL::float_t angles [4];
                int nangles = 1;
                angles[0] = VL::float_t(0);

                // compute descriptors
                for(int a = 0 ; a < nangles ; ++a)
                {
                    sift->computeKeypointDescriptor(input[keyIndex], *keyIt, angles[a]);
                    decimalToBinary(imageManager->getImageId(mANNParams.name, setIt->first), outputNeurons, output[keyIndex]);
                }
                */
                sift->computeKeypointDescriptor(input[keyIndex], *keyIt, 0);
                decimalToBinary(imageManager->getImageId(mANNParams->name, setIt->first), outputNeurons, output[keyIndex]);
                keyIndex++;
            }
        }

        trainData = fann_create_train(totalKeypoints, mANNParams->layerLayout[0], outputNeurons);
        for (int i = 0; i < totalKeypoints; i++)
        {
            for(int key = 0; key < 128; key++)
            {
                trainData->input[i][key] = input[i][key];
            }

            for(int out = 0; out < outputNeurons; out++)
            {
                trainData->output[i][out] = output[i][out];
            }
        }
        #ifdef DEBUG
        mLogManager.logDebug("ANN: Training ANN '" + mANNParams->name + "'. ");
        #endif
        fann_set_activation_function_hidden(mANN, FANN_GAUSSIAN);
        fann_set_activation_function_output(mANN, FANN_GAUSSIAN);
        fann_set_activation_steepness_hidden(mANN, 0.5);
        fann_set_activation_steepness_hidden(mANN, 0.5);
        fann_set_train_error_function(mANN, FANN_ERRORFUNC_LINEAR);
        fann_set_train_stop_function(mANN, FANN_STOPFUNC_BIT);
        fann_set_output_scaling_params(mANN, trainData, 0, 1);
        fann_train_on_data(mANN, trainData, mANNParams->maxEpochs, mANNParams->reportingInterval, mANNParams->desiredError);
        #ifdef DEBUG
        mLogManager.logDebug("ANN: Training complete!");
        #endif
    };