class_ann.h 2.68 KB
//OpenCV
#include <opencv2/opencv.hpp>

#include "progress_thread.h"

extern size_t X, Y, B;

//function for training a random forest classifier
void train_ann(CvANN_MLP* ann, cv::Mat &trainF, cv::Mat &trainR, std::vector<int> hidden, int iters, double epsilon, bool VERBOSE = false){
	
	//ANN criteria for termination
    CvTermCriteria criter;
    criter.max_iter = iters;
    criter.epsilon = epsilon;
    criter.type = CV_TERMCRIT_ITER | CV_TERMCRIT_EPS;

	//ANN parameters
    CvANN_MLP_TrainParams params;
    params.train_method = CvANN_MLP_TrainParams::BACKPROP;
    params.bp_dw_scale = 0.05f;
    params.bp_moment_scale = 0.05f;
    params.term_crit = criter; //termination criteria

	cv::Mat layers = cv::Mat((int)hidden.size() + 2, 1, CV_32SC1);		//create one input layer, one output layer, and n (specified) hidden layers

	
    layers.row(0) = cv::Scalar(trainF.cols);						//the first layer is based on the number of inputs
	for(size_t l = 0; l < hidden.size(); l++){						//for each hidden layer
		layers.row((int)l + 1) = cv::Scalar(hidden[l]);					//specify the number of hidden nodes
	}
	layers.row((int)hidden.size() + 1) = cv::Scalar(trainR.cols);		//the output layer is equal to the number of responses per pixel   

	std::cout<<"Creating an MLP with the structure: "<<std::endl<<"\t";

	std::cout<<"(in) "<<layers.at<int>(0, 0);
	for(size_t l = 1; l < layers.rows - 1; l++)
		std::cout<<"  "<<layers.at<int>((int)l, 0);
	std::cout<<"  "<<layers.at<int>(layers.rows-1, 0)<<" (out)"<<std::endl;
	ann->create(layers);														//create the neural network given the architecture

	std::cout<<"Training..."<<std::endl;
	ann->train(trainF, trainR, cv::Mat(), cv::Mat(), params);					//call the ANN training algorithm
	std::cout<<"done..."<<std::endl;

	/////	TEMPORARY  -  WILL BE DELETED IN THE ACTUAL IMPLEMENTATION		///////////////
	cv::Mat predicted(trainR.rows, 1, CV_32F);									//allocate space for a predicted response

	stim::image<unsigned char> test(X, Y, 3);								//create a 3-channel (color) image
	test = 0;
	for(size_t y = 0; y < Y; y++){											//for each pixel in the image
		for(size_t x = 0; x < X; x++){
			cv::Mat response(3, 1, CV_32FC1);
			cv::Mat sample = trainF.row((int)(y * X + x));					//load the corresponding value from the training array

			ann->predict(sample, response);										//predict the response for this pixel
			test(x, y, 0) = (unsigned char)(response.at<float>(0, 0) * 255);	//save the response in the color image (make sure to re-scale the values to [0, 255])
			test(x, y, 1) = (unsigned char)(response.at<float>(0, 1) * 255);
			test(x, y, 2) = (unsigned char)(response.at<float>(0, 2) * 255);
		}
    }
	test.save("response.bmp");													//save the response
}