Esempio n. 1
0
void add_feature(const Mat &image, const char *name)
{
    assert (prediction.size() == image.size());
    int found = 0;
    string _name = string(name);
    cout << "Got feature " << name << endl;
    for (int wi = 0; wi < weak_learners.size(); wi++) {
        if (weak_learners[wi].feature_name == _name) {
            found = 1;
            float *score_ptr = prediction.ptr<float>(0);
            const float *feature_ptr = image.ptr<float>(0);
            float thresh = weak_learners[wi].threshold;
            float left_val = weak_learners[wi].left_val;
            float right_val = weak_learners[wi].right_val;
            for (int i = 0; i < prediction.total(); i++, feature_ptr++, score_ptr++)
                    *score_ptr += ((*feature_ptr <= thresh) ? left_val : right_val);
        }
    }
    if (! found)
        cout << "Didn't find any uses of feature " << name << endl;

    
    // remove old weak learners from consideration
    weak_learners.erase(remove(weak_learners.begin(), weak_learners.end(), name), weak_learners.end());

    if (should_save(name))
        write_feature(h5f, image, name);
}
Esempio n. 2
0
int BaseDecisionTree::fit(Mat_<double> _X,
                          Mat_<double> _y,
                          Mat_<double> sample_weight)
{
    // Validation
    if (_X.rows == 0 || _X.cols == 0)
        return 1;

    // Determine output setting
    _n_samples = _X.rows;
    _n_features = _X.cols;

    // Reshape y to shape[n_samples, 1]
    _y = _y.reshape(1, _y.total());

    // Validation
    if (_y.rows != _n_samples)
        return 2;

    // Calculate class_weight
    Mat expended_class_weight(0, 0, CV_32F);
    // Get class_weight
    if (_class_weight.total() != 0)
        expended_class_weight = compute_sample_weight(_class_weight, _y);

    // Validation
    if (_max_depth <= 0)
        _max_depth = static_cast<int>(pow(2, 31) - 1);
    if (_max_leaf_nodes <= 0)
        _max_leaf_nodes = -1;
    if (_max_features <= 0)
        _max_features = _n_features;
    if (_max_leaf_nodes > -1 && _max_leaf_nodes < 2)
        return 3;
    if (_min_samples_split <= 0)
        return 4;
    if (_min_samples_leaf <= 0)
        return 5;
    if (_min_weight_fraction_leaf >= 0 && _min_weight_fraction_leaf <= 0.5)
        return 6;

    // Set samples' weight
    if (expended_class_weight.total())
    {
        for (int i = 0; i < sample_weight.total(); i++)
        {
            sample_weight.at<double>(i, 0) = sample_weight.at<double>(i, 0) * \
                                             expended_class_weight.at<double>(i, 0);
        }
    }
    else
    {
        sample_weight = expended_class_weight;
    }

    // Set min_weight_fraction_leaf
    if (_min_weight_fraction_leaf != 0.)
        _min_weight_fraction_leaf = _min_weight_fraction_leaf * cv::sum(sample_weight);
    else
        _min_weight_fraction_leaf = 0.;

    // Set min_samples_split
    _min_samples_split = max(_min_samples_split, 2 * _min_samples_leaf);




}
void UtilsSegmentation::MaxFlowSuperpixel(std::vector<SuperpixelStatistic>& spstat, const Mat_<float>& fgdEnergy,
		const Mat_<float>& bgdEnergy, float gamma, Mat_<int>& label)
{
	//::Graph<float,float,float> graph(nNode,nEdge,errfunc);
	//graph
	int nEdge = UtilsSuperpixel::CountEdge(spstat);
	Mat_<int> edgeVertex(nEdge,2);
	Mat_<float> edgeWeight(nEdge,1);
	Mat_<float> edgeLen(nEdge,1);

	int idx = 0;
	for(int i=0;i<spstat.size();i++)
	{
		SuperpixelStatistic& sp = spstat[i];
		for( set<int>::iterator j=sp.conn.begin();
			j!= sp.conn.end();
			j++)
		{
			int d = (*j);
			SuperpixelStatistic& dsp = spstat[d];
			if ( i != d)
			{
				edgeVertex(idx,0) = min(i,d);
				edgeVertex(idx,1) = max(i,d);
				float diff = (float) norm(sp.mean_color_ - dsp.mean_color_);
				edgeWeight(idx) = diff*diff;
				edgeLen(idx) = (float) cv::norm(sp.mean_position_-dsp.mean_position_);
				idx++;
			}
		}
	}

	float beta = (float) cv::mean(edgeWeight)[0];

	Graph<float,float,float> graph((int)spstat.size(), nEdge, errfunc);

	graph.add_node((int)spstat.size());

	for(int i=0;i<fgdEnergy.total();i++)
	{
		graph.add_tweights(i,bgdEnergy(i),fgdEnergy(i));
	}

	edgeWeight = - edgeWeight / beta;
	cv::exp(edgeWeight,edgeWeight);
	edgeWeight *= gamma;
	cv::divide(edgeWeight, edgeLen,edgeWeight);

	for(int i=0;i<nEdge;i++)
	{
		float w = edgeWeight(i);
		graph.add_edge(edgeVertex(i,0),edgeVertex(i,1),w,w);
	}

	graph.maxflow();

	label.create((int)spstat.size(),1);
	for(int i=0;i<spstat.size();i++)
	{
		if ( graph.what_segment(i) == Graph<float,float,float>::SOURCE)
		{
			label(i) = 1;
		}
		else
		{
			label(i) = 0;
		}
	}
}
Esempio n. 4
0
    //
    // thanks again, Haris. i wouldn't be anywhere without your mind here.
    //
    Mat project3d(const Mat & test) const
    {
        PROFILEX("project3d");

        int mid = mdl.cols/2;
        int midi = test.cols/2;
        Rect R(mid-crop/2,mid-crop/2,crop,crop);
        Rect Ri(midi-crop/2,midi-crop/2,crop,crop);

        // get landmarks
        vector<Point2d> pts2d;
        getkp2d(test, pts2d, Ri);
        //cerr << "nose :" << pts2d[30].x << endl;

        // get pose mat for our landmarks
        Mat KP = pnp(test.size(), pts2d);

        // project img to head, count occlusions
        Mat_<uchar> test2(mdl.size(),127);
        Mat_<uchar> counts(mdl.size(),0);
	    for (int i=R.y; i<R.y+R.height; i++)
        {
            PROFILEX("proj_1");
	        for (int j=R.x; j<R.x+R.width; j++)
            {
                Mat1d p = project_vec(KP, i, j);
		        int x = int(p(0) / p(2));
		        int y = int(p(1) / p(2));
                if (y < 0 || y > test.rows - 1) continue;
                if (x < 0 || x > test.cols - 1) continue;
                // stare hard at the coord transformation ;)
                test2(i, j) = test.at<uchar>(y, x);
                // each point used more than once is occluded
                counts(y, x) ++;
	        }
        }

        // project the occlusion counts in the same way
        Mat_<uchar> counts1(mdl.size(),0);
	    for (int i=R.y; i<R.y+R.height; i++)
        {
            PROFILEX("proj_2");
	        for (int j=R.x; j<R.x+R.width; j++)
            {
                Mat1d p = project_vec(KP, i, j);
		        int x = int(p(0) / p(2));
		        int y = int(p(1) / p(2));
                if (y < 0 || y > test.rows - 1) continue;
                if (x < 0 || x > test.cols - 1) continue;
                counts1(i, j) = counts(y, x);
	        }
        }
        blur(counts1, counts1, Size(9,9));
        counts1 -= eyemask;
        counts1 -= eyemask;

        // count occlusions in left & right half
        Rect left (0,  0,mid,counts1.rows);
        Rect right(mid,0,mid,counts1.rows);
        double sleft=sum(counts1(left))[0];
        double sright=sum(counts1(right))[0];

        // fix occlusions with soft symmetry
        Mat_<double> weights;
        Mat_<uchar> sym = test2.clone();
        if (abs(sleft-sright)>symThresh)
        {
            PROFILEX("proj_3");

            // make weights
            counts1.convertTo(weights,CV_64F);

            Point p,P;
            double m,M;
            minMaxLoc(weights,&m,&M,&p,&P);

            double *wp = weights.ptr<double>();
            for (size_t i=0; i<weights.total(); ++i)
                wp[i] = (1.0 - 1.0 / exp(symBlend+(wp[i]/M)));
            // cerr << weights(Rect(mid,mid,6,6)) << endl;

            for (int i=R.y; i<R.y+R.height; i++)
            {
                if (sleft-sright>symThresh) // left side needs fixing
                {
                    for (int j=R.x; j<mid; j++)
                    {
                        int k = mdl.cols-j-1;
                        sym(i,j) = test2(i,j) * (1-weights(i,j)) + test2(i,k) * (weights(i,j));
                    }
                }
                if (sright-sleft>symThresh) // right side needs fixing
                {
                    for (int j=mid; j<R.x+R.width; j++)
                    {
                        int k = mdl.cols-j-1;
                        sym(i,j) = test2(i,j) * (1-weights(i,j)) + test2(i,k) * (weights(i,j));
                    }
                }
            }
        }

        if (DEBUG_IMAGES)
        {
            cerr << (sleft-sright) << "\t" << (abs(sleft-sright)>symThresh) << endl;
            imshow("proj",test2);
            if (abs(sleft-sright)>symThresh)
                imshow("weights", weights);
            Mat t = test.clone();
            rectangle(t,Ri,Scalar(255));
            for (size_t i=0; i<pts2d.size(); i++)
                circle(t, pts2d[i], 1, Scalar(0));
            imshow("test3",t);
        }

        Mat gray;
        sym.convertTo(gray,CV_8U);

        return sym(R);
    }