예제 #1
0
	/**@brief A function to start a runLog in the named directory
	 * @param dirName The name of the directory to start the runLog, runLog name
	 *will be runLog_[NAME_OF_PROGRAM]
	 *
	 */
	void startARunLog(const std::string &dirName) {
		rLog_.setFilenameAndOpen(
				files::make_path(dirName,"runLog_"
						+ replaceString(replaceString(commands_.getProgramName(), "./", ""),
								" ", "-")+ "_" + getCurrentDate() + ".txt").string(), timer_.start_);
		rLog_.startRunLog(commands_);
	}
예제 #2
0
void  FeatureFileIO::SaveFeatureFile (const KKStr&          _fileName, 
                                      FeatureNumListConst&  _selFeatures,
                                      FeatureVectorList&    _examples,
                                      kkuint32&             _numExamplesWritten,
                                      VolConstBool&         _cancelFlag,
                                      bool&                 _successful,
                                      RunLog&               _log
                                     )
{
  _log.Level (10) << "FeatureFileIO::SaveFeatureFile - File[" << _fileName << "]." << endl;

  ofstream  out (_fileName.Str ());
  if  (!out.is_open())
  {
    _log.Level (-1) << "***ERROR***, SaveFeatureFile, Opening File[" << _fileName << "]" << endl;
    _successful = false;
  }

  out.precision (9);

  KKStr  errorMessage;
  SaveFile (_examples, _fileName, _selFeatures, out, _numExamplesWritten, _cancelFlag, _successful, errorMessage, _log);

  out.close ();
}  /* SaveFeatureFile */
예제 #3
0
void   ModelUsfCasCor::ProbabilitiesByClass (FeatureVectorPtr    _example,
                                             const MLClassList&  _mlClasses,
                                             double*             _probabilities,
                                             RunLog&             _log
                                            )
{
  if  (!usfCasCorClassifier)
  {
    KKStr errMsg = "ModelUsfCasCor::ProbabilitiesByClass   ***ERROR***      (usfCasCorClassifier == NULL)";
    _log.Level (-1) << endl << endl << errMsg << endl << endl;
    throw KKException (errMsg);
  }

  VectorFloat  probabilities;

  MLClassPtr  pc1 = NULL;
  MLClassPtr  pc2 = NULL;
  MLClassPtr  kc  = NULL;

  float  pc1p = 0.0f;
  float  pc2p = 0.0f;
  float  kcp  = 0.0f;

  bool  newExampleCreated = false;
  FeatureVectorPtr  encodedExample = PrepExampleForPrediction (_example, newExampleCreated);
  usfCasCorClassifier->PredictConfidences (encodedExample,
                                           kc,
                                           pc1, pc1p, 
                                           pc2, pc2p,
                                           kcp,
                                           _mlClasses,
                                           probabilities
                                          );
  if  (newExampleCreated)
  {
    delete encodedExample;
    encodedExample = NULL;
  }

  if  (_mlClasses.size () != probabilities.size ())
  {
    _log.Level (-1) << endl << "ModelUsfCasCor::ProbabilitiesByClass   ***ERROR***"  << endl
      << "\"_mlClasses.size () != probabilities.size ()\"   This should not ever be able to happen." << endl
      << endl;
    for  (int x = 0;  x < _mlClasses.QueueSize ();  ++x)
    {
      _probabilities[x] = 0.0;
    }
  }
  else
  {
    for  (kkuint32 x = 0;  x < probabilities.size ();  ++x)
      _probabilities[x] = probabilities[x];
  }

  return;
}  /* ProbabilitiesByClass */
예제 #4
0
FeatureVectorListPtr  FeatureFileIO::LoadFeatureFile 
                                      (const KKStr&   _fileName,
                                       MLClassList&   _mlClasses,
                                       OptionUInt32   _maxCount,
                                       VolConstBool&  _cancelFlag,    // will be monitored,  if set to True  Load will terminate.
                                       bool&          _successful,
                                       bool&          _changesMade,
                                       RunLog&        _log
                                      )
{
  _log.Level (10) << "LoadFeatureFile  File[" << _fileName << "]  FileFormat[" << driverName << "]" << endl;
  
  _changesMade = false;

  kkint32  estimatedNumOfDataItems = -1;
  
  _successful = true;

  ifstream  in (_fileName.Str (), ios_base::in);
  if  (!in.is_open ())
  {
    _log.Level (-1) << "LoadFeatureFile   ***ERROR***      Error Opening File[" << _fileName << "]." << endl;
    _successful = false;
    return  NULL;
  }

  KKStr  errorMessage;
  
  FileDescConstPtr  fileDescFromFile = GetFileDesc (_fileName, in, &_mlClasses, estimatedNumOfDataItems, errorMessage, _log);
  if  (fileDescFromFile == NULL)
  {
    _log.Level (-1) << endl << endl 
                   << "FeatureFileIO::LoadFeatureFile    ***ERROR***     Loading Feature File[" << _fileName << "]" << endl
                   << endl;
    _successful = false;
    return NULL;
  }

  FileDescConstPtr fileDesc = FileDesc::GetExistingFileDesc (fileDescFromFile);

  in.clear ();
  in.seekg (0, ios::beg);

  FeatureVectorListPtr  examples = LoadFile (_fileName, fileDesc, _mlClasses, in, _maxCount, _cancelFlag, _changesMade, errorMessage, _log);
  if  (examples == NULL)
  {
    _successful = false;
  }
  else
  {
    _successful = true;
  }

  in.close ();

  return  examples;
}  /* LoadFeatureFile */
예제 #5
0
ScannerFilePtr  ScannerFile::CreateScannerFileForOutput (const KKStr&  _fileName,
                                                         Format        _format,
                                                         kkuint32      _pixelsPerScanLine,
                                                         kkuint32      _frameHeight,
                                                         RunLog&       _log
                                                        )
{
  ScannerFilePtr  scannerFile = NULL;
  if  (osFileExists (_fileName))
  {
    KKStr  errMsg;
    errMsg << "ScannerFile::CreateScannerFileForOutput   ***ERROR***   ScannerFile[" << _fileName << "]  already exists.";
    _log.Level (-1) << endl << endl << errMsg << endl << endl;
  }
  else
  {
    switch  (_format)
    {
    case  Format::sfSimple:      
      scannerFile = new ScannerFileSimple (_fileName, _pixelsPerScanLine, _frameHeight, _log);
      break;

    case  Format::sf2BitEncoded:
      scannerFile = new ScannerFile2BitEncoded (_fileName, _pixelsPerScanLine, _frameHeight, _log);
      break;

    case  Format::sf3BitEncoded:
      scannerFile = new ScannerFile3BitEncoded (_fileName, _pixelsPerScanLine, _frameHeight, _log);
      break;

    case  Format::sf4BitEncoded:
      scannerFile = new ScannerFile4BitEncoded (_fileName, _pixelsPerScanLine, _frameHeight, _log);
      break;

    case  Format::sfZlib3BitEncoded:
      scannerFile = new ScannerFileZLib3BitEncoded (_fileName, _pixelsPerScanLine, _frameHeight, _log);
      break;

    case  Format::sfUnKnown:
      scannerFile = NULL;
      break;

    }

    if  (!scannerFile)
    {
      KKStr  errMsg;
      errMsg << "ScannerFile::CreateScannerFileForOutput   ***ERROR***   Invalid ScannerFile Format specified.";
      _log.Level (-1) << endl << endl << errMsg << endl << endl;
    }
  }

  return  scannerFile;
}  /* CreateScannerFileForOutput */
예제 #6
0
void  FeatureFileIO::SaveFile (FeatureVectorList&    _data,
                               const KKStr&          _fileName,
                               FeatureNumListConst&  _selFeatures,
                               std::ostream&         _out,
                               kkuint32&             _numExamplesWritten,
                               VolConstBool&         _cancelFlag,
                               bool&                 _successful,
                               KKStr&                _errorMessage,
                               RunLog&               _log
                              )
{
   _errorMessage = "Driver: '" + this->DriverName () + "' does not implement 'SaveFile' method.";
   _log.Level (-1) << endl
      << "FeatureFileIO::SaveFile    ***ERROR***   " << _errorMessage << endl
      << "     _data.size   : " << _data.size () << endl
      << "     _fileName    : " << _fileName << endl
      << "     _selFeatures : " << _selFeatures.ToCommaDelStr () << endl
      << "     _out.fail    : " << _out.fail () << endl
      << "     _cancelFlag  : " << _cancelFlag << endl
      << endl;
      
  _successful = false;
  _numExamplesWritten = 0;
  return;
}
예제 #7
0
FeatureVectorListPtr  FeatureFileIO::LoadFile (const KKStr&      _fileName,
                                               FileDescConstPtr  _fileDesc,
                                               MLClassList&      _classes, 
                                               istream&          _in,
                                               OptionUInt32      _maxCount,    // Maximum # images to load.
                                               VolConstBool&     _cancelFlag,
                                               bool&             _changesMade,
                                               KKStr&            _errorMessage,
                                               RunLog&           _log
                                              )
{
  _errorMessage = "Driver '" + DriverName () + "' does not implemenet  'LoadFile'  method.";
  _log.Level (10) << endl
      << "FeatureFileIO::LoadFile   ***ERROR***   " << _errorMessage << endl
      << "    _fileName   : " << _fileName << endl
      << "    _fileDesc   : " << _fileDesc->NumOfFields () << endl
      << "    _classes    : " << _classes.ToCommaDelimitedStr () << endl
      << "    _in.flags   : " << _in.flags () << endl
      << "    _maxCount   : " << _maxCount << endl
      << "    _cancelFlag : " << _cancelFlag << endl
      << "    _changesMade: " << _changesMade << endl
      << endl;

  _errorMessage = "ROBERTS read functionality not implemented.";
  return NULL;
}
예제 #8
0
void   ScannerFile::GetScannerFileParameters (const KKStr&             _scannerFileName,
                                              ScannerHeaderFieldsPtr&  _headerFields,
                                              Format&                  _scannerFileFormat,
                                              kkint32&                 _frameHeight,
                                              kkint32&                 _frameWidth,
                                              float&                   _scanRate,
                                              bool&                    _successful,
                                              RunLog&                  _log
                                             )
{
  _successful = false;
  ScannerFilePtr  sf = CreateScannerFile (_scannerFileName, _log);
  if  (sf == NULL)
  {
    _log.Level (-1) << "Could not determine scanner file format." << endl;
    return;
  }

  delete  _headerFields;
  _headerFields = new ScannerHeaderFields (*(sf->HeaderFields ()));
  _scannerFileFormat = sf->FileFormat        ();
  _frameHeight       = sf->FrameHeight       ();
  _frameWidth        = sf->PixelsPerScanLine ();
  _scanRate          = sf->ScanRate          ();

  _successful = true;
  delete  sf;
  sf = NULL;
}  /* GetScannerFileParameters */
예제 #9
0
ClassProbListPtr  ModelUsfCasCor::ProbabilitiesByClass (FeatureVectorPtr  example,
                                                        RunLog&           log
                                                       )
{
  if  (!usfCasCorClassifier)
  {
    KKStr errMsg = "ModelUsfCasCor::ProbabilitiesByClass   ***ERROR***      (usfCasCorClassifier == NULL)";
    log.Level (-1) << endl << endl << errMsg << endl << endl;
    throw KKException (errMsg);
  }

  bool  newExampleCreated = false;
  FeatureVectorPtr  encodedExample = PrepExampleForPrediction (example, newExampleCreated);

  ClassProbListPtr  results = usfCasCorClassifier->PredictClassConfidences (encodedExample);

  results->SortByProbability (true);  // 'true' = Sort High to Low.

  if  (newExampleCreated)
  {
    delete encodedExample;
    encodedExample = NULL;
  }

  return  results;
}  /* ProbabilitiesByClass */
예제 #10
0
ImageFeaturesPtr  PicesFVProducer::ComputeFeatureVectorFromImage (const KKStr&      fileName,
                                                                  const MLClassPtr  knownClass,
                                                                  RasterListPtr     intermediateImages,
                                                                  RunLog&           runLog
                                                                 )
{
  ImageFeaturesPtr  fv = NULL;

  bool  validFile = false;
  RasterSipperPtr  i = new RasterSipper (fileName, validFile);
  if  (!validFile)
  {
    delete i;
    i = NULL;
  }

  if  (i == NULL)
  {
    runLog.Level (-1) << "PicesFVProducer::ComputeFeatureVectorFromImage   ***ERROR***   Error loading ImageFile: " << fileName << endl << endl;
  }
  else
  {
    fv = ComputeFeatureVector (*i, knownClass, intermediateImages, 1.0f, runLog);
    delete  i;
    i = NULL;
  }

  return  fv;
}  /* ComputeFeatureVectorFromImage */
예제 #11
0
void  ModelUsfCasCor::Predict (FeatureVectorPtr example,
                               MLClassPtr       knownClass,
                               MLClassPtr&      predClass1,
                               MLClassPtr&      predClass2,
                               kkint32&         predClass1Votes,
                               kkint32&         predClass2Votes,
                               double&          probOfKnownClass,
                               double&          predClass1Prob,
                               double&          predClass2Prob,
                               kkint32&         numOfWinners,
                               bool&            knownClassOneOfTheWinners,
                               double&          breakTie,
                               RunLog&          log
                              )
{
  if  (!usfCasCorClassifier)
  {
    KKStr errMsg = "ModelUsfCasCor::Predict   ***ERROR***      (usfCasCorClassifier == NULL)";
    log.Level (-1) << endl << endl << errMsg << endl << endl;
    throw KKException (errMsg);
  }


  VectorFloat  probabilities;

  float  pc1p = 0.0f;
  float  pc2p = 0.0f;
  float  kcp = 0.0f;

  bool  newExampleCreated = false;
  FeatureVectorPtr  encodedExample = PrepExampleForPrediction (example, newExampleCreated);
  usfCasCorClassifier->PredictConfidences (encodedExample,
                                           knownClass,
                                           predClass1, pc1p, 
                                           predClass2, pc2p,
                                           kcp,
                                           *classes,
                                           probabilities
                                          );

  if  (newExampleCreated)
  {
    delete encodedExample;
    encodedExample = NULL;
  }

  predClass1Prob   = pc1p;
  predClass2Prob   = pc2p;
  probOfKnownClass = kcp;

  breakTie = pc1p - pc2p;
  numOfWinners = 1;

  knownClassOneOfTheWinners = (predClass1 == knownClass);

  return;
}  /* Predict */
void  CrossValidationMxN::CheckFileDescCopasetic (RunLog&  log)
{
  if  (config->FileDesc () != orderings->FileDesc ())
  {
    // The Configuration 'fileDesc' is different than the orderings 'FileDesc'.
    // This is a VERY VERY bad situation. Processing needs to stop NOW NOW NOW.
    KKStr errMsg = "CrossValidationMxN     ***ERROR***    File Description between config and orderings don't match.";
    log.Level (-1) << endl << errMsg << endl << endl;
    throw KKException (errMsg);
  }
}  /* CheckFileDescCopesetic */
예제 #13
0
void  ReportError (RunLog&       log,
                   const KKStr&  fileName,
                   const KKStr&  funcName,
                   kkint32       lineCount,
                   const KKStr&  errorDesc
                  )
{
  log.Level (-1) << endl
                 << funcName << "     *** ERROR ***" << endl
                 << "           File     [" << fileName      << "]" << endl
                 << "           LineCount[" << lineCount     << "]" << endl
                 << "           Error    [" << errorDesc     << "]" << endl
                 << endl;
}  /* ReportError */
예제 #14
0
void   FeatureFileIO::AppendToFile (const KKStr&          _fileName,
                                    FeatureNumListConst&  _selFeatures,
                                    FeatureVectorList&    _examples,
                                    kkuint32&             _numExamplesWritten,
                                    VolConstBool&         _cancelFlag,
                                    bool&                 _successful,
                                    RunLog&               _log
                                   )
{
  _log.Level (10) << "FeatureFileIO::AppendToFile - File[" << _fileName << "]." << endl;

  _successful = true;

  ofstream out (_fileName.Str (), ios::app);

  if  (!out.is_open())
  {
    KKStr  err;
    err << "AppendToFile  Error Opening File[" << _fileName << "]";
    _log.Level (-1)  << endl
                     << "FeatureFileIO::AppendToFile    ***ERROR***"  << endl
                     << endl
                     << "                   " << err  << endl
                     << endl;
    osDisplayWarning (err);
    _successful = false;
    return;
  }

  KKStr  errorMessage;
  SaveFile (_examples, _fileName,  _selFeatures, out, _numExamplesWritten, _cancelFlag, _successful, errorMessage, _log);

  out.close ();

  return;
}  /* AppendToFile */
예제 #15
0
void  ModelParamUsfCasCor::ReadXML (XmlStream&      s,
                                    XmlTagConstPtr  tag,
                                    VolConstBool&   cancelFlag,
                                    RunLog&         log
                                   )
{
  XmlTokenPtr  t = s.GetNextToken (cancelFlag, log);
  while  (t  &&  (!cancelFlag))
  {
    t = ReadXMLModelParamToken (t);
    if  ((t != NULL)  &&  (t->TokenType () == XmlToken::TokenTypes::tokElement))
    {
      XmlElementPtr e = dynamic_cast<XmlElementPtr> (t);
      const KKStr&  varName = e->VarName ();

      if  (varName.EqualIgnoreCase ("in_limit"))
        in_limit =  dynamic_cast<XmlElementInt32Ptr> (e)->Value ();

      else if  (varName.EqualIgnoreCase ("number_of_rounds"))
        number_of_rounds =  dynamic_cast<XmlElementInt32Ptr> (e)->Value ();

      else if  (varName.EqualIgnoreCase ("number_of_trials"))
        number_of_trials =  dynamic_cast<XmlElementInt32Ptr> (e)->Value ();

      else if  (varName.EqualIgnoreCase ("random_seed"))
        random_seed =  dynamic_cast<XmlElementInt64Ptr> (e)->Value ();

      else if  (varName.EqualIgnoreCase ("useCache"))
        useCache =  dynamic_cast<XmlElementBoolPtr> (e)->Value ();

      else
      {
        log.Level (-1) << endl
          << "ModelParamUsfCasCor::ReadXM   ***ERROR***   Unexpected Element: " << e->NameTag ()->ToString () << endl
          << endl;
      }
    }
    delete  t;
    t = s.GetNextToken (cancelFlag, log);
  }
  delete  t;
  t = NULL;

  bool  validFormat = false;
}  /* ReadXML */
예제 #16
0
FileDescConstPtr  FeatureFileIO::GetFileDesc (const KKStr&    _fileName,
                                              istream&        _in,
                                              MLClassListPtr  _classes,
                                              kkint32&        _estSize,
                                              KKStr&          _errorMessage,
                                              RunLog&         _log
                                             )
{
  _errorMessage = "Driver '" + DriverName () + "' does not implemenet  'GetFileDesc'  method.";
  _log.Level (10) << endl 
      << "FeatureFileIO::GetFileDesc   ***ERROR***    " << _errorMessage << endl
      << "    _fileName: " << _fileName << endl
      << "    _in.flags: " << _in.flags () << endl
      << "    _classes : " << _classes->ToCommaDelimitedStr () << endl
      << "    _estSize : " << _estSize << endl
      << endl;
  _errorMessage = "ROBERTS read_estSize, functionality not implemented.";
  return NULL; 
}
예제 #17
0
ImageFeaturesPtr  PicesFVProducer::ComputeFeatureVector (const Raster&     srcImage,
                                                         const MLClassPtr  knownClass,
                                                         RasterListPtr     intermediateImages,
                                                         float             priorReductionFactor,
                                                         RunLog&           runLog
                                                        )
{
  ImageFeaturesPtr  fv = NULL;
  RasterSipperListPtr  sipperRasterIntermediateImages = NULL;
  if  (intermediateImages)
    sipperRasterIntermediateImages = new RasterSipperList (false);

  if  (typeid (srcImage) != typeid (MLL::RasterSipper))
  {
    runLog.Level (-1) << endl
      << "PicesFVProducer::ComputeFeatureVector   ***ERROR***   'srcImage' not a 'RasterSipper' derived class." << endl
      << "   ExampleFileName[" << srcImage.FileName () << "]" << endl
      << endl;

    RasterSipperPtr sipperRaster = new RasterSipper (srcImage);
    fv = new ImageFeatures (*sipperRaster, knownClass, sipperRasterIntermediateImages, runLog);
    delete  sipperRaster;
    sipperRaster = NULL;
  }
  else
  {
    MLL::RasterSipperPtr  sipperImage = dynamic_cast<MLL::RasterSipperPtr> (&(Raster&)srcImage);
    fv = new ImageFeatures (*sipperImage, knownClass, sipperRasterIntermediateImages, runLog);
  }

  if  (sipperRasterIntermediateImages)
  {
    for  (auto intermdiateIDX: *sipperRasterIntermediateImages)
      intermediateImages->PushOnBack (intermdiateIDX);
    sipperRasterIntermediateImages->Owner (false);
  }

  delete  sipperRasterIntermediateImages;
  sipperRasterIntermediateImages = NULL;

  return fv;
}  /* ComputeFeatureVector */
예제 #18
0
MLClassPtr  ModelUsfCasCor::Predict (FeatureVectorPtr  example,
                                     RunLog&           log
                                    )
{
  if  (!usfCasCorClassifier)
  {
    log.Level (-1) << endl << endl << "ModelUsfCasCor::Predict   ***ERROR***      (usfCasCorClassifier == NULL)" << endl << endl;
    return NULL;
  }
  bool  newExampleCreated = false;
  FeatureVectorPtr  encodedExample = PrepExampleForPrediction (example, newExampleCreated);

  MLClassPtr  predictedClass = usfCasCorClassifier->PredictClass (encodedExample);
  if  (newExampleCreated)
  {
    delete encodedExample;
    encodedExample = NULL;
  }

  return  predictedClass;
}  /* Predict */
예제 #19
0
void  FeatureVectorList::RemoveEntriesWithMissingFeatures (RunLog&  log)
{
  log.Level (50) << "FeatureVectorList::RemoveEntriesWithMissingFeatures" << endl;

  vector<FeatureVectorPtr>  entriesToBeDeleted;

  for  (iterator idx = begin ();  idx != end ();  idx++)
  {
    FeatureVectorPtr example = *idx;
    if  (example->MissingData ())
      entriesToBeDeleted.push_back (example);
  }

  for  (kkint32 x = 0;  x < (kkint32)entriesToBeDeleted.size ();  x++)
  {
    FeatureVectorPtr example = entriesToBeDeleted[x];
    DeleteEntry (example);
    if  (Owner ())
      delete  example;
  }

}  /* RemoveEntriesWithMissingFeatures */
예제 #20
0
ScannerFilePtr  ScannerFile::CreateScannerFileForOutput (const KKStr&   _fileName,
                                                         const KKStr&   _formatStr,
                                                         kkuint32       _pixelsPerScanLine,
                                                         kkuint32       _frameHeight,
                                                         RunLog&        _log
                                                        )
{
  ScannerFilePtr  scannerFile = NULL;

  Format  format = ScannerFileFormatFromStr (_formatStr);
  if  (format == Format::sfUnKnown)
  {
    _log.Level (-1) << endl << endl << "ScannerFile::CreateScannerFileForOutput  ***ERROR***   Invalid Format[" << _formatStr << "]" << endl << endl;
  }
  else
  {
    scannerFile = ScannerFile::CreateScannerFileForOutput
    		(_fileName, (Format)format, _pixelsPerScanLine, _frameHeight, _log);
  }

  return  scannerFile;
}  /* CreateScannerFileForOutput */
예제 #21
0
RandomSampleJobListPtr  RandomSampleJobList::CreateNewList (RunLog&                   _log,
                                                            TrainingConfigurationPtr  _config,
                                                            OrderingsPtr              _orderings,
                                                            int                       _numExamplesToKeep,
                                                            const KKStr&             _fileName
                                                            )
{

  _log.Level (10) << endl << endl << endl;
  _log << "RandomSampleJobList::CreateNewList" << endl;
  _log << "            fileName         [" << _fileName          << "]" << endl;
  _log << "            numExamplesToKeep[" << _numExamplesToKeep << "]" << endl;          

  RandomSampleJobListPtr  list = NULL;

  if  (osFileExists (_fileName))
  {
    _log << "Ordering Files already Exists." << endl;
    list = new RandomSampleJobList (_log, _config, _orderings, _numExamplesToKeep, _fileName);
  }
  else
  {
    _log << "Ordering Files DID NOT exists" << endl;
    list = new RandomSampleJobList (_log);

    int  orderingNum = 0;

    for  (orderingNum = 0;  orderingNum < (int)_orderings->NumOfOrderings ();  orderingNum++)
    {
      RandomSampleJobPtr  j = new RandomSampleJob (_config, _orderings, orderingNum, orderingNum, _numExamplesToKeep);
      list->PushOnBack (j);
    }

    list->Save (_fileName);
  }

  return  list;
}  /* CreateNewList */
예제 #22
0
void  FeatureNumList::ReadXML (XmlStream&      s,
                               XmlTagConstPtr  tag,
                               VolConstBool&   cancelFlag,
                               RunLog&         log
                             )
{
  maxFeatureNum = (IntType)tag->AttributeValueInt32 ("MaxFeatureNum");
  auto expectedNumOfFeatures = (IntType)tag->AttributeValueInt32 ("NumOfFeatures");
  numOfFeatures = 0;

  XmlTokenPtr  t = s.GetNextToken (cancelFlag, log);
  while  (t  &&  (!cancelFlag))
  {
    if  (typeid (*t) == typeid(XmlContent))
    {
      XmlContentPtr c = dynamic_cast<XmlContentPtr> (t);
      if  (c  &&  (c->Content ()))
      {
        bool  valid = false;
        ParseToString (*(c->Content ()), valid);
      }
    }

    delete  t;
    t = s.GetNextToken (cancelFlag, log);
  }

  delete  t;
  t = NULL;

  if  (expectedNumOfFeatures != numOfFeatures)
  {
    log.Level (-1) << endl
      << "FeatureNumList::ReadXML   ***ERROR***   expectedNumOfFeatures["  << expectedNumOfFeatures << "]  not equal  numOfFeatures[" << numOfFeatures << "]" << endl
      << endl;
  }
}  /* ReadXML */
예제 #23
0
void  ModelUsfCasCor::ReadXML (XmlStream&      s,
                               XmlTagConstPtr  tag,
                               VolConstBool&   cancelFlag,
                               RunLog&         log
                              )
{
  delete  usfCasCorClassifier;
  usfCasCorClassifier = NULL;

  XmlTokenPtr  t = s.GetNextToken (cancelFlag, log);
  while  (t  &&  (!cancelFlag))
  {
    t = ReadXMLModelToken (t, log);
    if  (t)
    {
      if  (t->VarName ().EqualIgnoreCase ("UsfCasCorClassifier")  &&  (typeid (*t) == typeid(XmlElementUsfCasCor)))
      {
        delete usfCasCorClassifier;
        usfCasCorClassifier = dynamic_cast<XmlElementUsfCasCorPtr>(t)->TakeOwnership ();
      }
      else
      {
        KKStr  errMsg (128);
        errMsg << "ModelUsfCasCor::ReadXML   ***ERROR***   Unexpected Element: Section: " << t->SectionName () << " VarName:" << t->VarName ();
        log.Level (-1) << endl << errMsg << endl << endl;
        AddErrorMsg (errMsg, 0);
      }
    }
    delete  t;
    t = s.GetNextToken (cancelFlag, log);
  }

  delete  t;
  t = NULL;

  if  (!cancelFlag)
  {
    if  (!param)
      param = dynamic_cast<ModelParamUsfCasCorPtr> (Model::param);

    if  (Model::param == NULL)
    {
      KKStr errMsg (128);
      errMsg << "ModelUsfCasCor::ReadXML  ***ERROR***  Base class 'Model' does not have 'param' defined.";
      AddErrorMsg (errMsg, 0);
      log.Level (-1) << endl << errMsg << endl << endl;
    }

    else if  (typeid (*Model::param) != typeid(ModelParamUsfCasCor))
    {
      KKStr errMsg (128);
      errMsg << "ModelUsfCasCor::ReadXML  ***ERROR***  Base class 'Model' param parameter is of the wrong type;  found: " << Model::param->ModelParamTypeStr ();
      AddErrorMsg (errMsg, 0);
      log.Level (-1) << endl << errMsg << endl << endl;
    }

    else
    {
      param = dynamic_cast<ModelParamUsfCasCorPtr> (Model::param);
    }

    ReadXMLModelPost (log);
  }
}  /* ReadXML */
예제 #24
0
void  FeatureEncoder::EncodeIntoSparseMatrix
                               (FeatureVectorListPtr   src,
                                ClassAssignments&      assignments,
                                XSpacePtr&             xSpace,          
                                kkint32&               totalxSpaceUsed,
                                struct svm_problem&    prob,
                                RunLog&                log
                               )

{
  FeatureVectorListPtr  compressedExamples    = NULL;
  FeatureVectorListPtr  examplesToUseFoXSpace = NULL;
  kkint32               xSpaceUsed            = 0;

  totalxSpaceUsed = 0;

  examplesToUseFoXSpace = src;

  kkint32  numOfExamples = examplesToUseFoXSpace->QueueSize ();
  //kkint32  elements      = numOfExamples * xSpaceNeededPerExample;

  prob.l     = numOfExamples;
  prob.y     = (double*)malloc  (prob.l * sizeof (double));
  prob.x     = (struct svm_node **) malloc (prob.l * sizeof (struct svm_node*));
  prob.index = new kkint32[prob.l];
  prob.exampleNames.clear ();

  kkint32  numNeededXspaceNodes = DetermineNumberOfNeededXspaceNodes (examplesToUseFoXSpace);

  kkint32  totalBytesForxSpaceNeeded = (numNeededXspaceNodes + 10) * sizeof (struct svm_node);  // I added '10' to elements because I am paranoid

  xSpace = (struct svm_node*) malloc (totalBytesForxSpaceNeeded);
  if  (xSpace == NULL)
  {
    log.Level (-1) << endl << endl << endl
                   << " FeatureEncoder::Compress   *** Failed to allocates space for 'xSpace' ****" << endl
                   << endl
                   << "     Space needed          [" << totalBytesForxSpaceNeeded << "]" << endl
                   << "     Num of Examples       [" << numOfExamples             << "]" << endl
                   << "     Num XSpaceNodesNeeded [" << numNeededXspaceNodes      << "]" << endl
                   << endl;
    // we sill have to allocate space for each individual training example separately.
    //throw "FeatureEncoder::Compress     Allocation of memory for xSpace Failed.";
  }

  prob.W = NULL;

  kkint32 i = 0;
 
  FeatureVectorPtr  example      = NULL;
  MLClassPtr        lastMlClass  = NULL;
  kkint16           lastClassNum = -1;

  kkint32  bytesOfxSpacePerExample = xSpaceNeededPerExample * sizeof (struct svm_node);

  for (i = 0;  i < prob.l;  i++)
  {
    if  (totalxSpaceUsed > numNeededXspaceNodes)
    {
      log.Level (-1) << endl << endl
        << "FeatureEncoder::Compress   ***ERROR***   We have exceeded the number of XSpace nodes allocated." << endl
        << endl;
    }

    example = examplesToUseFoXSpace->IdxToPtr (i);

    if  (example->MLClass () != lastMlClass)
    {
      lastMlClass  = example->MLClass ();
      lastClassNum = assignments.GetNumForClass (lastMlClass);
    }

    prob.y[i]     = lastClassNum;
    prob.index[i] = i;
    prob.exampleNames.push_back (osGetRootName (example->ExampleFileName ()));

    if  (prob.W)
    {
      prob.W[i] = example->TrainWeight () * c_Param;
      if  (example->TrainWeight () <= 0.0f)
      {
        log.Level (-1) << endl 
                       << "FeatureEncoder::EncodeIntoSparseMatrix    ***ERROR***   Example[" << example->ExampleFileName () << "]" << endl
                       << "      has a TrainWeight value of 0 or less defaulting to 1.0" << endl
                       << endl;
        prob.W[i] = 1.0 * c_Param;
      }
    }

    if  (xSpace == NULL)
    {
      struct svm_node*  xSpaceThisExample = (struct svm_node*) malloc (bytesOfxSpacePerExample);
      prob.x[i] = xSpaceThisExample;
      EncodeAExample (example, prob.x[i], xSpaceUsed);
      if  (xSpaceUsed < xSpaceNeededPerExample)
      {
        kkint32  bytesNeededForThisExample = xSpaceUsed * sizeof (struct svm_node);
        struct svm_node*  smallerXSpaceThisExample = (struct svm_node*) malloc (bytesNeededForThisExample);
        memcpy (smallerXSpaceThisExample, xSpaceThisExample, bytesNeededForThisExample);
        free  (xSpaceThisExample);
        prob.x[i] = smallerXSpaceThisExample;
      }
    }
    else
    {
      prob.x[i] = &xSpace[totalxSpaceUsed];
      EncodeAExample (example, prob.x[i], xSpaceUsed);
    }
    totalxSpaceUsed += xSpaceUsed;
  }

  delete  compressedExamples;
  return;
}  /* Compress */
예제 #25
0
void  ImportGPSDataGPGGA (const KKStr&  fileName)
{
  RunLog  log;

  DataBasePtr  dbConn = new DataBase (log);

  ifstream  i (fileName.Str ());
  if  (!i.is_open ())
  {
    log.Level (-1) << endl << endl 
       << "ImpotrtGPSData  Could not open file[" << fileName << "]" << endl
       << endl;
    return;
  }
  log.Level (10) << endl << endl << endl << endl << endl
    << "ImpotrtGPSData   FileName[" << fileName << "]" << endl << endl
    << endl;

  char  buff[20480];
  bool  firstPass = true;

  int  lastMinute = 0;
  int  linesRead = 0;

  KKStr  ln (256);

  DateTime  lastDateTime;
  while  (i.getline (buff, sizeof (buff)))
  {
    linesRead++;
    ln = buff;
    ln.TrimLeft ();
     
    if  (!ln.LocateStr ("GPGGA"))
      continue;

    VectorKKStr  fields = ln.Parse (",");
    if  (fields.size () < 8)
      continue;

    if  (!fields[2].EqualIgnoreCase ("$GPGGA"))
      continue;


    /*
    0           1             2        3         4      5       6       7   8  
06/01/2010, 23:59:59.818,  $GPGGA,  235958,  2840.927,  N,  08828.458,  W,  2,  09,22.10,0,M,,,14,0000*12
06/02/2010, 00:00:10.818,  $GPGGA,  000009,  2840.931,  N,  08828.482,  W,  1,  09,0.89,0,M,,,,*2D
06/02/2010, 00:00:21.802,  $GPGGA,  000020,  2840.929,  N,  08828.505,  W,  1,  09,0.89,0,M,,,,*21
06/02/2010, 00:00:31.818,  $GPGGA,  000030,  2840.924,  N,  08828.526,  W,  1,  09,0.89,0,M,,,,*2C
06/02/2010, 00:00:42.818,  $GPGGA,  000041,  2840.917,  N,  08828.547,  W,  1,  09,0.89,0,M,,,,*2D
06/02/2010, 00:00:53.802,  $GPGGA,  000052,  2840.906,  N,  08828.568,  W,  1,  09,1.00,0,M,,,,*22
06/02/2010, 00:01:03.802,  $GPGGA,  000102,  2840.895,  N,  08828.585,  W,  1,  09,0.89,0,M,,,,*2E
06/02/2010, 00:01:13.818,  $GPGGA,  000112,  2840.883,  N,  08828.600,  W,  1,  09,0.89,0,M,,,,*26
   */


    KKStr  dateStr = fields[0];
    KKStr  timeStr = fields[1];

    KKStr  latStr = fields[4];
    KKStr  logStr = fields[6];

    auto  x = latStr.LocateCharacter ('.');
    if  (!x) 
      continue;

    KKStr latMinStr = latStr.SubStrPart (x - 2);
    KKStr latDegStr = latStr.SubStrSeg (0, x - 2);

    double latitude = latDegStr.ToDouble () + latMinStr.ToDouble () / 60.0;
    if  (fields[5].EqualIgnoreCase ("S"))
      latitude = 0.0 - latitude;

    x = logStr.LocateCharacter ('.');
    if  (!x) 
      continue;

    KKStr logMinStr = logStr.SubStrPart (x - 2);
    KKStr logDegStr = logStr.SubStrSeg (0, x - 2);

    double longitude = logDegStr.ToDouble () + logMinStr.ToDouble () / 60.0;
    if  (fields[7].EqualIgnoreCase ("W"))
      longitude = 0.0 - longitude;

    DateType  gmtDate (dateStr);
    TimeType  gmtTime (timeStr);

    DateTime  gmtDateTime (gmtDate, gmtTime);
    DateTime  localTime = gmtDateTime;
    localTime.HoursAdd (-4);

    DateTime  startDT = localTime;
    DateTime  endDT   = localTime;

    if  (firstPass)
    {
      firstPass = false;
      startDT.SecondsAdd (-180);
    }
    else
    {
      DateTime deltaDT = localTime - lastDateTime;
      long  deltaSecs = (long)deltaDT.Seconds ();
      startDT.SecondsAdd (-(deltaSecs / 2));
    }

    endDT.SecondsAdd (30);

    if  (gmtTime.Minute () != lastMinute)
    {
      lastMinute = gmtTime.Minute ();
      log.Level (10) << "LinesRead[" << linesRead << "]  File[" << osGetRootName (fileName) << "]  GMT Time[" << gmtDate.MMM_DD_YYYY () << " - " << gmtTime.HH_MM_SS () << "]" << endl;
    }

    if  ((endDT.Month () < 6)  &&  (endDT.Day () < 28))
    {
    }
    else
    {
      dbConn->InstrumentDataUpdateLatitudeAndLongitude (startDT, endDT, latitude, longitude);
    }

    lastDateTime = localTime;
  }

  i.close ();

  delete  dbConn;
  dbConn = NULL;
}  /* ImportGPSDataGPGGA */
예제 #26
0
void  ImpotrtGPSData (const KKStr&  fileName)
{
  RunLog  log;

  DataBasePtr  dbConn = new DataBase (log);

  ifstream  i (fileName.Str ());
  if  (!i.is_open ())
  {
    log.Level (-1) << endl << endl 
       << "ImpotrtGPSData  Could not open file[" << fileName << "]" << endl
       << endl;
    return;
  }

  log.Level (10) << endl << endl << endl << endl << endl
    << "ImpotrtGPSData   FileName[" << fileName << "]" << endl << endl
    << endl;

  char  buff[20480];
  bool  firstPass = true;

  int  lastHour = 0;

  DateTime  lastDateTime;
  while  (i.getline (buff, sizeof (buff)))
  {
    KKStr  ln = buff;
    ln.TrimLeft ();
    char  ch = ln[0];
    if  ((ch < '0')  ||  (ch > '9'))
      continue;

    KKStr  dateStr = ln.ExtractToken2 (",");
    KKStr  timeStr = ln.ExtractToken2 (",");

    KKStr  WXTP_TaStr = ln.ExtractToken2 (",");
    KKStr  WXTS_TaStr = ln.ExtractToken2 (",");

    KKStr  WXTS_PaStr = ln.ExtractToken2 (",");
    KKStr  WXTP_PaStr = ln.ExtractToken2 (",");
    KKStr  dec_latStr = ln.ExtractToken2 (",");
    KKStr  dec_lonStr = ln.ExtractToken2 (",");

    KKStr  cogStr = ln.ExtractToken2 (",");
    KKStr  sogStr = ln.ExtractToken2 (",");

    DateType  gmtDate (dateStr);
    TimeType  gmtTime (timeStr);

    DateTime  gmtDateTime (gmtDate, gmtTime);
    DateTime  localTime = gmtDateTime;
    localTime.HoursAdd (-4);

    DateTime  startDT = localTime;
    DateTime  endDT   = localTime;

    if  (firstPass)
    {
      firstPass = false;
      startDT.SecondsAdd (-180);
    }
    else
    {
      DateTime deltaDT = localTime - lastDateTime;
      long  deltaSecs = (long)deltaDT.Seconds ();
      startDT.SecondsAdd (-(deltaSecs / 2));
    }

    endDT.SecondsAdd (120);

    dbConn->InstrumentDataUpdateLatitudeAndLongitude (startDT, endDT, dec_latStr.ToDouble (), dec_lonStr.ToDouble ());

    lastDateTime = localTime;

    if  (gmtTime.Minute () != lastHour)
    {
      lastHour = gmtTime.Hour ();
      log.Level (10) << "File[" << osGetRootName (fileName) << "]  GMT Time[" << gmtDate.MMM_DD_YYYY () << " - " << gmtTime.HH_MM_SS () << "]" << endl;
    }
  }

  i.close ();

  delete  dbConn;
  dbConn = NULL;


}  /* ImpotrtGPSData */
예제 #27
0
void  Configuration::LoadFile (RunLog&  log)
{
  log.Level (10) << "Configuration::LoadFile: " << fileName << endl;

  kkint32  lastLineNum = 0;

  if  (fileName == "")
  {
    log.Level (-1) << endl
                   << "Configuration::LoadFile   ***ERROR***   File-Name is blank"  << endl
                   << endl;
    FormatGood (false);
    return;
  }

  FILE*  inFile = osFOPEN (fileName.Str (), "r");

  if  (!inFile)
  {
    log.Level (-1) << endl
                   << "Configuration::LoadFile   ***ERROR***    Opening File: " << fileName << endl
                   << endl;

    FormatGood (false);
    return;
  }

  char  buff[10240];
  kkint32 lineCount = 0;

  curSectionName = "";
  ConfSectionPtr  curSection = NULL;

  while  (fgets (buff, sizeof (buff), inFile))
  {
    lastLineNum++;
    KKStr  line (buff);
    line.TrimRight ();
    line.TrimLeft ();

    StripOutAnyComments (line);

    log.Level (70) << line << endl;
    
    StripOutAnyComments (line);

    if  (line.Empty ())            
    {
      // If we have a blank line, we do nothing.
    }

    else if  (line.FirstChar () == '[')
    {
      // Looks like definition of new section. 

      if  (line.LastChar () == ']')
      {
        curSectionName = line.SubStrPart (1, line.Len () - 2);
        curSectionName.TrimLeft ();
        curSectionName.TrimRight ();
        curSectionName.Upper ();

        curSection = new ConfSection (curSectionName, lastLineNum);
        sections->AddConfSection (curSection);
        log.Level (30) << "LoadFile   SectionName[" << curSectionName << "]." << endl;
      }
      else
      {
        log.Level (-1) << endl
                       << "Configuration::LoadFile   ***ERROR***    LineNumber[" << lastLineNum << "]  Improper Section Name[" << line << "]." << endl
                       << endl;
        formatGood = false;
      }
    }

    else
    {
      if  (!curSection)
      {
        log.Level (-1) << endl
                       << "Configuration::LoadFile   ***ERROR***  Format Error LineNumber[" << lastLineNum << "]" << endl
                       << "                            No Section Defined."  << endl 
                       << endl;
     
        formatGood = false;

        curSectionName = "GLOBAL";
        curSection = new ConfSection (curSectionName, lastLineNum);
        sections->AddConfSection (curSection);
      }

      kkint32  equalIdx = line.LocateCharacter ('=');

      if  (equalIdx < 0)
      {
        // We have a improperly formated line.
        log.Level (-1) << endl
                       << "Configuration::LoadFile   ***ERROR***   LineNumber[" << lastLineNum << "] Improperly Formated Line[" << line << "]." 
                       << endl;
        formatGood = false;
      }

      else
      {
        KKStr  settingName (line.SubStrPart (0, equalIdx - 1));
        settingName.TrimLeft ();
        settingName.TrimRight ();
        settingName.Upper ();

        KKStr  settingValue (line.SubStrPart (equalIdx + 1));
        settingValue.TrimLeft ();
        settingValue.TrimRight ();

        log.Level (30) << "LoadFile   SectionName[" << curSectionName << "], "
                       << "Setting[" << settingName << "], Value[" << settingValue   << "]."
                       << endl;

        curSection->AddSetting (settingName, settingValue, lastLineNum);
      }

      lineCount++;
    }
  }

  fclose (inFile);
}  /* LoadFile */
예제 #28
0
FeatureVectorListPtr  FeatureFileIO::FeatureDataReSink (FactoryFVProducerPtr  _fvProducerFactory,
                                                        const KKStr&          _dirName,
                                                        const KKStr&          _fileName, 
                                                        MLClassPtr            _unknownClass,
                                                        bool                  _useDirectoryNameForClassName,
                                                        MLClassList&          _mlClasses,
                                                        VolConstBool&         _cancelFlag,
                                                        bool&                 _changesMade,
                                                        KKB::DateTime&        _timeStamp,
                                                        RunLog&               _log
                                                      )
{
  _changesMade = false;
  _timeStamp = DateTime ();

  if  (_unknownClass == NULL)
    _unknownClass = MLClass::GetUnKnownClassStatic ();

  KKStr  className = _unknownClass->Name ();

  _log.Level (10) << "FeatureFileIO::FeatureDataReSink  dirName: " << _dirName << endl
                  << "               fileName: " << _fileName << "  UnKnownClass: " << className << endl;

  KKStr  fullFeatureFileName = osAddSlash (_dirName) +  _fileName;

  bool  successful = true;

  KKStr fileNameToOpen;
  if  (_dirName.Empty ())
    fileNameToOpen = _fileName;
  else
    fileNameToOpen = osAddSlash (_dirName) + _fileName;

  bool  versionsAreSame = false;

  FeatureVectorListPtr  origFeatureVectorData 
        = LoadFeatureFile (fileNameToOpen, _mlClasses, -1, _cancelFlag, successful, _changesMade, _log);

  if  (origFeatureVectorData == NULL)
  {
    successful = false;
    origFeatureVectorData = _fvProducerFactory->ManufacturFeatureVectorList (true);
  }

  if  (_cancelFlag)
  {
    delete  origFeatureVectorData;  origFeatureVectorData = NULL;
    return  _fvProducerFactory->ManufacturFeatureVectorList (true);
  }

  FeatureVectorListPtr  origFeatureData = NULL;

  if  (successful  &&
       (&typeid (*origFeatureVectorData) == _fvProducerFactory->FeatureVectorListTypeId ())  &&
       ((*(origFeatureVectorData->FileDesc ())) ==  (*(_fvProducerFactory->FileDesc ())))
      )
  {
     origFeatureData = origFeatureVectorData;
  }
  else
  {
    origFeatureData = _fvProducerFactory->ManufacturFeatureVectorList (true);
    delete  origFeatureVectorData;
    origFeatureVectorData = NULL;
  }

  KKStr  fileSpec = osAddSlash (_dirName) + "*.*";
  KKStrListPtr   fileNameList = osGetListOfFiles (fileSpec);

  if  (!fileNameList)
  {
    // There are no Image Files,  so we need to return a Empty List of Image Features.

    if  (origFeatureData->QueueSize () > 0)
      _changesMade = true;

    delete  origFeatureData;  origFeatureData = NULL;

    return  _fvProducerFactory->ManufacturFeatureVectorList (true);
  }

  FeatureVectorProducerPtr  fvProducer = _fvProducerFactory->ManufactureInstance (_log);

  if  (successful)
  {
    if  (origFeatureData->Version () == fvProducer->Version ())
    {
      versionsAreSame = true;
      _timeStamp = osGetFileDateTime (fileNameToOpen);
    }

    else
    {
      _changesMade = true;
    }
  }
  else
  {
    delete  origFeatureData;
    origFeatureData = _fvProducerFactory->ManufacturFeatureVectorList (true);
  }

  origFeatureData->SortByRootName (false);

  FeatureVectorListPtr  extractedFeatures = _fvProducerFactory->ManufacturFeatureVectorList (true);
  extractedFeatures->Version (fvProducer->Version ());

  fileNameList->Sort (false);

  KKStrList::iterator  fnIDX;
  fnIDX = fileNameList->begin ();   // fileNameList

  KKStrPtr  imageFileName;

  kkuint32  numImagesFoundInOrigFeatureData = 0;
  kkuint32  numOfNewFeatureExtractions = 0;

  for  (fnIDX = fileNameList->begin ();  (fnIDX != fileNameList->end ())  &&  (!_cancelFlag);  ++fnIDX)
  {
    imageFileName = *fnIDX;

    // pv414-_002_20140414-162243_02068814-1261.bmp
    KKStr  rootName = osGetRootName (*imageFileName);
    if  (rootName == "pv414-_002_20140414-162243_02068814-1261")
      cout << "Stop Here." << endl;

    bool validImageFileFormat = SupportedImageFileFormat (*imageFileName);
    
    if  (!validImageFileFormat)
      continue;

    bool  featureVectorCoputaionSuccessful = false;

    FeatureVectorPtr  origFV = origFeatureData->BinarySearchByName (*imageFileName);
    if  (origFV)
      numImagesFoundInOrigFeatureData++;

    if  (origFV  &&  versionsAreSame)
    {
      featureVectorCoputaionSuccessful = true;
      if  (_useDirectoryNameForClassName)
      {
        if  (origFV->MLClass () != _unknownClass)
        {
          _changesMade = true;
          origFV->MLClass (_unknownClass);
        }
      }

      else if  ((origFV->MLClass ()->UnDefined ())  &&  (origFV->MLClass () != _unknownClass))
      {
        _changesMade = true;
        origFV->MLClass (_unknownClass);
      }

      extractedFeatures->PushOnBack (origFV);
      origFeatureData->DeleteEntry (origFV);
    }
    else
    {
      // We either  DON'T have an original image    or    versions are not the same.

      KKStr  fullFileName = osAddSlash (_dirName) + (*imageFileName);
      FeatureVectorPtr fv = NULL;
      try
      {
        RasterPtr image = ReadImage (fullFileName);
        if  (image)
          fv = fvProducer->ComputeFeatureVector (*image, _unknownClass, NULL, 1.0f, _log);
        delete image;
        image = NULL;
        if  (fv)
          featureVectorCoputaionSuccessful = true;
        else
          featureVectorCoputaionSuccessful = false;
      }
      catch  (...)
      {
        _log.Level (-1) << endl << endl
          << "FeatureDataReSink   ***ERROR***"  << endl
          << "       Exception occurred calling constructor 'ComputeFeatureVector'." << endl
          << endl;
        featureVectorCoputaionSuccessful = false;
        fv = NULL;
      }

      if  (!featureVectorCoputaionSuccessful)
      {
        _log.Level (-1) << " FeatureFileIOKK::FeatureDataReSink  *** ERROR ***, Processing Image File["
                       << imageFileName << "]."
                       << endl;
        delete  fv;
        fv = NULL;
      }

      else
      {
        _changesMade = true;
        fv->ExampleFileName (*imageFileName);
        _log.Level (30) << fv->ExampleFileName () << "  " << fv->OrigSize () << endl;
        extractedFeatures->PushOnBack (fv);
        numOfNewFeatureExtractions++;

        if  ((numOfNewFeatureExtractions % 100) == 0)
          cout << numOfNewFeatureExtractions << " Images Extracted." << endl;
      }
    }
  }

  if  (numImagesFoundInOrigFeatureData != extractedFeatures->QueueSize ())
    _changesMade = true;
  
  extractedFeatures->Version (fvProducer->Version ());

  if  ((_changesMade)  &&  (!_cancelFlag))
  {
    //extractedFeatures->WriteImageFeaturesToFile (fullFeatureFileName, RawFormat, FeatureNumList::AllFeatures (extractedFeatures->FileDesc ()));

    kkuint32  numExamplesWritten = 0;

    SaveFeatureFile (fullFeatureFileName,  
                     FeatureNumList::AllFeatures (extractedFeatures->FileDesc ()),
                     *extractedFeatures,
                     numExamplesWritten,
                     _cancelFlag,
                     successful,
                     _log
                    );

    _timeStamp = osGetLocalDateTime ();
  }

  delete fvProducer;       fvProducer      = NULL;
  delete fileNameList;     fileNameList    = NULL;
  delete origFeatureData;  origFeatureData = NULL;

  _log.Level (10) << "FeatureDataReSink  Exiting  Dir: "  << _dirName << endl;

  return  extractedFeatures;
}  /* FeatureDataReSink */
예제 #29
0
FeatureVectorListPtr  FeatureFileIO::LoadInSubDirectoryTree 
                         (FactoryFVProducerPtr  _fvProducerFactory,
                          KKStr                 _rootDir,
                          MLClassList&          _mlClasses,
                          bool                  _useDirectoryNameForClassName,
                          VolConstBool&         _cancelFlag, 
                          bool                  _rewiteRootFeatureFile,
                          RunLog&               _log
                         )
{
  _log.Level (10) << "FeatureFileIO::LoadInSubDirectoryTree    rootDir[" << _rootDir << "]." << endl;

  osAddLastSlash (_rootDir);

  KKStr  featureFileName ("");
  KKStr  fullFeatureFileName ("");

  if  (!_rootDir.Empty ())
  {
    featureFileName = osGetRootNameOfDirectory (_rootDir) + ".data";
    fullFeatureFileName = _rootDir + featureFileName;
  }
  else
  {
    featureFileName     = "Root.data";
    fullFeatureFileName = "Root.data";
  }

  MLClassPtr  unKnownClass = _mlClasses.GetUnKnownClass ();
  if  (_useDirectoryNameForClassName)
  {
    KKStr className = MLClass::GetClassNameFromDirName (_rootDir);
    unKnownClass    = _mlClasses.GetMLClassPtr (className);
  }

  bool  changesMade = false;

  FeatureVectorListPtr  dirImages = NULL;

  if  (_rewiteRootFeatureFile)
  {
    DateTime  timeStamp;
    dirImages = FeatureDataReSink (_fvProducerFactory,
                                   _rootDir,
                                   featureFileName,
                                   unKnownClass,
                                   _useDirectoryNameForClassName,
                                   _mlClasses,
                                   _cancelFlag,
                                   changesMade,
                                   timeStamp,
                                   _log
                                  );
    if  (_useDirectoryNameForClassName)
    {
      FeatureVectorList::iterator  idx;
      for  (idx = dirImages->begin ();  idx != dirImages->end ();  idx++)
      {
        if  ((*idx)->MLClass () != unKnownClass)
        {
          (*idx)->MLClass (unKnownClass);
          changesMade = true;
        }
      }

      if  (changesMade)
      {
        KKStr  fullFileName = osAddSlash (_rootDir) + featureFileName;
        kkuint32  numExamplesWritten = 0;
        bool  cancel     = false;
        bool  successful = false;
        SaveFeatureFile (fullFileName, 
                         dirImages->AllFeatures (), 
                         *dirImages, 
                         numExamplesWritten,
                         cancel,
                         successful,
                         _log
                        );
      }
    }
  }
  else
  {
    dirImages =  _fvProducerFactory->ManufacturFeatureVectorList (true);
  }

  // Now that we have processed all image files in "rootDir",
  // lets process any sub-directories.

  KKStr  dirSearchPath = osAddSlash (_rootDir) + "*.*";

  KKStrListPtr  subDirectories = osGetListOfDirectories (dirSearchPath);
  if  (subDirectories)
  {
    KKStrList::iterator  idx;

    for  (idx = subDirectories->begin ();  (idx != subDirectories->end ()  &&  (!_cancelFlag));   idx++)
    {
      KKStr  subDirName (**idx);
      if  (subDirName == "BorderImages")
      {
        // We ignore this director
        continue;
      }

      KKStr  newDirPath = osAddSlash (_rootDir) + subDirName;

      FeatureVectorListPtr  subDirExamples = LoadInSubDirectoryTree (_fvProducerFactory,
                                                                     newDirPath, 
                                                                     _mlClasses, 
                                                                     _useDirectoryNameForClassName, 
                                                                     _cancelFlag,
                                                                     true,     // true = ReWriteRootFeatureFile
                                                                     _log
                                                                    );
      osAddLastSlash (subDirName);

      // We want to add the directory path to the ExampleFileName so that we can later locate the source image.
      for  (auto fv: *subDirExamples)
      {
        KKStr  newImageFileName = subDirName + fv->ExampleFileName ();
        fv->ExampleFileName (newImageFileName);
      }

      dirImages->AddQueue (*subDirExamples);
      subDirExamples->Owner (false);
      delete  subDirExamples;
      subDirExamples = NULL;
    }

    delete  subDirectories;  subDirectories = NULL;
  }

  _log.Level (10) << "LoadInSubDirectoryTree - Done" << endl;

  return  dirImages;
}  /* LoadInSubDirectoryTree */
예제 #30
0
void  FeatureEncoder::ReadXML (XmlStream&      s,
                               XmlTagConstPtr  tag,
                               VolConstBool&   cancelFlag,
                               RunLog&         log
                              )
{
  XmlTokenPtr t = s.GetNextToken (cancelFlag,  log);
  while  (t  &&  (!cancelFlag))
  {
    if  (t->TokenType () == XmlToken::TokenTypes::tokElement)
    {
      XmlElementPtr e = dynamic_cast<XmlElementPtr> (t);
      if  (e)
      {
        KKStr varName = e->VarName ();

        if  (varName.EqualIgnoreCase ("CodedNumOfFeatures"))
          codedNumOfFeatures= e->ToInt32 ();

        else if  (varName.EqualIgnoreCase ("C_Param"))
          c_Param = e->ToDouble ();

        else if  (varName.EqualIgnoreCase ("NumEncodedFeatures"))
          numEncodedFeatures = e->ToInt32 ();

        else if  (varName.EqualIgnoreCase ("NumOfFeatures"))
          numOfFeatures = e->ToInt32 ();

        else if  (varName.EqualIgnoreCase ("xSpaceNeededPerExample"))
          xSpaceNeededPerExample = e->ToInt32 ();

        else if  (typeid (*e) == typeid (XmlElementArrayInt32))
        {
          XmlElementArrayInt32Ptr xmlArray = dynamic_cast<XmlElementArrayInt32Ptr> (e);
          kkuint32 count = xmlArray->Count ();
          if  (count != numOfFeatures)
          {
            log.Level (-1) << endl
              << "FeatureEncoder::ReadXML   ***ERROR***  Variable[" << varName << "]  Invalid Length[" << count << "]  Expected[" << numOfFeatures << "]" << endl
              << endl;
          }
          else
          {
            if  (varName.EqualIgnoreCase ("CardinalityDest"))
            {
              delete  cardinalityDest;
              cardinalityDest = xmlArray->TakeOwnership ();
            }

            else if  (varName.EqualIgnoreCase ("DestFeatureNums"))
            {
              delete  destFeatureNums;
              destFeatureNums = xmlArray->TakeOwnership ();
            }

            else if  (varName.EqualIgnoreCase ("SrcFeatureNums"))
            {
              delete  srcFeatureNums;
              srcFeatureNums = xmlArray->TakeOwnership ();
            }
          }
        }

        else if  (varName.EqualIgnoreCase ("Class1"))
          class1 = MLClass::CreateNewMLClass (e->ToKKStr (), -1);

        else if  (varName.EqualIgnoreCase ("Class2"))
          class2 = MLClass::CreateNewMLClass (e->ToKKStr (), -1);

        else if  (varName.EqualIgnoreCase ("FileDesc")  &&  (typeid (*e) == typeid (XmlElementFileDesc)))
          fileDesc = dynamic_cast<XmlElementFileDescPtr> (e)->Value ();

        else if  (varName.EqualIgnoreCase ("DestFileDesc")  &&  (typeid (*e) == typeid (XmlElementFileDesc)))
          destFileDesc = dynamic_cast<XmlElementFileDescPtr> (e)->Value ();

        else if  (varName.EqualIgnoreCase ("DestWhatToDo")  &&  (typeid (*e) == typeid (XmlElementVectorInt32)))
        {
          XmlElementVectorInt32Ptr  xmlVect = dynamic_cast<XmlElementVectorInt32Ptr> (e);
          if  (xmlVect  &&  xmlVect->Value ())
          {
            const VectorInt32&  v = *(xmlVect->Value ());
            if  (v.size () != numOfFeatures)
            {
              log.Level (-1) << endl
                << "FeatureEncoder::ReadXML   ***ERROR***   Variable[" << varName << "]  Invalid Size[" << v.size () << "]  Expected[" << numOfFeatures << "]." << endl
                << endl;
            }
            else
            {
              delete  destWhatToDo;
              destWhatToDo = new FeWhatToDo[v.size ()];
              for  (kkuint32 x = 0;  x < v.size ();  ++x)
                destWhatToDo[x] = (FeWhatToDo)v[x];
            }
          }
        }

        else if  (varName.EqualIgnoreCase ("EncodingMethod"))
          encodingMethod = EncodingMethodFromStr (e->ToKKStr ());

        else
        {
          log.Level (-1) << "XmlElementTrainingClassList   ***ERROR***   Un-expected Section Element[" << e->SectionName () << "]" << endl;
        }
      }
    }

    delete  t;
    t = s.GetNextToken (cancelFlag, log);
  }
  delete  t;
  t = NULL;
}  /* ReadXML */