0% found this document useful (0 votes)
74 views35 pages

Keras-tensorflow-IT Haarlem 2023

1. Import Keras layers and models 2. Create a Sequential model 3. Add a Dense layer with 1 unit, linear activation and input shape of (1,) to process a single feature 4. Compile the model specifying loss, optimizer and metrics 5. Fit the model with input and target data 6. Evaluate model performance on test data

Uploaded by

projectjr2 Profs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views35 pages

Keras-tensorflow-IT Haarlem 2023

1. Import Keras layers and models 2. Create a Sequential model 3. Add a Dense layer with 1 unit, linear activation and input shape of (1,) to process a single feature 4. Compile the model specifying loss, optimizer and metrics 5. Fit the model with input and target data 6. Evaluate model performance on test data

Uploaded by

projectjr2 Profs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Computer Vision

Deep Learning: Fundamentals Keras and TensorFlow


Building a binary classifier in Keras

Marya Butt, PhD.


Researcher Data Driven Smart Society (DDSS)
BIG DATA, Computer Vision, Cloud Computing
Faculty of Engineering Design and Computing
Mob: +31(0) 0611878759
E: marya.butt@Inholland.nl
Agenda (Today)

Full example
• Recap Practice
Walkthrough
• Keras/tensorflow
How to split images into Channels?

It consists on 3 rectangles, one in Blue (B=255,


G=0, R=0), one in Green (B=0, G=255, R=0) and
another in Red (B=0, G=0, R=255). The
background is black (B=0, G=0, R=0).
How to split images into Channels?

# splitting image into channels


img = cv2.imread(‘ BGR.jpg')
blue, green, red = cv2.split(img)

# plotting channels individually


cv2.imshow('blue', blue)
cv2.imshow('green', green)
cv2.imshow('red', red)

cv2.waitKey(0)
cv2.destroyAllWindows()
How to split images into Channels?
#Creating blank channels with shape as that of the image
zeros = numpy.zeros(blue.shape, numpy.uint8)

# merge blank channels with each gray scale channel to get desired results
blueBGR = cv2.merge((blue,zeros,zeros))
greenBGR = cv2.merge((zeros,green,zeros))
redBGR = cv2.merge((zeros,zeros,red))

# display images
cv2.imshow('blue BGR', blueBGR)
cv2.imshow('green BGR', greenBGR)
cv2.imshow('red BGR', redBGR)

cv2.waitKey(0)
cv2.destroyAllWindows()
Activity

Open the previous week notebook file

Read the RGB image from the moodle and split it ito channels
Types of
problems in
Machine
Learning
Classification
Binary Classification MultiClass Classification
In binary classification problem, any of the samples For a given dataset, any of the samples that
from the dataset takes only one label out of two come from the dataset takes only one label out
classes. example, Let’s see an example of small data of the number of classes.
taken from amazon reviews data example: movies reviews dataset

MultiLabel Classification
Each input can have multiple, or even none, of the
designated output classes. Let’s see an example of small
data taken from the movies reviews dataset.
TensorFlow and Keras
TensorFlow
TensorFlow is a Python-based, free, open source machine learning platform
Developed primarily by Google
Like NumPy, the primary purpose of TensorFlow is to enable engineers and researchers to manipulate
mathematical expressions over numerical tensors, but TensorFlow goes far beyond the scope of NumPy

Keras
 Keras is a deep learning API for Python
 Built on top of TensorFlow
 Provides a convenient way to define and train any kind of deep learning
model
 Keras was initially developed for research, with the aim of enabling fast deep
learning experimentation
 Through TensorFlow, Keras can run on top of different types of hardware
TensorFlow vs Keras

Building a model with TensorFlow requires a strong understanding of every transformation/calculation


occurring throughout the model.

Keras is, on the other hand, is a high-level wrapper library. It abstracts away much of the complexity that
comes with TensorFlow itself.

Keras speeds up development time significantly. However, since TF2, both Keras and TensorFlow are
imported with install tensorflow as tf

Note: many people say ‘TensorFlow’ when referring to Keras, both are used interchangeably
Setting up a Deep Learning workspace
(Different possibilities)

Jupyter notebooks
The preferred way to run deep learning experiments
A notebook is a file generated by the Jupyter Notebook app (https://jupyter.org) that you can edit in your
browser.

Using Colaboratory
To get started with Colab, go to https://colab.research.google.com

Installing packages with pip


if there is a need to install something with pip, can be done by using the following syntax in a code cell
(note that the line starts with ! to indicate that it is a shell command rather than Python code):
!pip install package_name
Setting up a Deep Learning workspace
(Different possibilities)

Jupyter notebooks
The preferred way to run deep learning experiments
A notebook is a file generated by the Jupyter Notebook app (https://jupyter.org) that you can edit in your
browser.

Using Colaboratory
To get started with Colab, go to https://colab.research.google.com

Installing packages with pip


if there is a need to install something with pip, can be done by using the following syntax in a code cell
(note that the line starts with ! to indicate that it is a shell command rather than Python code):
!pip install package_name
What is building a neural network model?

Using
Numpy Using
library tensorflow
(without and keras
Keras and libraries
TF
A simple Example!
i/p o/p
{Model performance=
difference}
b
BP Age

72 50 BP * Predicted Age=Activation(BP * w
model +b)
80 51 w
w= Weight
64 66 b= Bias
88 44

Lets design a single layer neral network in Keras!


Using Keras to Develop a Neural Network

Design the network Compile Model


Import Libraries (Sequential vs Functional) • Loss
• Number of neurons • Optimizer
• Number of layers • Metrics

Fit the model


• Provide x and y
Make Predictions Plot Learning
• Epochs
curves • Batch_size
Using Keras to Develop a Neural Network
Model Building Approaches in Keras

Sequential model is used for simple, sequential Functional API is used for architectures
stacks of layers where each layer has one input that require more than a single
and one output input/output in any layer
Using Keras to Develop a Neural Network
Model Building Approaches in Keras

Sequential is easy and fast Functional API is flexible than its Sequential counterpart. Functional API
is used when:

• Information flow through the model is non-linear (i.e., not sequential).


• Layers need multiple inputs and/or outputs.
• The model uses multiple inputs and/or outputs.

 Keep in mind that you can still use the functional API for sequential models — nothing is
stopping you from doing so, but it may be cleaner to use the sequential approach
Using Keras to Develop a Neural Network

AgeBP * w +b
BP
model

If we want to predict age based on the Blood Pressure using a single layer
neuron, which building approach is better to be used?

 Sequential Approach is a better


option
Using Keras to Develop a Neural Network
1. Import Sequential and Dense Classes from models and layers library of Keras respectively

from tensorflow.keras.models import Sequential


from tensorflow.keras.layers import Dense

2. Sequential class allows to build a model of type sequential


model=Sequential()
AgeBP * w +b
. BP
model
Dense implements the operation:

output = activation(dot(input, kernel) +


Using Keras to Develop a Neural Network bias)

activation is the element-wise


3. Add a Dense layer (used for fully connected NN) activation function passed as the
activation argument,
tf.keras.layers.Dense(1, input_shape=(1,), activation=‘linear’) kernel is a weights matrix created by
the layer
units: Positive integer, dimensionality of the output space.
activation: Activation function to use. If you don't specify anything, no activation is applied (ie. "linear"
activation: a(x) = x).
use_bias: Boolean, whether the layer uses a bias vector.
kernel_initializer: Initializer for the kernel weights matrix.
bias_initializer: Initializer for the bias vector.
kernel_regularizer: Regularizer function applied to the kernel weights matrix.
bias_regularizer: Regularizer function applied to the bias vector.
activity_regularizer: Regularizer function applied to the output of the layer (its "activation").
kernel_constraint: Constraint function applied to the kernel weights matrix.
bias_constraint: Constraint function applied to the bias vector.
Using Keras to Develop a Neural Network
3. Add a Dense layer (used for fully connected NN)
tf.keras.layers.Dense(1, input_shape=(1,), activation=‘linear’)

tf.keras.layers.Dense(
units,
activation=None,
use_bias=True,
kernel_initializer="glorot_uniform",
bias_initializer="zeros",
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
)
Read more: Dense layer (keras.io)
Using Keras to Develop a Neural Network
4. Compile the model

model.compile (loss=‘mse’ , optimizer = ‘rmsprop’ , metrics=[‘mae’])

Loss: function that is used to compute the error is known as Loss Function
Optimizer: function used to modify/update weights (rmsprop/Adam/nadam/SGD)
Metrics: function that is used to judge the performance of the model

Note: Metric functions are similar to loss functions, except that the results from evaluating a metric are not used
when training the model. Note that you may use any loss function as a metric
Using Keras to Develop a Neural Network
4. Compile the model
How to choose the loss function?

Regression Loss Functions Binary Classification Loss Multi-Class Classification Loss


Mean Squared Error Loss (mse) Functions Functions
Mean Squared Logarithmic Error Loss Binary Cross-Entropy Multi-Class Cross-Entropy Loss
(msle) Hinge Loss Sparse Multiclass Cross-Entropy Loss
Mean Absolute Error Loss (mae) Squared Hinge Loss Kullback Leibler Divergence Loss
Using Keras to Develop a Neural Network
4. Compile the model

How to choose the optimizer?


Optimizers are used to update weights and biases i.e., the internal parameters of a model to reduce the
error. There are 2 categories of optimizers.

Adaptive Learning Non-adaptive learning algorithms


algorithms
 Adam Stochastic Gradient Descent falls
 Adagrad (sparse dataset) under this category
 Adadelta
 RMSprop
hyperparameters
η = learning rate
m= momentum (determines the velocity with
which learning rate has to be increased to
approach the minima)
Read More: Loss Functions and Optimization Algorithms. Demystified. | by Apoorva Agrawal | Data Science Group, IITR | Medium
Using Keras to Develop a Neural Network
4. Compile the model

How to choose the optimizer?


Optimizers are used to update weights and biases i.e., the internal parameters of a model to reduce the
error. There are 2 categories of optimizers.

Adaptive Learning Non-adaptive learning algorithms


algorithms
doesn’t need to set learning rate, need to set a hyperparameter learning
just need to initialize the learning rate before training the model. If this
rate parameters 0.001  and these learning rate doesn’t give good results, we
adaptive optimization algorithms need to change the learning rates and train
keep updating learning rates while the model again
training the model. 
Using Keras to Develop a Neural Network
4. Compile the model
Choice of the optimizer

Stochastic Gradient Decent is much faster than the other algorithms but the results produced are
far from optimum. Both, Adagrad and Adam produce better results that SGD, but they are
computationally extensive. Adam is slightly faster than Adagrad. Thus, while using a particular
optimization function, one has to make a trade off between more computation power and more
optimum results.

loss=‘mse’ , optimizer = ‘rmsprop’ , metrics=[‘mae’]

Read More: Loss Functions and Optimization Algorithms. Demystified. | by Apoorva Agrawal | Data Science Group, IITR | Medium
Using Keras to Develop a Neural Network
5. fit the model
All rows, first column
X= dataset=[:,0]
All rows, second column
Y= dataset=[:,1]

BP Age

model.fit (X, Y, epochs= 2000, batch_size=32, verbose=1) 72 50

80 51
X= input variables
64 66
Y= output variable
Epochs=no of times, the model sees the entire dataset 88 44
Batch_size= in one epoch, number of training examples utilized
Using Keras to Develop a Neural Network
6. Calculate the accuracy
7. Plot learning curves
What is different, if the Functional approach is
used to build a NN?
Using Keras to Develop a Neural Network
What is different, if Functional approach is used?

Let’s consider a multi-layer network

10 inputs
32 neurons in the hidden layer
2 neurons in the output layer
Using Keras to Develop a Neural Network
Using Sequential Approach
We can initialize a sequential model using tf.keras.Sequential.

From here, we have two options


1. Define the model at the same time
model = tf.keras.Sequential([
tf.keras.layers.Dense(32, input_shape=(10,),
activation='relu'),
tf.keras.layers.Dense(2, activation='softmax’)])

2. Add layers to the model as we go using the .add method


model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(32, input_shape=(10,),
activation='relu')
model.add(tf.keras.layers.Dense(2, activation='softmax'))
Using Keras to Develop a Neural Network
Using Functional Approach
Rather than initializing a class as the starting block of the model, begin
by defining the input(s) and layers. After this, set the input and output
layers using the Model class, like so:

# define the input layer


input_tensor = tf.keras.layers.Input(shape=(10,), dtype='int32’)

# setup network layers


x = tf.keras.layers.Dense(32, activation='relu')(input_tensor)
y = tf.keras.layers.Dense(2, activation='softmax')(x)

# create the model, specifying input and output tensors


model = tf.keras.Model(inputs=input_tensor, outputs=y)
Example Walkthrough in Jupyter notebook

What is the difference between Regression and Classification?


What is the difference between binary and multi-class classification?
Standard Scaling

StandardScaler : It transforms the data in such a manner that it has mean as 0 and standard
deviation as 1. In short, it standardizes the data. Standardization is useful for data which has
negative values. It arranges the data in a standard normal distribution. It is more useful in
classification than regression

You need to import standardscalar from preprocessing package of sklearn library


from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaled_data = scaler.fit_transform(data)
Try with standarized data
Why to scale data?

So if the data in any conditions has data points far from each other, scaling is a technique to
make them closer to each other or in simpler words, we can say that the scaling is used for
making data points generalized so that the distance between them will be lower.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy