Ejemplo n.º 1
0
RegressionData ClassificationData::reformatAsRegressionData() const{

    //Turns the classification into a regression data to enable regression algorithms like the MLP to be used as a classifier
    //This sets the number of targets in the regression data equal to the number of classes in the classification data
    //The output of each regression training sample will then be all 0's, except for the index matching the classLabel, which will be 1
    //For this to work, the labelled classification data cannot have any samples with a classLabel of 0!
    RegressionData regressionData;

    if( totalNumSamples == 0 ){
        return regressionData;
    }

    const UINT numInputDimensions = numDimensions;
    const UINT numTargetDimensions = getNumClasses();
    regressionData.setInputAndTargetDimensions(numInputDimensions, numTargetDimensions);

    for(UINT i=0; i<totalNumSamples; i++){
        VectorFloat targetVector(numTargetDimensions,0);

        //Set the class index in the target Vector to 1 and all other values in the target Vector to 0
        UINT classLabel = data[i].getClassLabel();

        if( classLabel > 0 ){
            targetVector[ classLabel-1 ] = 1;
        }else{
            regressionData.clear();
            return regressionData;
        }

        regressionData.addSample(data[i].getSample(),targetVector);
    }

    return regressionData;
}
bool LabelledRegressionData::loadDatasetFromCSVFile(const string &filename,const UINT numInputDimensions,const UINT numTargetDimensions){

    fstream file;
    string value;
    clear();
    datasetName = "NOT_SET";
    infoText = "";

    //Clear any previous data
    clear();
    
    //Parse the CSV file
    FileParser parser;
    
    if( !parser.parseCSVFile(filename,true) ){
        errorLog << "loadDatasetFromCSVFile(...) - Failed to parse CSV file!" << endl;
        return false;
    }
    
    if( !parser.getConsistentColumnSize() ){
        errorLog << "loadDatasetFromCSVFile(...) - The CSV file does not have a consistent number of columns!" << endl;
        return false;
    }
    
    if( parser.getColumnSize() != numInputDimensions+numTargetDimensions ){
        errorLog << "loadDatasetFromCSVFile(...) - The number of columns in the CSV file (" << parser.getColumnSize() << ")";
        errorLog << " does not match the number of input dimensions plus the number of target dimensions (" << numInputDimensions+numTargetDimensions << ")" << endl;
        return false;
    }
    
    //Setup the labelled classification data
    setInputAndTargetDimensions(numInputDimensions, numTargetDimensions);
    
    UINT n = 0;
    VectorDouble inputVector(numInputDimensions);
    VectorDouble targetVector(numTargetDimensions);
    for(UINT i=0; i<parser.getRowSize(); i++){
        
        //Reset n
        n = 0;
        
        //Get the input vector
        for(UINT j=0; j<numInputDimensions; j++){
            inputVector[j] = Util::stringToDouble( parser[i][n++] );
        }
        
        //Get the target vector
        for(UINT j=0; j<numTargetDimensions; j++){
            targetVector[j] = Util::stringToDouble( parser[i][n++] );
        }
        
        //Add the labelled sample to the dataset
        if( !addSample(inputVector, targetVector) ){
            warningLog << "loadDatasetFromCSVFile(string filename) - Could not add sample " << i << " to the dataset!" << endl;
        }
    }
    
    return true;
}
Ejemplo n.º 3
0
void SmrCCDSolver::computeErrorVect(void)
{

  if ( m_jointProcessed == m_IKChainPtr->getNumJoints() ) m_jointProcessed = 0;

  SmrKinematicJoint *currentJoint = m_IKChainPtr->getJoint(m_jointProcessed);

  //change this according to constraints.
  // Build target vector
  ColumnVector targetVector(3);
  ColumnVector currentVector(3);


  m_IKChainPtr->setMode( ABSOLUTEMODE );

  SmrIKConstraint* currentConstraint = getConstraintPtr(0);
  //get the related joint for every constraint
  SmrJoint* relatedJoint = currentConstraint->getRelatedJointPtr();
  SmrQuaternion relatedOrient = relatedJoint->getOrient();
  SmrVector3 currentOffset = currentConstraint->getOffset();
  //apply the local offsets defined in the constraint
  relatedOrient.rotate(currentOffset);
  // update error vector
  currentVector(1) = (relatedJoint->getPos()+currentOffset).X();
  currentVector(2) = (relatedJoint->getPos()+currentOffset).Y();
  currentVector(3) = (relatedJoint->getPos()+currentOffset).Z();

  targetVector(1) = (currentConstraint->getPosition()).X();
  targetVector(2) = (currentConstraint->getPosition()).Y();
  targetVector(3) = (currentConstraint->getPosition()).Z();
  //cout << currentOffset << endl;

  m_IKChainPtr->setMode( RELATIVEMODE );
  //cout << m_error << endl;// << currentVector << endl << targetVector << endl;
  currentVector-=targetVector;
  m_error=currentVector;
  //cout << m_IKChainPtr->getName() << " errorCalculated" << endl;
}
Ejemplo n.º 4
0
//-----------------------------------------------------------------------
Frustum::Frustum(Vector3f pos,Vector3f target,Vector3f up):
	m_positionVector(pos),
	m_targetVector(target),
	m_upVector(up),
	m_FOVy(45.0f),
	m_nearDist(1),	
	m_farDist(10000),
	m_aspectRadio(1.333333333f),
	m_viewChanged(true),
	m_projChanged(true),
	m_frustumPlanesChanged(true)
{	
	m_targetVector.Normalize();
	m_upVector.Normalize();
	
	//Initialize two angles
	Vector3f targetVector(m_targetVector.x, 0.0f, m_targetVector.z);//horizontal target
	targetVector.Normalize();
	if(targetVector.z >= 0.0f)
	{
		if(targetVector.x >= 0.0f)
		{
			m_angleHorizontal = 360.0f - ToDegree(asin(targetVector.z));
		}
		else
		{
			m_angleHorizontal = 180.0f + ToDegree(asin(targetVector.z));
		}
	}
	else
	{
		if(targetVector.x >= 0.0f)
		{
			m_angleHorizontal = ToDegree(asin(-targetVector.z));
		}else
		{
			m_angleHorizontal = 90.0f + ToDegree(asin(-targetVector.z));
		}
	}
	m_angleVertical = -ToDegree(asin(m_targetVector.y));
}
Ejemplo n.º 5
0
    void ann::add(int argc, const t_atom *argv)
    {
        if (get_data_type() != data_type::LABELLED_CLASSIFICATION)
        {
            ml::add(argc, argv);
            return;
        }
        
        // work around a bug in GRT where class labels must be contigious
        if (argc < 2)
        {
            flext::error("invalid input length, must contain at least 2 values");
            return;
        }
        
        GRT::UINT numInputDimensions = classification_data.getNumDimensions();
        GRT::UINT numOutputDimensions = 1;
        GRT::UINT combinedVectorSize = numInputDimensions + numOutputDimensions;
        
        if ((unsigned)argc != combinedVectorSize)
        {
            numInputDimensions = argc - numOutputDimensions;
            
            if (numInputDimensions < 1)
            {
                flext::error(std::string("invalid input length, expected at least " + std::to_string(numOutputDimensions + 1)).c_str());
                return;
            }
            post("new input vector size, adjusting num_inputs to " + std::to_string(numInputDimensions));
            set_num_inputs(numInputDimensions);
        }
        
        GRT::VectorDouble inputVector(numInputDimensions);
        GRT::VectorDouble targetVector(numOutputDimensions);
        
        for (uint32_t index = 0; index < (unsigned)argc; ++index)
        {
            float value = GetAFloat(argv[index]);
            
            if (index < numOutputDimensions)
            {
                targetVector[index] = value;
            }
            else
            {
                inputVector[index - numOutputDimensions] = value;
            }
        }
        
        GRT::UINT label = get_index_for_class((GRT::UINT)targetVector[0]);
        
        assert(label > 0);
        
//        if ((double)label != targetVector[0])
//        {
//            flext::error("class label must be a positive integer");
//            return;
//        }
//
//        if (label == 0)
//        {
//            flext::error("class label must be non-zero");
//            return;
//        }
        
        classification_data.addSample(label, inputVector);
    }
bool LabelledRegressionData::loadDatasetFromFile(const string &filename){

	std::fstream file;
	file.open(filename.c_str(), std::ios::in);
	clear();

	if( !file.is_open() ){
        errorLog << "loadDatasetFromFile(const string &filename) - Failed to open file!" << endl;
		return false;
	}

	string word;

	//Check to make sure this is a file with the Training File Format
	file >> word;
	if(word != "GRT_LABELLED_REGRESSION_DATA_FILE_V1.0"){
        errorLog << "loadDatasetFromFile(const string &filename) - Unknown file header!" << endl;
		file.close();
		return false;
	}

    //Get the name of the dataset
	file >> word;
	if(word != "DatasetName:"){
        errorLog << "loadDatasetFromFile(const string &filename) - failed to find DatasetName!" << endl;
		file.close();
		return false;
	}
	file >> datasetName;

    file >> word;
	if(word != "InfoText:"){
        errorLog << "loadDatasetFromFile(const string &filename) - failed to find InfoText!" << endl;
		file.close();
		return false;
	}

    //Load the info text
    file >> word;
    infoText = "";
    while( word != "NumInputDimensions:" ){
        infoText += word + " ";
        file >> word;
    }

	//Get the number of input dimensions in the training data
	if(word != "NumInputDimensions:"){
        errorLog << "loadDatasetFromFile(const string &filename) - Failed to find NumInputDimensions!" << endl;
		file.close();
		return false;
	}
	file >> numInputDimensions;

	//Get the number of target dimensions in the training data
	file >> word;
	if(word != "NumTargetDimensions:"){
        errorLog << "loadDatasetFromFile(const string &filename) - Failed to find NumTargetDimensions!" << endl;
		file.close();
		return false;
	}
	file >> numTargetDimensions;

	//Get the total number of training examples in the training data
	file >> word;
	if(word != "TotalNumTrainingExamples:"){
        errorLog << "loadDatasetFromFile(const string &filename) - Failed to find TotalNumTrainingExamples!" << endl;
		file.close();
		return false;
	}
	file >> totalNumSamples;

    //Check if the dataset should be scaled using external ranges
	file >> word;
	if(word != "UseExternalRanges:"){
        errorLog << "loadDatasetFromFile(const string &filename) - failed to find DatasetName!" << endl;
		file.close();
		return false;
	}
    file >> useExternalRanges;

    //If we are using external ranges then load them
    if( useExternalRanges ){
        externalInputRanges.resize(numInputDimensions);
        externalTargetRanges.resize(numTargetDimensions);
        for(UINT i=0; i<externalInputRanges.size(); i++){
            file >> externalInputRanges[i].minValue;
            file >> externalInputRanges[i].maxValue;
        }
        for(UINT i=0; i<externalTargetRanges.size(); i++){
            file >> externalTargetRanges[i].minValue;
            file >> externalTargetRanges[i].maxValue;
        }
    }

	//Get the main training data
	file >> word;
	if(word != "LabelledRegressionData:"){
        errorLog << "loadDatasetFromFile(const string &filename) - Failed to find LabelledRegressionData!" << endl;
		file.close();
		return false;
	}

	VectorDouble inputVector(numInputDimensions);
	VectorDouble targetVector(numTargetDimensions);
	data.resize( totalNumSamples, LabelledRegressionSample(inputVector,targetVector) );

	for(UINT i=0; i<totalNumSamples; i++){
		//Read the input vector
		for(UINT j=0; j<numInputDimensions; j++){
			file >> inputVector[j];
		}
		for(UINT j=0; j<numTargetDimensions; j++){
			file >> targetVector[j];
		}
        data[i].set(inputVector, targetVector);
	}

	file.close();
	return true;
}
Ejemplo n.º 7
0
//--------------------------------------------------------------
void ofApp::update(){

    synapseStreamer.update();
    //spacecraft.setRotation(1, 270 + ofGetElapsedTimef() * 60, 0, 0, 1);
    
    if( synapseStreamer.getNewMessage() ){
        leftHand = synapseStreamer.getLeftHandJointBody();
        rightHand = synapseStreamer.getRightHandJointBody();
        
        //Update the graphs
        leftHandPlot.update( leftHand );
        rightHandPlot.update( rightHand );
    
        //Update the training mode if needed
        if( trainingModeActive ){
            
            //Check to see if the countdown timer has elapsed, if so then start the recording
            if( !recordTrainingData ){
                if( trainingTimer.timerReached() ){
                    recordTrainingData = true;
                    trainingTimer.start( RECORDING_TIME );
                }
            }else{
                //We should be recording the training data - check to see if we should stop the recording
                if( trainingTimer.timerReached() ){
                    trainingModeActive = false;
                    recordTrainingData = false;
                }
            }
                        
            if( recordTrainingData ){
                //Add the current sample to the training data
                VectorFloat trainingSample(6);
                trainingSample[0] = leftHand[0];
                trainingSample[1] = leftHand[1];
                trainingSample[2] = leftHand[2];
                trainingSample[3] = rightHand[0];
                trainingSample[4] = rightHand[1];
                trainingSample[5] = rightHand[2];

                VectorFloat targetVector(2);
                targetVector[0] = rollRotationAngle;
                targetVector[1] = pitchRotationAngle;
                
                if( !trainingData.addSample(trainingSample,targetVector) ){
                    infoText = "WARNING: Failed to add training sample to training data!";
                }
            }
        }
        
        //Update the prediction mode if active
        if( predictionModeActive ){
            VectorFloat inputVector(6);
            inputVector[0] = leftHand[0];
            inputVector[1] = leftHand[1];
            inputVector[2] = leftHand[2];
            inputVector[3] = rightHand[0];
            inputVector[4] = rightHand[1];
            inputVector[5] = rightHand[2];
            if( pipeline.predict( inputVector ) ){
                rollRotationAngle = pipeline.getRegressionData()[0];
                pitchRotationAngle = pipeline.getRegressionData()[1];
            }else{
                infoText = "ERROR: Failed to run prediction!";
            }
        }
        
    }
}