Keras-tensorflow-IT Haarlem 2023
Keras-tensorflow-IT Haarlem 2023
Full example
• Recap Practice
Walkthrough
• Keras/tensorflow
How to split images into Channels?
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
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
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
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
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:
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?
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
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?
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.
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
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?
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.
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.