Logistic regression for detecting errors in the image

Basically, I want to detect an error in an image using logistic regression. I hope to get such feedback on my approach, which looks like this:

For training:

  • Take a small fragment of the image with the words "bad" and "good"
  • Grayscale, then break them into several segments measuring 5 * 5 pixels.
  • Calculate a histogram of pixel intensity for each of these segments.
  • Pass bar charts with labels to the Logistic Regression class for training
  • Divide the entire image into 5 * 5 segments and predict “good” / “bad” for each segment.

Using the sigmoid function, the linear regression equation:

1/ (1 - e^(xθ)) 

Where x is the input value and theta (θ) is the weight. I use gradient descent to train the network. My code for this:

 void LogisticRegression::Train(float **trainingSet,float *labels, int m) { float tempThetaValues[m_NumberOfWeights]; for (int iteration = 0; iteration < 10000; ++iteration) { // Reset the temp values for theta. memset(tempThetaValues,0,m_NumberOfWeights*sizeof(float)); float error = 0.0f; // For each training set in the example for (int trainingExample = 0; trainingExample < m; ++trainingExample) { float * x = trainingSet[trainingExample]; float y = labels[trainingExample]; // Partial derivative of the cost function. float h = Hypothesis(x) - y; for (int i =0; i < m_NumberOfWeights; ++i) { tempThetaValues[i] += h*x[i]; } float cost = hy; //Actual J(theta), Cost(x,y), keeps giving NaN use MSE for now error += cost*cost; } // Update the weights using batch gradient desent. for (int theta = 0; theta < m_NumberOfWeights; ++theta) { m_pWeights[theta] = m_pWeights[theta] - 0.1f*tempThetaValues[theta]; } printf("Cost on iteration[%d] = %f\n",iteration,error); } } 

Where the sigmoid and hypothesis are calculated using:

 float LogisticRegression::Sigmoid(float z) const { return 1.0f/(1.0f+exp(-z)); } float LogisticRegression::Hypothesis(float *x) const { float z = 0.0f; for (int index = 0; index < m_NumberOfWeights; ++index) { z += m_pWeights[index]*x[index]; } return Sigmoid(z); } 

And the final prediction is given:

 int LogisticRegression::Predict(float *x) { return Hypothesis(x) > 0.5f; } 

Since we use an intensity histogram, input and weighted arrays are 255 elements. My hope is to use it on something like an image of an apple with a bruise and use it to identify excess parts. The histograms (normalized) for all sets for brunts and apple learning look something like this:

For "good" apple sections (y = 0): the'0 'designated training set

For the "bad" sections of the apple (y = 1): enter image description here

I am not 100% sure that using only intensities will produce the results I want, but even though using it on an explicitly shared dataset does not work either. To test it, I transmitted it, a marked, completely white and completely black image. Then I run it in the small image below:

enter image description here

Even in this image, he cannot identify any segments as black.

Using MSE, I see that the price converges down to the point where it remains; for a black and white test, it starts at about 250 and settles at 100. Apple chunks start at about 4000 and settle at 1600.

I can’t say where the problems are.

Is there a sound approach, but the implementation is broken? Is logistic regression the wrong algorithm to use for this task? Is the gradient worthy not reliable enough?

+5
source share
1 answer

I forgot to answer this question ... Basically, the problem was in my histograms, which when generating were not memset to 0. As regards the general problem of whether logistic regression with grayscale images was a good solution, there is no answer. Grayscale is simply not enough information for a good classification. Using all the color channels was a little better, but I believe that the complexity of the problem I was trying to solve (bruises in apples) was not enough for a simple logistic regression in itself. You can see the results on my blog here .

+1
source

Source: https://habr.com/ru/post/1237021/


All Articles