Advertisement
Guest User

Untitled

a guest
Apr 16th, 2013
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. /*
  2.  * This program is free software; you can redistribute it and/or modify
  3.  * it under the terms of the GNU General Public License as published by
  4.  * the Free Software Foundation; either version 3 of the License, or
  5.  * (at your option) any later version.
  6.  *
  7.  * this code is inspired from
  8.  * examples/documented/libshogun/classifier_latent_svm.cpp
  9.  * it serves the purpose of instanciating and wrapping shogun::CLatenModel.
  10.  *
  11.  * Written (W) 2012 Christian Montanari
  12.  */
  13.  
  14. %include "Latent.i"
  15. %rename(ObjectDetector) CObjectDetector;
  16.  
  17. #if defined(SWIGPERL)
  18.  //PTZ121108 example of classifier in examples/undocumented/libshogun/classifier_latent_svm.cpp
  19.  //extention to make use of CData,CLatentModel
  20.  //TODO:PTZ121108 put it in another file like  classifier_latent_svm.i or %include  examples/undocumented/libshogun/classifier_latent_svm.cpp
  21.  //or find a clever way to wrap CLatenModel, CData  instanciation, bless({}, modshogun::LatentModel)
  22.  // is not enough and would need a new wrapper, but yet new CLatentModel() is not working,
  23.  // (with error: "cannot allocate an object of abstract type") ?
  24. %inline %{
  25.   namespace shogun {
  26. #define HOG_SIZE 1488
  27.     struct CBoundingBox : public CData
  28.     {
  29.     CBoundingBox(int32_t x, int32_t y) : CData(), x_pos(x), y_pos(y) {};      
  30.       int32_t x_pos, y_pos;
  31.       virtual const char* get_name() const { return "BoundingBox"; }
  32.     };
  33.     struct CHOGFeatures : public CData
  34.     {
  35.     CHOGFeatures(int32_t w, int32_t h) : CData(), width(w), height(h) {};      
  36.       int32_t width, height;
  37.       float64_t ***hog;      
  38.       virtual const char* get_name() const { return "HOGFeatures"; }
  39.     };
  40.     class CObjectDetector: public CLatentModel
  41.     {
  42.     public:
  43.       CObjectDetector() {};
  44.     CObjectDetector(CLatentFeatures* feat, CLatentLabels* labels)
  45.       : CLatentModel(feat, labels) {};      
  46.       virtual ~CObjectDetector() {};      
  47.       virtual int32_t get_dim() const { return HOG_SIZE; };
  48.       virtual CDotFeatures* get_psi_feature_vectors()
  49.       {
  50.         int32_t num_examples = this->get_num_vectors();
  51.         int32_t dim = this->get_dim();
  52.         SGMatrix<float64_t> psi_m(dim, num_examples);
  53.         for (int32_t i = 0; i < num_examples; ++i)
  54.           {
  55.             CHOGFeatures* hf = (CHOGFeatures*) m_features->get_sample(i);
  56.             CBoundingBox* bb = (CBoundingBox*) m_labels->get_latent_label(i);
  57.             memcpy(psi_m.matrix+i*dim, hf->hog[bb->x_pos][bb->y_pos], dim*sizeof(float64_t));
  58.           }
  59.         CDenseFeatures<float64_t>* psi_feats = new CDenseFeatures<float64_t>(psi_m);
  60.         return psi_feats;
  61.       };
  62.       virtual CData* infer_latent_variable(const SGVector<float64_t>& w, index_t idx)
  63.       {
  64.         int32_t pos_x = 0, pos_y = 0;
  65.         float64_t max_score = -CMath::INFTY;
  66.         CHOGFeatures* hf = (CHOGFeatures*) m_features->get_sample(idx);
  67.         for (int i = 0; i < hf->width; ++i)
  68.           {
  69.             for (int j = 0; j < hf->height; ++j)
  70.               {
  71.                 float64_t score = w.dot(w.vector, hf->hog[i][j], w.vlen);
  72.                 if (score > max_score)
  73.                   {
  74.                     pos_x = i;
  75.                     pos_y = j;
  76.                     max_score = score;
  77.                   }
  78.               }
  79.           }
  80.         SG_SDEBUG("%d %d %f\n", pos_x, pos_y, max_score);
  81.         CBoundingBox* h = new CBoundingBox(pos_x, pos_y);
  82.         SG_REF(h);
  83.         return h;
  84.       };
  85.     };
  86.   }      
  87. %}
  88. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement