[关闭]
@Gizmosir 2016-05-06T03:20:10.000000Z 字数 27151 阅读 796

Game Prediction Base On Machine Learning

EESM5720 Signal Analysis and Pattern Recognition Final report

Zheng Jiongbin
StuID: 20303666

Final_Report

Background Information

Kobe Bean Bryant is an American born basketballer, he was born on August 23, 1978. He played for Los Angeles Lakers of the National Basketball Association (NBA). He has been playing for Lakers for almost his entire lifetime, he joined NBA directly during his high school. Kobe Bryant’s successful career as a basketballer has inspired many young men. However, at the beginning of his 20th season with Lakers, Kobe made a surprised statement by announcing his retirement after the season.

Project Goals

The aim of this project is to predict the success of free shot Kobe is about to make during a game, at the moment he makes the free shoot we intend to predict as accurate as possible if the ball will score or not. In order to achieve this aim, we separated the project into four sub tasks, which are as listed below:

  1. Binary Classification problem:
    • Determine the input image is Kobe or not.
    • Input: color image with fixed size.
    • Output: Yes | No.
  2. Binary Classification problem:
    • Determine Kobe is in a present frame or not.
    • Input: color image with relatively large size using task 1 to determine.
    • Output: Yes | No.
  3. Multi-class Classification problem:
    • Determine what is Kobe doing in the given frame.
    • Input: color image from task 2.
    • Output: Multiclass class binary tag. For example it may including: Free throw; Defending; Running with ball,etc. And output is a column vector.
  4. Binary Classification problem:
    • Predict the free shot is going to score or not. It based on features such as the angle of knee, angle of arm, the angle of his forefinger and middle finger.
    • Input: color image from task 3.
    • Output: Yes | No.

Neural Network implementation

Network Architecture

In the implementation of Nerual Network, the first step is to determine the network architecture, which including the number of hidden layer as well as the number of units for each layer.

Since task1 aims at solving a binary classification problem, the output unit can only be one which has the value 1 as 'Yes' and 0 as 'No'.

There are usually two way to go about solving the hidden unit. One is to cosider only one hidden layer with many hidden units and the other is to consider having multiple hidden layers with the same number of hidden units. I decided to choose the one with only one hidden layer considering the fact that it is more popular and widely used. I used 25 as my hidden unit number and it work well. Usually more hidden unit produce better performance in term of accuracy.

As for input layer unit. Firstly, I used each image pixel intensity as my input features. So the input layer unit is bound to the size of the image. And the image size is also bound to different face recognition algorithm. The minimum image size requested for the two most commonly used algorithm EBSM[1] is 128*128, and LDA[2] is 96*84 or 24*24. So finally I decided to choose 128*128 as the image size as well as the input layer unit.

Forward propogation

The second step of Neural Network algorithm is to implement forward propagation to calculate hidden unit activation which denotes the activation of unit in layer .

Here I use logistic sigmoid as the activation function:

denotes the parameter or weight between unit in layer and in layer .

Regularized Cost function

The thrid step is to implement code to compute cost function . Here I decided to use cross entroy as my error function. In order to prevent overfitting problem, I also add a regularization term in the cost function. And after that the cost function is look like below:

Here N denotes the number of training dataset, M denotes the dimensions of output unit. denotes the parameter between unit in layer and in layer . , and are represent the total number of layer, the number of unit in layer and the number of units in layer seperately. It is important to mention that both and are not including the bias unit.

Back propogation

The four step is to implement back propogation to compute the partial derivatives of cost function .

First we have to calculate the for all the output units using:

denotes the activations calculate using forward propogation and denotes the example's lable value.

Then we need to compute for all hidden units using:

Again denotes j-th unit "error" in layer , denotes the weights between k-th unit in layter and j-th unit in layer , denotes the number of unit in layer . It also not including the bias unit.

Random Initialization

In order to apply gradient descent or other advanced optimization algorithm, it is significantly important to initialize the parameters properly. Otherwise it may lead to symmetry problem which lead to identical hidden unit value responding to the result input after each update.

One way to initialize the parameter is to choose value randomly. But due to the reason that the cost function of Neural Network is not a convex function, the algorithm sometimes may stick to some local optimum. One possible way to solve this problem is to run the algorithm with different initial parameter hundreds of time and pick the one with lowest validation error.

Gradient Checking

After defining and implementation of back propogation, it is always useful to implement gradient checking to see whether the gredient is compute correctly or not. In order to do this, we use numerical approximation:

As the decrease, the value of numerical approximation is become closer to the gradient value. As the result the complety and the calculate time would also increase. So would be a prefect reasonable default value.

Due to the fact that running numerical approximation is quite slow and time-comsuing, I have to turn it off before I actually train the classifier.

Data Collection

Data Acquisition

Acquiring Kobe's images is pretty much easy as he is a famous basketball player, there are a lot of his imaes on internet.

Above is showing a picture I collected from internet and the rest of the images I collected is look like this one. What we need to do is to extract the bounding face image from the picture.

In order to accelerate the processing, I worte a script to help me to do such work. The GUI of the script is shown below:

I used vision.CascadeObjectDetector(), which is included in the Matlab Computer vision system toolbox, to create a faceDetector. This script allows me to detect all the face appear in each picture and label it with yes or no.

Then in order to guarantee the correctness of the labeling result. I wrote a script to show all the face images belong to the same label, which is shown below:

Data Partitioning

After data acquisition, A totoal number of 1853 images were obtained. I decided to seperate it by 80%-10%-10% for training, cross-validation and testing set seperately. Finally I got precentage table which is listed below as my database.

Section Number Kobe Non-Kobe
Training 1482(80%) 563(38%) 919(62%)
Validation 185(10%) 70(38%) 115(62%)
Testing 186(10%) 73(39%) 113(61%)
Total 1853 706(38%) 1147(62%)

Data Usage

Usually people use training dataset to train the classifier and use validation dataset to stop the algorithm from overfitting. However, because I implement neural network and also collect the data myself. So I use training dataset to train the classifier and use validation dataset as the performace critiria. However, I keep the testing dataset untouched until all the parameters of my Neural Network algorithm are sentle down.

Parameters Optimization

Input unit number

At the very begining I used each pixel intensity as the Nueral Network input features, which produced features of about 16384. As expected, the training speed is extremely slow and take about 10 minutes to train the classifier once.

However, consider the fact that natural image pixel intensity are highly correlated, I decide to implement PCA(Principal Component Analysis), which can reduce the data features from high dimension to low dimension.

S denotes the empirical covariance matirx. An intuitive way to think about PCA is to extract the most important part, namely eigenvector, from the original data.

Using Matlab built-in function like svd could eaily get he eigenvector of S. And since S is a matrix which D is the dimension of input feature, is our case is 16384, it is quite computational-expensive.

After applying PCA, the training time drop dramatically from 10 minutes to only 30 seconds while 99% of variance are remained.

Regularization Lambda

After applying PCA, it makes it possible to train the classifier many times using different regularization term lambda to find out the optimal value.

As shown in the figure above, the training and validation error from different lambda produces the lowest error when lambda is 10. Therefore, I choose 10 as my regularization lambda.

Iteration times

I also trained the classifier many time with different iteration times to seek for the optimal parameter.

As shown in the figure above, the accuracy of training and validaton dataset increase significantly from around 65% to 85% at 300 iteration times. Then the accuracy of validation drops smoothly while as excepted the training accuracy increases. Therefore, I choose 300 as my training times.

Comparison

At the end, I also use PhD(Pretty helpful Development)[3][4] face recognition toolbox to apply LDA on my database.

After LDA, the feature dimension become and the data distribution looks like above. The training point is clearly seperated.

Then I projected the validation point using the same feature mapping rule. After that I run logistic regression to seperate the data and got the result which is shown below:

Section Accuracy
Training 100%
Validation 82%

Evaluation

Where to improve

At the parameter optimization section, I also tried to train the classifier with different training examples. The learning curve of result is look like above. As shown in the figure, the accuracy fluctuate a low as the number of training examples keep increasing.

First I tried to test the algorithm using a hand written digits and the result look like above.

So I guessed it may due to the extracted features from each image. To solve this problem I am excepted to implement face aligned before extracting the features from face image. However, it will become more complex and time-consuming.

What to continue

Due to the time limitation, We are only able to finished task 1, tast 2 and half of the task3. So we are going to continue our project with task3 and task4.

The idea of task 4 is to do segementation and motion estimation of kobe so that we could extraxt valuable features in time sequences and train the classifier.

Group Number & Responsibility

Schedule:

TimePeriod Details State
Apr. 11th ~ Apr. 17th Collect data; pre-processing for the data. 100%
Apr. 18th ~ Apr. 24th Train the classifier for Task1 and Task2 separately. 100%
Apr. 25th ~ May. 1st Train the classifier for Task 3 using SVM and NN; compare the result. 50%
May. 2nd ~ May. 8th Preparation for the oral presentation and final paper. 100%

Bibliography:

[1]: K. Okada, et al, "The Bochum/USC Face Recognition System and How it Fared in the FERET Phase III Test," Face Recognition From Theory to Applications, Springer-Verlag, pp. 186~205, 1998.
[2]: W. Zhao et al, "Subspace Linear Discriminant Analysis for Face Recognition," Technical Report CAR-TR-914, Center for Automation Research University of Maryland, 1999.
[3]: V. Sˇtruc and N Paveˇsi ́c, “The Complete Gabor-Fisher Classifier for Robust Face
Recognition”, EURASIP Advances in Signal Processing, pp. 26, 2010,
doi=10.1155/2010/847680.
[4]: V. Sˇtruc and N. Paveˇsi ́c, “Gabor-Based Kernel Partial-Least-Squares Discrimination
Features for Face Recognition”, Informatica (Vilnius), vol. 20, no. 1, pp. 115–138, 2009.
[5]: W. Zhao, et al. “Face Recognition: A Literature Survey”, Techinical Report CS-TR-4167, Center For Automation Research, University of Maryland, 2000.
[6]: A. Ng, “Machine Learning”, Coursera, 2015 [Online].
Available: https://www.coursera.org/learn/machine-learning/. [Accessed by Apr. 18th].

Appendix (Source Code)

Important: I usually sign my name as 'Gizmosir' at all my written document.

nnTrainer.m

  1. %%---nnTrainer---
  2. %% train a neural network using training image set
  3. %% ---Written by Gizmosir 2016-04-19----
  4. %% Initialization
  5. close all;
  6. clear;
  7. clc;
  8. %% Setup the structure parameters
  9. inputLayerUnit = 128*128; %default input image size 128*128
  10. hiddenLayerUnit = 25; %default
  11. outputLayerUnit = 2; %only 2: yes or no
  12. %% Load dataset
  13. load trainingData% trainingData trainingDataTest
  14. % imageTrain -- 1*1482 cell
  15. % labelTrain -- 1*1482 double
  16. % imageVal -- 1*185 cell
  17. % labelVal -- 1*185 double
  18. %------
  19. % data parameters
  20. numTrain = length(imageTrain);
  21. numVal = length(imageVal);
  22. X = zeros(numTrain, inputLayerUnit);
  23. XVal = zeros(numVal, inputLayerUnit);
  24. % reorder
  25. for i = 1:numTrain
  26. image = imageTrain{i};
  27. image = imresize(image, [sqrt(inputLayerUnit), sqrt(inputLayerUnit)]);
  28. X(i,:) = image(:);
  29. end
  30. for i = 1:numVal
  31. image = imageVal{i};
  32. image = imresize(image, [sqrt(inputLayerUnit), sqrt(inputLayerUnit)]);
  33. XVal(i,:) = image(:);
  34. end
  35. y = labelTrain';
  36. yVal = labelVal';
  37. % clear dataset
  38. clear image imageTrain imageVal labelTrain labelVal;
  39. fprintf('Loading data finished...\n');
  40. %% Visualizing Data
  41. % randomly pick 20 row of data
  42. sel = randperm(size(X, 1));
  43. sel = sel(1:20);
  44. displayData(X(sel, :));
  45. fprintf('Visualizing Data finished...\n');
  46. %% Apply PCA towards dataset to accelerate training speed
  47. load PCA
  48. % U_deduce -- 16384*428 double
  49. % K -- 428 double
  50. %------
  51. % Because PCA is extremely time-consuming so I load the pre-run data
  52. % instead of run it again every time.
  53. % [U_deduce, K] = PCA(X);
  54. % update new X and XVal
  55. X = X * U_deduce;
  56. XVal = XVal * U_deduce;
  57. % update new inputLayerUnit for Neural Network
  58. inputLayerUnit = K;
  59. %% Randomly initialize paremeters
  60. theta1Init = randInitializeWeights(inputLayerUnit, hiddenLayerUnit);
  61. theta2Init = randInitializeWeights(hiddenLayerUnit, outputLayerUnit);
  62. % Unroll parameters
  63. nnParamsInit = [theta1Init(:); theta2Init(:)];
  64. fprintf('Rradomly initialize paremeters finished...\n');
  65. %}
  66. % load selected parameters for better performance
  67. % since it highly related to the initial value
  68. % load nnParams
  69. %nnParamsInit -- 10777*1 double
  70. %-------
  71. %% Compute cost (Feedforward)
  72. % regularization parameter
  73. % lambda = 0 -- cost function without regularization term
  74. lambda = 0;
  75. J = nnCostFunction(nnParamsInit, inputLayerUnit, hiddenLayerUnit, ...
  76. outputLayerUnit, X, y, lambda);
  77. fprintf('Compute cost finiished...\n');
  78. fprintf('The cost with initial paramters is: %f\n', J);
  79. %% Gradinent checking(Should trun off in real training)
  80. lambda = 3;
  81. checkNNGradients(lambda);
  82. %% Train Neural Network
  83. % Using "fmincg" to find the optimal parameters
  84. %change the MaxIter number will get different result
  85. options = optimset('MaxIter', 300); %300 is the optimal value
  86. lambda = 23; %23 is the optimal value
  87. % Create "short hand" for the cost function to be minimized
  88. costFunction = @(p) nnCostFunction(p, ...
  89. inputLayerUnit, ...
  90. hiddenLayerUnit, ...
  91. outputLayerUnit, X, y, lambda);
  92. % Now, costFunction is a function that takes in only one argument (the
  93. % neural network parameters)
  94. tic
  95. [nn_params, cost] = fmincg(costFunction, nnParamsInit, options);
  96. toc
  97. % Obtain Theta1 and Theta2 back from nn_params
  98. Theta1 = reshape(nn_params(1:hiddenLayerUnit * (inputLayerUnit + 1)), ...
  99. hiddenLayerUnit, (inputLayerUnit + 1));
  100. Theta2 = reshape(nn_params((1 + (hiddenLayerUnit * (inputLayerUnit + 1))):end), ...
  101. outputLayerUnit, (hiddenLayerUnit + 1));
  102. fprintf('Tranining finished...\n');
  103. %% Visualize weights
  104. %round the parameter into a square number to display properly
  105. M = floor(sqrt(size(Theta1,2)))^2 + 1;
  106. figure;displayData(Theta1(:, 2:M));
  107. %% Predict
  108. pred = predict(Theta1, Theta2, X);
  109. fprintf('Training set accuracy: %f\n', mean(double(pred == y)) *100);
  110. pred = predict(Theta1, Theta2, XVal);
  111. fprintf('Validation set accuracy: %f\n', mean(double(pred == yVal)) *100);
  112. % clear
  113. % clearvars -except hiddenLayerUnit inputLayerUnit outputLayerUnit lambda;
  114. %% Validation for selecting lambda
  115. [lambdaVec, errorTrain, errorVal] = validationCurve(X, y, XVal, yVal, ...
  116. inputLayerUnit, hiddenLayerUnit, ...
  117. outputLayerUnit, nnParamsInit);
  118. % plot
  119. plot(lambdaVec, errorTrain, lambdaVec, errorVal);
  120. legend('Train', 'Cross Validation');
  121. xlabel('lambda');
  122. ylabel('Error');
  123. %}
  124. % lambda = 23; %optimal value
  125. %% Apply learning curve for selecting training example number
  126. [x, errorTrain, errorVal] = learningCurve(X, y, XVal, yVal, lambda, ...
  127. inputLayerUnit, hiddenLayerUnit, ...
  128. outputLayerUnit, nnParamsInit);
  129. % plot
  130. figure;plot(x, errorTrain, x, errorVal);
  131. title('Learning curve for Neural Network')
  132. legend('Train', 'Cross Validation')
  133. xlabel('Number of training examples')
  134. ylabel('Accuracy')
  135. %% Apply learning curve for selecting iteration times
  136. [x, errorTrain, errorVal] = learningCurve2(X, y, XVal, yVal, lambda, ...
  137. inputLayerUnit, hiddenLayerUnit, ...
  138. outputLayerUnit, nnParamsInit);
  139. % plot
  140. figure;plot(x, errorTrain, x, errorVal);
  141. title('Learning curve for Neural Network')
  142. legend('Train', 'Cross Validation')
  143. xlabel('Number of iteration times')
  144. ylabel('Accuracy')
  145. %}

nnCostFunction.m

  1. function [J grad] = nnCostFunction(nn_params, ...
  2. input_layer_size, ...
  3. hidden_layer_size, ...
  4. num_labels, ...
  5. X, y, lambda)
  6. %%----nnCostFunction----
  7. %% Return the cost function error and gradient
  8. %% ---Written by Gizmosir 2014-04-19---
  9. m = size(X, 1);
  10. %forwardPropogation to calculate the cost function
  11. a1 = [ones(1,m); X'];
  12. a2 = [ones(1,m); sigmoid(Theta1 * a1)];
  13. a3 = sigmoid(Theta2 * a2);
  14. %y is a row vector contains 1 & -1.
  15. %make it into 2*m matrix which each column contains [1 0] or [0 1]
  16. Y = zeros(num_labels, m);
  17. for i = 1:m
  18. if y(i) == 1
  19. Y(1, i) = 1;
  20. else
  21. Y(2, i) = 1;
  22. end
  23. end
  24. % for algorithm testing label
  25. % for i = 1:m
  26. % Y(y(i), i) = 1;
  27. % end
  28. %%backpropagation
  29. delta3 = a3 - Y;
  30. delta2 = Theta2' * delta3 .* sigmoidGradient([ones(1,m);Theta1 * a1]);
  31. delta2(1, :) = [];
  32. Delta2 = delta3 * (a2)';
  33. Delta1 = delta2 * (a1)';
  34. % cost function and gradient
  35. Theta1(:,1) = [];
  36. Theta2(:,1) = [];
  37. J = 1/m * sum(sum( - Y .* log(a3) - (1 - Y) .* log(1 - a3))) ...
  38. + lambda/(2*m) * ( sum(sum( Theta1.^2 )) + sum(sum( Theta2.^2 )));
  39. Theta2_grad = 1/m .* Delta2;
  40. Theta1_grad = 1/m .* Delta1;
  41. %Regularized
  42. Theta2_grad = [Theta2_grad(:,1), Theta2_grad(:,2:end) + lambda/m * Theta2];
  43. Theta1_grad = [Theta1_grad(:,1), Theta1_grad(:,2:end) + lambda/m * Theta1];
  44. % Unroll gradients
  45. grad = [Theta1_grad(:) ; Theta2_grad(:)];

LearningCurve.m

  1. function [x, error_train, error_val] = ...
  2. learningCurve(X, y, Xval, yval, lambda, inputLayerUnit, ...
  3. hiddenLayerUnit, outputLayerUnit, nnParamsInit)
  4. % Number of training examples
  5. m = size(X, 1);
  6. error_train = [];
  7. error_val = [];
  8. % parameter setup
  9. options = optimset('MaxIter', 500);
  10. x = 1:10:m;
  11. for i = 1:10:m
  12. costFunction = @(p) nnCostFunction(p, ...
  13. inputLayerUnit, ...
  14. hiddenLayerUnit, ...
  15. outputLayerUnit, X(1:i,:), y(1:i,:), ...
  16. lambda);
  17. [nn_params, cost] = fmincg(costFunction, nnParamsInit, options);
  18. %predict
  19. Theta1 = reshape(nn_params(1:hiddenLayerUnit * (inputLayerUnit + 1)), ...
  20. hiddenLayerUnit, (inputLayerUnit + 1));
  21. Theta2 = reshape(nn_params((1 + (hiddenLayerUnit * (inputLayerUnit + 1))):end), ...
  22. outputLayerUnit, (hiddenLayerUnit + 1));
  23. pred = predict(Theta1, Theta2, X(1:i,:));
  24. error_train = [error_train; mean(double(pred == y(1:i,:))) * 100];
  25. pred = predict(Theta1, Theta2, Xval);
  26. error_val = [error_val; mean(double(pred == yval)) *100];
  27. end

LearningCurve2.m

  1. function [x, error_train, error_val] = ...
  2. learningCurve2(X, y, Xval, yval, lambda, inputLayerUnit, ...
  3. hiddenLayerUnit, outputLayerUnit, nnParamsInit)
  4. % Number of iteration times
  5. x = 100:100:1000;
  6. costFunction = @(p) nnCostFunction(p, ...
  7. inputLayerUnit, ...
  8. hiddenLayerUnit, ...
  9. outputLayerUnit, X, y, ...
  10. lambda);
  11. % Compute the error
  12. for i = 100:100:1000
  13. % parameter setup
  14. options = optimset('MaxIter', i);
  15. [nn_params, cost] = fmincg(costFunction, nnParamsInit, options);
  16. %predict
  17. Theta1 = reshape(nn_params(1:hiddenLayerUnit * (inputLayerUnit + 1)), ...
  18. hiddenLayerUnit, (inputLayerUnit + 1));
  19. Theta2 = reshape(nn_params((1 + (hiddenLayerUnit * (inputLayerUnit + 1))):end), ...
  20. outputLayerUnit, (hiddenLayerUnit + 1));
  21. pred = predict(Theta1, Theta2, X);
  22. error_train = [error_train; mean(double(pred == y)) * 100];
  23. pred = predict(Theta1, Theta2, Xval);
  24. error_val = [error_val; mean(double(pred == yval)) *100];
  25. end

PCA.m

  1. function [U_deduce, K] = PCA(X)
  2. %%---PCA---
  3. %% Apply PCA on given data
  4. %%---Written by Gizmosir 2016-04-23---
  5. %normalize X by subtracting the mean value from each feature
  6. mu = mean(X);
  7. X_norm = bsxfun(@minus, X, mu);
  8. %calculate the covariance matrix
  9. Sigma = 1/numTrain .* X_norm' * X_norm;
  10. [U, S] = svd(Sigma);
  11. % find the minimum K such that 99% variance retained
  12. total = sum(sum(S));
  13. for i = 1:size(Sigma,1)
  14. accuracy = sum(sum(Sigma(1:i,1:i))) / total;
  15. if accuracy > 0.99
  16. K = i;
  17. break;
  18. end
  19. end
  20. %Keep only the importance of U
  21. U_deduce = U(:,1:K);

PhD_LDA_LR_FaceRecognition.m

  1. %PhD_LDA_LR_FaceRecognition
  2. %% Apply lda based Logistic Regression for face recogntion
  3. %% PhD_toolbox is used in this method
  4. %% ---Written by Gizmosir 2016-04-24---
  5. %% initialize
  6. close all;
  7. clear;
  8. clc;
  9. %% Setup the parameters
  10. inputLayerUnit = 128*128; %default input image size 128*128
  11. hiddenLayerUnit = 25; %default
  12. outputLayerUnit = 2; %only 2: yes or no
  13. %% Load dataset
  14. load trainingData% trainingData trainingDataTest
  15. % imageTrain -- 1*1482 cell
  16. % labelTrain -- 1*1482 double
  17. % imageVal -- 1*185 cell
  18. % labelVal -- 1*185 double
  19. %------
  20. % data parameters
  21. numTrain = length(imageTrain);
  22. numVal = length(imageVal);
  23. X = zeros(numTrain, inputLayerUnit);
  24. XVal = zeros(numVal, inputLayerUnit);
  25. % reorder
  26. for i = 1:numTrain
  27. image = imageTrain{i};
  28. image = imresize(image, [sqrt(inputLayerUnit), sqrt(inputLayerUnit)]);
  29. X(i,:) = image(:);
  30. end
  31. for i = 1:numVal
  32. image = imageVal{i};
  33. image = imresize(image, [sqrt(inputLayerUnit), sqrt(inputLayerUnit)]);
  34. XVal(i,:) = image(:);
  35. end
  36. y = labelTrain';
  37. y(y<0) = 0;
  38. yVal = labelVal';
  39. yVal(yVal<0) = 0;
  40. % clear dataset
  41. clear image imageTrain imageVal labelTrain labelVal;
  42. fprintf('Loading data finished...\n');
  43. %% Visualizing Data
  44. % randomly pick 20 row of data
  45. sel = randperm(size(X, 1));
  46. sel = sel(1:20);
  47. displayData(X(sel, :));
  48. fprintf('Visualizing Data finished...\n');
  49. %% Construct LDA subspace using PhD
  50. model = perform_lda_PhD(X', y', 1);
  51. featureVal = linear_subspace_projection_PhD(XVal', model, 1);
  52. %update training data
  53. X = model.train';
  54. XVal = featureVal';
  55. %% Visualizing Data
  56. figure; plot(X, y, '.'); title('Training data point after LDA',...
  57. 'FontSize', 18);
  58. figure; plot(XVal, yVal, '.'); title('Validation data point after LDA', ...
  59. 'FontSize', 18);
  60. %% Compute Cost and Gradient
  61. % initialize parameters
  62. [m, n] = size(X);
  63. X = [ones(m,1), X];
  64. initialTheta = zeros(n+1, 1);
  65. lambda = 10;
  66. % cost function
  67. [cost, grad] = costFunctionReg(initialTheta, X, y, lambda);
  68. %% Optimize using fminunc function
  69. % Set parameters
  70. options = optimset('GradObj', 'on', 'Maxiter', 50);
  71. % Optimize
  72. [theta, J] = fminunc(@(t)(costFunctionReg(t, X, y, lambda)), ...
  73. initialTheta, options);
  74. %% Display boundary for validation set
  75. xBdy = min(XVal):0.1:max(XVal);
  76. yBdy = sigmf(xBdy, [theta(1),theta(2)]);
  77. hold on;plot(xBdy,yBdy,'r-');
  78. xlabel('x');ylabel('t');
  79. boundary = - theta(1) / theta(2);
  80. %% Predict
  81. pred = predictReg(theta, X);
  82. fprintf('Training set accuracy: %f\n', mean(double(pred == y)) *100);
  83. [m, n] = size(XVal);
  84. XVal = [ones(m,1), XVal];
  85. pred = predictReg(theta, XVal);
  86. fprintf('Validation set accuracy: %f\n', mean(double(pred == yVal)) *100);

GUI.m (image labeling)

  1. function varargout = GUI(varargin)
  2. %% ----GUI----
  3. %% GUI for face labeling
  4. %% ----Written by Gizmosir 2016-04-18----
  5. % Begin initialization code - DO NOT EDIT
  6. gui_Singleton = 1;
  7. gui_State = struct('gui_Name', mfilename, ...
  8. 'gui_Singleton', gui_Singleton, ...
  9. 'gui_OpeningFcn', @GUI_OpeningFcn, ...
  10. 'gui_OutputFcn', @GUI_OutputFcn, ...
  11. 'gui_LayoutFcn', [] , ...
  12. 'gui_Callback', []);
  13. if nargin && ischar(varargin{1})
  14. gui_State.gui_Callback = str2func(varargin{1});
  15. end
  16. if nargout
  17. [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  18. else
  19. gui_mainfcn(gui_State, varargin{:});
  20. end
  21. % End initialization code - DO NOT EDIT
  22. % --- Executes just before GUI is made visible.
  23. function GUI_OpeningFcn(hObject, eventdata, handles, varargin)
  24. % Choose default command line output for GUI
  25. handles.output = hObject;
  26. % global variable
  27. global imageName;
  28. global imageLabel;
  29. global imageFace;
  30. global imageFaceFrame; %face in current frame
  31. global faceDetector;
  32. % load image file and get image Name
  33. file = dir('../kobe3/*.jpg'); %kobe3
  34. % file = dir('./test/*.jpg');
  35. imageName = {file.name};
  36. % define faceDetector
  37. faceDetector = vision.CascadeObjectDetector();
  38. % define image face as cell
  39. imageFace = {};
  40. imageFaceFrame = {};
  41. % define image label as a two row matrix
  42. imageLabel = []; % zeros(2, length(imageName));
  43. % Update imageFace and label
  44. update(handles);
  45. % Update handles structure
  46. guidata(hObject, handles);
  47. % --- Outputs from this function are returned to the command line.
  48. function varargout = GUI_OutputFcn(hObject, eventdata, handles)
  49. % varargout cell array for returning output args (see VARARGOUT);
  50. % hObject handle to figure
  51. % eventdata reserved - to be defined in a future version of MATLAB
  52. % handles structure with handles and user data (see GUIDATA)
  53. % Get default command line output from handles structure
  54. varargout{1} = handles.output;
  55. % --- Executes on button press in pushbutton1.(YES button)
  56. function pushbutton1_Callback(hObject, eventdata, handles)
  57. % record the current image label
  58. global imageLabel;
  59. imageLabel = [imageLabel, 1];
  60. % display next image
  61. update(handles);
  62. % --- Executes on button press in pushbutton2.(NO button)
  63. function pushbutton2_Callback(hObject, eventdata, handles)
  64. % record the current image label
  65. global imageLabel;
  66. imageLabel = [imageLabel, -1];
  67. % display next image
  68. update(handles);
  69. function imageFaceFrame = faceDetect(imageName, handles)
  70. %global variable
  71. global faceDetector;
  72. % load original image
  73. imageName = sprintf('../kobe3/%s',imageName);
  74. imageOri = imread(imageName);
  75. % detect the face using faceDetector
  76. BBox = step(faceDetector, imageOri);
  77. % delete face small then 60x60 pixels
  78. if ~isempty(BBox)
  79. BBox = sortrows(BBox, -3);
  80. index = min(find(BBox(:,3)<48));
  81. BBox(index:end, :) = [];
  82. end
  83. % randomly pick a windoe if there is no matching
  84. if isempty(BBox)
  85. BBox = [1, 1, 128, 128];
  86. end
  87. % show face rectangle
  88. B = insertObjectAnnotation(imageOri, 'rectangle', BBox, 'Face');
  89. axes(handles.axes2); imshow(B);
  90. % save detected face image
  91. for i = 1:size(BBox, 1)
  92. imageFaceFrame{i} = imageOri(BBox(i, 2):BBox(i, 2)+BBox(i, 4)-1, ...
  93. BBox(i, 1):BBox(i, 1)+BBox(i, 3)-1);
  94. end
  95. function update(handles)
  96. global imageName
  97. global imageFace
  98. global imageFaceFrame
  99. if isempty(imageName)
  100. finishLabeling(handles);
  101. elseif ~isempty(imageFaceFrame);
  102. %display and delete the next face image
  103. image = imageFaceFrame{1};
  104. imageFace = [imageFace, image];
  105. imageFaceFrame(1) = [];
  106. axes(handles.axes1); imshow(image);
  107. else
  108. % find boundingbox using faceDetecting
  109. imageFaceFrame = faceDetect(imageName{1}, handles);
  110. %store and delete the first image
  111. image = imageFaceFrame{1};
  112. imageFace = [imageFace, image];
  113. imageFaceFrame(1) = [];
  114. axes(handles.axes1); imshow(image);
  115. %change display tag text
  116. set(handles.text1, 'String', imageName(1));
  117. % delete the picture
  118. imageName(1) = [];
  119. end
  120. function finishLabeling(handles)
  121. % save imageFace and imageLabel
  122. global imageFace;
  123. global imageLabel;
  124. save('myData', 'imageFace', 'imageLabel');
  125. %clear
  126. close(GUI);
  127. clear;
  128. clc;
  129. display('finish...');

dataSeperate.m

  1. %%---dataSeparate---
  2. %% separate the image data set into three part
  3. %% Trainset:80%, Validationset: 10% Testset:10%
  4. %% ----Written by Gizmosir 2016-04-19----
  5. % clear
  6. close all;
  7. clear;
  8. clc;
  9. % load data
  10. load myData; %myDataCorrected dataAlgorithmTest
  11. m = length(imageFace);
  12. % First ramdomly reorder all the label so that it become totally randomly
  13. % distributed
  14. sel = randperm(m);
  15. index = [floor(m*0.8), floor(m*0.9)];
  16. % training dataset
  17. imageTrain = imageFace(sel(1:index(1)));
  18. labelTrain = imageLabel(sel(1:index(1)));
  19. % cross-validation dataset
  20. imageVal = imageFace(sel(index(1)+1:index(2)));
  21. labelVal = imageLabel(sel(index(1)+1:index(2)));
  22. % testing dataset
  23. imageTest = imageFace(sel(index(2)+1:end));
  24. labelTest = imageLabel(sel(index(2)+1:end));
  25. %% reshape the image(now move to nnTrainer)
  26. % for i = 1:length(imageTrain)
  27. % imageTrain{i} = imresize(imageTrain{i}, [128, 128]);
  28. % end
  29. %
  30. % %% reshape the image
  31. % for i = 1:length(imageVal)
  32. % imageVal{i} = imresize(imageVal{i}, [128, 128]);
  33. % end
  34. save('trainingDataTest', 'imageTrain', 'labelTrain', ...
  35. 'imageVal', 'labelVal');
  36. save('testingDataTest', 'imageTest', 'labelTest');

LabelChecking.m

  1. %%---LabelChcking---
  2. %% display and check the labeled image
  3. %% ---Written by Gizmosir 2016-04-21---
  4. % initilize
  5. close all;
  6. clear;
  7. clc;
  8. % load data
  9. load myDataCorrected
  10. % seperate into two group
  11. indexTrue = find(imageLabel>0);
  12. indexFalse = find(imageLabel<0);
  13. imageDisplay = [];
  14. for i = 1:length(indexFalse) %indexTrue OR indexFalse
  15. image = imageFace{indexFalse(i)}; %indexTrue indexFalse
  16. image = imresize(image, [128 128]);
  17. imageDisplay(rem(i, 64)+1,:) = image(:);
  18. if rem(i, 64) == 0
  19. displayData(imageDisplay(1:64, :));
  20. imageDisplay = [];
  21. pause;
  22. end
  23. end

imageRenamer.m

  1. %%----fileRenamer----
  2. %% rename all the image store in image file
  3. %% ----Written by Gizmosir 2016-04-18----
  4. %clear;
  5. close;
  6. clear;
  7. clc;
  8. %%load all the image in image folder
  9. fileDirectory = '../kobe3/';
  10. file = dir(fullfile(fileDirectory, '*.jpg'));
  11. fileNames = {file.name};
  12. %rename the file using movefile
  13. for i = 1:length(fileNames)
  14. newName = fullfile(fileDirectory, sprintf('%04d.jpg', i));
  15. movefile(fullfile(fileDirectory, fileNames{i}), newName);
  16. end
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注