Logistic regression for fault detection in an image

Davors72

Basically, I want to detect a fault in an image using logistic regression. I'm hoping to get so feedback on my approach, which is as follows:

For training:

  1. Take a small section of the image marked "bad" and "good"
  2. Greyscale them, then break them up into a series of 5*5 pixel segments
  3. Calculate the histogram of pixel intensities for each of these segments
  4. Pass the histograms along with the labels to the Logistic Regression class for training
  5. Break the whole image into 5*5 segments and predict "good"/"bad" for each segment.

Using the sigmod function the linear regression equation is:

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

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

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 = h-y; //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 sigmoid and the 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 by:

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

As we are using a histogram of intensities the input and weight arrays are 255 elements. My hope is to use it on something like a picture of an apple with a bruise and use it to identify the brused parts. The (normalized) histograms for the whole brused and apple training sets look somthing like this:

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

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

I'm not 100% convinced that using the intensites alone will produce the results I want but even so, using it on a clearly seperable data set isn't working either. To test it I passed it a, labeled, completely white and a completely black image. I then run it on the small image below:

enter image description here

Even on this image it fails to identify any segments as being black.

Using MSE I see that the cost is converging downwards to a point where it remains, for the black and white test it starts at about cost 250 and settles on 100. The apple chuncks start at about 4000 and settle on 1600.

What I can't tell is where the issues are.

Is, the approach sound but the implementation broken? Is logistic regression the wrong algorithm to use for this task? Is gradient decent not robust enough?

Davors72

I forgot to answer this... Basically the problem was in my histograms which when generated weren't being memset to 0. As to the overall problem of whether or not logistic regression with greyscale images was a good solution, the answer is no. Greyscale just didn't provide enough information for good classification. Using all colour channels was a bit better but I think the complexity of the problem I was trying to solve (bruises in apples) was a bit much for simple logistic regression on its own. You can see the results on my blog here.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Logistic Regression with GLM

分類Dev

python logistic regression (beginner)

分類Dev

Firth's Logistic Regression

分類Dev

sklearn RFE with logistic regression

分類Dev

Plot logistic regression curve in R

分類Dev

Dummy variables for Logistic regression in R

分類Dev

Logistic stepwise regression with a fixed number of predictors

分類Dev

Plot coefficients from a multinomial logistic regression model

分類Dev

Can logistic regression be used for variables containing lists?

分類Dev

Logistic regression: Drop Insignificant prediction Variables

分類Dev

Training logistic regression using scikit learn for multi-class classification

分類Dev

How to get feature importance in logistic regression using weights?

分類Dev

Massively worse performance in Tensorflow compared to Scikit-Learn for Logistic Regression

分類Dev

How to interpret probability column in spark logistic regression prediction?

分類Dev

value error on logistic regression model and how to check prediction accuracy?

分類Dev

how initial bias value is chosen in sklearn logistic regression?

分類Dev

Difference between Generalized linear modelling and regular logistic regression

分類Dev

Logistic regression - eval(family$initialize) : y values must be 0 <= y <= 1

分類Dev

Logistic Regression Model using Regularization (L1 / L2) Lasso and Ridge

分類Dev

Binary Image "Lines-of-Sight" Edge Detection

分類Dev

Removing lines from an image a notebook for digit detection python

分類Dev

Store Tensorflow object detection API image output with boxes in CSV format

分類Dev

sklearn Logistic Regression "ValueError:dim 3の配列が見つかりました。推定値<= 2です。"

分類Dev

グラデーションが間違っているのはなぜですか(Coursera、Logistic Regression、Julia)?

分類Dev

Should OpenCV Python Canny edge detection be giving me very different results depending on image size?

分類Dev

Segmentation fault in segmentation fault handler

分類Dev

C struct Segmentation fault

分類Dev

"Occasional" Segmentation Fault

分類Dev

Segmentation fault in a list with memcpy

Related 関連記事

  1. 1

    Logistic Regression with GLM

  2. 2

    python logistic regression (beginner)

  3. 3

    Firth's Logistic Regression

  4. 4

    sklearn RFE with logistic regression

  5. 5

    Plot logistic regression curve in R

  6. 6

    Dummy variables for Logistic regression in R

  7. 7

    Logistic stepwise regression with a fixed number of predictors

  8. 8

    Plot coefficients from a multinomial logistic regression model

  9. 9

    Can logistic regression be used for variables containing lists?

  10. 10

    Logistic regression: Drop Insignificant prediction Variables

  11. 11

    Training logistic regression using scikit learn for multi-class classification

  12. 12

    How to get feature importance in logistic regression using weights?

  13. 13

    Massively worse performance in Tensorflow compared to Scikit-Learn for Logistic Regression

  14. 14

    How to interpret probability column in spark logistic regression prediction?

  15. 15

    value error on logistic regression model and how to check prediction accuracy?

  16. 16

    how initial bias value is chosen in sklearn logistic regression?

  17. 17

    Difference between Generalized linear modelling and regular logistic regression

  18. 18

    Logistic regression - eval(family$initialize) : y values must be 0 <= y <= 1

  19. 19

    Logistic Regression Model using Regularization (L1 / L2) Lasso and Ridge

  20. 20

    Binary Image "Lines-of-Sight" Edge Detection

  21. 21

    Removing lines from an image a notebook for digit detection python

  22. 22

    Store Tensorflow object detection API image output with boxes in CSV format

  23. 23

    sklearn Logistic Regression "ValueError:dim 3の配列が見つかりました。推定値<= 2です。"

  24. 24

    グラデーションが間違っているのはなぜですか(Coursera、Logistic Regression、Julia)?

  25. 25

    Should OpenCV Python Canny edge detection be giving me very different results depending on image size?

  26. 26

    Segmentation fault in segmentation fault handler

  27. 27

    C struct Segmentation fault

  28. 28

    "Occasional" Segmentation Fault

  29. 29

    Segmentation fault in a list with memcpy

ホットタグ

アーカイブ