⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vjrgeneticweaklearnerinterface.cpp

📁 WeakLearner
💻 CPP
字号:
//LAB_LicenseBegin==============================================================
//  Copyright (c) 2005-2006, Hicham GHORAYEB < ghorayeb@gmail.com >
//  All rights reserved.
//
//	This software is a Library for Adaptive Boosting. It provides a generic 
//	framework for the study of the Boosting algorithms. The framework provides 
//	the different tasks for boosting: Learning, Validation, Test, Profiling and 
//	Performance Analysis Tasks.
//
//	This Library was developped during my PhD studies at:
//	Ecole des Mines de Paris - Centre de Robotique( CAOR )
//	http://caor.ensmp.fr
//	under the supervision of Pr. Claude Laurgeau and Bruno Steux
//
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//      * Redistributions of source code must retain the above copyright
//        notice, this list of conditions and the following disclaimer.
//      * Redistributions in binary form must reproduce the above copyright
//        notice, this list of conditions and the following disclaimer
//        in the documentation and/or other materials provided with the distribution.
//      * Neither the name of the Ecole des Mines de Paris nor the names of
//        its contributors may be used to endorse or promote products
//        derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
//  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
//  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
//  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
//  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
//  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
//  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
//  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
//  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//================================================================LAB_LicenseEnd
#include "VJRGeneticWeakLearnerInterface.h"
using namespace learners::wlinterfaces;

#include "LibAdaBoost/modules/options/ModuleOptions.h"
using namespace modules::options;

#include <iostream>
using namespace std;

#define MY_RAND(range) (rand()%range)
#define CHECK_RANGE(v, minVal, maxVal) (((v) < (minVal))? (minVal): (((v) >= (maxVal))? (maxVal)-1: (v)))

VJRGeneticWeakLearnerInterface::VJRGeneticWeakLearnerInterface()
{
}

VJRGeneticWeakLearnerInterface::~VJRGeneticWeakLearnerInterface()
{
}

// WeakLearnerInterface
WeakClassifier* 
VJRGeneticWeakLearnerInterface::Mutate(const WeakClassifier &wc) const
{
	const ViolaJonesRectangularFeature &cpObject = static_cast<const ViolaJonesRectangularFeature &>(wc);

	// CLone the Current WeakClassifier
	ViolaJonesRectangularFeature *vjf = (ViolaJonesRectangularFeature *)(cpObject.Clone());

	// GENERATE RANDOM MUTATION TYPE
	t_MutationType mutationType;
	mutationType = (t_MutationType) MY_RAND( VJRGeneticWeakLearnerInterface::MUTATE_UNKNOWN );

	MutateSpecific( *vjf, mutationType);
	return vjf;
}

WeakClassifier* 
VJRGeneticWeakLearnerInterface::Randomize(const WeakClassifier &wc) const
{
	const ViolaJonesRectangularFeature &vjrObject = static_cast<const ViolaJonesRectangularFeature &>(wc);

	ViolaJonesRectangularFeature *vjf = new ViolaJonesRectangularFeature();
    	
	int width;
	int height;
	short category;

	int channel;
	int NbrOfChannels;

	height = vjrObject.GetObjectHeight();
	width  = vjrObject.GetObjectWidth();

	category = vjrObject.GetCategory();

	channel = vjrObject.Getchannel();
	NbrOfChannels = vjrObject.GetNbrOfChannels();

	vjf->SetObjectHeight( height );
	vjf->SetObjectWidth( width );

	vjf->SetCategory( category );

	vjf->SetNbrOfChannels( NbrOfChannels );
	vjf->SetChannel( channel );

	short t;
	int x0,y0,x1,y1,sc0,sc1,sl0,sl1;

	vjrObject.GetData(t,x0,y0,x1,y1,sc0,sc1,sl0,sl1);

	// Do Randomize here

	vjf->SetData(t,x0,y0,x1,y1,sc0,sc1,sl0,sl1);
	return vjf;
}

WeakClassifier* 
VJRGeneticWeakLearnerInterface::CrossOver(const WeakClassifier &wc1, const WeakClassifier &wc2) const
{
	return NULL;
}

void 
VJRGeneticWeakLearnerInterface::SetOptions(const modules::options::ModuleOptions &options)
{
	//DO Nothing
}

void 
VJRGeneticWeakLearnerInterface::MutateSpecific(ViolaJonesRectangularFeature &wcOut, VJRGeneticWeakLearnerInterface::t_MutationType mutationType) const
{
	int oWidth  = wcOut.m_ObjectWidth;
	int oHeight = wcOut.m_ObjectHeight;

	short category = wcOut.GetCategory();
	int NbrOfChannels = wcOut.GetNbrOfChannels();

	short t;
	int x0,y0,x1,y1,sc0,sc1,sl0,sl1;

	wcOut.GetData(t,x0,y0,x1,y1,sc0,sc1,sl0,sl1);

	switch( mutationType )
	{
	case VJRGeneticWeakLearnerInterface::MUTATE_TRANSLATE:
		{
			short newType = GetRandType( category );
			wcOut.SetRegularShape(newType,x0,y0,x1,y1);
		}
		break;
	case VJRGeneticWeakLearnerInterface::MUTATE_SCALE:
		{
			short newType = GetRandType( category );
			wcOut.SetRegularShape(newType,x0,y0,x1,y1);
		}
		break;
	case VJRGeneticWeakLearnerInterface::MUTATE_CHANGE_CHANNEL:
		{
			wcOut.m_Channel = MY_RAND(NbrOfChannels);
		}
		break;
	case VJRGeneticWeakLearnerInterface::MUTATE_CHANGE_THRESHOILD:
		{
			int r = (MY_RAND(2)) ? -1:1;
			short step[] = {1,2,4,8,16};
			double newThreshold = wcOut.m_threshold + r * step[MY_RAND(5)];
			newThreshold = CHECK_RANGE(newThreshold, 0, 255);
			wcOut.m_threshold =  newThreshold;
		}
		break;
	case VJRGeneticWeakLearnerInterface::MUTATE_CHANGE_TYPE:
		{
			short newType = GetRandType( category );
			wcOut.SetRegularShape(newType,x0,y0,x1,y1);
		}
		break;
	default:
		;
	}
}

short 
VJRGeneticWeakLearnerInterface::GetRandType(short category) const
{
	int result = -1;

	switch( category )
	{
	case ViolaJonesRectangularFeature::CATEGORY_STD:
		{
			result = MY_RAND( 5 );
		}
		break;
	case ViolaJonesRectangularFeature::CATEGORY_BOX:
		{
			result = 5 + MY_RAND( 4 );
		}
		break;
	case ViolaJonesRectangularFeature::CATEGORY_MIX:
		{
			result = MY_RAND( 9 );
		}
		break;
	default:
		LABLog(LogERROR, "ViolaJonesRectangularFeatureGenerator::GetRandType(%d) Unknown Category.\n", category);
	}

	return result;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -