Using a Logistic regression along with Neural Networks for Cat vs Non-Cat Image Classification

Logistic Regression

Logistic regression is the appropriate regression analysis to conduct when the dependent variable is dichotomous (binary). Like all regression analyses, the logistic regression is a predictive analysis.

How do computers interpret Images

6x6 RGB abstract image representation (https://www.google.com/search?q=6+x+6+rgb+image+coursera&tbm=isch&ved=2ahUKEwiB2Lb8_uXtAhWaIjQIHS67CxEQ2-cCegQIABAA&oq=6+x+6+rgb+image+coursera&gs_lcp=CgNpbWcQA1CLlwFYg6UBYKumAWgAcAB4AIABUogBsgWSAQE5mAEAoAEBqgELZ3dzLXdpei1pbWfAAQE&sclient=img&ei=YTXkX8GjOZrF0PEPrvauiAE&bih=671&biw=1440#imgrc=A9RsI7mdYMq3oM)

General Architecture

Logistic Regression, using a Neural Network (https://www.coursera.org/learn/neural-networks-deep-learning/ungradedLab/63Vsy/lab)
  1. Define the model structure (such as number of input features)
  2. Initialize the model’s parameters
  3. Loop:
  • Calculate current loss (forward propagation)
  • Calculate current gradient (backward propagation)
  • Update parameters (gradient descent)

Step 1: Creating a new Notebook

Creating a library:

Step 2: Loading the dataset

The dataset is saved here:https://github.com/aditimukerjee/Cat-and-Non-cat-for-logistic-regression

Step 3: Analyzing the dataset

m_train = train_set_x_orig.shape[0]
m_test = test_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[1]

Number of training examples: m_train = 209
Number of testing examples: m_test = 50
Height/Width of each image: num_px = 64
Each image is of size: (64, 64, 3)
train_set_x shape: (209, 64, 64, 3)
train_set_y shape: (1, 209)
test_set_x shape: (50, 64, 64, 3)
test_set_y shape: (1, 50)

Step 3: Reshaping the dataset

We need to now reshape images of shape (num_px, num_px, 3) in a numpy-array of shape (num_px ∗ num_px ∗ 3, 1). After this, our training (and test) dataset is a numpy-array where each column represents a flattened image. There should be m_train (respectively m_test) columns.

train_set_x_flatten shape: (12288, 209)
train_set_y shape: (1, 209)
test_set_x_flatten shape: (12288, 50)
test_set_y shape: (1, 50)
sanity check after reshaping: [17 31 56 22 33]

Step 4: Sigmoid function

Writing down the sigmoid function , a prerequisite for logistic regression


print (“sigmoid([0, 2]) = “ + str(sigmoid(np.array([0,2]))))
Output : sigmoid([0, 2])[ 0.5 0.88079708]

Step 4: Initiating parameters

Initializing w as a vector of zeros. Initialize_with_zeros function creates a vector of zeros of shape (dim, 1) for w and initializes b to 0.

Output: w= [[ 0.] [ 0.]] b=0

Step 5: Forward and Backward Propogation

Implementing a function propagate() that computes the cost function and its gradient.

Output : dw=[[ 0.99845601] [ 2.39507239]] ,db=0.00145557813678 ,cost=5.801545319394553

Step 6: Optimization

The goal is to learn w and b by minimizing the cost function J. For a parameter θ, the update rule is θ=θ−α dθ, where α is the learning rate.

w = [[0.19033591]
[0.12259159]]
b = 1.9253598300845747
dw = [[0.67752042]
[1.41625495]]
db = 0.21919450454067652

Step 7 : Prediction

There are two steps to computing predictions:

  1. Calculate Ŷ =A=σ(wTX+b)Y^=A=σ(wTX+b)
  2. Convert the entries of a into 0 (if activation <= 0.5) or 1 (if activation > 0.5), stores the predictions in a vector Y_prediction. If you wish, you can use an if/else statement in a for loop (though there is also a way to vectorize this).
predictions = [[1. 1. 0.]]

Step 8 : Merge all functions into a mode

Merging all the functions developed so far together.

train accuracy: 99.99876382512535 %
test accuracy: 72.01052229722973 %

Step 12: Testing your model with your images

Created an image directory in the notebook and upload my images there, then assign the value of variable image in the following code as the image name.

Image from google search
Image from google search
Image from google search
Image from google search
Image from google search
Image from google search

Conclusion

The accuracy of the training set is 99.99% and the accuracy of the testing set is 72%. Out of the six images used for prediction, one image was predicted incorrectly since the accuracy is still low and it can be further improved.

If you enjoyed this story, please click the 👏 button and share to help others find it! Feel free to leave a comment below.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aditi Mukerjee

Aditi Mukerjee

Engineer. Data Analyst. Machine Learning enthusiast