0% found this document useful (0 votes)
12 views

2-Machine Learning & Deep Learning

Uploaded by

shetyaahmed789
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

2-Machine Learning & Deep Learning

Uploaded by

shetyaahmed789
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 87

Generative AI

Module 2: Machine Learning &


Deep Learning
Overview

❑ Introduction:
- Overview of the course topics
- Importance of Machine Learning (ML) and Deep Learning (DL)
❑ Topics Covered:
- TensorFlow and Keras
- Exploratory Data Analysis
- TensorFlow 2.0 Keras Projects
- Image Processing Techniques
- Morphological Operations
- Background Subtraction and Contours
- Convolutional Neural Networks (CNNs)
- Advanced CNN Techniques
Objectives

• Understand the fundamental concepts of ML and DL


• Learn to use TensorFlow and Keras for building models
• Perform data preprocessing and exploratory data analysis (EDA)
• Apply image processing techniques
• Develop and evaluate CNN models
Student Goals
• Acquire foundational knowledge in ML and DL
• Gain hands-on experience with TensorFlow and Keras
• Develop skills in data preprocessing and EDA
• Implement image processing algorithms
• Build and optimize CNN models
TensorFlow vs. Keras Explained
❑ TensorFlow:
• Developed by Google
• Provides flexibility and control
• Suitable for large-scale ML models
❑ Keras:
• High-level API for building and training
models quickly
• Runs on top of TensorFlow
• User-friendly and easy to use
TensorFlow vs. Keras Explained
Preparing the Data
❑ Steps:
• Data collection
• Data cleaning
• Data normalization
• Data splitting (training, validation, test sets)
❑ Example:
• Loading and preparing the MNIST dataset
Creating and Training the Model
❑ Model Architecture:
• Input layer
• Hidden layers with activation
functions
• Output layer with softmax
activation
❑ Training:
• Compile the model with optimizer
and loss function
• Fit the model to the training data
Model Evaluation
❑ Metrics:
• Accuracy
• Loss
• Confusion matrix
❑ Evaluation:
• Evaluate the model on the test
set
• Visualize performance with
accuracy and loss curves
Comprehension Question
❑ Question:
• What is the primary difference between TensorFlow and Keras?
❑ Options:
1. TensorFlow is easier to use, while Keras provides more flexibility.
2. Keras is a high-level API that runs on top of TensorFlow.
3. TensorFlow is suitable for small-scale models, while Keras is for large-
scale models.
Exploratory Data Analysis (EDA)
❑ Purpose:
• Understand the data distribution
• Identify patterns and anomalies
❑ Techniques:
• Summary statistics
• Data visualization (histograms, scatter plots, box plots)
Data Preprocessing and
Creating a Model
❑ Data Preprocessing:
• Handling missing values
• Encoding categorical
variables
• Normalization and scaling
❑ Model Creation:
• Define model architecture
• Compile and fit the model
Data Preprocessing and Creating a
Model
Model Evaluation and Predictions
❑ Evaluation Metrics:
• Accuracy, Precision, Recall, F1-Score
• Confusion matrix
❑ Making Predictions:
• Use the trained model to make predictions on
new data
• Evaluate the predictions using appropriate
metrics
Model Evaluation and Predictions
Optimizing Model Performance

Artificial Intelligence (AI) models are pivotal in modern applications, ranging


from computer vision to natural language processing (NLP). Optimizing
these models is crucial to enhance their efficiency, accuracy, and
generalization capabilities. The primary objective of optimizing model
performance is to ensure that AI systems can handle real-world data
effectively while maintaining high levels of reliability and performance.
Comprehension Question
• Question:
Why is exploratory data analysis (EDA) important before building a model?
• Options:
1. To determine the computational requirements for the model.
2. To understand data distribution and identify potential issues.
3. To decide the number of hidden layers in the neural network.
TensorFlow 2.0 Keras Project
Options Overview
• Project Ideas:
• Image classification
• Natural language processing
• Time series prediction
• Reinforcement learning
• Choosing a Project:
• Based on interest and application area
• Consider dataset availability and
complexity
Dealing with Overfitting and
Evaluation
Overfitting occurs when a machine learning model learns the training data too well, resulting
in poor performance on new, unseen data. Strategies to mitigate overfitting include
regularization, which adds a penalty term to the loss function to discourage overly complex
models, and early stopping, which halts training when validation performance begins to
degrade. Model evaluation involves assessing performance metrics such as accuracy,
precision, recall, and F1-score to gauge how well the model generalizes to unseen data.
Exploratory Data Analysis (EDA)
• EDA Techniques:
• Data visualization
• Summary statistics
• Correlation analysis
• Tools:
• Pandas, Matplotlib, Seaborn
Dealing with Missing Data
• Techniques:
• Removal of missing values
• Imputation methods (mean, median, mode)
• Advanced methods (KNN imputation, regression
imputation)
Categorical Data
• Encoding Techniques:
• One-hot encoding
• Label encoding
• Ordinal encoding
• Application:
• Suitable for different types of categorical data
Categorical Data
Data Pre-Processing
• Steps:
• Handling missing values
• Encoding categorical variables
• Feature scaling and normalization
• Importance:
• Improves model performance and accuracy
Comprehension Question
• Question:
What is the purpose of encoding categorical data?
• Options:
1. To reduce the dimensionality of the data.
2. To convert categorical values into a numerical format that
can be used by ML algorithms.
3. To improve the visual representation of the data.
Image Preprocessing
• Techniques:
• Resizing
• Normalization
• Augmentation
• Tools:
• OpenCV
• Pillow (PIL)
Image Preprocessing
Blurring - A Low Pass Filter
• Purpose:
• Reduce noise and detail
• Smooth the image
• Techniques:
• Gaussian blur
• Median blur
Inbuilt Blur Functions
• Functions:
• GaussianBlur
• MedianBlur
• BilateralFilter
• Application:
• Choose appropriate blur
function based on the use
case
Sharpening and Embossing
• Purpose:
• Enhance edges and details
• Create embossed effect
• Techniques:
• Kernel convolution for sharpening
• Kernel convolution for embossing
Sharpening and Embossing
High Pass Filtering - Edge Detection
• Purpose:
• Detect edges in the image
• Techniques:
• Sobel filter
• Canny edge detector
High Pass Filtering - Edge Detection
Histogram Equalization
• Purpose:
• Improve contrast in the image
• Technique:
• Equalize the histogram of the image
Histogram Equalization
Comprehension Question
• Question:
What is the primary purpose of using a blur filter in image
processing?
• Options:
1. To enhance the edges of the image.
2. To reduce noise and smooth the image.
3. To improve the color balance of the image.
Morphological Operations -
Erosion and Dilation
• Erosion:
• Removes pixels on object boundaries
• Useful for removing small white noises
• Dilation:
• Adds pixels to the boundaries of objects
• Useful for closing small holes in objects
Morphological Operations - Erosion
and Dilation
Basic Image Arithmetic
• Operations:
• Addition
• Subtraction
• Multiplication
• Division
• Application:
• Blend images
• Adjust brightness and contrast
Basic Image Arithmetic
Put One Image on Another
Background
• Techniques:
• Masking
• Bitwise operations
• Application:
• Composite images
• Create complex scenes
Put One Image on Another
Background
Put Green Image on Forest
Background
• Techniques:
• Convert image to HSV
• Change color based on HSV values
• Composite images using masks
Grab Cut Foreground
• Purpose:
• Extract foreground objects from an image
• Technique:
• GrabCut algorithm
Grab Cut Foreground
Identify Color
• Techniques:
• Convert image to HSV
• Thresholding based on color range
• Application:
• Detect specific colors in an image
Comprehension Question
• Question:
What is the purpose of using morphological operations like
erosion and dilation?
• Options:
1. To blend images together.
2. To extract edges from an image.
3. To remove noise and enhance structures in an image.
Background Subtraction
and Contours
• Purpose:
• Separate moving objects from the background
• Techniques:
• BackgroundSubtractorMOG2
• BackgroundSubtractorKNN
Background Subtraction
and Contours
Contours Prerequisite
• Purpose:
• Detect and analyze shapes in an image
• Techniques:
• FindContours
• DrawContours
Contours Prerequisite
Contours - Extract and Draw
• Techniques:
• Extract contours
• Draw contours on the image
• Application:
• Shape analysis
• Object detection
Contours - Extract and Draw
Image Matching Using Hu-Moments
and Count of Vertices
• Purpose:
• Match shapes based on
invariant moments
• Techniques:
• Calculate Hu-Moments
• Compare shapes
Image Matching Using Hu-Moments
and Count of Vertices
Image Grouping Using K-Means
Clustering
• Purpose:
• Group similar regions in an image
• Techniques:
• Apply K-Means clustering
Image Grouping Using K-Means
Clustering
Convexity Defect
• Purpose:
• Analyze and identify defects in shapes
• Techniques:
• Calculate convex hull
• Identify convexity defects
Comprehension Question

• Question:
What is the primary purpose of background subtraction in
image processing?
• Options:
1. To enhance the colors in an image.
2. To separate moving objects from the static background.
3. To smooth the image and reduce noise.
• What is Convolution?
What is Convolution?
Why Use 0-Indexing?

• Consistency: Matches programming languages like Python


and C++.
• Simplification: Simplifies calculations and indexing.
Convolution on Color Images
Process: Apply convolution separately to each color channel (R, G, B) and
combine the results.
Convolution on Color Images
CNN Architecture
• Components:
• Convolutional layers
• Activation functions (ReLU)
• Pooling layers (Max Pooling)
• Fully connected layers
• Softmax layer for classification
Comprehension Question
• Question:
What is the primary purpose of the convolution operation in a CNN?
• Options:
1. To reduce the size of the image.
2. To extract meaningful features from the image.
3. To convert the image to grayscale.
CNN Code Preparation
• Frameworks: TensorFlow and Keras
• Steps:
• Load libraries and dataset
• Define model architecture
• Compile the model
• Train the model
CNN for Fashion MNIST
• Dataset: Fashion MNIST contains 28x28 grayscale
images of 10 fashion categories.
• Model Architecture:
• Convolutional layers
• MaxPooling layers
• Fully connected layers
CNN for Fashion MNIST
CNN for CIFAR-10
• Dataset: CIFAR-10 contains 60,000 32x32 color
images in 10 classes.
• Model Architecture:
• Deeper network with more convolutional layers
• Regularization techniques (Dropout)
CNN for CIFAR-10
# Load CIFAR-10 dataset
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data()

# Normalize images
train_images = train_images / 255.0
test_images = test_images / 255.0

# Define CNN model


model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(128, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dropout(0.5),
layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))
Data Augmentation
• Purpose: Enhance model performance by creating
diverse training samples.
• Techniques:
• Rotation
• Zoom
• Horizontal flip
Data Augmentation
Batch Normalization
Purpose: Improve training speed and stability by
normalizing inputs of each layer.
Improving CIFAR-10 Results
• Techniques:
• Data augmentation
• Batch normalization
• Regularization (Dropout)
• Expected Results:
• Higher accuracy and reduced overfitting
Real-Life Examples of Convolution
• Applications:
• Image classification
• Object detection
• Facial recognition
• Examples:
• Self-driving cars
• Medical image analysis
• Security systems
Beginner's Guide to Convolution
• Key Concepts:
• Filters/Kernels
• Feature maps
• Stride and padding
• Visualization:
• How filters detect edges,
textures, and patterns
Alternative Views on Convolution
• Perspectives:
• Convolution as a feature extractor
• Relationship to Fourier transforms
• Advanced Topics:
• Depthwise separable convolutions
• Dilated convolutions
Introduction to Transfer
Learning
Transfer learning is a machine learning technique where a
model trained on one task is reused as a starting point for a
model on a second task. This approach leverages knowledge
learned from a source task to improve learning in a target
task, especially when data for the target task is limited.
Benefits of Transfer Learning
Benefits include accelerated training times, reduced need for
labeled data, and improved generalization to new data. It
allows models to leverage knowledge gained from large-scale
datasets in related domains.
Types of Transfer Learning
Feature extraction involves using pre-trained models to extract
relevant features from data. Fine-tuning adapts the entire pre-trained
model's parameters to the new task. Domain adaptation modifies
models to perform well in different data distributions.
Comprehension Question
• Question:
What technique can be used to improve training stability and
speed in CNNs?
• Options:
1. Data Augmentation
2. Batch Normalization
3. Dropout Regularization
Module Project
Building an Advanced Image Classifier Using TensorFlow and CNNs
Objective: Create an advanced image classifier using TensorFlow and
Convolutional Neural Networks (CNNs). The project will involve data preprocessing,
building and training the CNN model, applying advanced techniques such as data
augmentation and batch normalization, and evaluating the model performance.

1. Introduction
• Objective: Explain the project goal.
• Dataset: Choose a relevant dataset (e.g., Cats vs. Dogs, CIFAR-10, Fashion MNIST).
2. Data Preprocessing
• Data Loading: Load the dataset using TensorFlow.
• Data Augmentation: Apply data augmentation techniques to increase the dataset diversity.
• Normalization: Normalize the image data for better training performance.
3. Building the CNN Model
• Architecture: Design a CNN architecture suitable for the chosen dataset.
• Layers: Include convolutional layers, pooling layers, batch normalization, and fully connected layers.
• Activation Functions: Use appropriate activation functions (e.g., ReLU for hidden layers, Softmax for the output layer).
Module Project
4. Training the Model
• Compilation: Compile the model with a suitable optimizer, loss function, and metrics.
• Training: Train the model using the training data, and validate it with validation data.
• Hyperparameters: Experiment with different hyperparameters (e.g., learning rate, batch size, epochs).
5. Model Evaluation
• Performance Metrics: Evaluate the model using metrics such as accuracy, precision, recall, and F1-
score.
• Confusion Matrix: Visualize the confusion matrix to understand the classification performance.
• ROC Curve: Plot the ROC curve for binary classification problems.
6. Model Improvement
• Data Augmentation: Apply advanced data augmentation techniques to improve the model.
• Batch Normalization: Integrate batch normalization layers to stabilize and accelerate training.
• Dropout: Use dropout layers to prevent overfitting.
Module Project
7. Results and Analysis
• Performance Comparison: Compare the initial and improved model performance.
• Visualizations: Provide visualizations of the results (e.g., training/validation loss and accuracy plots).
• Misclassifications: Analyze misclassified images to understand model limitations.
8. Conclusion
• Summary: Summarize the project outcomes and key findings.
• Future Work: Suggest potential improvements and future work directions.
Module Project
Project Report
Students should submit a detailed report covering the following sections:
1. Introduction: Objective and dataset description.
2. Data Preprocessing: Techniques used and their impact.
3. Model Architecture: Description of the CNN model.
4. Training Process: Details of the training process, including hyperparameters.
5. Evaluation: Performance metrics, confusion matrix, and ROC curve.
6. Improvements: Steps taken to improve the model and their impact.
7. Results: Analysis of results, including visualizations and misclassifications.
8. Conclusion: Summary of findings and future work suggestions.

You might also like