manifold.cpp

来自「一个由Mike Gashler完成的机器学习方面的includes neural」· C++ 代码 · 共 1,682 行 · 第 1/5 页

CPP
1,682
字号
	}
	else if(m_eMode == ISOMAP_FACES)
	{
		// Reset the next rect
		double* pValues = m_pModel->GetPoint(0);
		m_nextRect.Set((float)pValues[ISOMAP_DIM_1], (float)pValues[ISOMAP_DIM_2], (float).000001, (float).000001);

		// Plot the points
		int nModelFaceCount = m_pModel->GetPointCount();
		int n;
		for(n = 0; n < nModelFaceCount; n++)
			DrawIsomapFace(pScreen, n);
	}
}



// -------------------------------------------------------------------------------


class ManifoldController : public ControllerBase
{
public:
	enum WhichDemo
	{
		MC_SWISS_ROLL,
		MC_S_CURVE,
		MC_SPIRALS,
		MC_SEMI_SUPERVISED,
		MC_PCA_PREPROC,
		MC_LLE_PREPROC,
		MC_FACE,
		MC_ISOMAP_FACES,
		MC_HALL,
		MC_TO_MATRIX,
		MC_FROM_MATRIX,
		MC_LLE_SWISS_ROLL,
	};

protected:
	WhichDemo m_eDemo;
	ManifoldModel* m_pModel;

public:
	ManifoldController(WhichDemo eDemo) : ControllerBase()
	{
		m_eDemo = eDemo;
		m_pModel = NULL;
		m_pView = NULL;
	}

	virtual ~ManifoldController()
	{
		delete(m_pView);
		delete(m_pModel);
	}

	void RunModal()
	{
		if(m_eDemo == MC_SWISS_ROLL)
			DoSwissRollDemo();
		else if(m_eDemo == MC_S_CURVE)
			DoSCurveDemo();
		else if(m_eDemo == MC_SPIRALS)
			DoSpiralsDemo();
		else if(m_eDemo == MC_SEMI_SUPERVISED)
			DoSemiSupervisedDemo();
		else if(m_eDemo == MC_PCA_PREPROC)
			DoPreProcDemo(false);
		else if(m_eDemo == MC_LLE_PREPROC)
			DoPreProcDemo(true);
		else if(m_eDemo == MC_FACE)
			DoFaceDemo();
		else if(m_eDemo == MC_ISOMAP_FACES)
			DoIsomapFaces();
		else if(m_eDemo == MC_HALL)
			DoHallDemo();
		else if(m_eDemo == MC_TO_MATRIX)
			DoToMatrix();
		else if(m_eDemo == MC_FROM_MATRIX)
			DoFromMatrix();
		else if(m_eDemo == MC_LLE_SWISS_ROLL)
			DoLLESwissRoll();
		else
			GAssert(false, "unrecognized demo");
	}

	void DoSwissRollDemo()
	{
		delete(m_pView);
		delete(m_pModel);
		m_pModel = new SwissRollModel(SwissRollModel::SWISS_ROLL, SWISS_ROLL_POINTS, false, 20/*neighbors*/, .98, false, 0, NULL);
		m_pView = new SwissRollView(m_pModel);
		double timeOld = GTime::GetTime();
		double time;
		m_pView->Update();
		GManifoldSculpting* pSculpter = m_pModel->GetSculpter();
		int nDataPoints = pSculpter->GetDataPointCount();
		while(m_bKeepRunning)
		{
			time = GTime::GetTime();
			HandleEvents(time - timeOld);
			pSculpter->SquishPass(rand() % nDataPoints);
			m_pView->Update();
			timeOld = time;
			//if(pSculpter->GetLearningRate() / pSculpter->GetAveNeighborDist() < .001)
			//	break;
		}
/*
		printf("Calculating error...\n");
		//double dMeanSquaredError = ((SwissRollModel*)m_pModel)->Measure2DError();
		double dMeanSquaredError = ((SwissRollModel*)m_pModel)->Measure1DError();
		printf("Mean Squared Error: %.20f\n", dMeanSquaredError);
*/
	}

	void DoSCurveDemo()
	{
		delete(m_pView);
		delete(m_pModel);
		m_pModel = new SwissRollModel(SwissRollModel::S_CURVE, SWISS_ROLL_POINTS, false, 20/*neighbors*/, .98, true, 0, NULL);
		m_pView = new SwissRollView(m_pModel);
		double timeOld = GTime::GetTime();
		double time;
		m_pView->Update();
		GManifoldSculpting* pSculpter = m_pModel->GetSculpter();
		int nDataPoints = pSculpter->GetDataPointCount();
		while(m_bKeepRunning)
		{
			time = GTime::GetTime();
			HandleEvents(time - timeOld);
			pSculpter->SquishPass(rand() % nDataPoints);
			m_pView->Update();
			timeOld = time;
		}
	}

	void DoSpiralsDemo()
	{
		delete(m_pView);
		delete(m_pModel);
		m_pModel = new SwissRollModel(SwissRollModel::SPIRALS, SWISS_ROLL_POINTS, false, 20/*neighbors*/, .98, true, 0, NULL);
		m_pView = new SwissRollView(m_pModel);
		double timeOld = GTime::GetTime();
		double time;
		m_pView->Update();
		GManifoldSculpting* pSculpter = m_pModel->GetSculpter();
		int nDataPoints = pSculpter->GetDataPointCount();
		while(m_bKeepRunning)
		{
			time = GTime::GetTime();
			HandleEvents(time - timeOld);
			pSculpter->SquishPass(rand() % nDataPoints);
			m_pView->Update();
			timeOld = time;
		}
	}

	void DoSemiSupervisedDemo()
	{
		delete(m_pView);
		delete(m_pModel);
		m_pModel = new SwissRollModel(SwissRollModel::SWISS_ROLL, SWISS_ROLL_POINTS, false, 40/*neighbors*/, .99, false, 0, NULL);
		m_pView = new SwissRollView(m_pModel);
		
		// First learn the points
		int nPass = 0;
		double timeOld = GTime::GetTime();
		double time;
		m_pView->Update();
		GManifoldSculpting* pSculpter = m_pModel->GetSculpter();
		int nDataPoints = pSculpter->GetDataPointCount();
		while(m_bKeepRunning)
		{
			time = GTime::GetTime();
			HandleEvents(time - timeOld);
			pSculpter->SquishPass(rand() % nDataPoints);
			m_pView->Update();
			timeOld = time;
			printf("Pass %d\n", nPass++);
		}

		// Now do the demo with semi-supervision
		nPass = 0;
		SwissRollModel* pPrevModel = (SwissRollModel*)m_pModel;
		m_pModel = new SwissRollModel(SwissRollModel::SWISS_ROLL, SWISS_ROLL_POINTS, false, 40/*neighbors*/, .99, false, 100, pPrevModel);
		((SwissRollView*)m_pView)->SetModel(m_pModel);
		delete(pPrevModel);
		pSculpter = m_pModel->GetSculpter();
		nDataPoints = pSculpter->GetDataPointCount();
		m_bKeepRunning = true;
		while(m_bKeepRunning)
		{
			time = GTime::GetTime();
			HandleEvents(time - timeOld);
			pSculpter->SquishPass(rand() % nDataPoints);
			m_pView->Update();
			timeOld = time;
			printf("Pass %d\n", nPass++);
		}
	}

	void DoPreProcDemo(bool bLLE)
	{
		delete(m_pView);
		delete(m_pModel);
		m_pModel = new SwissRollModel(SwissRollModel::SWISS_ROLL, SWISS_ROLL_POINTS, false, 40/*neighbors*/, .99, false, 0, NULL);
		m_pView = new SwissRollView(m_pModel);
		double timeOld = GTime::GetTime();
		double time;
		((SwissRollModel*)m_pModel)->PreProcess(bLLE);
		m_pView->Update();
		GManifoldSculpting* pSculpter = m_pModel->GetSculpter();
		int nDataPoints = pSculpter->GetDataPointCount();
		while(m_bKeepRunning)
		{
			time = GTime::GetTime();
			HandleEvents(time - timeOld);
			pSculpter->SquishPass(rand() % nDataPoints);
			m_pView->Update();
			timeOld = time;
		}
	}

	void DoFaceDemo()
	{
		delete(m_pView);
		delete(m_pModel);
		
		// 0 (srand=0, itters=100)
		m_pModel = new ImageModel(0, 50/*count*/, 43/*wid*/, 38/*hgt*/, 1/*target dims*/, 6/*neighbors*/, .995/*squishing rate*/, "faces/", 0, NULL, true);
		
		// 5 (srand=0, itters=300)
		//m_pModel = new ImageModel(0, 450/*count*/, 40/*wid*/, 30/*hgt*/, 1/*target dims*/, 6/*neighbors*/, .995/*squishing rate*/, "movies/fox1/", 0, NULL, true);
		
		// 5 (srand=0, itters=300)
		//m_pModel = new ImageModel(0, 450/*count*/, 40/*wid*/, 30/*hgt*/, 1/*target dims*/, 6/*neighbors*/, .995/*squishing rate*/, "movies/fox2/", 0, NULL, true);
		
		// 90 (srand=0, itters=200)
		//m_pModel = new ImageModel(0, 480/*count*/, 40/*wid*/, 30/*hgt*/, 1/*target dims*/, 6/*neighbors*/, .995/*squishing rate*/, "movies/games/", 0, NULL, true);
		
		// 1 (srand=0, itters=400)
		//m_pModel = new ImageModel(0, 230/*count*/, 40/*wid*/, 30/*hgt*/, 1/*target dims*/, 6/*neighbors*/, .995/*squishing rate*/, "movies/hall1/", 0, NULL, true);
		
		// 1 (srand=0, itters=300)
		//m_pModel = new ImageModel(0, 270/*count*/, 40/*wid*/, 30/*hgt*/, 1/*target dims*/, 6/*neighbors*/, .995/*squishing rate*/, "movies/hall2/", 0, NULL, true);
		
		// 34 (srand=0, itters=300)
		//m_pModel = new ImageModel(0, 510/*count*/, 40/*wid*/, 30/*hgt*/, 1/*target dims*/, 6/*neighbors*/, .995/*squishing rate*/, "movies/marsup/", 0, NULL, true);
		
		// 2 (srand=0, itters=400)
		//m_pModel = new ImageModel(0, 260/*count*/, 40/*wid*/, 30/*hgt*/, 1/*target dims*/, 6/*neighbors*/, .995/*squishing rate*/, "movies/pan1/", 0, NULL, true);
		
		// 1 (srand=0, itters=300)
		//m_pModel = new ImageModel(0, 280/*count*/, 40/*wid*/, 30/*hgt*/, 1/*target dims*/, 6/*neighbors*/, .995/*squishing rate*/, "movies/pan2/", 0, NULL, true);
		
		// 1 (srand=0, itters=300)
		//m_pModel = new ImageModel(0, 280/*count*/, 40/*wid*/, 30/*hgt*/, 1/*target dims*/, 6/*neighbors*/, .995/*squishing rate*/, "movies/pan3/", 0, NULL, true);
		
		m_pView = new ImageView((ImageModel*)m_pModel, ImageView::FACES);

		double timeOld = GTime::GetTime();
		double time;
		m_pView->Update();
		GManifoldSculpting* pSculpter = m_pModel->GetSculpter();
		int nDataPoints = pSculpter->GetDataPointCount();
//		int nCycle = 0;
		int nIterations = 0;
		while(m_bKeepRunning)
		{
			time = GTime::GetTime();
			HandleEvents(time - timeOld);
			pSculpter->SquishPass(rand() % nDataPoints);
//			if(++nCycle >= 15)
//			{
				m_pView->Update();
//				nCycle = 0;
//			}
			timeOld = time;
			//int nErrors = m_pModel->MeasureSingleDimOrderErrors();
			//printf("Iterations: %d, Ordering Errors: %d\n", nIterations, nErrors);
			//if(nIterations % 50 == 0)
			//{
				//double dMeanSquaredError = m_pModel->Measure1DError();
				int nOrderErrors = m_pModel->MeasureSingleDimOrderErrors();
				printf("Iterations: %d, Order Errors: %d\n", nIterations, nOrderErrors);
				if(nOrderErrors <= 0)
					break;
			//}
			nIterations++;
		}
		while(m_bKeepRunning)
		{
			time = GTime::GetTime();
			HandleEvents(time - timeOld);
			timeOld = time;
			GThread::sleep(15);
		}
	}

	void DoIsomapFaces()
	{
		delete(m_pView);
		delete(m_pModel);
		char szFilename[256];
		strcpy(szFilename, ControllerBase::GetAppPath());
		strcat(szFilename, "images.dat");
		m_pModel = new ImageModel(szFilename, 698);
		m_pView = new ImageView((ImageModel*)m_pModel, ImageView::ISOMAP_FACES);

		double timeOld = GTime::GetTime();
		double time;
		m_pView->Update();
		GManifoldSculpting* pSculpter = m_pModel->GetSculpter();
		int nDataPoints = pSculpter->GetDataPointCount();
//		int nCycle = 0;
		while(m_bKeepRunning)
		{
			time = GTime::GetTime();
			HandleEvents(time - timeOld);
			pSculpter->SquishPass(rand() % nDataPoints);
//			if(++nCycle >= 15)
//			{
			m_pView->Update();
//				nCycle = 0;
//			}
			timeOld = time;
			if(pSculpter->GetLearningRate() / pSculpter->GetAveNeighborDist() < .05)
				break;
		}
		while(m_bKeepRunning)
		{
			time = GTime::GetTime();
			HandleEvents(time - timeOld);
			timeOld = time;
		}

⌨️ 快捷键说明

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