Example #1
0
void extractSamples(ImageFileReader * fileReader,
                    const vector<ImageAnnotation> & annotations,
                    Features & features,
                    Mat & samples,
                    Mat & responses,
                    int & samplesNum,
                    bool doMirror = false)
{
    size_t n = 2 * getTotalBboxesNum(annotations);
    if (samples.empty())
    {
        samples.create(n, features.getTotalFeatureVectorLength(), CV_32F);
        responses.create(n, 1, CV_32F);
        samplesNum = 0;
    }
    else if (static_cast<size_t>(samples.rows) < samplesNum + n)
    {
        samples.resize(samplesNum + n);
        responses.resize(samplesNum + n);
    }
    CV_Assert(samples.cols == features.getTotalFeatureVectorLength());
    for (size_t i = 0; i < annotations.size(); ++i)
    {
        const ImageAnnotation & ann = annotations[i];
        for (auto j = ann.sources.begin(); j != ann.sources.end(); ++j)
        {
            fileReader->addSource(j->first, j->second);
        }
        fileReader->open();
        CV_Assert(fileReader->grab());
        SourcesMap sources;
        CV_Assert(fileReader->retrieve(sources, features));
        fileReader->release();

        size_t m = ann.bboxes.size();
        Mat sample = samples.rowRange(samplesNum, samplesNum + m);
        features.getROIDescription(sample, sources, ann.bboxes);

        for (size_t j = 0; j < ann.bboxes.size(); ++j)
        {
            responses.at<float>(samplesNum + j) = static_cast<float>(ann.labels[j]);
        }
        samplesNum += m;


        if (doMirror)
        {
            flipSources(sources);
            ImageAnnotation flipedAnn = getFlipedAnnotation(ann, sources);
            m = flipedAnn.bboxes.size();
            Mat sample = samples.rowRange(samplesNum, samplesNum + m);
            features.getROIDescription(sample, sources, flipedAnn.bboxes);
            for (size_t j = 0; j < flipedAnn.bboxes.size(); ++j)
            {
                responses.at<float>(samplesNum + j) = static_cast<float>(flipedAnn.labels[j]);
            }
            samplesNum += m;
        }
    }
}
Correspondences findCorrespondences(const Features &set1, const Features &set2) {
	Correspondences correspondences;
	for(size_t index1 = 0; index1 < set1.size(); ++index1) {
		FeaturePtr feature1 = set1[index1];

		double bestCost = INF;
		double secondBestCost = bestCost;

		FeaturePtr best;
		for(size_t index2 = 0; index2 < set2.size(); ++index2) {
			FeaturePtr feature2 = set2[index2];
			double cost = feature1->compare(feature2.get());
			if(cost < bestCost) {
				secondBestCost = bestCost;
				bestCost = cost;
				best = feature2;
			}
		}

		// Make sure the best correspondence is a certain ratio better than
		// the second best feature (to avoid repetitive image features)
		if(bestCost < INF && bestCost < 0.5 && bestCost < 0.8*secondBestCost)
			correspondences.push_back(Correspondence(feature1, best));
	}
	return correspondences;
}
void ExtractFeaturesFromEachFrame(const std::string & video_source,
                                  std::vector<Features> features_collection,
                                  const cv::Size & window_size,
                                  bool scale) {
  std::srand(std::time(0));

  cv::VideoCapture video(video_source);
  if(!video.isOpened()) return;

  cv::HOGDescriptor hog;
  hog.winSize=window_size;

  cv::Mat frame;
  cv::Mat extracted_frame;
  Features features;
  while(video.read(frame)) {
    if(scale) cv::resize(frame, extracted_frame, window_size);
    else {
      // Extract frame
      cv::Rect patch;
      patch.width=window_size.width;
      patch.height=window_size.height;
      patch.x=std::rand()%(frame.cols-window_size.width);
      patch.y=std::rand()%(frame.rows-window_size.height);
      extracted_frame=((frame)(patch)).clone();

      features_collection.push_back(Features((features_collection.size()==0?0:features_collection.front().size())));
      ComputeFeatures(extracted_frame, features_collection.back(), window_size);
    }
    features.clear();
  }
}
void LogLinearModel::ComputeNegativeFeatureExpectation (LogProbability& _rLogProb,
														Features_vec_t& _rvecFeatures, 
														double _dReward,
														double_Vec_t& _rvecExpectedFeatures)
{
	size_t iProbs = _rLogProb.Size ();
	if (0 == iProbs)
		return;

	Probability oProb (_rLogProb);
	for (size_t i = 0; i < iProbs; ++ i)
	{
		Features* pFeatures = _rvecFeatures [i];
		double dProb = oProb [i] * _dReward;

		for (int f = 0; f < pFeatures->Size (); ++ f)
		{
			size_t iIndex = pFeatures->Index (f);
			if (iIndex >= _rvecExpectedFeatures.Size ())
				cerr << "[ERROR] out-of-bounds " << iIndex
					 << " >= " << _rvecExpectedFeatures.Size () 
					 << endl;
			_rvecExpectedFeatures [pFeatures->Index (f)] -= dProb * pFeatures->Feature (f);
		}
	}
}
Example #5
0
void AbstractDetector::setExistingFeatures(const Features& fts)
{
  std::for_each(fts.begin(), fts.end(), [&](Feature* i){
    grid_occupancy_.at(
        static_cast<int>(i->px[1]/cell_size_)*grid_n_cols_
        + static_cast<int>(i->px[0]/cell_size_)) = true;
  });
}
Example #6
0
static int UpdateCurFeature(){	
	double angle = -features[features.size() - 1].angle;
	unsigned int yAxisNumber = features[features.size() - 1].number;
	for (unsigned int i = 0; i < n; i++){
		curFeature[i] = curFeature[i]*cos(angle) - x[yAxisNumber][i]*sin(angle);
	}
	return 0;
}
		auto loss(VectorXdRef coeffs, Features input, Features target) const {
			auto weights = unravel(coeffs.block(0, 0, _hidden_units * input.rows(), 1), _hidden_units, input.rows());
			auto hidden_intercepts = coeffs.block(_hidden_units * input.rows(), 0, _hidden_units, 1);
			auto reconstruction_intercepts = coeffs.block(_hidden_units * input.rows() + _hidden_units, 0, input.rows(), 1);

			return implementations::autoencoder::loss(_hidden_activation, _reconstruction_activation, weights, hidden_intercepts,
				weights.transpose(), reconstruction_intercepts, _regularization, input, target);
		}
/**
 * Gets the features this factory will make ready on constructed contacts.
 *
 * \return The set of features.
 */
Features ContactFactory::features() const
{
    Features features = mPriv->features;
    // FeatureAvatarData depends on FeatureAvatarToken
    if (features.contains(Contact::FeatureAvatarData) &&
        !features.contains(Contact::FeatureAvatarToken)) {
        features.insert(Contact::FeatureAvatarToken);
    }

    return features;
}
Features ChannelFactory::featuresFor(const ChannelClassSpec &channelClass) const
{
    Features features;

    foreach (const ChannelClassFeatures &pair, mPriv->features) {
        if (pair.first.isSubsetOf(channelClass)) {
            features.unite(pair.second);
        }
    }

    return features;
}
double LogLinearModel::ComputeLogProb (Features& _rFeatures)
{
	double dResult = 0;

	for (int i = 0; i < _rFeatures.Size (); ++ i)
	{
		int iIndex = _rFeatures.Index (i);
		if (iIndex >= i_Features)
			continue;
		dResult += _rFeatures.Feature (i) * vec_Weights [iIndex];
	}
	
	return dResult;
}
Example #11
0
//construit une instance de la classe a partir des images sources A et Ap
//à un level l, on cherchera une correspondance en utilisant un vosinage mgk
//au niveau l et mpk au niveau inferrieur
annUse::annUse(Image* A,Image* Ap,int l, int mpk,int mgk, FeatureType Type)
{
  pk=mpk;
  gk=mgk;
  int m,M;

  uint32 largeurA = A->getLargeur(l);
  if (largeurA/2 != (largeurA-1)/2) {
    largeurA--;
  }
  uint32 hauteurA = A->getHauteur(l);
  if (hauteurA/2 != (hauteurA-1)/2) {
    hauteurA--;
  }

  if(gk>=2*pk) {
    m=gk;
    M=gk+1;
  } else {
    m=pk*2;
    M=pk*2+1;
  }
  int gK=gk*2+1;
  int pK=pk*2+1;
  Features f;
  //calcul de la taille d'un point en fonction de la taille du voisinnage
  dim=(gK*gK+pK*pK*2+(gK*gK)/2)*f.getNBChamps(Type);
  eps=0;
  
  //calcul du nombre de points
  nPts= (largeurA-m-M)*(hauteurA-m-M);

  //allocation des ressources
  dataPts = annAllocPts(nPts, dim);
  nnIdx = new ANNidx[1];
  dists = new ANNdist[1];

  //remplissage du tableau dataPts
  int i=0;
  for(uint32 y=m; y<hauteurA-M  ; y++)
    for(uint32 x=m; x<largeurA-M; x++)
      {
	makeANNPoint(A,Ap,l,x,y,Type,dataPts[i]);
	i++;
      }
	  
  //construction de l'arbre de recherche
  kdTree = new ANNkd_tree( dataPts, nPts, dim);    
}
Example #12
0
//-----------------------------------------------------------------------------
void CompilerInfo::FillCxx14Features( Features& features )
{
    (void)features;

#ifdef CXX14_DECLTYPE_AUTO
    features.emplace_back( "cxx14_decltype_auto" );
#endif

#ifdef CXX14_INIT_CAPTURES
    features.emplace_back( "cxx14_init_captures" );
#endif

#ifdef CXX14_RELAXED_CONSTEXPR
    features.emplace_back( "cxx14_relaxed_constexpr" );
#endif
}
Example #13
0
void drawMatchesRelative(const Features& train, const Features& query, const std::vector<cv::DMatch>& matches,
                         Mat& img, const vector<unsigned char>& mask)
{
    for (int i = 0; i < (int)matches.size(); i++)
    {
        if (mask.empty() || mask[i])
        {
            Point2f pt_new = query.pts()[matches[i].queryIdx];
            Point2f pt_old = train.pts()[matches[i].trainIdx];
            Point2f dist = pt_new - pt_old;

            cv::line(img, pt_new, pt_old, Scalar(125, 255, 125), 1);
            cv::circle(img, pt_new, 2, Scalar(255, 0, 125), 1);

        }
    }
}
Example #14
0
//-----------------------------------------------------------------------------
void CompilerInfo::FillCxx17Features( Features& features )
{
    (void)features;

#ifdef CXX17_NESTED_NAMESPACES
    features.emplace_back( "cxx17_nested_namespaces" );
#endif
}
//Fit inside a scale_n x scale_n box inside [scale_N,scale_N]^2
void normalize(Features &fs)
{
  float x=fs[0].x;
  float X=fs[0].x;
  float y=fs[0].y;
  float Y=fs[0].y;
  for (int i=0;i<fs.size();i++) {
    x=min(fs[i].x,x);
    X=max(fs[i].x,X);
    y=min(fs[i].y,y);
    Y=max(fs[i].y,Y);
  }
  float scaleF=scale_n/max(max(X-x,Y-y),0.0001f);
  for (int i=0;i<fs.size();i++) {
    fs[i].x=(fs[i].x-0.5*(X+x))*scaleF+0.5*scale_N;
    fs[i].y=(fs[i].y-0.5*(Y+y))*scaleF+0.5*scale_N;
  }
}
Example #16
0
//-----------------------------------------------------------------------------
void CompilerInfo::DumpSupportedFeatures()
{
    Features features;

    FillMacros( features );
    FillCxx11Features( features );
    FillCxx14Features( features );
    FillCxx17Features( features );

    std::sort( features.begin(), features.end() );

    ScopedParagraph paragraph( COMPILER_IDENTIFICATION );

    for (const auto& feature : features)
    {
        std::cout << "> " << feature << std::endl;
    }
}
void featureCloudJiggle(Features &features, RNG &rng, float max_delta)
{
  float dx=rng.uniform(-max_delta,max_delta);
  float dy=rng.uniform(-max_delta,max_delta);
  for (int i=0;i<features.size();i++) {
    features[i].x+=dx;
    features[i].y+=dy;
  }
}
Example #18
0
//construit un ANNpoint pour le point (x,y) des images I et Ip
ANNpoint annUse::makeANNPoint(Image* I,Image* Ip,int l,uint32 x, uint32 y, FeatureType Type, ANNpoint p)
{
  Features f;
  
  //ajout des valeurs de I au niveau l
  int j=0;
  for(uint32 y1=y-gk ; y1<=y+gk ; y1++)
    for(uint32 x1=x-gk ; x1<=x+gk ; x1++)
      {
	I->getFeature(x1,y1,l)->add(p,j,Type);
	j+=f.getNBChamps(Type);
      }

  //ajout des valeurs de Ip au niveau l
  for(uint32 y1=y-gk;y1<=y+gk;y1++) {
    uint32 x1;
    for(x1=x-gk;x1<=x+gk;x1++) {
      if(x1==x && y1==y)
	break;
      Ip->getFeature(x1,y1,l)->add(p,j,Type);
      j+=f.getNBChamps(Type);
    }
    if(x1==x && y1==y)
      break;
  }
  
  uint xp=x/2;
  uint yp=y/2;
  //ajout des valeurs de I au niveau l-1
  for(uint32 y1=yp-pk;y1<=yp+pk;y1++)
    for(uint32 x1=xp-pk;x1<=xp+pk;x1++) {
      I->getFeature(x1,y1,l+1)->add(p,j,Type);
      j+=f.getNBChamps(Type);
    }
  
  //ajout des valeurs de Ip au niveau l-1
  for(uint32 y1=yp-pk;y1<=yp+pk;y1++)
    for(uint32 x1=xp-pk;x1<=xp+pk;x1++) {
      Ip->getFeature(x1,y1,l+1)->add(p,j,Type);
      j+=f.getNBChamps(Type);
    }
  return p;
}
Example #19
0
int main(int argc, char *argv[])
{
	CmdLine cmd;
	std::string img_name;
	cmd.add(make_option('i', img_name, "imgname"));
	try {
		if (argc == 1) throw std::string("Invalid command line parameter.");
		cmd.process(argc, argv);
	}
	catch (const std::string& s) {
		std::cerr << "Feature detector \nUsage: " << argv[0] << "\n"
			<< "[-i|--imgname name]\n"
			<< std::endl;

		std::cerr << s << std::endl;
		return EXIT_FAILURE;
	}
	cv::Mat img(cv::imread(img_name, 0));
	assert(img.type() == CV_8UC1 && !img.empty());

	AbstractCamera* cam = new PinholeCamera(752, 480, 315.5, 315.5, 376.0, 240.0);
	FramePtr frame(new Frame(cam, img, 0.0));
	Features fts;
	FastDetector fast_detector(img.cols, img.rows, 25, 3);
	Timer t;
	for (int i = 0; i < 100; ++i)
	{
		fast_detector.detect(frame.get(), frame->img_pyr_, 20.0, fts);
	}
	printf("Fast corner detection took %f ms, %d corners detected (ref i7-W520: 7.166360ms, 40000)\n", t.stop() * 10, fts.size());
	printf("Note, in this case, feature detection also contains the cam2world projection of the feature.\n");
	cv::Mat img_rgb = cv::Mat(img.size(), CV_8UC3);
	cv::cvtColor(img, img_rgb, CV_GRAY2RGB);
	std::for_each(fts.begin(), fts.end(), [&](Feature* i){
		cv::circle(img_rgb, cv::Point2f(i->px[0], i->px[1]), 4 * (i->level + 1), cv::Scalar(0, 255, 0), 1);
	});
	cv::imshow("ref_img", img_rgb);
	cv::waitKey(0);

	std::for_each(fts.begin(), fts.end(), [&](Feature* i){ delete i; });
	return 0;
}
		auto gradient(VectorXdRef coeffs, Features input, Features target) const {
			auto weights = unravel(coeffs.block(0, 0, _hidden_units * input.rows(), 1), _hidden_units, input.rows());
			auto hidden_intercepts = coeffs.block(_hidden_units * input.rows(), 0, _hidden_units, 1);
			auto reconstruction_intercepts = coeffs.block(_hidden_units * input.rows() + _hidden_units, 0, input.rows(), 1);

			MatrixXd weights_grad, weights_transp_grad;
			VectorXd hid_inter_grad, rec_inter_grad;
			tie(weights_grad, hid_inter_grad, weights_transp_grad, rec_inter_grad) = implementations::autoencoder::gradient(_hidden_activation,
				_reconstruction_activation, weights, hidden_intercepts, weights.transpose(), reconstruction_intercepts, _regularization, 
				input, target);

			VectorXd gradient(coeffs.rows());

			gradient.block(0, 0, weights_grad.size(), 1) = ravel(weights_grad + weights_transp_grad.transpose());
			gradient.block(weights_grad.size(), 0, hid_inter_grad.size(), 1) = hid_inter_grad;

			gradient.block(weights_grad.size() + hid_inter_grad.size(),
				0, rec_inter_grad.size(), 1) = rec_inter_grad;

			return gradient;
		}
Example #21
0
void decode(JKArgs& args)
{
	//pbmt -decode -pt=[phrase table file] -dmax=[max distortion limit] -beam=[beam size] -weights=[d:d:d...]"<<endl;
	if(!args.is_set("pt"))usage();
	
	//pt.print(cout);
	int dmax=4;
	int beamsize=100;
	Features weights;
	weights.s2t=weights.t2s=weights.s2tLex=weights.t2sLex=weights.length=weights.distort=0;
	weights.s2t=1;
	weights.distort=1;

	if(args.is_set("dmax"))dmax=atoi(args.value("dmax").c_str());
	if(args.is_set("beam"))beamsize=atoi(args.value("beam").c_str());
	if(args.is_set("weights"))weights.init(args.value("weights"));
	
	bool debug=false;
	int tlimit=10;
	if(args.is_set("tlimit"))tlimit=atoi(args.value("tlimit").c_str());
	if(args.is_set("debug")) debug=true;
	PhraseTable pt;
	pt.load(args.value("pt"));
	pt.logrize(10);
	pt.makeVector();
	pt.sortVector(weights);

	while(!cin.eof())
	{
		string curline="";
		getline(cin,curline);
		if(curline=="")continue;
		SearchSpace space;
		space.init(curline);
		space.setupAuxSpace(pt,weights);
		space.copeUNK(pt);
		space.beamSearch(pt,weights,beamsize,dmax,tlimit,debug);
		cout<<space.getNthTranslation(1)<<endl;
	}
}
Example #22
0
bool FeatureRef::applyValToFeature(uint32 val, Features & pDest) const
{ 
    if (val>maxVal() || !m_pFace)
      return false;
    if (pDest.m_pMap==NULL)
      pDest.m_pMap = &m_pFace->theSill().theFeatureMap();
    else
      if (pDest.m_pMap!=&m_pFace->theSill().theFeatureMap())
        return false;       //incompatible
    pDest.reserve(m_index);
    pDest[m_index] &= ~m_mask;
    pDest[m_index] |= (uint32(val) << m_bits);
    return true;
}
Example #23
0
void FastDetector::detect(
    Frame* frame,
    const ImgPyr& img_pyr,
    const double detection_threshold,
    Features& fts)
{
  Corners corners(grid_n_cols_*grid_n_rows_, Corner(0,0,detection_threshold,0,0.0f));
  for(int L=0; L<n_pyr_levels_; ++L)
  {
    const int scale = (1<<L);
    vector<fast::fast_xy> fast_corners;
#if __SSE2__
      fast::fast_corner_detect_10_sse2(
          (fast::fast_byte*) img_pyr[L].data, img_pyr[L].cols,
          img_pyr[L].rows, img_pyr[L].cols, 20, fast_corners);
#elif HAVE_FAST_NEON
      fast::fast_corner_detect_9_neon(
          (fast::fast_byte*) img_pyr[L].data, img_pyr[L].cols,
          img_pyr[L].rows, img_pyr[L].cols, 20, fast_corners);
#else
      fast::fast_corner_detect_10(
          (fast::fast_byte*) img_pyr[L].data, img_pyr[L].cols,
          img_pyr[L].rows, img_pyr[L].cols, 20, fast_corners);
#endif
    vector<int> scores, nm_corners;
    fast::fast_corner_score_10((fast::fast_byte*) img_pyr[L].data, img_pyr[L].cols, fast_corners, 20, scores);
    fast::fast_nonmax_3x3(fast_corners, scores, nm_corners);

    for(auto it=nm_corners.begin(), ite=nm_corners.end(); it!=ite; ++it)
    {
      fast::fast_xy& xy = fast_corners.at(*it);
      const int k = static_cast<int>((xy.y*scale)/cell_size_)*grid_n_cols_
                  + static_cast<int>((xy.x*scale)/cell_size_);
      if(grid_occupancy_[k])
        continue;
      const float score = vk::shiTomasiScore(img_pyr[L], xy.x, xy.y);
      if(score > corners.at(k).score)
        corners.at(k) = Corner(xy.x*scale, xy.y*scale, score, L, 0.0f);
    }
  }

  // Create feature for every corner that has high enough corner score
  std::for_each(corners.begin(), corners.end(), [&](Corner& c) {
    if(c.score > detection_threshold)
      fts.push_back(new Feature(frame, Vector2d(c.x, c.y), c.level));
  });

  resetGrid();
}
Example #24
0
//-----------------------------------------------------------------------------
void CompilerInfo::FillMacros( Features& features )
{
    (void)features;

#ifdef CXX_HAS_ATTRIBUTE
    features.emplace_back( "cxx_macro_has_attribute" );
#endif

#ifdef CXX_HAS_ATTRIBUTE_CPP
    features.emplace_back( "cxx_macro_has_attribute_cpp" );
#endif

#ifdef CXX_HAS_ATTRIBUTE_DECLSPEC
    features.emplace_back( "cxx_macro_has_attribute_declspec" );
#endif

#ifdef CXX_HAS_BUILTIN
    features.emplace_back( "cxx_macro_has_builtin" );
#endif

#ifdef CXX_HAS_EXTENSION
    features.emplace_back( "cxx_macro_has_extension" );
#endif

#ifdef CXX_HAS_FEATURE
    features.emplace_back( "cxx_macro_has_feature" );
#endif

#ifdef CXX_HAS_INCLUDE
    features.emplace_back( "cxx_macro_has_include" );
#endif

#ifdef CXX_HAS_INCLUDE_NEXT
    features.emplace_back( "cxx_macro_has_include_next" );
#endif

#ifdef CXX_HAS_WARNING
    features.emplace_back( "cxx_macro_has_warning" );
#endif
}
Example #25
0
static unsigned int GetRay(TPoint *ray){
	ray->resize(d);
	double drivingAxis = 1;
	for (unsigned int i = features.size() - 1; i > 0; i--){
		(*ray)[features[i].number] = drivingAxis*sin(features[i].angle);
		drivingAxis = drivingAxis*cos(features[i].angle);
	}
	(*ray)[features[0].number] = drivingAxis;
	
	UPoints points(n);
	for (unsigned int i = 0; i < n; i++){
		points[i].pattern = y[i];
		for (unsigned int j = 0; j < d; j++){
			points[i].value += (*ray)[j]*x[j][i];
		}
#ifdef DEF_OUT_ALPHA
		if (OUT_ALPHA) Rcout << points[i].value << ", ";
#endif
	}
#ifdef DEF_OUT_ALPHA
	if (OUT_ALPHA) Rcout << endl;
#endif
	sort(points.begin(), points.end(), Compare);
	unsigned int numLeftLess = 0;
	unsigned int numLeftMore = 0;
	for (unsigned int i = 0; i < n; i++){
		if (points[i].value > 0){break;}
		if (points[i].pattern > 0){numLeftMore++;}else{numLeftLess++;}
	}
	unsigned int errorLeftLess = numLeftMore + numLess - numLeftLess;
	unsigned int errorLeftMore = numLeftLess + numMore - numLeftMore;
	if (errorLeftLess > errorLeftMore){
		for (unsigned int i = 0; i < d; i++){
			(*ray)[i] *= -1.;
		}
	}
#ifdef DEF_OUT_ALPHA
	if (OUT_ALPHA){
	Rcout << errorLeftLess << " " << errorLeftMore << " ";
	}
#endif
	return 0;
}
Example #26
0
void FeatureGenerator::writeFeatures(std::ostream& out, Session& session, StatsCollector& stats) {

    std::unordered_set<int> seen_queries;
    std::unordered_set<int> seen_terms;
    std::unordered_set<int> seen_urls;
    std::unordered_set<int> seen_domains;
    std::unordered_set<int> clicked_urls;
    std::unordered_set<int> clicked_domains;

    int test_query_index = -1;
    int clicks = 0;
    int dwell = 0;
    for (int i = 0; i < session.queries.size(); ++i) {
        Query& q = session.queries[i];
        if (q.is_test) {
            test_query_index = i;
            break;
        }
        for (int j = 0; j < q.docs.size(); ++j) {
            seen_urls.insert(q.docs[j].first);
            seen_domains.insert(q.docs[j].second);
        }
        for (int j = 0; j < q.clicks.size(); ++j) {
            clicked_urls.insert(q.clicks[j].url_id);
            clicked_domains.insert(q.docs[q.urlid2pos[q.clicks[j].url_id]].second);
            ++clicks;
            dwell += q.clicks[j].dwell;
        }

        for (int j = 0; j < q.terms.size(); ++j) {
            seen_terms.insert(q.terms[j]);
        }

        seen_queries.insert(q.id);
    }

    if (test_query_index == -1) {
        std::cerr << "NO TEST QUERY IN SESSION " << session.id << std::endl;
        return;
    }
    const Query& test_query = session.queries[test_query_index];

    for (int i = 0; i < test_query.docs.size(); ++i) {
        int url_id = test_query.docs[i].first;
        int domain_id = test_query.docs[i].second;

        Features feats;

        if (stats.query_entropy.find(test_query.id) != stats.query_entropy.end()) {
            feats.qclicks1 = 1.0 * stats.query_entropy[test_query.id].posclicks[0] / (stats.query_entropy[test_query.id].clicks + 1);
            feats.qclicks2 = 1.0 * stats.query_entropy[test_query.id].posclicks[1] / (stats.query_entropy[test_query.id].clicks + 1);
            feats.qclicks3 = 1.0 * stats.query_entropy[test_query.id].posclicks[2] / (stats.query_entropy[test_query.id].clicks + 1);
            feats.qclicks4 = 1.0 * stats.query_entropy[test_query.id].posclicks[3] / (stats.query_entropy[test_query.id].clicks + 1);
            feats.qclicks5 = 1.0 * stats.query_entropy[test_query.id].posclicks[4] / (stats.query_entropy[test_query.id].clicks + 1);
            feats.qclicks6 = 1.0 * stats.query_entropy[test_query.id].posclicks[5] / (stats.query_entropy[test_query.id].clicks + 1);
            feats.qclicks7 = 1.0 * stats.query_entropy[test_query.id].posclicks[6] / (stats.query_entropy[test_query.id].clicks + 1);
            feats.qclicks8 = 1.0 * stats.query_entropy[test_query.id].posclicks[7] / (stats.query_entropy[test_query.id].clicks + 1);
            feats.qclicks9 = 1.0 * stats.query_entropy[test_query.id].posclicks[8] / (stats.query_entropy[test_query.id].clicks + 1);
            feats.qclicks10 = 1.0 * stats.query_entropy[test_query.id].posclicks[9] / (stats.query_entropy[test_query.id].clicks + 1);
            feats.qentropy = stats.query_entropy[test_query.id].getEntropy();
        }

        if (stats.user_entropy.find(session.user_id) != stats.user_entropy.end()) {
            feats.uclicks1 = 1.0 * stats.user_entropy[test_query.id].posclicks[0] / (stats.user_entropy[test_query.id].clicks + 1);
            feats.uclicks2 = 1.0 * stats.user_entropy[test_query.id].posclicks[1] / (stats.user_entropy[test_query.id].clicks + 1);
            feats.uclicks3 = 1.0 * stats.user_entropy[test_query.id].posclicks[2] / (stats.user_entropy[test_query.id].clicks + 1);
            feats.uclicks4 = 1.0 * stats.user_entropy[test_query.id].posclicks[3] / (stats.user_entropy[test_query.id].clicks + 1);
            feats.uclicks5 = 1.0 * stats.user_entropy[test_query.id].posclicks[4] / (stats.user_entropy[test_query.id].clicks + 1);
            feats.uclicks6 = 1.0 * stats.user_entropy[test_query.id].posclicks[5] / (stats.user_entropy[test_query.id].clicks + 1);
            feats.uclicks7 = 1.0 * stats.user_entropy[test_query.id].posclicks[6] / (stats.user_entropy[test_query.id].clicks + 1);
            feats.uclicks8 = 1.0 * stats.user_entropy[test_query.id].posclicks[7] / (stats.user_entropy[test_query.id].clicks + 1);
            feats.uclicks9 = 1.0 * stats.user_entropy[test_query.id].posclicks[8] / (stats.user_entropy[test_query.id].clicks + 1);
            feats.uclicks10 = 1.0 * stats.user_entropy[test_query.id].posclicks[9] / (stats.user_entropy[test_query.id].clicks + 1);
            feats.uentropy = stats.user_entropy[test_query.id].getEntropy();
        }

        if (stats.overall_stats.url_stats.find(url_id) != stats.overall_stats.url_stats.end()) {
            ClickStats& cur_stats = stats.overall_stats.url_stats[url_id].overall;

            // Just URL
            feats.u_shows = cur_stats.shows;
            feats.u_clicks = cur_stats.clicks;
            feats.u_ctr = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 100);
            feats.u_ctr2 = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 10);
            feats.u_satctr = 1.0 * (cur_stats.sat_clicks + 1) / (cur_stats.shows + 100);
            feats.u_ssatctr = 1.0 * (cur_stats.ssat_clicks + 1) / (cur_stats.shows + 100);
            feats.u_avedwell = 1.0 * (cur_stats.dwell) / (cur_stats.clicks + 1);
            feats.u_avepos = 1.0 * cur_stats.pos / (cur_stats.shows + 1);
            feats.u_lastinq_clicks = cur_stats.last_in_query;
            feats.u_lastinq_ctr = 1.0 * cur_stats.last_in_query / (cur_stats.clicks + 1);
            feats.u_lastinsession_clicks = cur_stats.last_in_session;
            feats.u_lastinsession_ctr = 1.0 * cur_stats.last_in_session / (cur_stats.clicks + 1);

            // QUERY - URL
            if (stats.overall_stats.url_stats[url_id].query_clicks.find(test_query.id) != stats.overall_stats.url_stats[url_id].query_clicks.end()) {
                cur_stats = stats.overall_stats.url_stats[url_id].query_clicks[test_query.id];

                feats.qu_shows = cur_stats.shows;
                feats.qu_clicks = cur_stats.clicks;
                feats.qu_ctr = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 100);
                feats.qu_ctr2 = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 10);
                feats.qu_satctr = 1.0 * (cur_stats.sat_clicks + 1) / (cur_stats.shows + 100);
                feats.qu_ssatctr = 1.0 * (cur_stats.ssat_clicks + 1) / (cur_stats.shows + 100);
                feats.qu_avedwell = 1.0 * cur_stats.dwell / (cur_stats.clicks + 1);
                feats.qu_avepos = 1.0 * cur_stats.pos / (cur_stats.shows + 1);
                
                feats.qu_lastinq_clicks = cur_stats.last_in_query;
                feats.qu_lastinq_ctr = 1.0 * cur_stats.last_in_query / (cur_stats.clicks + 1);
                feats.qu_lastinsession_clicks = cur_stats.last_in_session;
                feats.qu_lastinsession_ctr = 1.0 * cur_stats.last_in_session / (cur_stats.clicks + 1);
            }

            // URL for terms
            feats.q_terms = test_query.terms.size();
            feats.tu_shows = 0;
            feats.tu_clicks = 0;
            feats.tu_ctr = 0;
            feats.tu_ctr2 = 0;
            feats.tu_avedwell = 0;
            for (int k = 0; k < test_query.terms.size(); ++k) {
                if (stats.overall_stats.url_stats[url_id].term_clicks.find(test_query.terms[k]) != stats.overall_stats.url_stats[url_id].term_clicks.end()) {
                    cur_stats = stats.overall_stats.url_stats[url_id].term_clicks[test_query.terms[k]];

                    feats.tu_shows += cur_stats.shows;
                    feats.tu_clicks += cur_stats.clicks;
                    feats.tu_ctr += 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 100);
                    feats.tu_ctr2 += 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 10);
                    feats.tu_avedwell += 1.0 * (cur_stats.dwell) / (cur_stats.clicks + 1);
                }
            }
            feats.tu_shows /= test_query.terms.size();
            feats.tu_clicks /= test_query.terms.size();
            feats.tu_ctr /= test_query.terms.size();
            feats.tu_ctr2 /= test_query.terms.size();
            feats.tu_avedwell /= test_query.terms.size();

            feats.prevtu_ctr = 0;
            // Previous terms
            for (auto it = seen_terms.begin(); it != seen_terms.end(); ++it) {
                if (stats.overall_stats.url_stats[url_id].term_clicks.find(*it) != stats.overall_stats.url_stats[url_id].term_clicks.end()) {
                    feats.prevtu_ctr = std::max(feats.prevtu_ctr, 1.0 * (stats.overall_stats.url_stats[url_id].term_clicks[*it].clicks) / (stats.overall_stats.url_stats[url_id].term_clicks[*it].shows + 100));
                }
            }

            // URL for previous urls and queries
            feats.max_u_prevq_ctr = 0;
            feats.max_u_prevu_ctr = 0;
            for (auto it = seen_queries.begin(); it != seen_queries.end(); ++it) {
                if (stats.overall_stats.url_stats[url_id].prev_query_clicks.find(*it) != stats.overall_stats.url_stats[url_id].prev_query_clicks.end()) {
                    feats.max_u_prevq_ctr = std::max(feats.max_u_prevq_ctr, 1.0 * (stats.overall_stats.url_stats[url_id].prev_query_clicks[*it].clicks) / (stats.overall_stats.url_stats[url_id].prev_query_clicks[*it].shows + 1));
                }
            }

            for (auto it = clicked_urls.begin(); it != clicked_urls.end(); ++it) {
                if (stats.overall_stats.url_stats[url_id].coclicks.find(*it) != stats.overall_stats.url_stats[url_id].coclicks.end()) {
                    feats.max_u_prevu_ctr = std::max(feats.max_u_prevu_ctr, 1.0 * (stats.overall_stats.url_stats[url_id].coclicks[*it].clicks) / (stats.overall_stats.url_stats[url_id].coclicks[*it].shows + 1));
                }
            }

        }

        // Domains
        if (stats.overall_stats.domain_stats.find(domain_id) != stats.overall_stats.domain_stats.end()) {
            ClickStats& cur_stats = stats.overall_stats.domain_stats[domain_id].overall;

            feats.d_shows = cur_stats.shows;
            feats.d_clicks = cur_stats.clicks;
            feats.d_ctr = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 100);
            feats.d_ctr2 = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 10);
            feats.d_satctr = 1.0 * (cur_stats.sat_clicks + 1) / (cur_stats.shows + 100);
            feats.d_ssatctr = 1.0 * (cur_stats.ssat_clicks + 1) / (cur_stats.shows + 100);
            feats.d_avedwell = 1.0 * (cur_stats.dwell) / (cur_stats.clicks + 1);
            feats.d_avepos = 1.0 * cur_stats.pos / (cur_stats.shows + 1);
            feats.d_lastinq_clicks = cur_stats.last_in_query;
            feats.d_lastinq_ctr = 1.0 * cur_stats.last_in_query / (cur_stats.clicks + 1);
            feats.d_lastinsession_clicks = cur_stats.last_in_session;
            feats.d_lastinsession_ctr = 1.0 * cur_stats.last_in_session / (cur_stats.clicks + 1);

            // Query Domains
            if (stats.overall_stats.domain_stats[domain_id].query_clicks.find(test_query.id) != stats.overall_stats.domain_stats[domain_id].query_clicks.end()) {
                cur_stats = stats.overall_stats.domain_stats[domain_id].query_clicks[test_query.id];
                feats.qd_shows = cur_stats.shows;
                feats.qd_clicks = cur_stats.clicks;
                feats.qd_ctr = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 100);
                feats.qd_ctr2 = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 10);
                feats.qd_satctr = 1.0 * (cur_stats.sat_clicks + 1) / (cur_stats.shows + 100);
                feats.qd_ssatctr = 1.0 * (cur_stats.ssat_clicks + 1) / (cur_stats.shows + 100);
                feats.qd_avedwell = 1.0 * cur_stats.dwell / (cur_stats.clicks + 1);
                feats.qd_avepos = 1.0 * cur_stats.pos / (cur_stats.shows + 1);
                
                feats.qd_lastinq_clicks = cur_stats.last_in_query;
                feats.qd_lastinq_ctr = 1.0 * cur_stats.last_in_query / (cur_stats.clicks + 1);
                feats.qd_lastinsession_clicks = cur_stats.last_in_session;
                feats.qd_lastinsession_ctr = 1.0 * cur_stats.last_in_session / (cur_stats.clicks + 1);
            }

            // Domains - terms
            feats.td_shows = 0;
            feats.td_clicks = 0;
            feats.td_ctr = 0;
            feats.td_ctr2 = 0;
            feats.td_avedwell = 0;
            for (int k = 0; k < test_query.terms.size(); ++k) {
                if (stats.overall_stats.domain_stats[domain_id].term_clicks.find(test_query.terms[k]) != stats.overall_stats.domain_stats[domain_id].term_clicks.end()) {
                    cur_stats = stats.overall_stats.domain_stats[domain_id].term_clicks[test_query.terms[k]];

                    feats.td_shows += cur_stats.shows;
                    feats.td_clicks += cur_stats.clicks;
                    feats.td_ctr += 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 100);
                    feats.td_ctr2 += 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 10);
                    feats.td_avedwell += 1.0 * (cur_stats.dwell) / (cur_stats.clicks + 1);
                }
            }
            feats.td_shows /= test_query.terms.size();
            feats.td_clicks /= test_query.terms.size();
            feats.td_ctr /= test_query.terms.size();
            feats.td_ctr2 /= test_query.terms.size();
            feats.td_avedwell /= test_query.terms.size();


            feats.prevtd_ctr = 0;
            // Previous terms
            for (auto it = seen_terms.begin(); it != seen_terms.end(); ++it) {
                if (stats.overall_stats.domain_stats[domain_id].term_clicks.find(*it) != stats.overall_stats.domain_stats[domain_id].term_clicks.end()) {
                    feats.prevtd_ctr = std::max(feats.prevtd_ctr, 1.0 * (stats.overall_stats.domain_stats[domain_id].term_clicks[*it].clicks) / (stats.overall_stats.domain_stats[domain_id].term_clicks[*it].shows + 100));
                }
            }

            // Domain for previous urls
            feats.max_d_prevq_ctr = 0;
            feats.max_d_prevd_ctr = 0;
            for (auto it = seen_queries.begin(); it != seen_queries.end(); ++it) {
                if (stats.overall_stats.domain_stats[domain_id].prev_query_clicks.find(*it) != stats.overall_stats.domain_stats[domain_id].prev_query_clicks.end()) {
                    feats.max_d_prevq_ctr = std::max(feats.max_d_prevq_ctr, 1.0 * (stats.overall_stats.domain_stats[domain_id].prev_query_clicks[*it].clicks) / (stats.overall_stats.domain_stats[domain_id].prev_query_clicks[*it].shows + 1));
                }
            }

            for (auto it = clicked_domains.begin(); it != clicked_domains.end(); ++it) {
                if (stats.overall_stats.domain_stats[domain_id].coclicks.find(*it) != stats.overall_stats.domain_stats[domain_id].coclicks.end()) {
                    feats.max_d_prevd_ctr = std::max(feats.max_d_prevd_ctr, 1.0 * (stats.overall_stats.domain_stats[domain_id].coclicks[*it].clicks) / (stats.overall_stats.domain_stats[domain_id].coclicks[*it].shows + 1));
                }
            }
        }

        if ((feats.have_user_history = stats.user_stats.find(session.user_id) != stats.user_stats.end())) {
            if (stats.user_stats[session.user_id].url_stats.find(url_id) != stats.user_stats[session.user_id].url_stats.end()) {
                ClickStats& cur_stats = stats.user_stats[session.user_id].url_stats[url_id].overall;

                feats.u_shows_user = cur_stats.shows;
                feats.u_clicks_user = cur_stats.clicks;
                feats.u_ctr_user = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 100);
                feats.u_ctr2_user = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 10);
                feats.u_satctr_user = 1.0 * (cur_stats.sat_clicks + 1) / (cur_stats.shows + 100);
                feats.u_ssatctr_user = 1.0 * (cur_stats.ssat_clicks + 1) / (cur_stats.shows + 100);
                feats.u_avedwell_user = 1.0 * (cur_stats.dwell) / (cur_stats.clicks + 1);
                feats.u_avepos_user = 1.0 * cur_stats.pos / (cur_stats.shows + 1);
                feats.u_lastinq_clicks_user = cur_stats.last_in_query;
                feats.u_lastinq_ctr_user = 1.0 * cur_stats.last_in_query / (cur_stats.clicks + 1);
                feats.u_lastinsession_clicks_user = cur_stats.last_in_session;
                feats.u_lastinsession_ctr_user = 1.0 * cur_stats.last_in_session / (cur_stats.clicks + 1);

                if (stats.user_stats[session.user_id].url_stats[url_id].query_clicks.find(test_query.id) != stats.user_stats[session.user_id].url_stats[url_id].query_clicks.end()) {
                    cur_stats = stats.user_stats[session.user_id].url_stats[url_id].query_clicks[test_query.id];

                    feats.qu_shows_user = cur_stats.shows;
                    feats.qu_clicks_user = cur_stats.clicks;
                    feats.qu_ctr_user = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 100);
                    feats.qu_ctr2_user = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 10);
                    feats.qu_satctr_user = 1.0 * (cur_stats.sat_clicks + 1) / (cur_stats.shows + 100);
                    feats.qu_ssatctr_user = 1.0 * (cur_stats.ssat_clicks + 1) / (cur_stats.shows + 100);
                    feats.qu_avedwell_user = 1.0 * cur_stats.dwell / (cur_stats.clicks + 1);
                    feats.qu_avepos_user = 1.0 * cur_stats.pos / (cur_stats.shows + 1);
                    
                    feats.qu_lastinq_clicks_user = cur_stats.last_in_query;
                    feats.qu_lastinq_ctr_user = 1.0 * cur_stats.last_in_query / (cur_stats.clicks + 1);
                    feats.qu_lastinsession_clicks_user = cur_stats.last_in_session;
                    feats.qu_lastinsession_ctr_user = 1.0 * cur_stats.last_in_session / (cur_stats.clicks + 1);
                }


                feats.tu_shows_user = 0;
                feats.tu_clicks_user = 0;
                feats.tu_ctr_user = 0;
                feats.tu_ctr2_user = 0;
                feats.tu_avedwell_user = 0;
                for (int k = 0; k < test_query.terms.size(); ++k) {
                    if (stats.user_stats[session.user_id].url_stats[url_id].term_clicks.find(test_query.terms[k]) != stats.user_stats[session.user_id].url_stats[url_id].term_clicks.end()) {
                        cur_stats = stats.user_stats[session.user_id].url_stats[url_id].term_clicks[test_query.terms[k]];

                        feats.tu_shows_user += cur_stats.shows;
                        feats.tu_clicks_user += cur_stats.clicks;
                        feats.tu_ctr_user += 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 100);
                        feats.tu_ctr2_user += 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 10);
                        feats.tu_avedwell_user += 1.0 * (cur_stats.dwell) / (cur_stats.clicks + 1);;
                    }
                }
                feats.tu_shows_user /= test_query.terms.size();
                feats.tu_clicks_user /= test_query.terms.size();
                feats.tu_ctr_user /= test_query.terms.size();
                feats.tu_ctr2_user /= test_query.terms.size();
                feats.tu_avedwell_user /= test_query.terms.size();

                feats.prevtu_ctr_user = 0;
                // Previous terms
                for (auto it = seen_terms.begin(); it != seen_terms.end(); ++it) {
                    if (stats.user_stats[session.user_id].url_stats[url_id].term_clicks.find(*it) != stats.user_stats[session.user_id].url_stats[url_id].term_clicks.end()) {
                        feats.prevtu_ctr_user = std::max(feats.prevtu_ctr_user, 1.0 * (stats.user_stats[session.user_id].url_stats[url_id].term_clicks[*it].clicks) / (stats.user_stats[session.user_id].url_stats[url_id].term_clicks[*it].shows + 100));
                    }
                }

                feats.max_u_prevq_ctr_user = 0;
                feats.max_u_prevu_ctr_user = 0;
                for (auto it = seen_queries.begin(); it != seen_queries.end(); ++it) {
                    if (stats.user_stats[session.user_id].url_stats[url_id].prev_query_clicks.find(*it) != stats.user_stats[session.user_id].url_stats[url_id].prev_query_clicks.end()) {
                        feats.max_u_prevq_ctr_user = std::max(feats.max_u_prevq_ctr_user, 1.0 * (stats.user_stats[session.user_id].url_stats[url_id].prev_query_clicks[*it].clicks) / (stats.user_stats[session.user_id].url_stats[url_id].prev_query_clicks[*it].shows + 1));
                    }
                }

                for (auto it = clicked_urls.begin(); it != clicked_urls.end(); ++it) {
                    if (stats.user_stats[session.user_id].url_stats[url_id].coclicks.find(*it) != stats.user_stats[session.user_id].url_stats[url_id].coclicks.end()) {
                        feats.max_u_prevu_ctr_user = std::max(feats.max_u_prevu_ctr_user, 1.0 * (stats.user_stats[session.user_id].url_stats[url_id].coclicks[*it].clicks) / (stats.user_stats[session.user_id].url_stats[url_id].coclicks[*it].shows + 1));
                    }
                }

            }

            // Domains
            if (stats.user_stats[session.user_id].domain_stats.find(domain_id) != stats.user_stats[session.user_id].domain_stats.end()) {
                ClickStats cur_stats = stats.user_stats[session.user_id].domain_stats[domain_id].overall;

                feats.d_shows_user = cur_stats.shows;
                feats.d_clicks_user = cur_stats.clicks;
                feats.d_ctr_user = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 100);
                feats.d_ctr2_user = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 10);
                feats.d_satctr_user = 1.0 * (cur_stats.sat_clicks + 1) / (cur_stats.shows + 100);
                feats.d_ssatctr_user = 1.0 * (cur_stats.ssat_clicks + 1) / (cur_stats.shows + 100);
                feats.d_avedwell_user = 1.0 * (cur_stats.dwell) / (cur_stats.clicks + 1);
                feats.d_avepos_user = 1.0 * cur_stats.pos / (cur_stats.shows + 1);
                feats.d_lastinq_clicks_user = cur_stats.last_in_query;
                feats.d_lastinq_ctr_user = 1.0 * cur_stats.last_in_query / (cur_stats.clicks + 1);
                feats.d_lastinsession_clicks_user = cur_stats.last_in_session;
                feats.d_lastinsession_ctr_user = 1.0 * cur_stats.last_in_session / (cur_stats.clicks + 1);

                if (stats.user_stats[session.user_id].domain_stats[domain_id].query_clicks.find(test_query.id) != stats.user_stats[session.user_id].domain_stats[domain_id].query_clicks.end()) {
                    cur_stats = stats.user_stats[session.user_id].domain_stats[domain_id].query_clicks[test_query.id];

                    feats.qd_shows_user = cur_stats.shows;
                    feats.qd_clicks_user = cur_stats.clicks;
                    feats.qd_ctr_user = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 100);
                    feats.qd_ctr2_user = 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 10);
                    feats.qd_satctr_user = 1.0 * (cur_stats.sat_clicks + 1) / (cur_stats.shows + 100);
                    feats.qd_ssatctr_user = 1.0 * (cur_stats.ssat_clicks + 1) / (cur_stats.shows + 100);
                    feats.qd_avedwell_user = 1.0 * cur_stats.dwell / (cur_stats.clicks + 1);
                    feats.qd_avepos_user = 1.0 * cur_stats.pos / (cur_stats.shows + 1);
                    
                    feats.qd_lastinq_clicks_user = cur_stats.last_in_query;
                    feats.qd_lastinq_ctr_user = 1.0 * cur_stats.last_in_query / (cur_stats.clicks + 1);
                    feats.qd_lastinsession_clicks_user = cur_stats.last_in_session;
                    feats.qd_lastinsession_ctr_user = 1.0 * cur_stats.last_in_session / (cur_stats.clicks + 1);
                }

                feats.td_shows_user = 0;
                feats.td_clicks_user = 0;
                feats.td_ctr_user = 0;
                feats.td_ctr2_user = 0;
                feats.td_avedwell_user = 0;
                for (int k = 0; k < test_query.terms.size(); ++k) {
                    if (stats.user_stats[session.user_id].domain_stats[domain_id].term_clicks.find(test_query.terms[k]) != stats.user_stats[session.user_id].domain_stats[domain_id].term_clicks.end()) {
                        cur_stats = stats.user_stats[session.user_id].domain_stats[domain_id].term_clicks[test_query.terms[k]];

                        feats.td_shows_user += cur_stats.shows;
                        feats.td_clicks_user += cur_stats.clicks;
                        feats.td_ctr_user += 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 100);
                        feats.td_ctr2_user += 1.0 * (cur_stats.clicks + 1) / (cur_stats.shows + 10);
                        feats.td_avedwell_user += 1.0 * (cur_stats.dwell) / (cur_stats.clicks + 1);;
                    }
                }
                feats.td_shows_user /= test_query.terms.size();
                feats.td_clicks_user /= test_query.terms.size();
                feats.td_ctr_user /= test_query.terms.size();
                feats.td_ctr2_user /= test_query.terms.size();
                feats.td_avedwell_user /= test_query.terms.size();

                feats.prevtd_ctr_user = 0;
                // Previous terms
                for (auto it = seen_terms.begin(); it != seen_terms.end(); ++it) {
                    if (stats.user_stats[session.user_id].domain_stats[domain_id].term_clicks.find(*it) != stats.user_stats[session.user_id].domain_stats[domain_id].term_clicks.end()) {
                        feats.prevtd_ctr_user = std::max(feats.prevtd_ctr_user, 1.0 * (stats.user_stats[session.user_id].domain_stats[domain_id].term_clicks[*it].clicks) / (stats.user_stats[session.user_id].domain_stats[domain_id].term_clicks[*it].shows + 100));
                    }
                }

                feats.max_d_prevq_ctr_user = 0;
                feats.max_d_prevd_ctr_user = 0;
                for (auto it = seen_queries.begin(); it != seen_queries.end(); ++it) {
                    if (stats.user_stats[session.user_id].domain_stats[domain_id].prev_query_clicks.find(*it) != stats.user_stats[session.user_id].domain_stats[domain_id].prev_query_clicks.end()) {
                        feats.max_d_prevq_ctr_user = std::max(feats.max_d_prevq_ctr_user, 1.0 * (stats.user_stats[session.user_id].domain_stats[domain_id].prev_query_clicks[*it].clicks) / (stats.user_stats[session.user_id].domain_stats[domain_id].prev_query_clicks[*it].shows + 1));
                    }
                }

                for (auto it = clicked_domains.begin(); it != clicked_domains.end(); ++it) {
                    if (stats.user_stats[session.user_id].domain_stats[domain_id].coclicks.find(*it) != stats.user_stats[session.user_id].domain_stats[domain_id].coclicks.end()) {
                        feats.max_d_prevd_ctr_user = std::max(feats.max_d_prevd_ctr_user, 1.0 * (stats.user_stats[session.user_id].domain_stats[domain_id].coclicks[*it].clicks) / (stats.user_stats[session.user_id].domain_stats[domain_id].coclicks[*it].shows + 1));
                    }
                }
            }
        }

        feats.query_asked = seen_queries.find(test_query.id) != seen_queries.end();
        feats.url_shown = seen_urls.find(url_id) != seen_urls.end();
        feats.url_clicked = clicked_urls.find(url_id) != clicked_urls.end();
        feats.cur_total_dwell = dwell;
        feats.time_passed = test_query.time_passed;
        feats.cur_queries = test_query_index;
        feats.cur_clicks = clicks;
        feats.cur_pos = i;

        int target = 0;
        for (int j = 0; j < test_query.clicks.size(); ++j) {
            if (test_query.clicks[j].url_id == url_id) {
                if (test_query.clicks[j].dwell >= 50)
                    target = 1;
                if (test_query.clicks[j].dwell >= 400)
                    target = 2;
                if (j == test_query.clicks.size() - 1 && test_query_index == session.queries.size() - 1)
                    target = 2;
            }
        }

        out << target << "," << session.id << "," << url_id << "," << feats.toString() << "\n";
    }
}
Example #27
0
int main()
{
	try {
		System myOSF;
		myOSF.init();
		
		// create input object
		InputKinect* input = dynamic_cast<InputKinect*>(myOSF.createInput(InputKinect::getType()));
		input->setRegisterDepth(true);

		// resize for better performance
		input->setResizing(cv::Size(320, 240));

		// create segmentation object
		Segmentation* seg = 0;
		seg = myOSF.createSegmentation(SegmentationBackground::getType());

		// create feature detection
		Features* feat = 0;
		feat = myOSF.createFeatures(Features::getType());

		// set parameters for feature detection
		if (feat) {
			feat->setGeoMaxZDistThreshold(0.1f);
			feat->setGeoNeighborPrecision(8);
			feat->setIsoPatchResizing(cv::Size(160, 120));
			feat->setIsoPatchWidth(0.2f);
			feat->setTrSearchRadius(0.3f);
			feat->setTrFtLifespan(10);
			feat->setTrFtPointTempTimespan(20);
			feat->setTrFtKfMeasurementNoise(1e-5);
			feat->setTrFtKfProcessNoise(1e-6);
		}
		
		// create skeleton fitting
		Fitting* fitting = 0;
		fitting = myOSF.createFitting(Fitting::getType());

		// set parameters for skeleton fitting
		if (fitting) {
			fitting->setNNDepthStep(3);
			fitting->setNNDepthMaxLeafSize(15);
			fitting->setNNGeoStep(1);
			fitting->setNNGeoMaxLeafSize(15);
			fitting->setNNGeoCutoffFactor(0.5f);
			fitting->setFitCCDMaxIter(1);
			fitting->setFitCCDChangeThresh(0.001);
			fitting->setFitCCDMinimzeSize(true);

			// select skeleton to track
			fitting->createSkeleton<SkeletonUpperBody>();
			//fitting->createSkeleton<SkeletonLowerBody>();
			//fitting->createSkeleton<SkeletonFullBody>();
			//fitting->createSkeleton<SkeletonSimple>();
			//fitting->createSkeleton<SkeletonManipulator>();
		}
		
		// init system
		myOSF.prepare();

		// main loop
		bool terminate = false;
		bool paused = false;
		do {
			// process
			if (!paused)
				myOSF.process();

			terminate |= myOSF.getTerminate();
		} while (!terminate);
	}
	catch (Exception& e) {
		ERR << "Exception: \"" <<
			e.what() << "\"" << ENDL;
	}
	catch (std::exception& e) {
		ERR << "std::exception: \"" <<
			e.what() << "\"" << ENDL;
	}
	catch (...) {
		ERR << "unknown exception" << ENDL;
	}

	return 0;
}
Example #28
0
//-----------------------------------------------------------------------------
void CompilerInfo::FillCxx11Features( Features& features )
{
    (void)features;

#ifdef CXX11_ALIAS_TEMPLATES
    features.emplace_back( "cxx11_alias_templates" );
#endif

#ifdef CXX11_CONSTEXPR
    features.emplace_back( "cxx11_constexpr" );
#endif

#ifdef CXX11_DECLTYPE
    features.emplace_back( "cxx11_decltype" );
#endif

#ifdef CXX11_DEFAULTED_FUNCTIONS
    features.emplace_back( "cxx11_defaulted_functions" );
#endif

#ifdef CXX11_DELEGATING_CONSTRUCTORS
    features.emplace_back( "cxx11_delegating_constructors" );
#endif

#ifdef CXX11_DELETED_FUNCTIONS
    features.emplace_back( "cxx11_deleted_functions" );
#endif

#ifdef CXX11_GENERALIZED_INITIALIZERS
    features.emplace_back( "cxx11_generalized_initializers" );
#endif

#ifdef CXX11_IMPLICIT_MOVES
    features.emplace_back( "cxx11_implicit_moves" );
#endif

#ifdef CXX11_INHERITING_CONSTRUCTORS
    features.emplace_back( "cxx11_inheriting_constructors" );
#endif

#ifdef CXX11_LAMBDAS
    features.emplace_back( "cxx11_lambdas" );
#endif

#ifdef CXX11_NOEXCEPT
    features.emplace_back( "cxx11_noexcept" );
#endif

#ifdef CXX11_NULLPTR
    features.emplace_back( "cxx11_nullptr" );
#endif

#ifdef CXX11_OVERRIDE_CONTROL
    features.emplace_back( "cxx11_override_control" );
#endif

#ifdef CXX11_STRONG_ENUMS
    features.emplace_back( "cxx11_strong_enums" );
#endif

#ifdef CXX11_VARIADIC_TEMPLATES
    features.emplace_back( "cxx11_variadic_templates" );
#endif
}
// main routine
int main(int argc, char* argv[]) {

    //%%%%%%%%%%%%%%%%%%%%%%%% init %%%%%%%%%%%%%%%%%%%%%%%%

    // read arguments
    if(argc<3) {
        cerr << "Usage: ComputeFeatures config.txt override(no(0)/yes(1))" << endl;
        exit(-1);
    }

    // read config file
    StructParam param;
    if(!param.loadConfigFeature(argv[1])) {
        cerr << "Could not parse " << argv[1] << endl;
        exit(-1);
    }

    // read test/anno data (uses same data structure)
    AnnotationData TestD;
    TestD.loadAnnoFile(param.test_file.c_str());

    //if(atoi(argv[2])==2)
    //system(("rm " + param.feature_path + "/*.pgm").c_str());


    // detect hypotheses on all images
    for(int i=0; i<TestD.AnnoData.size(); ++i) {

        // read image
        Mat originImg = imread((param.image_path+"/"+TestD.AnnoData[i].image_name).c_str());
        if(originImg.empty()) {
            cerr << "Could not read image file " << param.image_path << "/" << TestD.AnnoData[i].image_name << endl;
            continue;
        }

        cout << system(("mkdir " + param.feature_path + "/" + TestD.AnnoData[i].image_name).c_str());

        // extract features
        for(int k=0; k<param.scales.size(); ++k) {

            Features Feat;
            string fname(param.feature_path+"/"+TestD.AnnoData[i].image_name+"/"+TestD.AnnoData[i].image_name);
            if( atoi(argv[2])==1 || !Feat.loadFeatures( fname, param.scales[k]) ) {

                Mat scaledImg;
                resize(originImg, scaledImg, Size(int(originImg.cols * param.scales[k] + 0.5), int(originImg.rows * param.scales[k] + 0.5)) );
                Feat.extractFeatureChannels(scaledImg);
                Feat.saveFeatures( fname, param.scales[k]);

#if 0
                // debug!!!!
                Features Feat2;
                namedWindow( "ShowF", CV_WINDOW_AUTOSIZE );
                imshow( "ShowF", Feat.Channels[0] );

                Feat2.loadFeatures( fname, param.scales[k]);

                namedWindow( "ShowF2", CV_WINDOW_AUTOSIZE );
                imshow( "ShowF2", Feat2.Channels[0] );

                cout << scaledImg.rows << " " << scaledImg.cols << " " << scaledImg.depth() << " " << scaledImg.channels() << " " << scaledImg.isContinuous() << endl;
                cout << Feat.Channels[0].rows << " " << Feat.Channels[0].cols << " " << Feat.Channels[0].depth() << " " << Feat.Channels[0].channels() << " " << Feat.Channels[0].isContinuous() << endl;
                cout << Feat2.Channels[0].rows << " " << Feat2.Channels[0].cols << " " << Feat2.Channels[0].depth() << " " << Feat2.Channels[0].channels() << " " << Feat.Channels[0].isContinuous() << endl;


                Mat diff(Size(scaledImg.cols,scaledImg.rows),CV_8UC1);
                cout << diff.rows << " " << diff.cols << " " << diff.depth() << " " << diff.channels() << " " << scaledImg.isContinuous() << endl;

                diff = Feat.Channels[0] - Feat2.Channels[0];

                namedWindow( "ShowDiff", CV_WINDOW_AUTOSIZE );
                imshow( "ShowDiff", diff );
                waitKey(0);
#endif
            }

        }

    }

    return 0;

}
Example #30
0
/*----------------------------------------------------------------DlmDscTable-+
| Constructor for a DlmDscTable                                               |
| This table is for temporary use by DelimBuilder (thru the Row cursor class) |
+----------------------------------------------------------------------------*/
DlmDscTable::DlmDscTable(
   DelimList const & dlmlist,
   Features const & ft,
   bool& isOk
) {
   int const iCountDelim = dlmlist.count();
   if (
      (!iCountDelim) ||
      (aDlmdsc = new Item[1+iCountDelim], !aDlmdsc)
   ) {
      iCountDlmDsc = 0;
      isOk = false;
      return;                      // memory shortage!
   }

   Item *pDlmdsc = aDlmdsc;
   new(pDlmdsc) Item((UCS_2)0, Delimiter::IX_NOT_FOUND, (unsigned int)~0);
   unsigned int ix = 0;

   while (ix < NBR_SDELIM) {
      if ((ix != Delimiter::IX_HCRO) || dlmlist[(Delimiter::e_Ix)ix].good()) {
         new(++pDlmdsc) Item(
            dlmlist[(Delimiter::e_Ix)ix], (Delimiter::e_Ix)ix, rcgnTable[ix]
         );
      }
      ++ix;
   }
   while (ix < (NBR_SDELIM + NBR_DDELIM)) {
      switch(ix) {
      case Delimiter::IX_STAGO_TAGC:
      case Delimiter::IX_ETAGO_TAGC:
         if (!ft.isShorttag()) {
            ++ix;
            continue;
         }
         break;
      case Delimiter::IX_STAGO_GRPO:
      case Delimiter::IX_ETAGO_GRPO:
         if (!ft.isConcur()) {
            ++ix;
            continue;
         }
         break;
      case Delimiter::IX_ERO_GRPO:
      case Delimiter::IX_PERO_GRPO:
         if (
            (!ft.isConcur()) && (!ft.isSimple())  &&
            (!ft.isExplicit()) && (!ft.isImplicit())
         ) {
            ++ix;
            continue;
         }
         break;
      default:
         break;
      }
      new(++pDlmdsc) Item(
         dlmlist[(Delimiter::e_Ix)ix],
         (Delimiter::e_Ix)ix,
         ddlmdef[ix-NBR_SDELIM].iMode
      );
      ++ix;
   }
   while (ix < (unsigned int)iCountDelim) {
      new(++pDlmdsc) Item(
         dlmlist[(Delimiter::e_Ix)ix], (Delimiter::e_Ix)ix, CON_MO
      );
      ++ix;
   }
   iCountDlmDsc =  1 + (pDlmdsc - aDlmdsc);
   sort();

   isOk = true;
}