Lab Manual
Lab Manual
1 Syllabus
2 Task Plan
5
Construct a Convnet model for image classification
13
TASK 1
Aim:
To use tensorflow for implementation of multiclass classification for gray scale image.
Procedure:
Program
import tensorflow as tf
from tensorflow.keras import datasets
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
from tensorflow.keras import models, layers
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten()) model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10)) model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 8))
plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.5, 1])
plt.legend(loc='lower right')
plt.savefig("output_report.png")
OUTPUT:
Epoch 1/10
1563/1563 [==============================] - 80s 50ms/step - loss: 1.5129 - accuracy:
0.4485 - val_loss: 1.2795 - val_accuracy: 0.5465
Epoch 2/10
1563/1563 [==============================] - 76s 49ms/step - loss: 1.1397 - accuracy:
0.5965 - val_loss: 1.0586 - val_accuracy: 0.6189
Epoch 3/10
1563/1563 [==============================] - 75s 48ms/step - loss: 0.9859 - accuracy:
0.6548 - val_loss: 0.9609 - val_accuracy: 0.6641
Epoch 4/10
1563/1563 [==============================] - 78s 50ms/step - loss: 0.8883 - accuracy:
0.6898 - val_loss: 0.9351 - val_accuracy: 0.6731
Epoch 5/10
1563/1563 [==============================] - 76s 49ms/step - loss: 0.8145 - accuracy:
0.7147 - val_loss: 0.8953 - val_accuracy: 0.6889
Epoch 6/10
1563/1563 [==============================] - 76s 49ms/step - loss: 0.7576 - accuracy:
0.7353 - val_loss: 0.8864 - val_accuracy: 0.6949
Epoch 7/10
1563/1563 [==============================] - 76s 49ms/step - loss: 0.7055 - accuracy:
0.7533 - val_loss: 0.8584 - val_accuracy: 0.7112
Epoch 8/10
1563/1563 [==============================] - 75s 48ms/step - loss: 0.6600 - accuracy:
0.7691 - val_loss: 0.9043 - val_accuracy: 0.6992
Epoch 9/10
1563/1563 [==============================] - 75s 48ms/step - loss: 0.6200 - accuracy:
0.7819 - val_loss: 0.8517 - val_accuracy: 0.7128
Epoch 10/10
1563/1563 [==============================] - 73s 47ms/step - loss: 0.5821 - accuracy:
0.7948 - val_loss: 0.9044 - val_accuracy: 0.7027
[]
Result:
Thus the Implementation of multiclass classification for gray scale image is successfully
completed.
TASK 2
Aim:
OUTPUT:
Epoch 1/10
1875/1875 [==============================] - 43s 23ms/step - loss: 0.1620 - accuracy:
0.9506
Epoch 2/10
1875/1875 [==============================] - 43s 23ms/step - loss: 0.0565 - accuracy:
0.9823
Epoch 3/10
1875/1875 [==============================] - 41s 22ms/step - loss: 0.0374 - accuracy:
0.9883
Epoch 4/10
1875/1875 [==============================] - 41s 22ms/step - loss: 0.0249 - accuracy:
0.9923
Epoch 5/10
1875/1875 [==============================] - 41s 22ms/step - loss: 0.0169 - accuracy:
0.9947
Epoch 6/10
1875/1875 [==============================] - 41s 22ms/step - loss: 0.0133 - accuracy:
0.9957
Epoch 7/10
1875/1875 [==============================] - 42s 22ms/step - loss: 0.0090 - accuracy:
0.9972
Epoch 8/10
1875/1875 [==============================] - 42s 23ms/step - loss: 0.0074 - accuracy:
0.9977
Epoch 9/10
1875/1875 [==============================] - 40s 21ms/step - loss: 0.0055 - accuracy:
0.9982
Epoch 10/10
1875/1875 [==============================] - 40s 21ms/step - loss: 0.0052 - accuracy:
0.9983 [ ]
Result:
Thus the Implementing convolutional neural network using tensor flow is successfully
completed.
TASK 3
Aim:
Procedure:
break
from tensorflow.keras import layers
from tensorflow.keras.models import Model
def AlexNet():
inp = layers.Input((224, 224, 3))
x = layers.Conv2D(96, 11, 4, activation='relu')(inp)
x = layers.BatchNormalization()(x)
x = layers.MaxPooling2D(3, 2)(x)
x = layers.Conv2D(256, 5, 1, activation='relu')(x)
x = layers.BatchNormalization()(x)
x = layers.MaxPooling2D(3, 2)(x)
x = layers.Conv2D(384, 3, 1, activation='relu')(x)
x = layers.Conv2D(384, 3, 1, activation='relu')(x)
x = layers.Conv2D(256, 3, 1, activation='relu')(x)
x = layers.MaxPooling2D(3, 2)(x)
x = layers.Flatten()(x)
x = layers.Dense(4096, activation='relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(4096, activation='relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(1, activation='sigmoid')(x)
model = Model(inputs=inp, outputs=x)
return model
model = AlexNet()
model.summary()
tf.keras.utils.plot_model(
model,
to_file='model.png',
show_shapes=True,
show_dtype=False,
show_layer_names=False,
show_layer_activations=True,
dpi=100
4653/4653
[==============================] -
2146s 461ms/step - loss: 0.6935 - accuracy:
TASK 4
Aim:
Procedure:
Program:
#pytefrom google.colab import drive
#drive.mount('/content/drive')
from google.colab import drive
drive.mount('/content/drive')
import cv2
import numpy as np
import os
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Dense,Flatten,Conv2D,Activation,Dropout
from keras import backend as K
import keras
from keras.models import Sequential, Model
from keras.models import load_model
#from keras.optimizers import SGD
from keras.callbacks import EarlyStopping,ModelCheckpoint
from keras.layers import MaxPool2D
from google.colab.patches import cv2_imshow
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
#K.tensorflow_backend._get_available_gpus()
import keras.backend as K
train_path="/content/drive/MyDrive/archive/train"
test_path="/content/drive/MyDrive/archive/test"
class_names=os.listdir(train_path)
class_names_test=os.listdir(test_path)
print(class_names)
print(class_names_test)
#Sample datasets images
image_criminal=cv2.imread("/content/drive/MyDrive/archive/test/ALTAMIRA
YELLOWTHROAT/1.jpg")
cv2_imshow(image_criminal)
image_normal=cv2.imread("/content/drive/MyDrive/archive/test/AMERICAN AVOCET/1.jpg")
cv2_imshow(image_normal)
image_Suspicious=cv2.imread("/content/drive/MyDrive/archive/test/AMERICAN BITTERN/2.jpg")
cv2_imshow(image_Suspicious)
train_datagen =
ImageDataGenerator(zoom_range=0.15,width_shift_range=0.2,height_shift_range=0.2,shear_range=0
.15)
test_datagen = ImageDataGenerator()
def VGG16():
model = Sequential()
model.add(Conv2D(input_shape=(224,224,3),filters=64,kernel_size=(3,3),padding="same",
activation="relu"))
model.add(Conv2D(filters=64,kernel_size=(3,3),padding="same", activation="relu"))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2),name='vgg16'))
model.add(Flatten(name='flatten'))
model.add(Dense(256, activation='relu', name='fc1'))
model.add(Dense(128, activation='relu', name='fc2'))
model.add(Dense(3, activation='softmax', name='output'))
return model
model=VGG16()
model.summary()
Vgg16 = Model(inputs=model.input, outputs=model.get_layer('vgg16').output)
Vgg16.load_weights("/content/drive/MyDrive/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5"
)
'''
if(preds==0):
print("Predicted Label:AMERICAN KESTREL")
if(preds==1):
print("Predicted Label:AMERICAN GOLDFINCH")
else:
print("Predicted Label: AMERICAN FLAMINGO")
'''
['AMERICAN KESTREL', 'AMERICAN GOLDFINCH', 'AMERICAN FLAMINGO']
#Perform Classification
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
predict_("/content/drive/MyDrive/vggTL/data/test/ALTAMIRA YELLOWTHROAT/4.jpg")
predict_("/content/drive/MyDrive/vggTL/data/test/AMERICAN AVOCET/5.jpg")
predict_("/content/drive/MyDrive/vggTL/data/test/AMERICAN BITTERN/4.jpg")
test_path="/content/drive/MyDrive/test"
class_names_test=os.listdir(test_path)
for path in class_names_test :
files=os.listdir(test_path+ "/"+path )
for f in files:
print(f)
predict_(test_path+ "/"+path + "/" + f)
import numpy as np
from sklearn.metrics import confusion_matrix,classification_report
batch_size = 32
num_of_test_samples = 600
predictions = model.predict_generator(test_generator, num_of_test_samples // batch_size+1)
true_classes = test_generator.classes
class_labels = list(test_generator.class_indices.keys())
print(class_labels)
print(confusion_matrix(test_generator.classes, y_pred))
report = classification_report(true_classes, y_pred, target_names=class_labels)
print(report)
OUTPUT:
Epoch 1/10
50/50 [==============================] - 1000s 20s/step - loss: 6.0878 - accuracy: 0.7510 -
val_loss: 1.2246 - val_accuracy: 0.9150
Epoch 2/10
50/50 [==============================] - 975s 20s/step - loss: 1.2780 - accuracy: 0.8771 -
val_loss: 1.0288 - val_accuracy: 0.9183
Epoch 3/10
50/50 [==============================] - 970s 19s/step - loss: 0.6588 - accuracy: 0.9134 -
val_loss: 1.0429 - val_accuracy: 0.9150
Epoch 4/10
50/50 [==============================] - 970s 19s/step - loss: 0.4398 - accuracy: 0.9255 -
val_loss: 1.0595 - val_accuracy: 0.9100
Epoch 5/10
50/50 [==============================] - 970s 20s/step - loss: 0.4480 - accuracy: 0.9344 -
val_loss: 1.0376 - val_accuracy: 0.9017
Epoch 6/10
50/50 [==============================] - 970s 19s/step - loss: 0.5306 - accuracy: 0.9229 -
val_loss: 1.0845 - val_accuracy: 0.9133
Epoch 7/10
50/50 [==============================] - 968s 19s/step - loss: 0.4033 - accuracy: 0.9446 -
val_loss: 0.7878 - val_accuracy: 0.9200
Epoch 8/10
50/50 [==============================] - 968s 19s/step - loss: 0.3513 - accuracy: 0.9414 -
val_loss: 1.0129 - val_accuracy: 0.9233
Epoch 9/10
50/50 [==============================] - 968s 19s/step - loss: 0.3326 - accuracy: 0.9510 -
val_loss: 0.7167 - val_accuracy: 0.9250
Epoch 10/10
50/50 [==============================] - 968s 19s/step - loss: 0.2232 - accuracy: 0.9541 -
val_loss: 0.7805 - val_accuracy: 0.9283
Result:
Thus Creating vgg-16 pretrained model from transfer learning is successfully completed.
TASK 5
Aim:
Procedure:
Step 5: Train the loop & save the generated images for classification
Step 6: Visualize it
PROGRAM:
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import layers
# Load and preprocess the MNIST dataset
(train_images, _), (_, _) = tf.keras.datasets.mnist.load_data()
train_images = train_images.reshape(train_images.shape[0], 28, 28, 1).astype('float32')
train_images = (train_images - 127.5) / 127.5
# Generator model def
build_generator():
model = tf.keras.Sequential()
model.add(layers.Dense(128, input_shape=(100,), use_bias=False))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU(alpha=0.01))
model.add(layers.Dense(784, activation='tanh'))
model.add(layers.Reshape((28, 28, 1)))
return model
# Discriminator model def
build_discriminator():
model = tf.keras.Sequential()
model.add(layers.Flatten(input_shape=(28, 28, 1)))
model.add(layers.Dense(128))
model.add(layers.LeakyReLU(alpha=0.01))
model.add(layers.Dense(1, activation='sigmoid'))
return model
# Build GAN
def build_gan(generator, discriminator):
discriminator.trainable = False
model = tf.keras.Sequential([generator, discriminator])return
model
generator = build_generator()
discriminator = build_discriminator()
gan = build_gan(generator, discriminator)#
Compile models
discriminator.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(0.0002, 0.5))
gan.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(0.0002, 0.5)) #
Training loop
epochs = 10
batch_size = 64
for epoch in range(epochs):
idx = np.random.randint(0, train_images.shape[0], batch_size)real_images
= train_images[idx]
noise = np.random.normal(0, 1, (batch_size, 100))
generated_images = generator.predict(noise)
real_labels = np.ones((batch_size, 1))
fake_labels = np.zeros((batch_size, 1))
d_loss_real = discriminator.train_on_batch(real_images, real_labels) d_loss_fake
= discriminator.train_on_batch(generated_images, fake_labels) d_loss = 0.5 *
np.add(d_loss_real, d_loss_fake)
noise = np.random.normal(0, 1, (batch_size, 100))
fake_labels = np.ones((batch_size, 1))
g_loss = gan.train_on_batch(noise, fake_labels) if
epoch % 100 == 0:
print("Epoch {}, D Loss: {}, G Loss: {}".format(epoch, d_loss, g_loss))#
Save generated images for visualization
if epoch % 1000 == 0:
generated_images = generator.predict(np.random.normal(0, 1, (16, 100)))
generated_images = 0.5 * generated_images + 0.5 # Rescale images plt.figure(figsize=(4,
4))
for i in range(generated_images.shape[0]): plt.subplot(4,
4, i + 1) plt.imshow(generated_images[i, :, :, 0],
cmap='gray')
plt.axis('off')
plt.show()
OUTPUT:
2/2 [==============================] - 0s 6ms/step
Result
Thus the Implementing a simple generative adversial network is successfully completed.
Task 6
AIM:
PROCEDURE:
Step5: Prepare input and target data and build RNN model
corpus = [
'I enjoy playing football',
'Football is my favorite sport',
'I watch football matches regularly'
]# Tokenize the corpus
tokenizer = Tokenizer()
tokenizer.fit_on_texts(corpus)
total_words = len(tokenizer.word_index) + 1
# Convert text to sequences
sequences = tokenizer.texts_to_sequences(corpus)
# Pad sequences to have the same length
max_sequence_length = max([len(seq) for seq in sequences])
padded_sequences = pad_sequences(sequences, maxlen=max_sequence_length, padding='post')
# Prepare input and target data
input_data = padded_sequences[:, :-1]
target_data = padded_sequences[:, -1]
# Build the RNN model
embedding_dim = 10
model = Sequential()
model.add(Embedding(total_words, embedding_dim, input_length=max_sequence_length-1))
model.add(LSTM(50))
model.add(Dense(total_words, activation='softmax'))
model.summary()
# Compile the model
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train the model
model.fit(input_data, target_data, epochs=100, verbose=1)
# Get the word embeddings
word_embeddings = model.layers[0].get_weights()[0]
# Print word embeddings
for word, index in
tokenizer.word_index.items():
embedding_vector = word_embeddings[index]
print(f"{word}: {embedding_vector}")
OUTPUT:
Epoch 1/100
1/1 [==============================] - 2s 2s/step - loss: 2.4875 - accuracy: 0.0000e+00
Epoch 2/100
1/1 [==============================] - 0s 17ms/step - loss: 2.4826 - accuracy: 0.0000e+00
Epoch 3/100
1/1 [==============================] - 0s 13ms/step - loss: 2.4776 - accuracy: 0.0000e+00
Epoch 4/100
1/1 [==============================] - 0s 13ms/step - loss: 2.4727 - accuracy: 0.3333
Epoch 95/100
1/1 [==============================] - 0s 16ms/step - loss: 0.7661 - accuracy: 1.0000
Epoch 96/100
1/1 [==============================] - 0s 16ms/step - loss: 0.7504 - accuracy: 1.0000
Epoch 97/100
1/1 [==============================] - 0s 16ms/step - loss: 0.7344 - accuracy: 1.0000
Epoch 98/100
1/1 [==============================] - 0s 20ms/step - loss: 0.7179 - accuracy: 1.0000
Epoch 99/100
1/1 [==============================] - 0s 16ms/step - loss: 0.7010 - accuracy: 1.0000
Epoch 100/100
1/1 [==============================] - 0s 15ms/step - loss: 0.6838 - accuracy: 1.0000
Result:
Thus, the Creating & performing recurrent neural network is sucessfully completed.
TASK 7
AUTO ENCODES
AIM:
PROCEDURE:
Program:
# -*- coding: utf-8 -*-
"""Autoencoder
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1J678VYoIlJT7kfn9SbQNdBdnSTdS06_I
"""
x_train = x_train.astype('float32')/255
x_test = x_test.astype('float32')/255
x_train = x_train.reshape((len(x_train), (np.prod(x_train.shape[1:]))))
x_test = x_test.reshape((len(x_test), (np.prod(x_test.shape[1:]))))
print(f'x_train shape = {x_train.shape}')
print(f'x_test shape = {x_test.shape}')
autoencoder.fit(x_train, x_train, epochs=5, batch_size=32, shuffle=True, validation_data=(x_test,
x_test))
encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)
n = 10
plt.figure(figsize=(20,4))
for i in range(n):
ax = plt.subplot(2, n, i+1)
plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax = plt.subplot(2, n, i+1+n)
plt.imshow(decoded_imgs[i].reshape(28,28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
from google.colab import drive
drive.mount('/content/drive')
OUTPUT:
Result:
Thus Creating a autoencodes by using keras modelis sucessfully completed.
Task 8
AIM:
PROCEDURE:
Program:
# -*- coding: utf-8 -*-
"""LSTM.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/19SKdhO8GsH-bNjjdExu1AFMfA5MjNZ93
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from google.colab import drive
drive.mount('/content/drive')
df =
pd.read_csv('/content/drive/MyDrive/monthlyMilkProduction.csv',index_col='Date',parse_dates=True
)
df.index.freq='MS'
df.head()
df.plot(figsize=(10,6))
from statsmodels.tsa.seasonal import seasonal_decompose
results = seasonal_decompose(df['Production'])
results.plot()
len(df)
train = df.iloc[:156]
test = df.iloc[156:]
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
df.head(),df.tail()
scaler.fit(train)
scaled_train = scaler.transform(train)
scaled_test = scaler.transform(test)
scaled_train[:10]
OUTPUT:
Epoch 1/50
Epoch 2/50
Epoch 3/50
Epoch 4/50
Epoch 5/50
Epoch 6/50
Epoch 7/50
Epoch 8/50
Epoch 9/50
Epoch 10/50
Epoch 11/50
144/144 [==============================] - 1s 9ms/step - loss: 0.0042
Epoch 12/50
Epoch 13/50
Epoch 14/50
Epoch 15/50
Epoch 16/50
Epoch 17/50
Epoch 18/50
Epoch 19/50
Epoch 20/50
Epoch 21/50
Epoch 22/50
Epoch 23/50
Epoch 24/50
Epoch 25/50
Epoch 26/50
144/144 [==============================] - 1s 7ms/step - loss: 0.0052
Epoch 27/50
Epoch 28/50
Epoch 29/50
Epoch 30/50
Epoch 31/50
Epoch 32/50
Epoch 33/50
Epoch 34/50
Epoch 35/50
Epoch 36/50
Epoch 37/50
Epoch 38/50
Epoch 39/50
Epoch 40/50
Epoch 41/50
144/144 [==============================] - 1s 9ms/step - loss: 0.0027
Epoch 42/50
Epoch 43/50
Epoch 44/50
Epoch 45/50
Epoch 46/50
Epoch 47/50
Epoch 48/50
Epoch 49/50
Epoch 50/50
Task 9
AIM:
PROCEDURE:
import cv2
import time
from ultralytics import YOLO
# define some parameters
CONFIDENCE = 0.5
font_scale = 1
thickness = 1
# loading the YOLOv8 model with the default weight file
model = YOLO("yolov8n.pt")
# loading all the class labels (objects)
labels = open("/content/coco.names").read().strip().split("\n")
# generating colors for each object for later plotting
colors = np.random.randint(0, 255, size=(len(labels), 3), dtype="uint8")
path_name = "/content/dog.jpg"
image = cv2.imread(path_name)
file_name = os.path.basename(path_name) # "dog.jpg"
filename, ext = file_name.split(".") # "dog", "jpg"
# measure how much it took in seconds
start = time.perf_counter()
# run inference on the image
# see: https://docs.ultralytics.com/modes/predict/#arguments for full list of arguments
results = model.predict(image, conf=CONFIDENCE)[0]
time_took = time.perf_counter() - start
print(f"Time took: {time_took:.2f}s")
print(results.boxes.data)
# loop over the detections
for data in results.boxes.data.tolist():
# get the bounding box coordinates, confidence, and class id
xmin, ymin, xmax, ymax, confidence, class_id = data
# converting the coordinates and the class id to integers
xmin = int(xmin)
ymin = int(ymin)
xmax = int(xmax)
ymax = int(ymax)
class_id = int(class_id)
# draw a bounding box rectangle and label on the image
for class_id in class_ids:
color = [int(c) for c in colors[class_id]]
cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color=color, thickness=thickness)
text = f"{labels[class_id]}: {confidence:.2f}"
# calculate text width & height to draw the transparent boxes as background of the text
(text_width, text_height) = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX,
fontScale=font_scale, thickness=thickness)[0]
text_offset_x = xmin
text_offset_y = ymin - 5
box_coords = ((text_offset_x, text_offset_y), (text_offset_x + text_width + 2, text_offset_y -
text_height))
overlay = image.copy()
cv2.rectangle(overlay, box_coords[0], box_coords[1], color=color, thickness=cv2.FILLED)
# add opacity (transparency to the box)
image = cv2.addWeighted(overlay, 0.6, image, 0.4, 0)
# now put the text (label: confidence %)
cv2.putText(image, text, (xmin, ymin - 5), cv2.FONT_HERSHEY_SIMPLEX,
fontScale=font_scale, color=(0, 0, 0), thickness=thickness)
import cv2
from google.colab.patches import cv2_imshow
# display output image
cv2_imshow(image)
cv2.waitKey(0)
# save output image to disk
cv2.imwrite(filename + "_yolo8." + ext, image)
OUTPUT:
Result:
Thus, the Creating & performing classification with localization is successfully completed.
TASK 10
OPTIMIZERS
AIM:
PROCEDURE:
"""Untitled10.ipynb
https://colab.research.google.com/drive/1w7xl7q4u8bQ9dOo7vh98b3iB9POgt7Gn
"""
import tensorflow as tf
import numpy as np
# %matplotlib inline
display.clear_output()
import tensorflow as tf
import numpy as np
X, Y = np.meshgrid(x_vals, y_vals)
Z = loss_func(X, Y)
return X, Y, Z
# Usage example:
X, Y, Z = gen_2d_loss_surface(rosenbrock_banana)
plt.colorbar()
plt.xlabel('x')
plt.ylabel('y')
plt.show()
Z = loss_func(X, Y).numpy()
return X, Y, Z
if levels == None:
ax = fig.gca()
ax.set_xlabel('x')
ax.set_ylabel('y')
return fig, ax
if levels == None:
fig = plt.figure(figsize=(10,6))
ax = fig.gca(projection='3d')
ax.view_init(elevation, azimuth_angle)
ax.plot_surface(X, Y, Z, cmap='viridis', alpha=0.2)
ax.set_xlabel('x')
ax.set_ylabel('y')
return fig, ax
X, Y, Z = gen_2d_loss_surface(rosenbrock_banana)
fig, ax = make_contour_plot(X, Y, Z)
ax.legend()
fig.show()
def f(x):
return -np.cos(x)
def tangent_f(x):
return np.sin(x)
def perpindicular_unit_f(x_0):
slope_f = tangent_f(x_0)
y_0 = f(x_0)
def interactive_gradient_visual(x_0):
plt.figure(figsize=(12, 8))
x = np.linspace(-np.pi, 2 * np.pi)
f_x = f(x)
# plot f(x)
plt.plot(x, f_x, label=r"$f(x)$", color="green")
perp_unit_vector = perpindicular_unit_f(x_0)
# Usage example:
# Define the perp_unit_vector (this is just an example; you need to calculate it based on your specific
scenario)
dx = x_1 - x_0
dy = 0 # y_1 - y_1
arrow = plt.arrow(
plt.legend(loc="upper left")
plt.xlim(min_x, max_x)
plt.ylim(min_y, max_y)
plt.xlabel(r"$x_0$")
plt.show()
# Add the following line to store and reset the initial font size
init_size = plt.rcParams['font.size']
plt.show()
# Reset to the initial font size (you may skip this line if you don't want to reset it)
plt.rcParams.update({'font.size': init_size})
interactive_gradient_visual(x_0)
def f(x):
return -np.cos(x)
def tangent_f(x):
return np.sin(x)
def perpindicular_unit_f(x_0):
slope_f = tangent_f(x_0)
y_0 = f(x_0)
slope_f = tangent_f(noisy_x_0)
y_0 = f(x_0)
x = np.linspace(-np.pi, 2 * np.pi)
f_x = f(x)
y_0 = f(x_0)
# plot f(x)
dx = x_1 - x_0
dy = 0
arrow = plt.arrow(
color="red", label=r"$f'(x_0)$",
plt.legend(loc="upper left")
plt.xlim(min_x, max_x)
plt.ylim(min_y, max_y)
plt.xlabel(r"$x_0$")
plt.show()
# reset to initial font size
plt.rcParams.update({'font.size': init_size})
np.random.seed(0)
interactive_noisy_gradient_visual(x_0, noisy_x_0)
x_0 = 2.7
batch_size = 20
interactive_batch_size_visual(x_0, batch_size)
param.assign_sub(hyper_params['lr'] * grad)
X, Y, Z = gen_2d_loss_surface(rosenbrock_banana)
fig, ax = make_contour_plot(X, Y, Z)
loss = rosenbrock_banana(x, y)
old_x = params[0].numpy()
old_y = params[1].numpy()
# update the parameters using SGD
update_func(params, grads, states, hyper_params)
xytext=(old_x, old_y),
va='center', ha='center')
ax.legend()
fig.show()
start_x = -1
start_y = 0.73334
learning_rate = 0.015
epochs = 150
x = tf.Variable(start_x, dtype='float32')
y = tf.Variable(start_y, dtype='float32')
params = [x, y]
states = []
OUTPUT:
Result:
Thus the Creating & performing ptimization by using tensorflow successfully completed.
Task 10
Implement simple facts using python
Algorithm:
Step:1Define a list of facts containing the statements to be verified.
Step:2 Create a function named verify_fact that takes a fact as input and returns a boolean value
indicating whether the fact is true or false.
Step:3 In the verify_fact function:
a. Remove the trailing period from the fact using the rstrip function.
b. Check the fact against the known conditions to determine its truth value. You can use
conditional statements (if, elif, else) for this.
If the fact matches a known condition, return True to indicate that the fact is true.
If the fact does not match any known condition, return False to indicate that the fact is
false.
Step:4 Iterate over each fact in the list of facts:
a. Call the verify_fact function for each fact.
b. Print the fact and the corresponding "Yes" or "No" based on its truth value.
Program:
# Define a list of facts
facts = [
"john_is_cold.", # john is cold
"raining.", # it is raining
"john_Forgot_His_Raincoat.", # john forgot his raincoat
"fred_lost_his_car_keys.", # fred lost his car keys
"peter_footballer." # peter plays football
]
Output:
john_is_cold. - No
raining. - Yes
john_Forgot_His_Raincoat. - Yes
fred_lost_his_car_keys. - No
peter_footballer. – No
Result:
Thus the implementation of simple facts using python was successfully executed and output
was verified.