bool RandomForests::combineModels( const RandomForests &forest ){ if( !getTrained() ){ errorLog << "combineModels( const RandomForests &forest ) - This instance has not been trained!" << endl; return false; } if( !forest.getTrained() ){ errorLog << "combineModels( const RandomForests &forest ) - This external forest instance has not been trained!" << endl; return false; } if( this->getNumInputDimensions() != forest.getNumInputDimensions() ) { errorLog << "combineModels( const RandomForests &forest ) - The number of input dimensions of the external forest ("; errorLog << forest.getNumInputDimensions() << ") does not match the number of input dimensions of this instance ("; errorLog << this->getNumInputDimensions() << ")!" << endl; return false; } //Add the trees in the other forest to this model DecisionTreeNode *node; for(UINT i=0; i<forest.getForestSize(); i++){ node = forest.getTree(i); if( node ){ this->forest.push_back( node->deepCopy() ); forestSize++; } } return true; }
bool RandomForests::setDecisionTreeNode( const DecisionTreeNode &node ){ if( decisionTreeNode != NULL ){ delete decisionTreeNode; decisionTreeNode = NULL; } this->decisionTreeNode = node.deepCopy(); return true; }
RandomForests::RandomForests(const DecisionTreeNode &decisionTreeNode,const UINT forestSize,const UINT numRandomSplits,const UINT minNumSamplesPerNode,const UINT maxDepth,const UINT trainingMode,const bool removeFeaturesAtEachSpilt,const bool useScaling,const Float bootstrappedDatasetWeight) { this->decisionTreeNode = decisionTreeNode.deepCopy(); this->forestSize = forestSize; this->numRandomSplits = numRandomSplits; this->minNumSamplesPerNode = minNumSamplesPerNode; this->maxDepth = maxDepth; this->trainingMode = trainingMode; this->removeFeaturesAtEachSpilt = removeFeaturesAtEachSpilt; this->useScaling = useScaling; this->bootstrappedDatasetWeight = bootstrappedDatasetWeight; classType = "RandomForests"; classifierType = classType; classifierMode = STANDARD_CLASSIFIER_MODE; useNullRejection = false; supportsNullRejection = false; useValidationSet = true; validationSetSize = 20; debugLog.setProceedingText("[DEBUG RandomForests]"); errorLog.setProceedingText("[ERROR RandomForests]"); trainingLog.setProceedingText("[TRAINING RandomForests]"); warningLog.setProceedingText("[WARNING RandomForests]"); }
bool RandomForests::loadModelFromFile(fstream &file){ clear(); if(!file.is_open()) { errorLog << "loadModelFromFile(string filename) - Could not open file to load model" << endl; return false; } std::string word; std::string treeNodeType; file >> word; //Find the file type header if(word != "GRT_RANDOM_FOREST_MODEL_FILE_V1.0"){ errorLog << "loadModelFromFile(string filename) - Could not find Model File Header" << endl; return false; } //Load the base settings from the file if( !Classifier::loadBaseSettingsFromFile(file) ){ errorLog << "loadModelFromFile(string filename) - Failed to load base settings from file!" << endl; return false; } file >> word; if(word != "DecisionTreeNodeType:"){ Classifier::errorLog << "loadModelFromFile(string filename) - Could not find the DecisionTreeNodeType!" << endl; return false; } file >> treeNodeType; if( treeNodeType != "NULL" ){ decisionTreeNode = dynamic_cast< DecisionTreeNode* >( DecisionTreeNode::createInstanceFromString( treeNodeType ) ); if( decisionTreeNode == NULL ){ Classifier::errorLog << "loadModelFromFile(string filename) - Could not create new DecisionTreeNode from type: " << treeNodeType << endl; return false; } if( !decisionTreeNode->loadFromFile( file ) ){ Classifier::errorLog <<"loadModelFromFile(fstream &file) - Failed to load decisionTreeNode settings from file!" << endl; return false; } }else{ Classifier::errorLog <<"loadModelFromFile(fstream &file) - Failed to load decisionTreeNode! DecisionTreeNodeType is NULL!" << endl; return false; } file >> word; if(word != "ForestSize:"){ errorLog << "loadModelFromFile(string filename) - Could not find the ForestSize!" << endl; return false; } file >> forestSize; file >> word; if(word != "NumSplittingSteps:"){ errorLog << "loadModelFromFile(string filename) - Could not find the NumSplittingSteps!" << endl; return false; } file >> numRandomSplits; file >> word; if(word != "MinNumSamplesPerNode:"){ errorLog << "loadModelFromFile(string filename) - Could not find the MinNumSamplesPerNode!" << endl; return false; } file >> minNumSamplesPerNode; file >> word; if(word != "MaxDepth:"){ errorLog << "loadModelFromFile(string filename) - Could not find the MaxDepth!" << endl; return false; } file >> maxDepth; file >> word; if(word != "RemoveFeaturesAtEachSpilt:"){ errorLog << "loadModelFromFile(string filename) - Could not find the RemoveFeaturesAtEachSpilt!" << endl; return false; } file >> removeFeaturesAtEachSpilt; file >> word; if(word != "TrainingMode:"){ errorLog << "loadModelFromFile(string filename) - Could not find the TrainingMode!" << endl; return false; } file >> trainingMode; file >> word; if(word != "ForestBuilt:"){ errorLog << "loadModelFromFile(string filename) - Could not find the ForestBuilt!" << endl; return false; } file >> trained; if( trained ){ //Find the forest header file >> word; if(word != "Forest:"){ errorLog << "loadModelFromFile(string filename) - Could not find the Forest!" << endl; return false; } //Load each tree UINT treeIndex; forest.reserve( forestSize ); for(UINT i=0; i<forestSize; i++){ file >> word; if(word != "Tree:"){ errorLog << "loadModelFromFile(string filename) - Could not find the Tree Header!" << endl; cout << "WORD: " << word << endl; cout << "Tree i: " << i << endl; return false; } file >> treeIndex; if( treeIndex != i+1 ){ errorLog << "loadModelFromFile(string filename) - Incorrect tree index: " << treeIndex << endl; return false; } file >> word; if(word != "TreeNodeType:"){ errorLog << "loadModelFromFile(string filename) - Could not find the TreeNodeType!" << endl; cout << "WORD: " << word << endl; cout << "i: " << i << endl; return false; } file >> treeNodeType; //Create a new DTree DecisionTreeNode *tree = dynamic_cast< DecisionTreeNode* >( DecisionTreeNode::createInstanceFromString( treeNodeType ) ); if( tree == NULL ){ errorLog << "loadModelFromFile(fstream &file) - Failed to create new Tree!" << endl; return false; } //Load the tree from the file tree->setParent( NULL ); if( !tree->loadFromFile( file ) ){ errorLog << "loadModelFromFile(fstream &file) - Failed to load tree from file!" << endl; return false; } //Add the tree to the forest forest.push_back( tree ); } } return true; }
void DecisionTree::setAntTreeAfraid(){ DecisionTreeNode defaultNode; m_rootNode = new DecisionTreeNode(1, &defaultNode, "root -> State:VIEW"); DecisionTreeNode* lView = new DecisionTreeNode(2, &defaultNode, "lView -> T:HOME"); DecisionTreeNode* rView = new DecisionTreeNode(3, &defaultNode, "rView -> State:HUNGER"); DecisionTreeNode* lHunger = new DecisionTreeNode(4, &defaultNode, "lHunger -> T:FOOD"); DecisionTreeNode* rHunger = new DecisionTreeNode(5, &defaultNode, "rHunger -> T:HOME"); m_rootNode->setStateID(States::VIEW); m_rootNode->setLeftNode(lView); lView->setCondition(true); lView->setLeaf(true); lView->setTarget(TreeOutput::HOME); m_rootNode->setRightNode(rView); rView->setCondition(false); rView->setStateID(States::HUNGER); rView->setLeftNode(lHunger); lHunger->setCondition(true); lHunger->setLeaf(true); lHunger->setTarget(TreeOutput::FOOD); rView->setRightNode(rHunger); rHunger->setCondition(false); rHunger->setLeaf(true); rHunger->setTarget(TreeOutput::HOME); m_currentNode = m_rootNode; }
void DecisionTree::setAntTreeAggressiv(){ DecisionTreeNode defaultNode; m_rootNode = new DecisionTreeNode(1, &defaultNode); DecisionTreeNode* lView = new DecisionTreeNode(2, &defaultNode); DecisionTreeNode* rView = new DecisionTreeNode(3, &defaultNode); DecisionTreeNode* lHunger = new DecisionTreeNode(4, &defaultNode); DecisionTreeNode* rHunger = new DecisionTreeNode(5, &defaultNode); m_rootNode->setStateID(States::VIEW); m_rootNode->setLeftNode(lView); lView->setCondition(true); lView->setLeaf(true); lView->setTarget(TreeOutput::PLAYER); m_rootNode->setRightNode(rView); rView->setCondition(false); rView->setStateID(States::HUNGER); rView->setLeftNode(lHunger); lHunger->setCondition(true); lHunger->setLeaf(true); lHunger->setTarget(TreeOutput::FOOD); rView->setRightNode(rHunger); rHunger->setCondition(false); rHunger->setLeaf(true); rHunger->setTarget(TreeOutput::PATROL); m_currentNode = m_rootNode; }
bool RandomForests::loadModelFromFile(fstream &file){ clear(); if(!file.is_open()) { errorLog << "loadModelFromFile(string filename) - Could not open file to load model" << endl; return false; } std::string word; //Find the file type header file >> word; if(word != "GRT_RANDOM_FOREST_MODEL_FILE_V1.0"){ errorLog << "loadModelFromFile(string filename) - Could not find Model File Header" << endl; return false; } file >> word; if(word != "NumFeatures:"){ errorLog << "loadModelFromFile(string filename) - Could not find NumFeatures!" << endl; return false; } file >> numInputDimensions; file >> word; if(word != "NumClasses:"){ errorLog << "loadModelFromFile(string filename) - Could not find NumClasses!" << endl; return false; } file >> numClasses; file >> word; if(word != "UseScaling:"){ errorLog << "loadModelFromFile(string filename) - Could not find UseScaling!" << endl; return false; } file >> useScaling; file >> word; if(word != "UseNullRejection:"){ errorLog << "loadModelFromFile(string filename) - Could not find UseNullRejection!" << endl; return false; } file >> useNullRejection; ///Read the ranges if needed if( useScaling ){ //Resize the ranges buffer ranges.resize(numInputDimensions); file >> word; if(word != "Ranges:"){ errorLog << "loadModelFromFile(string filename) - Could not find the Ranges!" << endl; return false; } for(UINT n=0; n<ranges.size(); n++){ file >> ranges[n].minValue; file >> ranges[n].maxValue; } } file >> word; if(word != "ForestSize:"){ errorLog << "loadModelFromFile(string filename) - Could not find the ForestSize!" << endl; return false; } file >> forestSize; file >> word; if(word != "NumSplittingSteps:"){ errorLog << "loadModelFromFile(string filename) - Could not find the NumSplittingSteps!" << endl; return false; } file >> numRandomSplits; file >> word; if(word != "MinNumSamplesPerNode:"){ errorLog << "loadModelFromFile(string filename) - Could not find the MinNumSamplesPerNode!" << endl; return false; } file >> minNumSamplesPerNode; file >> word; if(word != "MaxDepth:"){ errorLog << "loadModelFromFile(string filename) - Could not find the MaxDepth!" << endl; return false; } file >> maxDepth; file >> word; if(word != "ForestBuilt:"){ errorLog << "loadModelFromFile(string filename) - Could not find the ForestBuilt!" << endl; return false; } file >> trained; if( trained ){ file >> word; if(word != "Forest:"){ errorLog << "loadModelFromFile(string filename) - Could not find the Forest!" << endl; return false; } UINT treeIndex; for(UINT i=0; i<forestSize; i++){ file >> word; if(word != "Tree:"){ errorLog << "loadModelFromFile(string filename) - Could not find the Tree Header!" << endl; return false; } file >> treeIndex; if( treeIndex != i+1 ){ errorLog << "loadModelFromFile(string filename) - Incorrect tree index: " << treeIndex << endl; return false; } //Create a new DTree DecisionTreeNode *tree = new DecisionTreeNode; if( tree == NULL ){ errorLog << "loadModelFromFile(fstream &file) - Failed to create new Tree!" << endl; return false; } tree->setParent( NULL ); if( !tree->loadFromFile( file ) ){ errorLog << "loadModelFromFile(fstream &file) - Failed to load tree from file!" << endl; return false; } //Add the tree to the forest forest.push_back( tree ); } } return true; }
bool computeFeatureWeights( CommandLineParser &parser ){ infoLog << "Computing feature weights..." << endl; string resultsFilename = ""; string modelFilename = ""; bool combineWeights = false; //Get the model filename if( !parser.get("model-filename",modelFilename) ){ errorLog << "Failed to parse filename from command line! You can set the model filename using the --model." << endl; printUsage(); return false; } //Get the results filename if( !parser.get("filename",resultsFilename) ){ errorLog << "Failed to parse results filename from command line! You can set the results filename using the -f." << endl; printUsage(); return false; } //Get the results filename parser.get("combine-weights",combineWeights); //Load the model GestureRecognitionPipeline pipeline; if( !pipeline.load( modelFilename ) ){ errorLog << "Failed to load model from file: " << modelFilename << endl; printUsage(); return false; } //Make sure the pipeline contains a random forest model and that it is trained RandomForests *forest = pipeline.getClassifier< RandomForests >(); if( !forest ){ errorLog << "Model loaded, but the pipeline does not contain a RandomForests classifier!" << endl; printUsage(); return false; } if( !forest->getTrained() ){ errorLog << "Model loaded, but the RandomForests classifier is not trained!" << endl; printUsage(); return false; } //Compute the feature weights if( combineWeights ){ VectorFloat weights = forest->getFeatureWeights(); if( weights.getSize() == 0 ){ errorLog << "Failed to compute feature weights!" << endl; printUsage(); return false; } //Save the results to a file fstream file; file.open( resultsFilename.c_str(), fstream::out ); const unsigned int N = weights.getSize(); for(unsigned int i=0; i<N; i++){ file << weights[i] << endl; } file.close(); }else{ double norm = 0.0; const unsigned int K = forest->getForestSize(); const unsigned int N = forest->getNumInputDimensions(); VectorFloat tmp( N, 0.0 ); MatrixDouble weights(K,N); for(unsigned int i=0; i<K; i++){ DecisionTreeNode *tree = forest->getTree(i); tree->computeFeatureWeights( tmp ); norm = 1.0 / Util::sum( tmp ); for(unsigned int j=0; j<N; j++){ tmp[j] *= norm; weights[i][j] = tmp[j]; tmp[j] = 0; } } //Save the results to a file weights.save( resultsFilename ); } return true; }