Deep Learning with Keras

By LiterallyTheOne

0: Introduction

Deep Learning with Keras Tutorial, introduction

Keras

  • High level API
  • Building and Training Deep Learning Models
  • Tensorflow, PyTorch, Jax
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

Why Keras?

  • Super easy to start with
  • Train and test with only a few lines
  • Can start a practical project really fast
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

Google Colab

  • One of the best ways to run Python
  • Free
  • Cloud-based
  • Jupyter notebook
  • All the codes in this tutorial can be executed on it
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

Open a GitHub notebook into Colab

  • All the codes are available in GitHub

By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

Hello World: Setup

import os

os.environ["KERAS_BACKEND"] = "torch"
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

Hello World: Imports

from keras.datasets import mnist
import keras
from keras import layers
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

Hello World: Prepare Data

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype("float32") / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype("float32") / 255
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

Hello World: Define the model

model = keras.Sequential([
    layers.Dense(512, activation="relu"),
    layers.Dense(10, activation="softmax")
])

model.compile(optimizer="adam",
              loss="sparse_categorical_crossentropy",
              metrics=["accuracy"])
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

Hello World: Train the model

model.fit(train_images, train_labels, epochs=5, batch_size=128)
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

Hello World: Test the model

test_loss, test_acc = model.evaluate(test_images, test_labels)
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

Hello World: Output

"""
--------
output: 

Epoch 1/5
469/469 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9259 - loss: 0.2622
Epoch 2/5
469/469 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9685 - loss: 0.1092
Epoch 3/5
469/469 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9797 - loss: 0.0710
Epoch 4/5
469/469 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9852 - loss: 0.0515
Epoch 5/5
469/469 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9901 - loss: 0.0363
313/313 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.9801 - loss: 0.0616
"""
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

Kaggle

  • A platform:
    • Data Science
    • Machine learning
  • Datasets
  • Competitions
  • Pretrained Models
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

Image Classification Dataset

  • tag: Image Classification
  • Dataset structure:
class_a/
...a_image_1.jpg
...a_image_2.jpg
class_b/
...b_image_1.jpg
...b_image_2.jpg
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

Example 1

By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

Example 2

By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

Assignment

  • Select a dataset from Kaggle

    • Image classification
    • Each class has its own directory, and its images are in that directory
    • It's better for our dataset size not to exceed GB.
    • Each dataset should be unique
    • Should not be a Well-known Dataset.
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, introduction

By Ramin Zarebidoky (LiterallyTheOne)