Git Product home page Git Product logo

dit-deep-learning / cat-dog-classification-flask-app Goto Github PK

View Code? Open in Web Editor NEW

This project forked from reichu31/cat-dog-classification-flask-app

0.0 1.0 0.0 12.17 MB

We successfully built a deep neural network model by implementing Convolutional Neural Network (CNN) to classify dog and cat images with very high accuracy 97.32 %. In addition, we also built a Flask application so user can upload their images and classify easily.

Python 7.52% JavaScript 0.72% HTML 43.59% Jupyter Notebook 48.17%

cat-dog-classification-flask-app's Introduction

Building a Flask app on Image Classification of Dog/Cat Dataset implemented by Convolutional Neural Network (CNN)

Phuong T.M. Chu & Minh H. Nguyen

This is the project that we finished after the 6th week of studying Machine Learning.

INTRODUCTION

1. The Dog vs. Cat Dataset

Dogs vs. Cats dataset provided by Microsoft Research contains 25,000 images of dogs and cats with the labels

  • 1 = dog
  • 0 = cat

2. Project goals

  • Building a deep neural network using TensorFlow to classify dogs and cats images.

  • Making a Flask application so user can upload their photos and receive the prediction.

3. Project plan

During this project, we need to answer these following questions:

A. Build the model

  • How to import the data
  • How to preprocess the images
  • How to create a model
  • How to train the model with the data
  • How to export the model
  • How to import the model

B. Build the Flask app

Front end

  • HTML
    • How to connect frontend to backend
    • How to draw a number on HTML
    • How to make UI looks good

Back end

  • Flask
    • How to set up Flask
    • How to handle backend error
    • How to make real-time prediction
    • Combine the model with the app

SETUP ENVIRONMENT

  • In order to run our model on a Flask application locally, you need to clone this repository and then set up the environment by these following commands:
python3 -m pip install --user pipx
python3 -m pipx ensurepath

pipx install pipenv

# Install dependencies
pipenv install --dev

# Setup pre-commit and pre-push hooks
pipenv run pre-commit install -t pre-commit
pipenv run pre-commit install -t pre-push
  • On the Terminal, use these commands:
# enter the environment
pipevn shell
pipenv graph
set FLASK_APP=app.py
set FLASK_ENV=development
export FLASK_DEBUG=1
flask run
  • If you have error ModuleNotFoundError: No module named 'tensorflow' then use
pipenv install tensorflow==2.0.0beta-1
  • If * Debug mode: off then use
export FLASK_DEBUG=1
  • Run the model by
pipenv run flask run
  • If you want to exit pipenv shell, use exit

HOW IT WORK: CONVOLUTIONAL NEURAL NETWORK (CNN)

In deep learning, a convolutional neural network (CNN, or ConvNet) is a class of deep neural networks, most commonly applied to analyzing visual imagery. (Wiki)

For this project, we used pre-trained model MobileNetV2 from keras. MobileNetV2 is a model that was trained on a large dataset to solve a similar problem to this project, so it will help us to save lots of time on buiding low-level layers and focus on the application.

Note: You can learn more about CNN architecture here

1. Load and preprocess images:

  • Import path, listdir from os library.
  • Find and save all the image's path to all_image_paths. (note that our images is in folder train).
all_image_path = [path.join('train', p) for p in listdir('train') if isfile(path.join('train', p))]
  • Define a function to load and preprocess image from path:
def load_and_preprocess_image(path):
    file = tf.io.read_file(path)
    image = tf.image.decode_jpeg(file , channels=3)
    image = tf.image.resize(image, [192, 192]) # resize all images to the same size.
    image /= 255.0  # normalize to [0,1] range
    image = 2*image-1  # normalize to [-1,1] range
    return image
  • Load and preprocess all images which path is in all_image_path:
all_images = [load_and_preprocess_image(path) for path in all_image_path]
  • Save all image labels in all_image_labels:
dict = {'cat': 0, 'dog': 1}

# path.split('.')[0][-3:] return the name of the image ('dog' or 'cat')
labels = [path.split('.')[0][-3:] for path in all_image_path] 

# Transfer name-labels to number-labels:
all_image_labels = [dict[label] for label in labels]
  • To implement batch training, we put the images and labels into Tensorflow dataset:
ds = tf.data.Datasets.from_tensor_slices((all_images, all_image_labels))

2. Building CNN model:

The CNN model contain MobileNetV2, Pooling, fully-connected hidden layer and Output layer.

  • First we create mobile_net as an instance of MobileNetV2:
mobile_net = tf.keras.applications.MobileNetV2(input_shape=(192, 192, 3), include_top=False)
mobile_net.trainable=False # this told the model not to train the mobile_net.
  • Then we build CNN model:
cnn_model = keras.models.Sequential([
    mobile_net, # mobile_net is low-level layers
    keras.layers.GlobalAveragePooling2D(), 
    keras.layers.Flatten(), 
    keras.layers.Dense(64, activation="relu"), # fully-connected hidden layer 
    keras.layers.Dense(2, activation="softmax") # output layer
])

3. Training model:

We almost there! But before training our cnn_model, we need to implement batch to the training data so that the model will train faster.

BATCH_SIZE = 32
AUTOTUNE = tf.data.experimental.AUTOTUNE

train_ds = ds.shuffle(buffer_size = len(all_image_labels))
train_ds = train_ds.repeat()
train_ds = train_ds.batch(BATCH_SIZE)
train_ds = train_ds.prefetch(buffer_size=AUTOTUNE)

Now we train the model.

cnn_model.compile(optimizer=tf.keras.optimizers.Adam(),
              loss='sparse_categorical_crossentropy',
              metrics=["accuracy"])
steps_per_epoch=tf.math.ceil(len(all_image_dirs)/BATCH_SIZE).numpy()
cnn_model.fit(train_ds, epochs=2, steps_per_epoch=steps_per_epoch)

After training, save the model for later use.

cnn_model.save('my_model.h5')

MODEL PERFOMANCE SUMARY

Our model has the accuracy of 97.79 % for the train dataset and 97.32 % for the test dataset.

FLASH APPLICATION

Homepage

Example of results

CONCLUSION

We successfully built a deep neural network model by implementing Convolutional Neural Network (CNN) to classify dog and cat images with very high accuracy 97.32 %. In addition, we also built a Flask application so user can upload their images and classify easily.

cat-dog-classification-flask-app's People

Contributors

reichu31 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.