选择用于图像识别的神经网络变量

时间:2018-04-16 15:45:21

标签: matlab machine-learning neural-network octave

我有一套训练套装,包括6张不同的多米诺骨牌以及一个婴儿的“对照”组的89张图像 - 所有这些图像分为7组。因此输出y是7.每个图像是100x100并且是黑色和白色,导致X为100.000。

我正在使用来自Andrew Ng使用Octave的课程的1个隐藏层神经网络代码。它略有修改。

我首先尝试了3个不同的组(两个多米诺骨牌,一个婴儿),并且设法获得接近100%的准确度。我现在已将它增加到7个不同的图像组。 WAY的精确度下降了,除了婴儿照片(与多米诺骨牌相差很大)之外几乎没有任何正确的答案。

我尝试了10个不同的lambda值,5到20之间的10个不同神经元数,以及尝试不同的迭代次数,并根据成本和准确度绘制它,以便找到最佳拟合值。

我也尝试了功能规范化(在下面的代码中注释掉了)但它没有帮助。

这是我正在使用的代码:

% Initialization
clear ; close all; clc; more off;
pkg load image;

fprintf('Running Domino Identifier ... \n');

%iteration_vector = [100, 300, 1000, 3000, 10000, 30000];
%accuracies = [];
%costs = [];

%for iterations_i = 1:length(iteration_vector)

  # INPUTS
  input_layer_size  = 10000;  % 100x100 Input Images of Digits
  hidden_layer_size = 50;   % Hidden units
  num_labels = 7;          % Number of different outputs
  iterations = 100000; % Number of iterations during training
  lambda = 0.13;
  %hidden_layer_size = hidden_layers(hidden_layers_i);
  %lambda = lambdas(lambda_i)
  %iterations = %iteration_vector(iterations_i)

  [X,y] = loadTrainingData(num_labels);
  %[X_norm, mu, sigma] = featureNormalize(X_unnormed);
  %X = X_norm;

  initial_Theta1 = randInitializeWeights(input_layer_size, hidden_layer_size);
  initial_Theta2 = randInitializeWeights(hidden_layer_size, num_labels);
  initial_nn_params = [initial_Theta1(:) ; initial_Theta2(:)];

  [J grad] = nnCostFunction(initial_nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lambda);

  fprintf('\nTraining Neural Network... \n')

  %  After you have completed the assignment, change the MaxIter to a larger
  %  value to see how more training helps.
  options = optimset('MaxIter', iterations);

  % Create "short hand" for the cost function to be minimized
  costFunction = @(p) nnCostFunction(p, input_layer_size, hidden_layer_size, num_labels, X, y, lambda);

  % Now, costFunction is a function that takes in only one argument (the
  % neural network parameters)
  [nn_params, cost] = fmincg(costFunction, initial_nn_params, options);

  % Obtain Theta1 and Theta2 back from nn_params
  Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...
                   hidden_layer_size, (input_layer_size + 1));

  Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...
                   num_labels, (hidden_layer_size + 1));

  displayData(Theta1(:, 2:end));
  [predictionData, images] = loadTrainingData(num_labels);
  [h2_training, pred_training] = predict(Theta1, Theta2, predictionData);
  fprintf('\nTraining Accuracy: %f\n', mean(double(pred_training' == y)) * 100);

  %if length(accuracies) > 0
  %  accuracies = [accuracies; mean(double(pred_training' == y))];
  %else
  % accuracies = [mean(double(pred_training' == y))];
  %end

  %last_cost = cost(length(cost));
  %if length(costs) > 0
  %  costs = [costs; last_cost];
  %else
  % costs = [last_cost];
  %end
%endfor % Testing samples

fprintf('Loading prediction images');
[predictionData, images] = loadPredictionData();
[h2, pred] = predict(Theta1, Theta2, predictionData)

for i = 1:length(pred)  
  figure;
  displayData(predictionData(i, :));
  title (strcat(translateIndexToTile(pred(i)), " Certainty:", num2str(max(h2(i, :))*100))); 
  pause;
endfor
%y = provideAnswers(im_vector);

我现在的问题是:

  1. 就X和其他人之间的巨大差异而言,我的数字是否“关闭”?

  2. 我该怎么做才能改善这个神经网络?

  3. 如果我进行功能标准化,我是否需要在某处再将数字乘以0-255范围?

1 个答案:

答案 0 :(得分:1)

  

我该怎么做才能改善这个神经网络?

使用具有多个层(例如,5个层)的卷积神经网络(CNN)。对于视力问题,CNN的优势明显优于MLP。在这里,您正在使用具有单个隐藏层的MLP。这个网络在7个类的图像问题上表现不佳是合理的。一个问题是您拥有的培训数据量。通常,我们希望每个班级至少有数百个样本。

  

如果我进行了标准化功能,我是否需要在某处再将数字乘以0-255范围?

一般情况下,不适用于分类。归一化可视为预处理步骤。但是,如果您处理图像重建等问题,则需要在最后转换回原始域。