123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- #include "Example.h"
- #ifdef NICE_USELIB_OPENMP
- #include <omp.h>
- #endif
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- Example::Example()
- {
- weight = 0.0;
- height = 0;
- depth = 0;
- width = 0;
- x = y = z = 0;
- vec = NULL;
- svec = NULL;
- ce = NULL;
- position = 0;
- }
- Example::~Example ()
- {
- }
- Example::Example ( CachedExample *_ce )
- {
- if ( _ce->getNumImages() > 1 )
- {
- _ce->getImageSize3 ( width, height, depth );
- }
- else
- {
- _ce->getImageSize( width, height );
- depth = 1;
- }
- if ( width % 2 == 0 ) width--;
- if ( height % 2 == 0 ) height--;
- if ( depth % 2 == 0 ) depth--;
- x = width/2;
- y = height/2;
- z = depth/2;
- ce = _ce;
- vec = NULL;
- svec = NULL;
- weight = 1.0;
- position = 0;
- }
- Example::Example ( CachedExample *_ce,
- int _x,
- int _y,
- double _weight )
- {
- ce = _ce;
- x = _x;
- y = _y;
- z = 0;
- width = 0;
- height = 0;
- depth = 0;
- vec = NULL;
- svec = NULL;
- weight = _weight;
- position = 0;
- }
- Example::Example ( CachedExample *_ce,
- int _x,
- int _y,
- int _z,
- double _weight )
- {
- ce = _ce;
- x = _x;
- y = _y;
- z = _z;
- width = 0;
- height = 0;
- depth = 0;
- vec = NULL;
- svec = NULL;
- weight = _weight;
- position = 0;
- }
- Example::Example ( CachedExample *_ce,
- int _x,
- int _y,
- int _width,
- int _height,
- double _weight )
- {
- ce = _ce;
- x = _x;
- y = _y;
- z = 0;
- width = _width;
- height = _height;
- depth = 1;
- assert ( (width > 0) && (height > 0) );
- vec = NULL;
- svec = NULL;
- weight = _weight;
- position = 0;
- }
- Example::Example ( CachedExample *_ce,
- int _x,
- int _y,
- int _z,
- int _width,
- int _height,
- int _depth,
- double _weight )
- {
- ce = _ce;
- x = _x;
- y = _y;
- z = _z;
- width = _width;
- height = _height;
- depth = _depth;
- assert ( (width > 0) && (height > 0) && assert(depth > 0) );
- vec = NULL;
- svec = NULL;
- weight = _weight;
- position = 0;
- }
- Example::Example ( NICE::Vector *_vec,
- double _weight )
- {
- x = y = z = 0;
- width = height = depth = 0;
- ce = NULL;
- vec = _vec;
- svec = NULL;
- weight = _weight;
- position = 0;
- }
- void Example::clean ()
- {
- if ( ce != NULL )
- {
- delete ce;
- ce = NULL;
- }
- if ( vec != NULL )
- {
- delete vec;
- vec = NULL;
- }
- if ( svec != NULL )
- {
- delete svec;
- svec = NULL;
- }
- }
- Example::Example ( const Example &ex)
- {
- copy(ex);
- }
- void Example::copy ( const Example &ex)
- {
- vec = ex.vec;
- svec = ex.svec;
- weight = ex.weight;
- position = ex.position;
- width = ex.width;
- height = ex.height;
- depth = ex.depth;
- x = ex.x;
- y = ex.y;
- z = ex.z;
- ce = ex.ce;
- scale = ex.scale;
- }
- void Example::restore (istream & is, int format)
- {
- if ( format == 1 )
- {
- is >> weight;
- is >> x;
- is >> y;
- is >> z;
- is >> width;
- is >> height;
- is >> depth;
- is >> position;
- }
- else
- {
- is >> weight;
- is >> x;
- is >> y;
- is >> width;
- is >> height;
- is >> position;
- depth = 1;
- z = 0;
- }
- int tmp;
- is >> tmp;
- if(tmp == 1)
- {
- svec = new SparseVector();
- svec->restore(is);
- }
- else
- svec = NULL;
- is >> tmp;
- if(tmp >= 0 )
- {
- vec = new Vector(tmp);
- for(int i = 0; i < tmp; i++)
- {
- is >> vec[i];
- }
- }
- else
- vec = NULL;
- }
- void Example::store (ostream & os, int format) const
- {
- if ( format == 1 )
- os << weight << " " << x << " " << y << " " << z << " " << width << " " << height << " " << depth << " " << position << endl;
- else
- os << weight << " " << x << " " << y << " " << width << " " << height << " " << position << endl;
- if( svec == NULL )
- os << 0 << endl;
- else
- {
- os << 1 << endl;
- svec->store(os);
- }
- if(vec == NULL)
- os << -1 << endl;
- else
- {
- os << vec->size() << endl;
- for(int i = 0; i < (int)vec->size(); i++)
- {
- os << vec[i] << " ";
- }
- }
- }
- void Examples::clean ()
- {
- for ( iterator i = begin(); i != end(); i++ )
- {
- Example & example = i->second;
- example.clean();
- }
- clear();
- }
- bool Examples::wrapExamplesAroundFeatureMatrix(const Matrix &p_MatFeaturesColumWiseSamples, const Vector &p_VecLabels, Examples &p_Examples)
- {
- size_t t_iNumSamples = p_MatFeaturesColumWiseSamples.cols();
- size_t t_iNumFeatures = p_MatFeaturesColumWiseSamples.rows();
- if(p_VecLabels.size() != t_iNumSamples) // for every columnwise sample there need to be a label
- return false;
- p_Examples.reserve( t_iNumSamples );
- const double *pDataPtr = p_MatFeaturesColumWiseSamples.getDataPointer();
- for (size_t i = 0; i < t_iNumSamples; i++, pDataPtr+= t_iNumFeatures )
- {
- NICE::Vector *t_pVecTrainData = new NICE::Vector( pDataPtr , t_iNumFeatures);
- double t_fWeight = 1.0f;
- OBJREC::Example t_Example(t_pVecTrainData, t_fWeight);
- p_Examples.push_back( std::pair<int, OBJREC::Example>( (int)p_VecLabels[i], t_Example ) );
- }
- return true;
- }
- bool Examples::wrapExamplesAroundFeatureMatrix(const Matrix &p_MatFeaturesColumWiseSamples, const VectorT<int> &p_VecLabels, Examples &p_Examples)
- {
- size_t t_iNumSamples = p_MatFeaturesColumWiseSamples.cols();
- size_t t_iNumFeatures = p_MatFeaturesColumWiseSamples.rows();
- if(p_VecLabels.size() != t_iNumSamples) // for every columnwise sample there need to be a label
- return false;
- p_Examples.resize( t_iNumSamples );
- const double *pDataPtr = p_MatFeaturesColumWiseSamples.getDataPointer();
- #ifdef NICE_USELIB_OPENMP
- #pragma omp parallel for default(none) shared(p_VecLabels, p_Examples, t_iNumFeatures, t_iNumSamples, pDataPtr)
- #endif
- for (size_t i = 0; i < t_iNumSamples; i++)
- {
- const double *pDataIteration = pDataPtr + (i * t_iNumFeatures);
- NICE::Vector *t_pVecTrainData = new NICE::Vector( (double*) pDataIteration , t_iNumFeatures, VectorBase::external);
- // OBJREC::Example t_Example(t_pVecTrainData, t_fWeight);
- p_Examples[i] = std::pair<int, OBJREC::Example>( p_VecLabels[i], OBJREC::Example(t_pVecTrainData, 1.0) ) ;
- }
- return true;
- }
|