Niraj DL
Niraj DL
Pratical File
INDEX
Sr Name of the Experiment Page Date Sign
No. No.
OUTPUT:-
1
Practical 2
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, accuracy_score
np.random.seed(0)
X = np.random.rand(100, 2)
y = (X[:, 0] + X[:, 1] > 1).astype(int)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model_underfit = LogisticRegression(solver='lbfgs')
model_underfit.fit(X_train, y_train)
model_overfit = LogisticRegression(solver='lbfgs', C=1e5)
model_overfit.fit(X_train, y_train)
y_pred_underfit = model_underfit.predict(X_test)
y_pred_overfit = model_overfit.predict(X_test)
accuracy_underfit = accuracy_score(y_test, y_pred_underfit)
accuracy_overfit = accuracy_score(y_test, y_pred_overfit)
confusion_matrix_underfit = confusion_matrix(y_test, y_pred_underfit)
confusion_matrix_overfit = confusion_matrix(y_test, y_pred_overfit)
print("Accuracy (Underfitting):", accuracy_underfit)
print("Confusion Matrix (Underfitting):\n", confusion_matrix_underfit)
print("Accuracy (Overfitting):", accuracy_overfit)
print("Confusion Matrix (Overfitting):\n", confusion_matrix_overfit)
OUTPUT:-
2
Practical 3
Dataset from patient medical record data for Pima Indians with information whether they had an
On set of diabete within five years.
OUTPUT:-
3
Practical 4
model = models.load_model('resnet50_coco_best_v2.1.0.h5')
urllib.request.urlretrieve('https://raw.githubusercontent.com/amikelive/coco-labels/master
/coc o-labels-paper.txt', 'coco-labels-paper.txt')
img = img[:,:,:3]
img_proc = preprocess_image(img) img_proc, scale =resize_image(img_proc) print(f"Shape of
the preprocessed image: {img_proc.shape}")
box = box.astype(np.int32)
color = label_color(label) draw_box(img, box,color=color) class_label = class_labels[label]
caption = f"{class_label} {score:.3f}" draw_caption(img, box,caption)
plt.axis('off')
plt.imshow(img)
plt.show()
!wget https://c0.wallpaperflare.com/preview/814/948/832/de6l8nfk6nqltrackcl9liu6ss.jpg
plt.rcParams['figure.figsize'] = [20, 10]
detect_draw_bounding_boxes('de6l8nfk6nqltrackcl9liu6ss.jpg')
OUTPUT:-
4
!wget
https://i1.wp.com/www.chakracommunity.com/wp-content/uploads/2016/01/bigstock-Busy
-S treet-in-Manhattan-93909977.jpg
detect_draw_bounding_boxes('bigstock-Busy-Street-in-Manhattan-93909977.jpg')
5
Practical 5
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Embedding, Input, Dot, Flatten, Dense
from tensorflow.keras.models import Model
6
# Train the model
model.fit([user_ids, item_ids], ratings, epochs=10, batch_size=32)
OUTPUT:-
7
Practical 6
Introduction
Backpropagation is one of the important concepts of a neural network. For a single training
example, Backpropagation algorithm calculates the gradient of the error function. It can be
written as a function of the neural network. Backpropagation algorithms are a set of methods
used to efficiently train artificial neural networks following a gradient descent approach which
exploits the chain rule.
The main features of Backpropagation are the iterative, recursive and efficient method through
which it calculates the updated weight to improve the network until it is not able to perform the
Practicalfor which it is being trained. Derivatives of the activation function to be known at
network design time is required for Backpropagation.
import numpy as np
import math
import random as rand
epoch = 10000
mean = 0
exp_mean = 0.0
lr = 0.01
xi = 7
xh = 5
xy = 1
w1 = np.random.random([xi, xh - 1])
w2 = np.random.random([xh])
target_weight_1 = np.random.random([xi, xh - 1])
target_weight_2 = np.random.random([xh])
for i in range(0, epoch):
x = [1]
s1 = 0
for j in range(0, xi - 1):
val = 1 if rand.random() > 0.5 else -1
x.append(val)
s1 += val
x = np.array(x, dtype="int64")
x_h = [1]
x_ht = [1]
8
h = np.dot(x, w1)
ht = np.dot(x, target_weight_1)
for j in range(0, xh - 1):
h[j] = math.tanh(h[j])
x_h.append(h[j])
ht[j] = math.tanh(ht[j])
x_ht.append(ht[j])
x_h = np.array(x_h)
x_ht = np.array(x_ht)
y = np.dot(x_h, w2)
y = math.tanh(y)
t = np.dot(x_ht, target_weight_2)
t = math.tanh(t)
loss = 0.5 * (t - y) ** 2
mean = mean + loss
exp_mean = (exp_mean * 0.99) + (loss * 0.01)
m1 = -1 * (t - y) * (1 - y ** 2)
dw2 = []
for j in range(0, xh):
dw2.append(m1 * x_h[j])
dw2 = np.array(dw2)
dw1 = []
for j in range(1, xh):
dw = []
for k in range(0, xi):
dmw = m1 * w2[j] * (1 - x_h[j] ** 2) * x[k]
dw.append(dmw)
dw1.append(dw)
dw1 = np.array(dw1)
dw1 = np.transpose(dw1)
w1 -= dw1 * lr
w2 -= dw2 * lr
if i % 100 == 0:
print("=== X: %s ===" % (x,))
print("Y: %s " % (y,))
print("T: %s " % (t,))
print("LOSS: %s " % (loss,))
print("Running mean: %s " % (exp_mean,))
print("MSL: %s " % (mean / (i + 1)))
9
OUTPUT:-
10
Practical 7
Fully Convolutional Neural Networks (FCNs) are a type of neural network that can be used for
pixel-level tasks such as image segmentation, object detection, and depth estimation. FCNs differ
from traditional convolutional neural networks (CNNs) in that they do not have any fully
connected layers. This means that FCNs can be applied to images of any size, without having to
resize or crop them.
Steps :-
1. Choose a dataset:- FCNs can be used for a variety of tasks, such as image segmentation,
object detection, and depth estimation. Choose a dataset that is appropriate for the Practicalyou
want to solve.
2. Preprocess the data:- This may involve resizing the images, normalizing the pixel values,
and converting the labels to a format that the model can understand.
3. Define the FCN architecture:- A typical FCN architecture consists of a stack of
convolutional and pooling layers, followed by a decoder that upsamples the feature maps to the
original image size.
4. Implement the FCN in Keras:- Keras is a popular Python library for deep learning. It
provides a high-level API for defining and training neural networks.
5. Train the FCN:- This involves feeding the training data to the model and adjusting the
weights of the model so that it minimizes the loss function on the training data.
6. Evaluate the FCN on the test data:- This will give you an estimate of how well the model
will perform on unseen data.
CODE
import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Conv2DTranspose,
Concatenate
# Define the FCN architecture
def fcn_model(input_shape, num_classes):
# Input layer
input_layer = Input(shape=input_shape)
# Encoder
conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(input_layer)
conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv1)
11
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool1)
conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool2)
conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
# Bottleneck
conv4 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool3)
conv4 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
# Decoder
conv5 = Conv2D(1024, (3, 3), activation='relu', padding='same')(pool4)
conv5 = Conv2D(1024, (3, 3), activation='relu', padding='same')(conv5)
up1 = Conv2DTranspose(512, (2, 2), strides=(2, 2), padding='same')(conv5)
up1 = Concatenate()([conv4, up1])
up2 = Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(up1)
up2 = Concatenate()([conv3, up2])
up3 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(up2)
up3 = Concatenate()([conv2, up3])
up4 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(up3)
up4 = Concatenate()([conv1, up4])
# Output layer
output_layer = Conv2D(num_classes, (1, 1), activation='softmax')(up4)
model = tf.keras.models.Model(inputs=input_layer, outputs=output_layer)
return model
# Define the input shape and number of classes
input_shape = (256, 256, 3) # Example input shape
num_classes = 21 # Example number of classes
# Create the FCN model
model = fcn_model(input_shape, num_classes)
# Compile the model (add loss function, optimizer, etc.)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Print model summary
model.summary()
12
OUTPUT:-
13