bool NeuralDetector::Train(std::vector<Image_t> images)
{
    size_t images_count = images.size();
    cv::Mat trainData(images_count, ROWS * COLS, CV_32FC1);
    cv::Mat trainOutput(images_count, 1, CV_32FC1);

    for (size_t i = 0; i < images_count; ++i) {
        (images[i].image.reshape(0, 1)).copyTo(trainData.row(i));
        if (images[i].info == SI_UNDEF) {
            return false;
        }
        trainOutput.at<float>(i, 0) = (images[i].info == SI_EXBNZ ? 1 : -1);
    }
    machineBrain.train(
       trainData,
       trainOutput,
       cv::Mat(),
       cv::Mat(),
       CvANN_MLP_TrainParams(
           cvTermCriteria(
               CV_TERMCRIT_ITER | CV_TERMCRIT_EPS,
               MAX_ITER,
               MLP_EPSILON
               ),
           CvANN_MLP_TrainParams::BACKPROP,
           PARAM1,
           PARAM2
           )
       );
    machineBrain.save("savednet.txt");
    std::cout << "Saved\n";
    return true;
}
Exemple #2
0
 Impl(const std::string& learnpath, const std::string& allchars)
 {
     std::set<char> goodChars(allchars.begin(), allchars.end());
     std::ifstream train((learnpath + "/train.txt").c_str());
     std::vector< std::pair<char, std::string> > samples;
     char symbol;
     std::string imageFile;
     while (train >> symbol >> imageFile) {
         if (goodChars.find(symbol) == goodChars.end()) continue;
         samples.push_back(std::make_pair(symbol, imageFile));
     }
     cv::Mat trainData(samples.size(), FEATURE_COUNT, cv::DataType<float>::type);
     cv::Mat trainClasses(samples.size(), 1, cv::DataType<float>::type);
     for (size_t i = 0; i < samples.size(); ++i) {
         std::string path = learnpath + samples[i].second;
         cv::Mat inp = cv::imread(path, 0), out, canny;
         if (inp.empty()) continue;
         cv::Canny(inp, canny, 100, 50, 3);
         std::vector< std::vector<cv::Point> > contours;
         cv::findContours(canny, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
         int maxx = -1, maxy = -1, minx = 1e9, miny = 1e9;
         for (size_t j = 0; j < contours.size(); ++j) {
             cv::Rect r = cv::boundingRect(contours[j]);
             if (r.x + r.width > maxx) maxx = r.x + r.width;
             if (r.y + r.height > maxy) maxy = r.y + r.height;
             if (r.x < minx) minx = r.x;
             if (r.y < miny) miny = r.y;
         }
         cv::Rect bound(minx, miny, maxx - minx, maxy - miny);
         cv::resize(cv::Mat(inp, bound), out, cv::Size(10, 16), 0, 0, cv::INTER_CUBIC);
         for (int j = 0; j < FEATURE_COUNT; ++j) trainData.at<float>(i, j) = out.data[j];
         trainClasses.at<float>(i, 0) = samples[i].first;
     }
     oracle.train(trainData, trainClasses);
 }
Mat extractFeature(vector<string> imgName)
{
    Mat trainData(0,1764, CV_32FC1);
    for (int i=0; i != imgName.size(); i++) {
        Mat img=imread(imgName[i].c_str(),1);
        resize(img, img, Size(64,64));
        HOGDescriptor hog=HOGDescriptor(Size(64,64), Size(16,16), Size(8,8), Size(8,8), 9);
        vector<float> descriptors;
        hog.compute(img, descriptors);
        Mat descriptorsMat(1,descriptors.size(),CV_32F);
        for (int j=0; j<descriptors.size(); j++) {
            descriptorsMat.at<float>(0, j)=descriptors[j];
        }
        trainData.push_back(descriptorsMat);
    }
    return trainData;
}
int main()
{

  // init variables
  double error = 0.;
  int truecnt = 0;
  int times,timed;
  
  // print useful info for reference
  std::cout << "\n" << "hidden neurons: " << "\t \t" << HIDDEN << std::endl;
  // init random number generator
  srand((int)time(NULL));  

  // create network
  std::cout << "initializing network..." << "\t \t";
  NeuralNet DigitNet;

  NeuralLayer * pHiddenLayer1 = new NeuralTanhLayer(INPUT,HIDDEN);
  DigitNet.addLayer( pHiddenLayer1 );
  NeuralLayer * pOutputLayer = new NeuralSoftmaxLayer(HIDDEN,OUTPUT);
  DigitNet.addLayer( pOutputLayer );

  // set output type:
  // SCALAR = tanh or sigmoid output layer (use one output neuron)
  // PROB = softmax output layer, 1-of-N output encoding (use two output neurons)
  const unsigned int outType = PROB;

  // set learning rate, momentum, decay rate
  const double learningRate = 0.15;
  const double momentum =     0.0;
  const double decayRate =    0.0;
  DigitNet.setParams(learningRate,momentum,decayRate,outType);

  std::cout << "done" << std::endl;
  
  // load training and test data
  std::cout << "loading data..." << "\t \t \t";
  std::vector< std::vector<double> > bigData( DATA_SIZE,std::vector<double>(INPUT+1,0.0) );
  loadFromFile(bigData,"train.txt");

  std::vector< std::vector<double> > trainData( TRAIN_SIZE,std::vector<double>(INPUT+1,0.0) );
  std::vector< std::vector<double> > testData( TEST_SIZE,std::vector<double>(INPUT+1,0.0) );
  
  buildData(bigData,trainData,TRAIN_SIZE,testData,TEST_SIZE);
  std::cout << "done" << std::endl;
  
  // loop over training data points and train net
  // slice off first column of each row (example)
  times=(int)time(NULL);   // init time counter
  std::cout << "\n" << "training examples: " << "\t \t" << TRAIN_SIZE << std::endl;
  std::cout << "learning rate: " << "\t \t \t" << learningRate << std::endl;
  std::cout << "momentum: " << "\t \t \t" << momentum << std::endl;
  std::cout << "weight decay: " << "\t \t \t" << decayRate << std::endl;
  std::cout << "training network..." << "\t \t";
  for(int i=0;i<TRAIN_SIZE;++i)
  {
    std::vector<double> data = trainData[i];            // extract data point
    double label = data[0];                             // extract point label
    data.erase(data.begin());
    std::vector<double> nLabel = encode((int)label);    // encode to 1-of-N   
    
    std::vector<double> outputs = DigitNet.runNet(data);
    error = DigitNet.trainNet(data,nLabel,outType);    // train net, return MSE

    // decode output and compare to correct output 
    if( decode(outputs) == (int)label )
        truecnt++;    
  }

  // stop timer and print out useful info
  timed=(int)time(NULL);
  times=timed-times;
  std::cout << "done" << std::endl;
  std::cout << "training time: " << "\t \t \t" << times << " seconds " << std::endl;
  std::cout << "training accuracy: " << "\t \t" << truecnt*100./TRAIN_SIZE << "%" << std::endl;
  
  // test net on test data
  times=(int)time(NULL);   // init time counter
  std::cout << "\n" << "test points: " << "\t \t \t" << TEST_SIZE << std::endl;
  std::cout << "testing network..." << "\t \t";
  truecnt = 0;
  for(int i=0;i<TEST_SIZE;++i)
  {
    
    std::vector<double> data = testData[i];     // extract data point 
    double label = data[0];                     // extract label
    data.erase(data.begin());
   
    std::vector<double> outputs = DigitNet.runNet(data);    // run net

    // decode output and compare to correct output 
    if( decode(outputs) == (int)label )
        truecnt++;    
    
  }

  // stop timer and print out useful info
  timed=(int)time(NULL);
  times=timed-times;
  std::cout << "done" << std::endl;
  std::cout << "testing time: " << "\t \t \t" << times << " seconds " << std::endl;
  std::cout << "test accuracy: " << "\t \t \t" << truecnt*100./TEST_SIZE << "% " << std::endl;
  
  // save weights to reuse net in the future
  DigitNet.saveNet();
  
}
void testMakeAppearanceColor()
{
    std::cout << "mappearanceTest testMakeAppearanceColor" << std::endl;

    aam::Vertices2DList verticesA(TOTAL_POINTS_NUMBER),
            verticesB(TOTAL_POINTS_NUMBER);
    aam::Vertices2DList meansVertices(TOTAL_POINTS_NUMBER);
    aam::Vertices2DList basePoints(TOTAL_POINTS_NUMBER);

    for (int i = 0; i < TOTAL_POINTS_NUMBER; i++)
    {
        verticesA[i].y = verticesAData[i * 2];
        verticesA[i].x = verticesAData[i * 2 + 1];
        verticesB[i].y = verticesBData[i * 2];
        verticesB[i].x = verticesBData[i * 2 + 1];
        meansVertices[i].y = meansData[i];
        meansVertices[i].x = meansData[i + TOTAL_POINTS_NUMBER];
        basePoints[i].y = basePointsData[2 * i];
        basePoints[i].x = basePointsData[2 * i + 1];
    }

    aam::ShapeData shapeData;
    shapeData.xMean = meansVertices;
    shapeData.lines = boost::assign::list_of<int>
        (0) (38) (37) (36) (31) (32) (33) (12) (11) (10) (9)
        (8) (7) (6) (5) (4) (3) (2) (1);
    shapeData.textureSize = cv::Size(255, 255);

    try
    {
        cv::Mat im = cv::imread("data/IMM/01-1m.jpg");
        std::vector<aam::TrainModelInfo> trainData(2);
        trainData[0].setVertices(verticesA);
        trainData[0].setImage(im, false);

        im = cv::imread("data/IMM/01-2m.jpg");
        trainData[1].setVertices(verticesB);
        trainData[1].setImage(im, false);

        aam::CommonFunctions::delaunay(verticesA, shapeData.triangles);

        aam::AppearanceData appearanceData;

        aam::AAMFunctions2D::makeAppearanceModel(
                trainData, shapeData,
                aam::AAMFunctions2D::DEFAULT_PCA_CUT_THRESOLD,
                appearanceData);

        cv::Mat objectPixels = cv::imread("data/object_pixels_synth.bmp",
                cv::IMREAD_GRAYSCALE);

        for (int i = 0; i < std::min(3, appearanceData.eigenVectors.cols);
                i++)
        {
            aam::RealMatrix eigv =
                    appearanceData.eigenVectors.col(i);
            aam::RealType minElemR = *std::min_element(eigv.begin(),
                    eigv.begin() + appearanceData.nPixels);
            aam::RealType maxElemR = *std::max_element(eigv.begin(),
                    eigv.begin() + appearanceData.nPixels);
            aam::RealType minElemG = *std::min_element(
                    eigv.begin() + appearanceData.nPixels,
                    eigv.begin() + 2 * appearanceData.nPixels);
            aam::RealType maxElemG = *std::max_element(
                    eigv.begin() + appearanceData.nPixels,
                    eigv.begin() + 2 * appearanceData.nPixels);
            aam::RealType minElemB = *std::min_element(
                    eigv.begin() + 2 * appearanceData.nPixels,
                    eigv.begin() + 3 * appearanceData.nPixels);
            aam::RealType maxElemB = *std::max_element(
                    eigv.begin() + 2 * appearanceData.nPixels,
                    eigv.begin() + 3 * appearanceData.nPixels);

            cv::Mat result;
            aam::AAMFunctions2D::vector2Appearance(
                    appearanceData.eigenVectors.col(i), objectPixels,
                    cv::Size(255, 255), result);

            std::vector<cv::Mat> channels;
            cv::split(result, channels);
            channels[0] = (channels[0] - minElemR) / (maxElemR - minElemR);
            channels[1] = (channels[1] - minElemG) / (maxElemG - minElemG);
            channels[2] = (channels[2] - minElemB) / (maxElemB - minElemB);

            std::ostringstream winName;

            cv::Mat img;
            cv::merge(channels, img);

            winName << "eigen appearance " << i + 1;

            cv::imshow(winName.str(), img);
        }
        cv::waitKey(0);

        if (appearanceData.basePoints.size() != TOTAL_POINTS_NUMBER)
        {
            std::cout << "%TEST_FAILED% time=0 testname=testMakeAppearanceColor (mappearanceTest) message=Invalid base points array size" << std::endl;
        }
        else if (cv::countNonZero(cv::abs(
                aam::pointVector2Mat(aam::operator -(appearanceData.basePoints,
                basePoints))) > 1e-6))
        {
            std::cout << "%TEST_FAILED% time=0 testname=testMakeAppearanceColor (mappearanceTest) message=Invalid base points array data" << std::endl;
        }
    }
    catch (std::exception& e)
    {
        std::cout << "%TEST_FAILED% time=0 testname=testMakeAppearance (mappearanceTest) message=Exception occured: " <<
                e.what() << std::endl;
    }
}
Exemple #6
0
int main()
{
	//learning rate
	double alpha = 0.03;
	int maxIter = 15000;
	int miniBatchSize = 1000;
	double noiseRatio = 0.3;
	int inputSize = 28*28;
	int hiddenSize = 28;
	int imgWidth = 8; 
	char *fileBuf  = new char[4096];
	bool ret = loadFileToBuf("ParamConfig.ini",fileBuf,4096);
	if(ret)
	{
		getConfigDoubleValue(fileBuf,"noiseRatio:",noiseRatio);
		getConfigDoubleValue(fileBuf,"alpha:",alpha);
		getConfigIntValue(fileBuf,"maxIter:",maxIter);
		getConfigIntValue(fileBuf,"miniBatchSize:",miniBatchSize);
		getConfigIntValue(fileBuf,"hiddenSize:",hiddenSize);
		getConfigIntValue(fileBuf,"inputSize:",inputSize);
		getConfigIntValue(fileBuf,"imgWidth:",imgWidth);
		cout << "noiseRatio:" << noiseRatio << endl;
		cout << "alpha:" << alpha << endl;
		cout << "maxIter:" << maxIter << endl;
		cout << "miniBatchSize:" << miniBatchSize << endl;
		cout << "hiddenSize:" << hiddenSize << endl;
		cout << "inputSize:" << inputSize << endl;
		cout << "imgWidth:" << imgWidth << endl;
	}
	delete []fileBuf;

	//set eigen threads
	SYSTEM_INFO info;
	GetSystemInfo(&info);
	Eigen::setNbThreads(info.dwNumberOfProcessors);

	MatrixXd trainData(1,inputSize);
	DAE dae(inputSize,hiddenSize);
	ret = loadMnistData(trainData,"mnist\\train-images-idx3-ubyte");
	cout << "Loading training data..." << endl;
	if(ret == false)
	{
		return -1;
	}
	clock_t start = clock();
	//cout << trainData.rows() << " " << trainData.cols() << endl; 
	MatrixXd showImage = trainData.leftCols(100).transpose();
	buildImage(showImage,imgWidth,"data.jpg");
	dae.train(trainData,noiseRatio,alpha,maxIter,miniBatchSize);
	cout << "End Train" << endl;
	MatrixXd hiddenTheta = dae.getTheta();
	buildImage(hiddenTheta,imgWidth,"weights.jpg",true);
	cout << "Saving hidden neurons" << endl;
	dae.saveModel("DAE_Model.txt");
	clock_t end = clock();
	cout << "The code ran for " << (end - start)/(double)(CLOCKS_PER_SEC*60)
		<< " minutes on " << Eigen::nbThreads() << " thread(s)." << endl;
	cout << "noiseRatio: " << noiseRatio << endl;
	cout << "alpha: " << alpha << endl;
	cout << "miniBatchSize: " << miniBatchSize << endl;
	system("pause");
	return 0;
}
Exemple #7
0
void Learner::learn(const Mat &img, const Mat &imgB, const Mat &img32F, const TYPE_BBOX &ret)
{
    TYPE_TRAIN_DATA_SET &nnTrainDataset = detector->trainDataSetNN;
    nnTrainDataset.clear();
    TYPE_TRAIN_DATA_SET &rfTrainDataset = detector->trainDataSetRF;
    rfTrainDataset.clear();
    auto &scanBBs = detector->scanBBs;
    
    detector->sortByOverlap(ret, true);
    
    int count = 0;
    
    // P-expert - NN
    nnTrainDataset.push_back(make_pair(img32F(scanBBs[0]), CLASS_POS));
    
    // P-expert - RF
    int tlx = img.cols, tly = img.rows, brx = 0, bry = 0;
    
    for(int i = 0; i < LEARNER_N_GOOD_BB && scanBBs[i].overlap >= GOODBB_OL; i++)
    {
        tlx = min(tlx, scanBBs[i].tl().x);
        tly = min(tly, scanBBs[i].tl().y);
        brx = max(brx, scanBBs[i].br().x);
        bry = max(bry, scanBBs[i].br().y);
    }
    
    Point tl(tlx, tly), br(brx, bry);
    Rect bbHull(tl, br);
    
    int cx, cy;
    cx = round((double)(tlx + brx) / 2);
    cy = round((double)(tly + bry) / 2);
    
    for(int j = 0; j < LEARNER_N_WARPED; j++)
    {
        Mat warped;
        
        if(j != 0)
        {
            patchGenerator(imgB, Point(cx, cy), warped, bbHull.size(), theRNG());
            // for optimum in RF::getcode()
            Mat tmp(imgB.size() ,CV_8U, Scalar::all(0));
            warped.copyTo(tmp(Rect(0, 0, bbHull.size().width, bbHull.size().height)));
            warped = tmp;
        }
        else
        {
            warped = imgB(bbHull);
        }
        
        for(int i = 0; i < LEARNER_N_GOOD_BB && scanBBs[i].overlap >= GOODBB_OL; i++)
        {
            Rect rect(scanBBs[i].tl() - tl, scanBBs[i].br() - tl);
            
            TYPE_TRAIN_DATA trainData(make_pair(warped(rect), CLASS_POS));
            rfTrainDataset.push_back(trainData);
            
            count++;
        }
    }

    // N-expert - NN
    int nCountNN = 0;
    for(int i = 0; i < detector->scanBBs.size(); i++)
    {
        TYPE_DETECTOR_SCANBB &sbb = detector->scanBBs[i];
        
        if(sbb.status != DETECTOR_REJECT_VAR)
        {
            if(sbb.overlap < BADBB_OL)
            {
                nCountNN++;
                
                TYPE_TRAIN_DATA trainData(make_pair(img32F(sbb), CLASS_NEG));
                nnTrainDataset.push_back(trainData);
            }
        }
        
        if(nCountNN == LEARNER_N_NN_NEG) break;
    }
    
    // N-expert - RF
    int nCountRF = 0;
    for(int i = 0; i < detector->scanBBs.size(); i++)
    {
        TYPE_DETECTOR_SCANBB &sbb = detector->scanBBs[i];
        
        if(sbb.status != DETECTOR_REJECT_VAR)
        {
            if(sbb.overlap < BADBB_OL && sbb.posterior >= 0.1)
            {
                nCountRF++;
                
                TYPE_TRAIN_DATA trainData(make_pair(imgB(sbb), CLASS_NEG));
                rfTrainDataset.push_back(trainData);
            }
        }
    }
    
    stringstream info;
    info << "Generated 1 positive example for NN, and " << count << " positive example for RF.";
    outputInfo("Learner", info.str());
    
    stringstream info2;
    info2 << "Generated " << nCountNN << " NN negative sample(s), and " << nCountRF << " RF negative example(s).";
    outputInfo("Learner", info2.str());
    
    detector->update();
    stringstream info3;
    info3 << "Updated detector.";
    outputInfo("Learner", info3.str());
    
    detector->nNClassifier.showModel();
}