LAB FILE
DEEP LEARNING AND COMPUTER VISION(UCS753)
Submitted To: Dr. Mukesh Dalal
Submitted by:- Sarthak Grover
Roll No.:- 102105007
Group:- 4EI1
7th Sem(2425ODDSEM)
Electronics (Instrumentation & Control) Engineering
1
INDEX
Exp Experiment Signature
No.
2
EXPERIMENT 1
AIM: To create a Teachable Machine model, that can classify images into different categories,
such as objects, animals, or gestures, using a custom dataset or live webcam input.
THEORY:
Teachable Machine is a web-based tool by Google that allows users to create machine learning
models without needing to code. It is beginner-friendly and widely used for experiments related
to image, sound, and pose detection. You can train a model with your own data and deploy it
in applications easily.
OUTPUT:
Figure 1.1 Classifying data in Class 1
Figure 1.2 Classifying data in Class 2
3
Figure 1.3 Classifying data in Class 3
CONCLUSION:
The model successfully classifies inputs (images, sounds, or poses) with a high accuracy rate,
depending on the quality and variety of training data.
4
EXPERIMENT 2
AIM: To apply and analyze the effects of five different image processing operations- image
enhancement, RGB to greyscale conversion, blur operation, image rotation, crop image.
THEORY:
Several techniques are employed in image processing to improve and modify images for
improved analysis and visual quality. Whereas RGB to grey conversion streamlines the image
by reducing colour information to intensity values while maintaining structure, image
enhancement increases brightness, contrast, and sharpness to highlight minor details. By
redistributing intensity values, histogram equalization improves overall contrast and highlights
features in bright or dark regions. By identifying notable changes in intensity, edge detection—
like the Canny algorithm—highlights borders and is essential for object recognition and
segmentation. Lastly, image smoothing creates a softer look that keeps key features while
eliminating extraneous information by averaging pixel values to reduce noise. These functions
are essential in domains such as machine learning, computer vision, and medical imaging.
CODE AND OUTPUT:
from PIL import Image, ImageEnhance, ImageFilter
from [Link] import display
image = [Link]('/content/image_im.jpg')
Figure 2.1 Original Image
enhancer = [Link](image)
enhanced_image = [Link](1.5)
display(enhanced_image) # Use display to show the image in notebook
Figure 2.2 Enhanced Image
5
grayscale_image = [Link]("L")
display(grayscale_image) # Display grayscale image
Figure 2.3 Greyscale Image
blurred_image = [Link]([Link](radius=5))
display(blurred_image) # Display blurred image
Figure 2.4 Blurred Image
rotated_image = [Link](45)
display(rotated_image) # Display rotated image
Figure 2.5 Rotated Image
box = (100, 100, 400, 400)
cropped_image = [Link](box)
display(cropped_image) # Display cropped image
6
Figure 2.6 Cropped Image
CONCLUSION:
Each of the image processing operations applied had a distinct effect. Image enhancement
improved the clarity, RGB to greyscale conversion simplified the data while retaining the
structure. These operations can be used to preprocess images for further analysis or to enhance
them for visual applications.
7
EXPERIMENT 3
AIM: To create a CNN-based Deep learning model for MNIST image classification.
THEORY:
A Convolutional Neural Network (CNN) is the best choice for image classification tasks
because it uses fully connected layers to map features to output classes, pooling layers to
minimize computational complexity and spatial dimensions, and convolutional layers to
identify characteristics like edges and textures. The CNN uses the Softmax function to generate
class probabilities after processing 28x28 greyscale images and finding patterns across several
layers for MNIST digit classification. Handwritten digits can be classified with excellent
accuracy thanks to the model's optimisation using the Adam optimiser and categorical cross-
entropy loss.
CODE AND OUTPUT:
import tensorflow as tf
from [Link] import layers, models
from [Link] import mnist
import [Link] as plt
import numpy as np
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((train_images.shape[0], 28, 28, 1)).astype('float32') / 255
test_images = test_images.reshape((test_images.shape[0], 28, 28, 1)).astype('float32') / 255
train_labels = [Link].to_categorical(train_labels, 10)
test_labels = [Link].to_categorical(test_labels, 10)
model = [Link]()
[Link](layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
[Link](layers.MaxPooling2D((2, 2)))
[Link](layers.Conv2D(64, (3, 3), activation='relu'))
[Link](layers.MaxPooling2D((2, 2)))
[Link](layers.Conv2D(64, (3, 3), activation='relu'))
[Link]([Link]())
[Link]([Link](64, activation='relu'))
[Link]([Link](10, activation='softmax'))
[Link](optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
[Link]()
history = [Link](train_images, train_labels, epochs=5, batch_size=64, validation_split=0.2)
test_loss, test_acc = [Link](test_images, test_labels)
print(f'Test accuracy: {test_acc:.4f}')
[Link]([Link]['accuracy'], label='Training accuracy')
[Link]([Link]['val_accuracy'], label='Validation accuracy')
[Link]('Training and Validation Accuracy')
[Link]('Epochs')
[Link]('Accuracy')
[Link]()
[Link]()
8
Figure 3.1 Dataset loaded and model trained
Figure 3.2 Training and Validation Accuracy Graph
CONCLUSION:
CNN-based Deep learning model for MNIST image classification has been created and a graph
between the training and validation has been plotted.
9
EXPERIMENT 4
AIM: Consider the problem of median housing price in any district of california, use california
housing prices dataset (StackLibrary). Objective is to create:
1. Regression network
2. Deep Neural Network
THEORY:
For jobs like predicting house prices, it's critical to understand the differences between deep
learning and simple regression models in machine learning. For linearly separable data, a basic
linear regression model is appropriate since it creates a straight linear relationship between the
input characteristics and the target variable. Its simplicity, however, restricts its capacity to
identify intricate, non-linear patterns. Deep neural networks (DNNs), on the other hand, can
learn from large datasets and spot subtle patterns because they represent complex interactions
among features using numerous layers and non-linear activation functions. As a result, DNNs
frequently perform better than linear regression in complicated settings, highlighting the
necessity of choosing the right modeling approaches depending on the properties of the data.
CODE AND OUTPUT:
import tensorflow as tf
from [Link] import fetch_california_housing
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
housing = fetch_california_housing()
X_train, X_test, y_train, y_test = train_test_split([Link], [Link], test_size=0.2,
random_state=42)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = [Link](X_test)
Linear Regression Model
# Simple Linear Regression Model (Single Layer)
linear_regression_model = [Link]([
[Link](input_shape=X_train_scaled.shape[1:]),
[Link](1) # Single output for linear regression (no activation function)
])
linear_regression_model.compile(optimizer='adam', loss='mse', metrics=['mae'])
history_linear = linear_regression_model.fit(X_train_scaled, y_train, epochs=10,
validation_split=0.2, verbose=1)
test_loss_linear, test_mae_linear = linear_regression_model.evaluate(X_test_scaled, y_test)
print(f"Test MAE (Linear Regression): {test_mae_linear}")
10
Figure 4.1 Linear Regression Model
Deep Neural Network Model
# Build a deeper neural network model
deep_nn_model = [Link]([
[Link](input_shape=X_train_scaled.shape[1:]),
[Link](128, activation='relu'), # Increased units to improve capacity
[Link](64, activation='relu'),
[Link](32, activation='relu'),
[Link](1) # Output layer for regression
])
# Compile the model
deep_nn_model.compile(optimizer='adam', loss='mse', metrics=['mae'])
# Train the model
history_dnn = deep_nn_model.fit(X_train_scaled, y_train, epochs=10, validation_split=0.2,
verbose=1)
# Evaluate the model
test_loss_dnn, test_mae_dnn = deep_nn_model.evaluate(X_test_scaled, y_test)
print(f"Test MAE (Deep NN): {test_mae_dnn}")
Figure 4.2 Deep Neural Network Model
11
Figure 4.3 Model Comparison
CONCLUSION:
The deep neural network model outperforms the linear regression model, as indicated by a
lower Mean Absolute Error (MAE) on the California housing prices dataset. This demonstrates
the DNN's ability to capture complex relationships and non-linear patterns, making it a more
suitable choice for tasks like housing price prediction.
12
EXPERIMENT 5
AIM: To create an MLP network on Fashion-MNIST dataset using SGD
THEORY:
Multiple layers of neurons, including input, hidden, and output layers, define Multilayer
Perceptrons (MLPs), a type of feedforward neural network. For categorisation tasks like picture
recognition, they work especially well. We use the Fashion-MNIST dataset in this context,
which comprises 10,000 testing images and 60,000 training photos of fashion items divided
into 10 classes. During training, we may effectively minimize the loss function by using
Stochastic Gradient Descent (SGD) as the optimisation algorithm. SGD improves
generalization and speeds up convergence by gradually updating the model weights depending
on each training sample. An input layer that matches the image pixel values, one or more hidden
layers with activation functions, and an output layer that forecasts the fashion item's class will
make up the MLP architecture.
CODE AND OUTPUT:
import tensorflow as tf
from [Link] import layers, models
from [Link] import fashion_mnist
(X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()
X_train = X_train.reshape(-1, 28 * 28).astype('float32') / 255.0 # Flatten and normalize
X_test = X_test.reshape(-1, 28 * 28).astype('float32') / 255.0
model = [Link]([
[Link](128, activation='relu', input_shape=(28 * 28,)), # Input layer
[Link](64, activation='relu'), # Hidden layer
[Link](10, activation='softmax') # Output layer (10 classes)
])
[Link](optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
[Link](X_train, y_train, epochs=5, batch_size=32, validation_split=0.2)
test_loss, test_accuracy = [Link](X_test, y_test)
print(f"Test Accuracy: {test_accuracy:.4f}")
13
Figure 5.1 MLP network on Fashion-MNIST dataset using SGD
CONCLUSION:
The MLP network trained on the Fashion-MNIST dataset using Stochastic Gradient Descent
(SGD) effectively classifies fashion items with competitive accuracy. By leveraging multiple
layers and non-linear activation functions, the model captures complex patterns, showcasing
the power of MLPs for image classification. The results highlight SGD's efficiency in
optimizing convergence and performance on unseen data, underscoring the practical
application of neural networks in multi-class classification tasks in computer vision.
14