[go: up one dir, main page]

0% found this document useful (0 votes)
82 views58 pages

DL Practical File

Uploaded by

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

DL Practical File

Uploaded by

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

Task 1 –

Output –
Task 1 –
Basics of Deep Learning approach with required methods and
definition.
Program –

import numpy as np
import pandas as pd

df = pd.read_csv('/content/mushroom_cleaned.csv')

# Import necessary libraries (numpy for numerical operations, pandas


for #data manipulation) and load a dataset named
mushroom_cleaned.csv into # a DataFrame called df.

df.head()

#df.head(): Displays the first 5 rows of the DataFrame to give an initial


look #at the data.

output = df.duplicated().sum()

#df.duplicated().sum(): Checks each row to see if it's a duplicate and


sums #up the number of duplicates.

df.shape

#Returns the shape of the DataFrame as a tuple (number of rows,


number #of columns).
y = df['class']
X = df.drop(columns = ['class'])

# y = df['class']: Extracts the target variable (or label) 'class' from the
#DataFrame, which is the variable you want to predict.
#X = df.drop(columns = ['class']): Separates the features (independent
#variables) by dropping the 'class' column from the DataFrame. X
contains #the input data for the model

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=2)

#Splits the data into training and testing sets.


#train_test_split: A function from sklearn that randomly splits the
dataset #into training and testing subsets.
#Parameters:
#test_size=0.2: Specifies that 20% of the data should be used for testing,
#and 80% for training.
#random_state=2: Sets a seed for random number generation to ensure
#reproducibility of the split.
#Output:
#X_train, X_test: Features for training and testing.
#y_train, y_test: Corresponding target values for training and testing

from sklearn.preprocessing import StandardScaler


scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Standardizes the features by scaling them so that they have a mean of 0
#and a standard deviation of 1. This is important for many machine
#learning algorithms that are sensitive to the scale of input features.
#StandardScaler: A tool from sklearn used for standardizing the dataset.
#scaler.fit_transform(X_train):
#fit: Calculates the mean and standard deviation of the training data.
#transform: Applies the scaling to the training data using the calculated
#mean and standard deviation.
#scaler.transform(X_test): Applies the same scaling to the test set (using
#the mean and standard deviation from the training data).

 BASICS OF DEEP LEARNING: -


Deep learning is a subset of machine learning that involves using neural networks with many
layers (hence "deep") to model complex patterns in data. It's particularly powerful for tasks
like image recognition, natural language processing, and more. Here’s an overview of the
basics of deep learning, including essential methods and definitions:

1. Neural Networks:

 Definition: A neural network is a computational model inspired by the way biological


neural networks in the brain process information. It consists of layers of nodes
(neurons) connected by edges (weights).
 Structure:
o Input Layer: Receives the input data.
o Hidden Layers: Intermediate layers where computations are performed; the
number of these layers determines the "depth" of the network.
o Output Layer: Produces the final output.
 Activation Functions: Non-linear functions applied at each node that allow the
network to learn complex patterns (e.g., ReLU, Sigmoid, Tanh).

2. Deep Learning Architectures:

 Convolutional Neural Networks (CNNs):


o Purpose: Mainly used for image processing tasks.
o Key Components: Convolutional layers (for feature extraction), pooling
layers (for downsampling), fully connected layers (for decision making).
 Recurrent Neural Networks (RNNs):
o Purpose: Designed for sequence data (e.g., time series, text).
o Key Features: Loops in the network allow information to persist, making
them suitable for temporal data.
o Variants: Long Short-Term Memory (LSTM) networks and Gated Recurrent
Units (GRUs) address vanishing gradient problems in standard RNNs.
 Generative Adversarial Networks (GANs):
o Purpose: Used for generating new data samples (e.g., images, music).
o Components: Consist of a generator (creates fake data) and a discriminator
(distinguishes between real and fake data).
 Autoencoders:
o Purpose: Learn efficient representations of data, often for dimensionality
reduction or denoising.
o Structure: Encoder (compresses data), decoder (reconstructs data).

3. Training Deep Learning Models:

 Loss Function:
o Purpose: Measures how well the model's predictions match the actual data.
o Examples: Mean Squared Error (MSE) for regression, Cross-Entropy Loss for
classification.
 Optimization Algorithms:
o Purpose: Minimize the loss function by adjusting the weights of the network.
o Methods:
 Gradient Descent: Updates weights in the direction of the negative
gradient of the loss function.
 Variants: Stochastic Gradient Descent (SGD), Adam (Adaptive
Moment Estimation).
 Backpropagation:
o Purpose: Algorithm used to calculate the gradient of the loss function with
respect to each weight by chain rule, crucial for updating weights in neural
networks.

4. Regularization Techniques:

 Purpose: Prevents overfitting by penalizing complex models.


 Methods:
o Dropout: Randomly ignores some neurons during training to prevent the
network from becoming too reliant on particular nodes.
o L1 and L2 Regularization: Adds a penalty for larger weights to the loss
function, encouraging simpler models.
5. Hyperparameters:

 Definition: Parameters set before the training process that affect the model's learning
process.
 Examples: Learning rate, batch size, number of epochs, number of hidden layers,
activation functions.

6. Evaluation Metrics:

 Purpose: Measure the performance of the model.


 Examples: Accuracy, Precision, Recall, F1-Score for classification; Mean Absolute
Error (MAE), Root Mean Squared Error (RMSE) for regression.

7. Transfer Learning:

 Definition: Utilizing a pre-trained model on a new, similar problem, which speeds up


training and requires less data.
 Application: Common in image classification tasks using models like VGG, ResNet,
or BERT for NLP tasks.

8. Tools and Libraries:

 Popular Libraries: TensorFlow, PyTorch, Keras.


 Platforms: Jupyter Notebooks, Google Colab (for free GPU access), Kaggle.

Summary

Deep learning involves stacking multiple layers of neurons to learn complex


data patterns. It relies on various architectures like CNNs for images and RNNs
for sequences, with the training process guided by optimization algorithms and
evaluated through various metrics. Understanding the basics, like the structure
of neural networks, activation functions, and regularization methods, is crucial
for diving into deeper and more complex models.
Task 2 –

Output –
Task 2 –
Program to create a basic ANN with printing summary of model
architecture of the model.

Program –
#Continuing from previous program
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense

model = Sequential()

This line initializes an empty neural network model as a Sequential


model. The Sequential model is a linear stack of layers, meaning you can
add layers to it one after another using the .add() method.

model.add(Dense(30,activation='relu',input_dim=9))
model.add(Dense(15,activation='relu'))
model.add(Dense(1,activation='sigmoid'))

model.summary()

EXPLANATION: -
 tensorflow: A popular open-source platform for machine learning and
deep learning.
 keras: A high-level neural networks API running on top of TensorFlow. It
simplifies the process of building and training neural networks.
 Sequential: A linear stack of layers in Keras, allowing you to build a
neural network layer by layer in a straightforward manner.
 Dense: A fully connected layer where each neuron in the layer is connected
to every neuron in the previous layer. This is one of the most common layers
used in neural networks.
Task 3 –

Output –
Task – 3
Download Mushroom Dataset from Kaggle and create a Deep Neural
Network to check whether the mushroom is edible or not.

Program -
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense

model = Sequential()
model.add(Dense(64, activation='relu', input_dim=8))
model.add(Dense(36, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()

model.compile(loss='binary_crossentropy', optimizer='Adam')
model.fit(X_train_scaled, y_train, epochs=50, verbose=0)

y_log = model.predict(X_test_scaled)
y_pred = np.where(y_log>0.5,1,0)
from sklearn.metrics import accuracy_score
acc = accuracy_score(y_test, y_pred)
print(f"The accuracy is: {acc: 0.3F}")
Task 4 –

Output –
Task – 4
Write a program to develop the ANN model, then print the Accuracy
plot of the PIMA dataset downloaded from Kaggle.

Program -
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score,
f1_score, recall_score
import tkinter as tk
from tkinter import filedialog

def get_dataset():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(title="Select Dataset CSV",
filetypes=[("CSV files", "*.csv")])
if file_path:
data = pd.read_csv(file_path)
return data
else:
raise FileNotFoundError("No file selected!")

data = get_dataset()

X = data.drop(columns=['Outcome'])
y = data['Outcome']
X_train, X_temp, y_train, y_temp = train_test_split(X, y,
test_size=0.4, random_state=42)
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp,
test_size=0.5, random_state=42)

model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu',
input_shape=(X_train.shape[1],)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])

history = model.fit(X_train, y_train, validation_data=(X_val, y_val),


epochs=10, verbose=0)

y_pred = model.predict(X_test).flatten()
y_pred_binary = (y_pred > 0.5).astype(int)

accuracy = accuracy_score(y_test, y_pred_binary)


precision = precision_score(y_test, y_pred_binary)
recall = recall_score(y_test, y_pred_binary)
f1 = f1_score(y_test, y_pred_binary)

print(f'Accuracy: {accuracy: .4f}')


print(f'Precision: {precision: .4f}')
print(f'Recall: {recall: .4f}')
print(f'F1: {f1: .4f}')

# To Plot Graph

import matplotlib.pyplot as plt

plt.figure(figsize=(12,6))
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Training and Validation Accuracy', fontsize=14,
fontweight='bold')
plt.xlabel('Epochs', fontsize=14, fontweight='bold')
plt.ylabel('Accuracy', fontsize=14, fontweight='bold')
plt.legend()
plt.grid(True)
plt.show()

Task 5 –

Output –
Task – 5
Write a program to develop the ANN model, then print the Accuracy
plot of the Mushroom dataset downloaded from Kaggle.

import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score,
f1_score, recall_score
import tkinter as tk
from tkinter import filedialog

def get_dataset():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(title="Select Dataset CSV",
filetypes=[("CSV files", "*.csv")])
if file_path:
data = pd.read_csv(file_path)
return data
else:
raise FileNotFoundError("No file selected!")
data = get_dataset()
X = data.drop(columns=['Outcome'])
y = data['Outcome']
X_train, X_temp, y_train, y_temp = train_test_split(X, y,
test_size=0.4, random_state=42)
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp,
test_size=0.5, random_state=42)
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu',
input_shape=(X_train.shape[1],)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
history = model.fit(X_train, y_train, validation_data=(X_val, y_val),
epochs=10, verbose=0)
y_pred = model.predict(X_test).flatten()
y_pred_binary = (y_pred > 0.5).astype(int)
accuracy = accuracy_score(y_test, y_pred_binary)
precision = precision_score(y_test, y_pred_binary)
recall = recall_score(y_test, y_pred_binary)
f1 = f1_score(y_test, y_pred_binary)
print(f'Accuracy: {accuracy: .4f}')
print(f'Precision: {precision: .4f}')
print(f'Recall: {recall: .4f}')
print(f'F1: {f1: .4f}')
# To Plot Graph
import matplotlib.pyplot as plt
plt.figure(figsize=(12,6))
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Training and Validation Accuracy', fontsize=14,
fontweight='bold')
plt.xlabel('Epochs', fontsize=14, fontweight='bold')
plt.ylabel('Accuracy', fontsize=14, fontweight='bold')
plt.legend()
plt.grid(True)
plt.show()

Task 6 –

Output –
1.
2.

3.
Task 6 –
Write a program to develop optimized ANN model, then print the
Accuracy plot of the Mushroom dataset downloaded from Kaggle.
Program –
import pandas as pd
import numpy as np

df = pd.read_csv('mushroom_cleaned.csv')

#REMOVING DUPLICATES
df1 = df.drop_duplicates()

#LABEL ENCODING
label_encoder = LabelEncoder()
for column in df1.columns:
df1.loc[:, column] = label_encoder.fit_transform(df1[column])

#TRAINING TEST SPLIT


x = df1.drop(['class'], axis =1, inplace = False)
y = df1['class']

from sklearn.model_selection import train_test_split

x_train,x_test, y_train, y_test = train_test_split(x,y, test_size =0.3,


random_state = 42)

#STANDARD SCALING
from sklearn.preprocessing import StandardScaler

sc = StandardScaler()
sc.fit(x_train)
x_train = sc.transform(x_train)
x_test = sc.transform(x_test)

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Dropout
model = Sequential()

#This line initializes an empty neural network model as a


Sequential model.
#The Sequential model is a linear stack of layers,
#meaning you can add layers to it one after another using
the .add() method.
# Input layer and first hidden layer with 128 neurons and dropout

model.add(Dense(128,activation='relu',input_dim= x_train.shape[1]))
model.add(Dropout(0.3))

# Second hidden layer with 64 neurons


model.add(Dense(64, activation='relu'))

# Third hidden layer with 32 neurons


model.add(Dense(32, activation='relu'))

# Output layer for binary classification


model.add(Dense(1, activation='sigmoid'))

model.summary() #1

#COMPILING MODEL
from tensorflow.keras.optimizers import Adam

optimizer = Adam(learning_rate=0.001)
model.compile(loss='binary_crossentropy', optimizer=optimizer,
metrics=['accuracy'])

# Train the model


history = model.fit(x_train, y_train, epochs=50, batch_size=32,
validation_data=(x_test, y_test), verbose=0)
#ACCURACY
from sklearn.metrics import accuracy_score

# Evaluate the model on the test data


y_pred = (model.predict(x_test) > 0.5).astype("int32")
accuracy = accuracy_score(y_test, y_pred)
print(f"Test Accuracy: {accuracy:.3f}")
#2

# PLOT TRAINING & VALIDATION ACCURACY VALUES


import matplotlib.pyplot as plt

plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
#3

Task 7 –
Output –
1.

2.

Task 7 –
Write a Program to develop a ANN model, then print the accuracy
plot of TITANIC dataset.
Program –
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt

# Load and preprocess data


df = pd.read_csv('titanic.csv')

df.drop(columns=["PassengerId", "Name", "Ticket", "Cabin"],


inplace=True)

df["Age"].fillna(df["Age"].median(), inplace=True)
df["Embarked"].fillna(df["Embarked"].mode()[0], inplace=True)

# Convert categorical data to numeric


label_encoder = LabelEncoder()
df["Sex"] = label_encoder.fit_transform(df["Sex"])
df["Embarked"] = label_encoder.fit_transform(df["Embarked"])

# Define features and target


x = df.drop(['Embarked'], axis=1)
y = to_categorical(df['Embarked'])

# Split the dataset


x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3,
random_state=42)

# Standardize features
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)

# Build model
model = Sequential([
Dense(64, activation='relu', input_dim=x_train.shape[1]),
Dropout(0.3),
Dense(32, activation='relu'),
Dense(y.shape[1], activation='softmax')
])

# Compile model
optimizer = Adam(learning_rate=0.001)
model.compile(loss='categorical_crossentropy',
optimizer=optimizer, metrics=['accuracy'])

# Train model
history = model.fit(x_train, y_train, epochs=50, batch_size=32,
validation_data=(x_test, y_test), verbose=1)

# Evaluate model
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
print(f"Test Accuracy: {test_accuracy:.3f}") #1

# Plot accuracy
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.title('Training and Validation Accuracy')
plt.show() #2

Task 8 –
Output –
1.

2.
3.

Task 8 –
Print the Confusion Matrix and Loss Plot of PIMA dataset and write
the value of the matrix such as precision, recall, F1 score.
Program –
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, precision_score,
recall_score, f1_score, roc_auc_score
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping

# Load the dataset


path = 'diabetes.csv'
columns = ['Pregnancies', 'Glucose', 'BloodPressure',
'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age',
'Outcome']
df = pd.read_csv(path, names=columns)

# Data preprocessing
df[columns] = df[columns].apply(pd.to_numeric, errors='coerce')
df.dropna(inplace=True)

# Split features and target


X = df.drop('Outcome', axis=1)
y = df['Outcome']

# Standardize features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Split dataset
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y,
test_size=0.2, random_state=42)

# Build model
model = Sequential([
Dense(16, input_dim=8, activation='relu'),
Dropout(0.3),
Dense(12, activation='relu'),
Dense(8, activation='relu'),
Dense(1, activation='sigmoid')
])

# Compile model
optimizer = Adam(learning_rate=0.001)
model.compile(loss='binary_crossentropy', optimizer=optimizer,
metrics=['accuracy'])

# Add early stopping


early_stopping = EarlyStopping(monitor='val_loss', patience=10,
restore_best_weights=True)

# Train model
history = model.fit(X_train, y_train, epochs=150, batch_size=32,
validation_split=0.2, verbose=0, callbacks=[early_stopping])

# Predictions
y_pred = (model.predict(X_test) > 0.5).astype("int32")

# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.title('Confusion Matrix')
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()

# Plot Loss
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Loss Plot')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()

# Evaluation Metrics
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
roc_auc = roc_auc_score(y_test, y_pred)

print(f"Precision: {precision:0.3F}")
print(f"Recall: {recall:0.3F}")
print(f"F1-Score: {f1:0.3F}")
print(f"ROC-AUC: {roc_auc:0.3F}")

Task 9 –
Output –

1.

Task 9 –
Print the accuracy of ANN model using PIMA dataset with dataset
with different values of Optimizers.
List of Optimizers: Adam, RMSprop, SGD, Adadelta, Adagrad,
Nadam.
Program –
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import Adam, SGD, RMSprop,
Adadelta, Adagrad, Nadam

# Load the PIMA Indians Diabetes Dataset


url =
“https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima
-indians-diabetes.data.csv”

columns = ['Pregnancies', 'Glucose', 'BloodPressure',


'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age',
'Outcome']

data = pd.read_csv(url, header=None, names=columns)

# Split features and labels


X = data.iloc[:, :-1]
y = data.iloc[:, -1]

# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42, stratify=y)

# Scale the features


scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# Function to create and train the model


def create_and_train_model(optimizer):
model = Sequential([
Dense(64, activation='relu', input_dim=X_train_scaled.shape[1]),
Dropout(0.3),
Dense(32, activation='relu'),
Dense(16, activation='relu'),
Dense(1, activation='sigmoid')
])
model.compile(optimizer=optimizer, loss='binary_crossentropy',
metrics=['accuracy'])
# Train the model
history = model.fit(X_train_scaled, y_train, epochs=50,
batch_size=32, verbose=0, validation_data=(X_test_scaled, y_test))
# Evaluate the model
y_pred = (model.predict(X_test_scaled) > 0.5).astype("int32")
acc = accuracy_score(y_test, y_pred)
return acc, history

# Test with different optimizers


optimizers = {
'Adam': Adam(),
'SGD': SGD(),
'RMSprop': RMSprop(),
"Adadelta": Adadelta(),
"Adagrad": Adagrad(),
"Nadam": Nadam()
}

results = {}
for name, optimizer in optimizers.items():
accuracy, history = create_and_train_model(optimizer)
results[name] = accuracy
print(f"Optimizer: {name}, Test Accuracy: {accuracy:.3f}")

Task 10 –
Output –

Task 10 –
Object detection with pre – trained RetinaNet with Kerras.
Program –
pip install keras-retinanet opencv-python

import numpy as np
import cv2
import matplotlib.pyplot as plt
from keras_retinanet import models
from keras_retinanet.utils.image import preprocess_image,
resize_image
from keras_retinanet.utils.visualization import draw_box,
draw_caption
from keras_retinanet.utils.colors import label_color

# Load pre-trained RetinaNet model (ResNet50 backbone)


model_path = 'resnet50_coco_best_v2.1.0.h5' # Pre-trained model
on COCO dataset
model = models.load_model(model_path,
backbone_name='resnet50')

# Load label to names mapping for COCO dataset


labels_to_names = {0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle',
4: 'airplane',
5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light',
# Add remaining COCO classes as needed
}

# Load image
image_path = r"K:\market.jpg" # Path to the image
image = cv2.imread(image_path)

# Preprocess the image


image = preprocess_image(image)
image, scale = resize_image(image)

# Run detection
boxes, scores, labels =
model.predict_on_batch(np.expand_dims(image, axis=0))
# Correct for image scale
boxes /= scale

# Visualize detections
for box, score, label in zip(boxes[0], scores[0], labels[0]):
if score < 0.5: # Adjust threshold as needed
continue

# Draw the box


color = label_color(label)
b = box.astype(int)
draw_box(image, b, color=color)

# Caption for the object


caption = f"{labels_to_names[label]}: {score:.2f}"
draw_caption(image, b, caption)

# Convert back to original format (BGR -> RGB)


image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Display the image with bounding boxes


plt.figure(figsize=(50, 50))
plt.imshow(image)
plt.axis('off')
plt.show()

Task 11 –
Output –

Task 11 –
NEURAL RECOMMENDATION SYSTEM.
Program –
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Embedding, Flatten, Input, Dot
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt

# Triplet loss function


def triplet_loss(y_true, y_pred, margin=1.0):
anchor, positive, negative = tf.split(y_pred, 3, axis=1)
positive_dist = tf.reduce_sum(tf.square(anchor - positive),
axis=1)
negative_dist = tf.reduce_sum(tf.square(anchor - negative),
axis=1)
return tf.reduce_mean(tf.maximum(positive_dist - negative_dist +
margin, 0.0))

# Neural Recommender System with Implicit Feedback


class NeuralRecommenderWithTripletLoss:
def __init__(self, num_users, num_items, embedding_dim=32,
margin=1.0):
self.num_users = num_users
self.num_items = num_items
self.embedding_dim = embedding_dim
self.margin = margin
self.model = self.build_model()

def build_model(self):
# Input layers for user, positive item, and negative item
user_input = Input(shape=(1,), name="user_input")
pos_item_input = Input(shape=(1,),
name="positive_item_input")
neg_item_input = Input(shape=(1,),
name="negative_item_input")

# Embedding layers for user and items


user_embedding = Embedding(self.num_users,
self.embedding_dim, name="user_embedding")
item_embedding = Embedding(self.num_items,
self.embedding_dim, name="item_embedding")

# User embedding
user_embedded = Flatten()(user_embedding(user_input))

# Positive and negative item embeddings


pos_item_embedded = Flatten()
(item_embedding(pos_item_input))
neg_item_embedded = Flatten()
(item_embedding(neg_item_input))

# Concatenate embeddings to compute triplet loss


merged_embeddings = tf.concat([user_embedded,
pos_item_embedded, neg_item_embedded], axis=1)
# Build and compile model
model = Model(inputs=[user_input, pos_item_input,
neg_item_input], outputs=merged_embeddings)
model.compile(optimizer=Adam(learning_rate=0.001),
loss=triplet_loss)
return model

def train(self, user_ids, pos_item_ids, neg_item_ids, epochs=10,


batch_size=64):
# Train the model with the triplet inputs
history = self.model.fit(
[user_ids, pos_item_ids, neg_item_ids],
np.zeros(len(user_ids)), # Dummy targets for triplet loss
epochs=epochs,
batch_size=batch_size
)
return history

# Simulate data
num_users = 1000
num_items = 1000
embedding_dim = 32

# Generate random training data


user_ids = np.random.randint(0, num_users, size=10000)
pos_item_ids = np.random.randint(0, num_items, size=10000)
neg_item_ids = np.random.randint(0, num_items, size=10000)

# Create and train the recommender system


recommender = NeuralRecommenderWithTripletLoss(num_users,
num_items, embedding_dim)
history = recommender.train(user_ids, pos_item_ids, neg_item_ids,
epochs=10, batch_size=64)

# Plot the training loss


plt.plot(history.history['loss'])
plt.xlabel('Epochs')
plt.ylabel('Triplet Loss')
plt.title('Training Loss over Epochs')
plt.show()

Task 12 –

Output –
Task 12 -
Implementation of Backpropagation in neural network using numpy.
Program -
import numpy as np
import matplotlib.pyplot as plt

# Sigmoid activation function and its derivative


def sigmoid(x):
return 1 / (1 + np.exp(-x))

def sigmoid_derivative(x):
return x * (1 - x)

# Mean Squared Error (MSE) Loss function and its derivative


def mse_loss(y_true, y_pred):
return np.mean((y_true - y_pred) ** 2)

def mse_loss_derivative(y_true, y_pred):


return y_pred - y_true

# Neural Network class with backpropagation and loss tracking


class NeuralNetwork:
def __init__(self, input_size, hidden_size, output_size,
learning_rate=0.1): # Corrected __init__
# Initialize weights and biases
self.learning_rate = learning_rate
self.weights_input_hidden = np.random.randn(input_size,
hidden_size)
self.bias_hidden = np.random.randn(1, hidden_size)
self.weights_hidden_output = np.random.randn(hidden_size,
output_size)
self.bias_output = np.random.randn(1, output_size)
# Track loss
self.loss_history = []

def forward(self, X):


# Forward pass
self.hidden_layer_input = np.dot(X,
self.weights_input_hidden) + self.bias_hidden
self.hidden_layer_output = sigmoid(self.hidden_layer_input)
self.output_layer_input = np.dot(self.hidden_layer_output,
self.weights_hidden_output) + self.bias_output
self.output = sigmoid(self.output_layer_input)
return self.output
def backpropagate(self, X, y):
# Compute output error
output_error = mse_loss_derivative(y, self.output)
output_delta = output_error * sigmoid_derivative(self.output)

# Compute hidden layer error


hidden_error = output_delta.dot(self.weights_hidden_output.T)
hidden_delta = hidden_error *
sigmoid_derivative(self.hidden_layer_output)

# Update weights and biases


self.weights_hidden_output -= self.learning_rate *
self.hidden_layer_output.T.dot(output_delta)
self.bias_output -= self.learning_rate * np.sum(output_delta,
axis=0, keepdims=True)
self.weights_input_hidden -= self.learning_rate *
X.T.dot(hidden_delta)
self.bias_hidden -= self.learning_rate * np.sum(hidden_delta,
axis=0, keepdims=True)

def train(self, X, y, epochs=10000):


for epoch in range(epochs):
# Forward pass
self.forward(X)
# Backpropagation
self.backpropagate(X, y)
# Calculate and store loss
loss = mse_loss(y, self.output)
self.loss_history.append(loss)
# Print loss every 1000 epochs
if epoch % 1000 == 0:
print(f"Epoch {epoch}, Loss: {loss}")

def plot_loss(self):
plt.plot(self.loss_history)
plt.xlabel("Epochs")
plt.ylabel("Mean Squared Error Loss")
plt.title("Loss Over Epochs")
plt.show()

# Example usage
if __name__ == "__main__": # Corrected __name__ check
# XOR dataset
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])

# Define and train the neural network


nn = NeuralNetwork(input_size=2, hidden_size=2,
output_size=1, learning_rate=0.1)
nn.train(X, y, epochs=10000)

# Test the neural network


predictions = nn.forward(X)
print("Predictions:")
print(predictions)

# Plot the loss curve


nn.plot_loss()

Task 13 –

Output –
Task 13 –
Implementation of Neural Recommender Systems with Implicit
Feedback and the Triplet Loss.
Program –
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Embedding, Flatten, Input,
Concatenate
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt

# Triplet loss function


def triplet_loss(_, y_pred, margin=1.0): # Removed unused `y_true`
anchor, positive, negative = tf.split(y_pred, 3, axis=1)
positive_dist = tf.reduce_sum(tf.square(anchor - positive),
axis=1)
negative_dist = tf.reduce_sum(tf.square(anchor - negative),
axis=1)
return tf.reduce_mean(tf.maximum(positive_dist - negative_dist +
margin, 0.0))

# Neural Recommender System with Implicit Feedback


class NeuralRecommenderWithTripletLoss:
def __init__(self, num_users, num_items, embedding_dim=32,
margin=1.0):
self.num_users = num_users
self.num_items = num_items
self.embedding_dim = embedding_dim
self.margin = margin
self.model = self.build_model()

def build_model(self):
# Input layers for user, positive item, and negative item
user_input = Input(shape=(1,), name="user_input")
pos_item_input = Input(shape=(1,),
name="positive_item_input")
neg_item_input = Input(shape=(1,),
name="negative_item_input")

# Embedding layers for user and items


user_embedding = Embedding(self.num_users,
self.embedding_dim, name="user_embedding")
item_embedding = Embedding(self.num_items,
self.embedding_dim, name="item_embedding")

# User embedding
user_embedded = Flatten()(user_embedding(user_input))

# Positive and negative item embeddings


pos_item_embedded = Flatten()
(item_embedding(pos_item_input))
neg_item_embedded = Flatten()
(item_embedding(neg_item_input))

# Concatenate embeddings to compute triplet loss


merged_embeddings = Concatenate(axis=1)([user_embedded,
pos_item_embedded, neg_item_embedded])
# Build and compile model
model = Model(inputs=[user_input, pos_item_input,
neg_item_input], outputs=merged_embeddings)
model.compile(optimizer=Adam(learning_rate=0.001),
loss=triplet_loss)
return model

def train(self, user_ids, pos_item_ids, neg_item_ids, epochs=10,


batch_size=64):
# Train the model with the triplet inputs
history = self.model.fit(
[user_ids, pos_item_ids, neg_item_ids],
np.zeros((len(user_ids), self.embedding_dim * 3)), #
Dummy targets for triplet loss
epochs=epochs,
batch_size=batch_size
)
return history

# Simulate data
num_users = 1000
num_items = 1000
embedding_dim = 32

# Generate random training data


user_ids = np.random.randint(0, num_users, size=10000)
pos_item_ids = np.random.randint(0, num_items, size=10000)
neg_item_ids = np.random.randint(0, num_items, size=10000)

# Create and train the recommender system


recommender = NeuralRecommenderWithTripletLoss(num_users,
num_items, embedding_dim)
history = recommender.train(user_ids, pos_item_ids, neg_item_ids,
epochs=10, batch_size=64)

# Plot the training loss


plt.plot(history.history['loss'])
plt.xlabel('Epochs')
plt.ylabel('Triplet Loss')
plt.title('Training Loss over Epochs')
plt.show()

Task 14 –

Output –
Task 14 –
Write a program to print the accuracy of CNN using max pooling
technique.
Program –
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D,
Flatten, Dense
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt

# Load and preprocess the CIFAR-10 dataset


(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0 # Normalize pixel
values

# Convert labels to categorical format


y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)

# Build the CNN model with Max Pooling layers


model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
MaxPooling2D(pool_size=(2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Conv2D(128, (3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])

# Compile the model


model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])

# Train the model and capture the history


history = model.fit(x_train, y_train, epochs=10, batch_size=64,
validation_data=(x_test, y_test))
# Evaluate the model and print test accuracy
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
print(f"Test Accuracy: {test_accuracy:.4f}")

# Plot training & validation accuracy values


plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.show()

Task 15 –

Output –
Task 15 –
Write a program to print the accuracy of CNN using average pooling
technique.
Program –
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, AveragePooling2D,
Flatten, Dense
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt

# Load and preprocess the CIFAR-10 dataset


(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0 # Normalize pixel
values

# Convert labels to categorical format


y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)

# Build the CNN model with Average Pooling layers


model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
AveragePooling2D(pool_size=(2, 2)),
Conv2D(64, (3, 3), activation='relu'),
AveragePooling2D(pool_size=(2, 2)),
Conv2D(128, (3, 3), activation='relu'),
AveragePooling2D(pool_size=(2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])

# Compile the model


model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])

# Train the model and capture the history


history = model.fit(x_train, y_train, epochs=10, batch_size=64,
validation_data=(x_test, y_test))
# Evaluate the model and print test accuracy
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
print(f"Test Accuracy: {test_accuracy:.4f}")

# Plot training & validation accuracy values


plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.show()

Task 16 –

Output –
Task 16 –
Write a program to print the accuracy of CNN using global pooling
technique.
Program –
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D,
GlobalAveragePooling2D, Dense
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt

# Load and preprocess the CIFAR-10 dataset


(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0 # Normalize pixel
values

# Convert labels to categorical format


y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)

# Build the CNN model with Global Average Pooling


model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
Conv2D(64, (3, 3), activation='relu'),
Conv2D(128, (3, 3), activation='relu'),
GlobalAveragePooling2D(), # Replaces Flatten and reduces
feature maps globally
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])

# Compile the model


model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])

# Train the model and capture the history


history = model.fit(x_train, y_train, epochs=10, batch_size=64,
validation_data=(x_test, y_test))
# Evaluate the model and print test accuracy
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
print(f"Test Accuracy: {test_accuracy:.4f}")

# Plot training & validation accuracy values


plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.show()

You might also like