How do I update this Bytefish Neural Network to do something more interesting

asked 2014-05-26 11:11:03 -0600

joeish80829 gravatar image

I'm learning Neural Networks from this bytefish machine learning guide and code. I understand it well but I would like to update the code at the previous link to use image pixel data instead of random values as the input data. In this section of the aforementioned code:

cv::randu(trainingData,0,1);
cv::randu(testData,0,1);

the training and test matrices are filled with random data. Then label data is added to the classes matrices here:

cv::Mat trainingClasses = labelData(trainingData, eq);
cv::Mat testClasses = labelData(testData, eq);

using this function:

        // label data with equation
        cv::Mat labelData(cv::Mat points, int equation) {
          cv::Mat labels(points.rows, 1, CV_32FC1);
          for(int i = 0; i < points.rows; i++) {
            float x = points.at<float>(i,0);
            float y = points.at<float>(i,1);
            labels.at<float>(i, 0) = f(x, y, equation); 

   // the f() function used above
  //has a case statement with 5 
  //functions in it eg on of the equations is:

  //case 0:
  //return y > sin(x*10) ? -1 : 1;
  //break;
          }

          return labels;
        }

Then points are plotted in a window here:

plot_binary(trainingData, trainingClasses, "Training Data");
plot_binary(testData, testClasses, "Test Data");

with this function:

    ;; Plot Data and Class function
    void plot_binary(cv::Mat& data, cv::Mat& classes, string name) {
      cv::Mat plot(size, size, CV_8UC3);
      plot.setTo(cv::Scalar(255.0,255.0,255.0));
      for(int i = 0; i < data.rows; i++) {

        float x = data.at<float>(i,0) * size;
        float y = data.at<float>(i,1) * size;
        if(classes.at<float>(i, 0) > 0) {
          cv::circle(plot, Point(x,y), 2, CV_RGB(255,0,0),1);
        } else {
          cv::circle(plot, Point(x,y), 2, CV_RGB(0,255,0),1);
        }
      }
      imshow(name, plot);
    }

The plotted points, as I understand it, represent the input data multiplied by the equations in the f() function and is used by the predict functions to predict which point to plot in the mlp, knn, svm etc. functions. How do I update what is going on here to do something cooler. Something with Image pixel data would be good, but help on doing anything different than this would be appreciated. Even showing me how to do a simple XOR with this code would be awesome. I can fill the trainingData and testData matrices with random 0's and 1's but not exactly sure what to do after that.

edit retag flag offensive close merge delete