DLLEXPORT void XGBoosterUpdateInteract( void *handle, void *dtrain, const char *action ){ Booster *bst = static_cast<Booster*>(handle); DMatrix *dtr = static_cast<DMatrix*>(dtrain); bst->CheckInit(); dtr->CheckInit(); std::string act( action ); bst->UpdateInteract( act, *dtr ); }
DLLEXPORT void XGBoosterBoostOneIter( void *handle, void *dtrain, float *grad, float *hess, size_t len, int bst_group ){ Booster *bst = static_cast<Booster*>(handle); DMatrix *dtr = static_cast<DMatrix*>(dtrain); bst->CheckInit(); dtr->CheckInit(); bst->BoostOneIter( *dtr, grad, hess, len, bst_group ); }
// Prediction // Accepts a single integral image/channel, thus only to be used for testing // Assumes that predPtr is already allocated, of same size as imgPtr DLL_EXPORT void predictWithChannel( void *modelPtr, ImagePixelType *imgPtr, IntegralImagePixelType *chImgPtr, int width, int height, int depth, PredictionPixelType *predPtr ) { Matrix3D<PredictionPixelType> predMatrix; predMatrix.fromSharedData( predPtr, width, height, depth ); // create roi for image, no GT available ROIData roi; roi.init( imgPtr, 0, 0, 0, width, height, depth ); // get the precomputed integral images ROIData::IntegralImageType ii; ii.fromSharedData(chImgPtr, width, height, depth); roi.addII( ii.internalImage().data() ); MultipleROIData allROIs; allROIs.add( shared_ptr_nodelete(ROIData, &roi) ); Booster adaboost; adaboost.setModel( *((BoosterModel *) modelPtr) ); adaboost.predict<false>( allROIs, &predMatrix ); }
// input: multiple imgPtr, gtPtr (arrays of pointers) // multiple img sizes // returns a BoosterModel * // -- BEWARE: this function is a mix of dirty tricks right now DLL_EXPORT void * trainWithChannels( ImagePixelType **imgPtr, void **evecPtr, GTPixelType **gtPtr, int *width, int *height, int *depth, int numStacks, IntegralImagePixelType **chImgPtr, int numChannels, double zAnisotropyFactor, int numStumps, int gtNegativeLabel, int gtPositiveLabel, int debugOutput ) { BoosterModel *modelPtr = 0; try { std::unique_ptr<ROIData[]> rois(new ROIData[numStacks]); // TODO: remove MultipleROIData allROIs; std::unique_ptr<ROIData::IntegralImageType[]> ii(new ROIData::IntegralImageType[numStacks*numChannels]); // TODO: remove for (int i=0; i < numStacks; i++) { rois[i].setGTNegativeSampleLabel(gtNegativeLabel); rois[i].setGTPositiveSampleLabel(gtPositiveLabel); rois[i].init( imgPtr[i], gtPtr[i], 0, 0, width[i], height[i], depth[i], zAnisotropyFactor, 0.0, (const ROIData::RotationMatrixType *) evecPtr[i] ); for (int ch=0; ch < numChannels; ch++) { ii[i*numChannels+ch].fromSharedData(chImgPtr[i*numChannels+ch], width[i], height[i], depth[i]); rois[i].addII( ii[i*numChannels+ch].internalImage().data() ); } allROIs.add( shared_ptr_nodelete(ROIData, &rois[i]) ); } BoosterInputData bdata; bdata.init( shared_ptr_nodelete(MultipleROIData, &allROIs) ); bdata.showInfo(); Booster adaboost; adaboost.setShowDebugInfo( debugOutput != 0 ); adaboost.train( bdata, numStumps ); // create by copying modelPtr = new BoosterModel( adaboost.model() ); } catch( std::exception &e ) { printf("Error training: %s\n", e.what()); delete modelPtr; return 0; } return modelPtr; }
void XGBoosterUpdateOneIter(void *handle, int iter, void *dtrain) { Booster *bst = static_cast<Booster*>(handle); DataMatrix *dtr = static_cast<DataMatrix*>(dtrain); bst->CheckInitModel(); bst->CheckInit(dtr); bst->UpdateOneIter(iter, *dtr); }
int XGBoosterSaveModel(BoosterHandle handle, const char *fname) { API_BEGIN(); Booster *bst = static_cast<Booster*>(handle); bst->CheckInitModel(); bst->SaveModel(fname, false); API_END(); }
void XGBoosterBoostOneIter(void *handle, void *dtrain, float *grad, float *hess, bst_ulong len) { Booster *bst = static_cast<Booster*>(handle); DataMatrix *dtr = static_cast<DataMatrix*>(dtrain); bst->CheckInitModel(); bst->CheckInit(dtr); bst->BoostOneIter(*dtr, grad, hess, len); }
// Prediction for a single ROI // Accepts an arbitrary number of integral images/channels. // Assumes that predPtr is already allocated, of same size as imgPtr // Returns 0 if ok DLL_EXPORT int predictWithChannels( void *modelPtr, ImagePixelType *imgPtr, void *eigVecImgPtr, int width, int height, int depth, IntegralImagePixelType **chImgPtr, int numChannels, double zAnisotropyFactor, int useEarlyStopping, int useROI, int x1, int y1, int z1, int x2, int y2, int z2, // used only if useROI != 0 PredictionPixelType *predPtr ) { Matrix3D<PredictionPixelType> predMatrix; predMatrix.fromSharedData( predPtr, width, height, depth ); // create roi for image, no GT available ROIData roi; roi.init( imgPtr, 0, 0, 0, width, height, depth, zAnisotropyFactor, 0.0, (const ROIData::RotationMatrixType *) eigVecImgPtr ); std::unique_ptr<ROIData::IntegralImageType[]> ii(new ROIData::IntegralImageType[numChannels]); // TODO: remove for (int ch=0; ch < numChannels; ch++) { ii[ch].fromSharedData(chImgPtr[ch], width, height, depth); roi.addII( ii[ch].internalImage().data() ); } MultipleROIData allROIs; allROIs.add( shared_ptr_nodelete(ROIData, &roi) ); try { Booster adaboost; adaboost.setModel( *((BoosterModel *) modelPtr) ); if(useEarlyStopping != 0) { if (useROI) { ROICoordinates subROI; subROI.x1 = x1; subROI.y1 = y1; subROI.z1 = z1; subROI.x2 = x2; subROI.y2 = y2; subROI.z2 = z2; adaboost.predictWithFeatureOrdering<true>( allROIs, &predMatrix, 0, IIBOOST_NUM_THREADS, &subROI ); } else adaboost.predictWithFeatureOrdering<true>( allROIs, &predMatrix ); } else adaboost.predictWithFeatureOrdering<false>( allROIs, &predMatrix ); } catch( std::exception &e ) { printf("Error in prediction: %s\n", e.what()); return -1; } return 0; }
int XGBoosterUpdateOneIter(BoosterHandle handle, int iter, DMatrixHandle dtrain) { API_BEGIN(); Booster *bst = static_cast<Booster*>(handle); DataMatrix *dtr = static_cast<DataMatrix*>(dtrain); bst->CheckInitModel(); bst->CheckInit(dtr); bst->UpdateOneIter(iter, *dtr); API_END(); }
DLLEXPORT void XGBoosterEvalOneIter( void *handle, int iter, void *dmats[], const char *evnames[], size_t len ){ Booster *bst = static_cast<Booster*>(handle); bst->CheckInit(); std::vector<std::string> names; std::vector<const xgboost::regrank::DMatrix*> mats; for( size_t i = 0; i < len; ++i ){ mats.push_back( static_cast<DMatrix*>(dmats[i]) ); names.push_back( std::string( evnames[i]) ); } bst->EvalOneIter( iter, mats, names, stderr ); }
const char* XGBoosterEvalOneIter(void *handle, int iter, void *dmats[], const char *evnames[], bst_ulong len) { Booster *bst = static_cast<Booster*>(handle); std::vector<std::string> names; std::vector<const DataMatrix*> mats; for (bst_ulong i = 0; i < len; ++i) { mats.push_back(static_cast<DataMatrix*>(dmats[i])); names.push_back(std::string(evnames[i])); } bst->CheckInitModel(); bst->eval_str = bst->EvalOneIter(iter, mats, names); return bst->eval_str.c_str(); }
int XGBoosterBoostOneIter(BoosterHandle handle, DMatrixHandle dtrain, float *grad, float *hess, bst_ulong len) { API_BEGIN(); Booster *bst = static_cast<Booster*>(handle); DataMatrix *dtr = static_cast<DataMatrix*>(dtrain); bst->CheckInitModel(); bst->CheckInit(dtr); bst->BoostOneIter(*dtr, grad, hess, len); API_END(); }
// input: multiple imgPtr, gtPtr (arrays of pointers) // multiple img sizes // returns a BoosterModel * // -- BEWARE: this function is a mix of dirty tricks right now DLL_EXPORT void * train( ImagePixelType **imgPtr, GTPixelType **gtPtr, int *width, int *height, int *depth, int numStacks, int numStumps, int debugOutput ) { BoosterModel *modelPtr = 0; try { std::unique_ptr<ROIData[]> rois(new ROIData[numStacks]); // TODO: remove std::unique_ptr<ROIData::IntegralImageType[]> ii(new ROIData::IntegralImageType[numStacks]); // TODO: remove MultipleROIData allROIs; for (int i=0; i < numStacks; i++) { rois[i].init( imgPtr[i], gtPtr[i], 0, 0, width[i], height[i], depth[i] ); // raw image to integral image // TODO: this should be removed and passed directly to train() ii[i].compute( rois[i].rawImage ); rois[i].addII( ii[i].internalImage().data() ); allROIs.add( shared_ptr_nodelete(ROIData, &rois[i]) ); } BoosterInputData bdata; bdata.init( shared_ptr_nodelete(MultipleROIData, &allROIs) ); bdata.showInfo(); Booster adaboost; adaboost.setShowDebugInfo( debugOutput != 0 ); adaboost.train( bdata, numStumps ); // create by copying modelPtr = new BoosterModel( adaboost.model() ); } catch( std::exception &e ) { printf("Error training: %s\n", e.what()); delete modelPtr; return 0; } return modelPtr; }
DLL_EXPORT int predictIndividualWeakLearnersWithChannels( void *modelPtr, ImagePixelType *imgPtr, void *eigVecImgPtr, int width, int height, int depth, IntegralImagePixelType **chImgPtr, int numChannels, double zAnisotropyFactor, WLPredictionPixelType **predPtr) { BoosterModel* model = static_cast<BoosterModel*>(modelPtr); int numWL = model->size(); Matrix3D<WLPredictionPixelType> predMatrix[numWL]; // TODO: remove for(int i=0; i < numWL; ++i) predMatrix[i].fromSharedData(predPtr[i], width, height, depth); // create roi for image, no GT available ROIData roi; roi.init( imgPtr, 0, 0, 0, width, height, depth, zAnisotropyFactor, 0.0, (const ROIData::RotationMatrixType *) eigVecImgPtr ); ROIData::IntegralImageType ii[numChannels]; // TODO: remove for (int ch=0; ch < numChannels; ch++) { ii[ch].fromSharedData(chImgPtr[ch], width, height, depth); roi.addII( ii[ch].internalImage().data() ); } MultipleROIData allROIs; allROIs.add( shared_ptr_nodelete(ROIData, &roi) ); try { Booster adaboost; adaboost.setModel(*model); adaboost.predictIndividualWeakLearners(allROIs, predMatrix); } catch( std::exception &e ) { printf("Error in prediction: %s\n", e.what()); return -1; } return 0; }
int XGBoosterEvalOneIter(BoosterHandle handle, int iter, DMatrixHandle dmats[], const char *evnames[], bst_ulong len, const char **out_str) { API_BEGIN(); Booster *bst = static_cast<Booster*>(handle); std::vector<std::string> names; std::vector<const DataMatrix*> mats; for (bst_ulong i = 0; i < len; ++i) { mats.push_back(static_cast<DataMatrix*>(dmats[i])); names.push_back(std::string(evnames[i])); } bst->CheckInitModel(); bst->eval_str = bst->EvalOneIter(iter, mats, names); *out_str = bst->eval_str.c_str(); API_END(); }
DLLEXPORT void XGBoosterUpdateOneIter( void *handle, void *dtrain ){ Booster *bst = static_cast<Booster*>(handle); DMatrix *dtr = static_cast<DMatrix*>(dtrain); bst->CheckInit(); dtr->CheckInit(); bst->UpdateOneIter( *dtr ); }