/CV-CNN-for-binary-Classification

Automatic preparation and classification of binary image data with Keras.

Primary LanguageJupyter Notebook

Computer Vision: CNN for binary Classification

readme crisp dm

Source: DataFlair

The goal of this project is to use a convolutional neural network (CNN) to determine cats and dogs images. How I proceeded exactly and what results I achieved can be read in my blog post: Computer Vision - Convolutional Neural Network

Table of Contents

  1. Introduction
  2. Software Requirements
  3. Getting Started
  4. Folder Structure
  5. Running the Jupyter Notebook
    1. Execution of the preprocessing_CNN.py file
    2. Classify Cat Dog Images
  6. Project Results
  7. Link to the Publication
  8. Authors
  9. Project Motivation
  10. References

Introduction

For this repository I wrote a preprocessing.py file which automatically randomizes the provided image data and divides it into a training, validation and test part. This is followed by model training using a CNN. The storage of the best model as well as the safeguarding of all important metrics during the model training is also fully automatic. This is a best practice guideline on how to create a binary image classifier and bring it to production.

Software Requirements

Required libraries:

  • Python 3.x
  • Scikit-Learn
  • Keras
  • TensorFlow
  • Numpy
  • Pandas
  • Matplotlib
  • OpenCV

Please run pip install -r requirements.txt

Getting Started

  1. Make sure Python 3 is installed.
  2. Clone the repository and navigate to the project's root directory in the terminal
  3. Download the cats dataset. Unzip the folder and place the images in the cloned repository in the folder cats. If the folder does not exist yet, please create one.
  4. Download the dogs dataset. Unzip the folder and place the images in the cloned repository in the folder dogs. If the folder does not exist yet, please create one.
  5. Start the notebook Computer Vision - CNN for binary Classification.ipynb.

Folder Structure

The current folder structure should look like this:

C:.
│   Computer Vision - CNN.ipynb
│   preprocessing_CNN.py
│
├───cats
├───dogs
└───test_pictures

Running the Jupyter Notebook

Execution of the preprocessing_CNN.py file

Import all necessary libraries and execute the train-validation-test-split function.

Here is the output of the function:

result_py_file

Classify Cat Dog Images

Execute all remaining lines of code in the notebook.

Descriptive statistics

statistics1

statistics2

Model Evaluation

validation_plot

Final Folder Structure

The final folder structure should now look like this:

C:.
│   Computer Vision - CNN.ipynb
│   preprocessing_CNN.py
│
├───cats
├───cats_and_dogs
│   ├───test
│   │   ├───cats
│   │   └───dogs
│   ├───train
│   │   ├───cats
│   │   └───dogs
│   └───validation
│       ├───cats
│       └───dogs
├───ckpt_1_simple_CNN
│       Cats_Dogs_CNN_4_Conv_F32_64_128_128_epoch_30.h5
│       class_assignment_df_Cats_Dogs_CNN_4_Conv_F32_64_128_128_epoch_30.csv
│       history_df_Cats_Dogs_CNN_4_Conv_F32_64_128_128_epoch_30.csv
│
├───ckpt_2_CNN_with_augm
│       Cats_Dogs_CNN_4_Conv_F32_64_128_128_epoch_60_es.h5
│       class_assignment_df_Cats_Dogs_CNN_4_Conv_F32_64_128_128_epoch_60_es.csv
│       history_df_Cats_Dogs_CNN_4_Conv_F32_64_128_128_epoch_60_es.csv
│       img_height.pkl
│       img_width.pkl
│
├───dogs
└───test_pictures

Project Results

The best model achieved a validation accuracy of over 91%.

results

Here are a few more test predictions:

test_prediction1

test_prediction2

test_prediction3

test_prediction4

Link to the Publication

Here is the link to my blog post: Computer Vision - Convolutional Neural Network.

If this repository/publication helped you, you are welcome to read other blog posts I wrote on the topic of Computer Vision:

Authors

Project Motivation:

I've been blogging since 2018 on my homepages about all sorts of topics related to Machine Learning, Data Analytics, Data Science and much more. You are welcome to visit them:

I also publish individual interesting sections from my publications in separate repositories to make their access even easier.

References

The content of the entire Repository was created using the following sources:

Chollet, F. (2018). Deep learning with Python (Vol. 361). New York: Manning.