0% found this document useful (0 votes)
76 views34 pages

Applied Machine and Deep Learning

Uploaded by

vickydasuri111
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)
76 views34 pages

Applied Machine and Deep Learning

Uploaded by

vickydasuri111
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/ 34

1

Practical Course on Advanced Deep Learning (Online Mode)


Practical No :01
Implement Feed-forward Neural Network and train the network with different
optimizers and compare the results.

pip install tensorflow keras

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelBinarizer
from sklearn.metrics import classification_report
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.datasets import mnist
from tensorflow.keras import backend as K

# Load the MNIST dataset


(trainX, trainY), (testX, testY) = mnist.load_data()

# Normalize pixel values to [0, 1]


trainX = trainX.astype("float32") / 255.0
testX = testX.astype("float32") / 255.0

# Flatten the images (convert 28x28 images to 784-dimensional vectors)


trainX = trainX.reshape((trainX.shape[0], 28 * 28))
testX = testX.reshape((testX.shape[0], 28 * 28))

# One-hot encode the labels


lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)

# Define the feedforward neural network architecture


model = Sequential()
model.add(Dense(256, input_shape=(784,), activation="sigmoid"))
model.add(Dense(128, activation="sigmoid"))
model.add(Dense(10, activation="softmax"))

# Compile the model


2

model.compile(optimizer=SGD(learning_rate=0.01),
loss="categorical_crossentropy", metrics=["accuracy"])

# Train the model


history = model.fit(trainX, trainY, validation_data=(testX, testY),
epochs=20, batch_size=128)

# Evaluate the model


predictions = model.predict(testX, batch_size=128)
print(classification_report(testY.argmax(axis=1),
predictions.argmax(axis=1)))
3

# Plot training history


plt.figure(figsize=(8, 6))
plt.plot(history.history["loss"], label="Training Loss")
plt.plot(history.history["val_loss"], label="Validation Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.legend()
plt.show()
4

Practical No :02
Write a Program to implement regularization to prevent the model from
overfitting

import numpy as np
import tensorflow as tf
from tensorflow import keras
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification

# Generate synthetic dataset


X, y = make_classification(n_samples=1000, n_features=20, n_classes=2,
random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)

# Define the neural network architecture with L2 regularization


model = keras.Sequential([
keras.layers.Dense(64, activation='relu',
kernel_regularizer=keras.regularizers.l2(0.001), input_shape=(20,)),
keras.layers.Dense(32, activation='relu',
kernel_regularizer=keras.regularizers.l2(0.001)),
keras.layers.Dense(1, activation='sigmoid')
])

# Compile the model


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

# Train the model


history = model.fit(X_train, y_train, epochs=10, batch_size=32,
validation_split=0.2)

# Evaluate the model


loss, accuracy = model.evaluate(X_test, y_test)
print(f'Test Loss: {loss}, Test Accuracy: {accuracy}')
5
6

Practical No :- 03
Implement deep learning for recognizing classes for datasets
like CIFAR-10 images for previously unseen images and assign
them to one of the 10 classes.

import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models, datasets
import matplotlib.pyplot as plt

# Load MNIST dataset


(train_images, _), (test_images, _) = datasets.mnist.load_data()

# Normalize pixel values to be between 0 and 1


train_images = train_images.astype('float32') / 255.0
test_images = test_images.astype('float32') / 255.0

# Flatten images for autoencoder


train_images_flat = train_images.reshape((train_images.shape[0], 28 * 28))
test_images_flat = test_images.reshape((test_images.shape[0], 28 * 28))

# Define the autoencoder architecture


encoding_dim = 32
input_img = tf.keras.Input(shape=(784,))
encoded = layers.Dense(encoding_dim, activation='relu')(input_img)
decoded = layers.Dense(784, activation='sigmoid')(encoded)

# Create autoencoder model


autoencoder = models.Model(input_img, decoded)

# Create encoder model


encoder = models.Model(input_img, encoded)

# Compile the autoencoder


autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

# Train the autoencoder


autoencoder.fit(train_images_flat, train_images_flat,
epochs=10,
7

batch_size=256,
shuffle=True,
validation_data=(test_images_flat, test_images_flat))

# Predict on test data


decoded_imgs = autoencoder.predict(test_images_flat)

# Display some examples of original and decoded images


n = 10
plt.figure(figsize=(20, 4))
for i in range(n):
# Display original images
ax = plt.subplot(2, n, i + 1)
plt.imshow(test_images[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

# Display reconstructed images


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()
8

Practical No :- 04
Implement deep learning for the Prediction of the autoencoder from the test
data (e.g. MNIST data set)
import torch
import torch.nn as nn
import torchvision
from torchvision import transforms

import numpy as np
import matplotlib.pyplot as plt

transform = transforms.ToTensor()
train_dataset = torchvision.datasets.MNIST(root = "./data" , train =
True , download = True , transform = transform)
valid_dataset = torchvision.datasets.MNIST(root = "./data" , train = False
, download = True , transform = transform)

train_dl = torch.utils.data.DataLoader(train_dataset , batch_size = 100)


class Encoder(nn.Module):
def __init__(self , input_size = 28*28 , hidden_size1 = 128 ,
hidden_size2 = 16 , z_dim = 2):
super().__init__()
self.fc1 = nn.Linear(input_size , hidden_size1)
self.fc2 = nn.Linear(hidden_size1 , hidden_size2)
self.fc3 = nn.Linear(hidden_size2 , z_dim)
self.relu = nn.ReLU()
def forward(self , x):
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = self.fc3(x)
return x

class Decoder(nn.Module):
def __init__(self , output_size = 28*28 , hidden_size1 = 128 ,
hidden_size2 = 16 , z_dim = 2):
super().__init__()
self.fc1 = nn.Linear(z_dim , hidden_size2)
self.fc2 = nn.Linear(hidden_size2 , hidden_size1)
self.fc3 = nn.Linear(hidden_size1 , output_size)
self.relu = nn.ReLU()
def forward(self , x):
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
9

x = torch.sigmoid(self.fc3(x))
return x

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


device

enc = Encoder().to(device)
dec = Decoder().to(device)

loss_fn = nn.MSELoss()
optimizer_enc = torch.optim.Adam(enc.parameters())
optimizer_dec = torch.optim.Adam(dec.parameters())

train_loss = []
num_epochs = 200

for epoch in range(num_epochs):


train_epoch_loss = 0
for (imgs , _) in train_dl:
imgs = imgs.to(device)
#100 , 1 , 28 , 28 ---> (100 , 28*28)
imgs = imgs.flatten(1)
latents = enc(imgs)
output = dec(latents)
loss = loss_fn(output , imgs)
train_epoch_loss += loss.cpu().detach().numpy()
optimizer_enc.zero_grad()
optimizer_dec.zero_grad()
loss.backward()
optimizer_enc.step()
optimizer_dec.step()
train_loss.append(train_epoch_loss)

plt.plot(train_loss)
10

values = None
all_labels = []

with torch.no_grad():
for (imgs , labels) in train_dl:
imgs = imgs.to(device)
imgs = imgs.flatten(1)
all_labels.extend(list(labels.numpy()))
latents = enc(imgs)
if values is None:
values = latents.cpu()
else:
values = torch.vstack([values , latents.cpu()])
values.shape

all_labels

cmap = plt.get_cmap('viridis', 10)


cmap

all_labels = np.array(all_labels)
values = values.numpy()

In [ ]:
pc = plt.scatter(values[: , 0] , values[: , 1] , c = all_labels , cmap = cmap)
11

plt.colorbar(pc)

Practical No :- 05
12

Implement Convolutional Neural Network for Digit Recognition on the MNIST


Dataset
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models, datasets
import matplotlib.pyplot as plt

# Load MNIST dataset


(train_images, train_labels), (test_images, test_labels) =
datasets.mnist.load_data()

# Normalize pixel values to be between 0 and 1


train_images = train_images.astype('float32') / 255.0
test_images = test_images.astype('float32') / 255.0

# Add a channel dimension (since the images are grayscale)


train_images = np.expand_dims(train_images, axis=-1)
test_images = np.expand_dims(test_images, axis=-1)

# Define the CNN architecture


model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])

# Compile the model


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

# Train the model


history = model.fit(train_images, train_labels, epochs=5, batch_size=64,
validation_split=0.1)

# Evaluate the model


test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(f'Test accuracy: {test_acc}')
13

# Plot training history


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, 1])
plt.legend(loc='lower right')
plt.show()
14

Practical No :- 06
Write a program to implement Transfer Learning on the suitable dataset (e.g.
classify the cats versus dogs dataset from Kaggle).

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]="2"
import warnings
warnings.filterwarnings("ignore")

from sklearn.metrics import confusion_matrix,classification_report

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, GlobalAveragePooling2D,
Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
from tensorflow.keras.losses import BinaryCrossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import VGG16, ResNet50

train_dataset_path = '/kaggle/input/dogs-cats-images/dataset/training_set/'
test_dataset_path = '/kaggle/input/dogs-cats-images/dataset/test_set/'

BATCH_SIZE = 32
IMG_WIDTH = 256
IMG_HEIGHT = 256
IMG_CHANNELS = 3
IMG_SHAPE = (IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS)

print(f"Batch Size: {BATCH_SIZE}")


print(f"Image Shape: {IMG_SHAPE}")

Batch Size: 32
Image Shape: (256, 256, 3)
15

train_datagen = ImageDataGenerator(rescale=1.0/255,
zoom_range = 0.2,
width_shift_range = 0.2,
height_shift_range = 0.2,
vertical_flip = True,
fill_mode = 'nearest',
validation_split = 0.1)

train_generator = train_datagen.flow_from_directory(train_dataset_path,
target_size = (IMG_WIDTH, IMG_HEIGHT),
batch_size = BATCH_SIZE,
class_mode = 'binary',
shuffle = True,
subset = 'training',
seed = 2)

Found 7200 images belonging to 2 classes.

validation_generator =
train_datagen.flow_from_directory(train_dataset_path,
target_size =
(IMG_WIDTH, IMG_HEIGHT),
batch_size =
BATCH_SIZE,
class_mode =
'binary',
subset =
'validation',
shuffle = True,
seed = 2)

Found 800 images belonging to 2 classes.

test_datagen = ImageDataGenerator(rescale = 1.0/ 255)

test_generator = test_datagen.flow_from_directory(test_dataset_path,
target_size = (IMG_WIDTH, IMG_HEIGHT),
batch_size = BATCH_SIZE,
class_mode = 'binary',
shuffle = False)
Found 2000 images belonging to 2 classes.

labels = {value: key for key, value in train_generator.class_indices.items()}

print("Label Mappings for classes present in the training and validation datasets\n")
for key, value in labels.items():
print(f"{key} : {value}")
16

Label Mappings for classes present in the training and validation datasets

0 : cats
1 : dogs

fig, ax = plt.subplots(nrows = 3, ncols = 3, figsize = (12, 10))


idx = 0
plt.suptitle("Sample Training Images", fontsize = 20)
for i in range(3):
for j in range(3):
label = labels[train_generator[0][1][idx]]
ax[i, j].set_title(f"{label}")
ax[i, j].imshow(train_generator[0][0][idx][:, :, :])
ax[i, j].axis("off")
idx += 1

plt.tight_layout()
plt.show()

Practical No :- 07
Write a program for the Implementation of a Generative
Adversarial Network for generating synthetic shapes (like
digits)
import tensorflow as tf
from tensorflow.keras.layers import (Dense,
BatchNormalization,
LeakyReLU,
Reshape,
Conv2DTranspose,
17

Conv2D,
Dropout,
Flatten)

# underscore to omit the label arrays


(train_images, train_labels), (_, _) = 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 # Normalize the images to [-
1, 1]

BUFFER_SIZE = 60000
BATCH_SIZE = 256

# Batch and shuffle the data


train_dataset =
tf.data.Dataset.from_tensor_slices(train_images).shuffle(BUFFER_SIZE).batc
h(BATCH_SIZE)

def generator_model():
model = tf.keras.Sequential()
model.add(Dense(7*7*256, use_bias=False, input_shape=(100,)))
model.add(BatchNormalization())
model.add(LeakyReLU())

model.add(Reshape((7, 7, 256)))
assert model.output_shape == (None, 7, 7, 256) # Note: None is the
batch size

model.add(Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same',


use_bias=False))
assert model.output_shape == (None, 7, 7, 128)
model.add(BatchNormalization())
model.add(LeakyReLU())

model.add(Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same',


use_bias=False))
assert model.output_shape == (None, 14, 14, 64)
model.add(BatchNormalization())
model.add(LeakyReLU())
18

model.add(Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same',


use_bias=False, activation='tanh'))
assert model.output_shape == (None, 28, 28, 1)

print(model.summary())

return model

generator = generator_model()

# Create a random noise and generate a sample


noise = tf.random.normal([1, 100])
generated_image = generator(noise, training=False)
# Visualize the generated sample
plt.imshow(generated_image[0, :, :, 0], cmap='gray')
19

def discriminator_model():
model = tf.keras.Sequential()

model.add(Conv2D(64, (5, 5), strides=(2, 2), padding='same',


input_shape=[28, 28, 1]))
model.add(LeakyReLU())
model.add(Dropout(0.3))

model.add(Conv2D(128, (5, 5), strides=(2, 2), padding='same'))


model.add(LeakyReLU())
model.add(Dropout(0.3))

model.add(Flatten())
model.add(Dense(1))

print(model.summary())

return model

In [ ]:

discriminator = discriminator_model()
20

# This method returns a helper function to compute cross entropy loss


cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)

def discriminator_loss(real_output, fake_output):


real_loss = cross_entropy(tf.ones_like(real_output), real_output)
fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)
total_loss = real_loss + fake_loss
return total_loss

def generator_loss(fake_output):
return cross_entropy(tf.ones_like(fake_output), fake_output)

generator_optimizer = tf.keras.optimizers.Adam(1e-4)

import os

checkpoint_dir = './training_checkpoints'
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
21

checkpoint = tf.train.Checkpoint(generator_optimizer=generator_optimizer,

discriminator_optimizer=discriminator_optimizer,
generator=generator,
discriminator=discriminator)

EPOCHS = 60
# We will reuse this seed overtime (so it's easier)
# to visualize progress in the animated GIF)
num_examples_to_generate = 16
noise_dim = 100
seed = tf.random.normal([num_examples_to_generate, noise_dim])

# tf.function annotation causes the function


# to be "compiled" as part of the training
@tf.function
def train_step(images):

# 1 - Create a random noise to feed it into the model


# for the image generation
noise = tf.random.normal([BATCH_SIZE, noise_dim])

# 2 - Generate images and calculate loss values


# GradientTape method records operations for automatic
differentiation.
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
generated_images = generator(noise, training=True)

real_output = discriminator(images, training=True)


fake_output = discriminator(generated_images, training=True)

gen_loss = generator_loss(fake_output)
disc_loss = discriminator_loss(real_output, fake_output)

# 3 - Calculate gradients using loss values and model variables


# "gradient" method computes the gradient using
# operations recorded in context of this tape (gen_tape and
disc_tape).

# It accepts a target (e.g., gen_loss) variable and


# a source variable (e.g.,generator.trainable_variables)
# target --> a list or nested structure of Tensors or Variables to be
differentiated.
22

# source --> a list or nested structure of Tensors or Variables.


# target will be differentiated against elements in sources.

# "gradient" method returns a list or nested structure of Tensors


# (or IndexedSlices, or None), one for each element in sources.
# Returned structure is the same as the structure of sources.
gradients_of_generator = gen_tape.gradient(gen_loss,

generator.trainable_variables)
gradients_of_discriminator = disc_tape.gradient(disc_loss,

discriminator.trainable_variables)
# 4 - Process Gradients and Run the Optimizer
# "apply_gradients" method processes aggregated gradients.
# ex: optimizer.apply_gradients(zip(grads, vars))
"""
Example use of apply_gradients:
grads = tape.gradient(loss, vars)
grads = tf.distribute.get_replica_context().all_reduce('sum', grads)
# Processing aggregated gradients.
optimizer.apply_gradients(zip(grads, vars),
experimental_aggregate_gradients=False)
"""
generator_optimizer.apply_gradients(zip(gradients_of_generator,
generator.trainable_variables))

discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator,
discriminator.trainable_variables))

def generate_and_save_images(model, epoch, test_input):


# Notice `training` is set to False.
# This is so all layers run in inference mode (batchnorm).
# 1 - Generate images
predictions = model(test_input, training=False)
# 2 - Plot the generated images
fig = plt.figure(figsize=(4,4))
for i in range(predictions.shape[0]):
plt.subplot(4, 4, i+1)
plt.imshow(predictions[i, :, :, 0] * 127.5 + 127.5, cmap='gray')
plt.axis('off')
# 3 - Save the generated images
plt.savefig('image_at_epoch_{:04d}.png'.format(epoch))
plt.show()

import time
from IPython import display # A command shell for interactive computing in
Python.
23

def train(dataset, epochs):


# A. For each epoch, do the following:
for epoch in range(epochs):
start = time.time()
# 1 - For each batch of the epoch,
for image_batch in dataset:
# 1.a - run the custom "train_step" function
# we just declared above
train_step(image_batch)

# 2 - Produce images for the GIF as we go


display.clear_output(wait=True)
generate_and_save_images(generator,
epoch + 1,
seed)

# 3 - Save the model every 5 epochs as


# a checkpoint, which we will use later
if (epoch + 1) % 5 == 0:
checkpoint.save(file_prefix = checkpoint_prefix)

# 4 - Print out the completed epoch no. and the time spent
print ('Time for epoch {} is {} sec'.format(epoch + 1, time.time()-
start))

# B. Generate a final image after the training is completed


display.clear_output(wait=True)
generate_and_save_images(generator,
epochs,
seed)

train(train_dataset, EPOCHS)
24

Practical No :- 08
Write a program to implement a simple form of a recurrent neural network.

a.) E.g. (4-to-1 RNN) to show that the quantity of rain on a certain day also
depends on the values of the previous day

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, SimpleRNN

# Example data: quantity of rain for 10 days


# You can replace this with your actual data
rain_data = np.array([0.2, 0.3, 0.1, 0.5, 0.4, 0.6, 0.2, 0.3, 0.4, 0.5])

# Prepare the data: X will be previous day's rain, y will be current day's
rain
X = []
y = []
for i in range(len(rain_data) - 1):
X.append(rain_data[i])
y.append(rain_data[i + 1])

X = np.array(X).reshape(-1, 1, 1) # Reshape X to (samples, time steps,


features)
y = np.array(y)

# Define the RNN model


model = Sequential([
SimpleRNN(units=10, input_shape=(1, 1)),
Dense(units=1)
])
25

# Compile the model


model.compile(optimizer='adam', loss='mean_squared_error')

# Train the model


model.fit(X, y, epochs=100, verbose=1)

# Now, let's predict the quantity of rain for the next day based on the
last day's value
last_day_rain = np.array([[rain_data[-1]]]) # Take the last day's rain as
input
predicted_rain = model.predict(last_day_rain)
print("Predicted rain for the next day:", predicted_rain[0][0])
26
27

Practical No :09
9 Write a program for object detection from the image/video.

!pip install tensorflow-gpu

import tensorflow as tf
print(tf.__version__)

!git clone https://github.com/tensorflow/models.git

pwd

cd /content/models/research

pwd

!protoc object_detection/protos/*.proto --python_out=.

!git clone https://github.com/cocodataset/cocoapi.git

cd cocoapi/PythonAPI

!make

cp -r pycocotools /content/models/research

cd ..

cd ..

cp object_detection/packages/tf2/setup.py .
!python -m pip install

# From within TensorFlow/models/research/


28

!python object_detection/builders/model_builder_tf2_test.py
cd /content/trainingdemo/pre-trained-models
!wget
http://download.tensorflow.org/models/object_detection/tf2/20200711/
ssd_resnet101_v1_fpn_640x640_coco17_tpu-8.tar.gz

pwd
cd /content/trainingdemo

ls

# Create train data:


!python generate_tfrecord.py -x /content/trainingdemo/images/train -l
/content/trainingdemo/annotations/label_map.pbtxt -o
/content/trainingdemo/annotations/train.record

# Create test data:


!python generate_tfrecord.py -x /content/trainingdemo/images/test -l
/content/trainingdemo/annotations/label_map.pbtxt -o
/content/trainingdemo/annotations/test.record

pwd

ls

!python model_main_tf2.py
--model_dir=/content/training_demo/models/my_ssd_resnet101_v1_fpn --
pipeline_config_path=/content/training_demo/models/my_ssd_resnet101_v1_fpn
/pipeline.config

pwd

!python exporter_main_v2.py --input_type image_tensor --


pipeline_config_path
/content/training_demo/models/my_ssd_resnet101_v1_fpn/pipeline.config --
trained_checkpoint_dir
29

/content/training_demo/models/my_ssd_resnet101_v1_fpn --
output_directory /content/training_demo/exported_models/my_model

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging
(1)
import pathlib
import tensorflow as tf
import cv2
import argparse
from google.colab.patches import cv2_imshow

# Enable GPU dynamic memory allocation


gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)

# PROVIDE PATH TO IMAGE DIRECTORY


IMAGE_PATHS = '/content/training_demo/images/train/image1.jpg'

# PROVIDE PATH TO MODEL DIRECTORY


PATH_TO_MODEL_DIR = '/content/training_demo/exported_models/my_model'

# PROVIDE PATH TO LABEL MAP


PATH_TO_LABELS = '/content/training_demo/annotations/label_map.pbtxt'

# PROVIDE THE MINIMUM CONFIDENCE THRESHOLD


MIN_CONF_THRESH = float(0.60)

# LOAD THE MODEL

import time
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils

PATH_TO_SAVED_MODEL = PATH_TO_MODEL_DIR + "/saved_model"

print('Loading model...', end='')


start_time = time.time()

# LOAD SAVED MODEL AND BUILD DETECTION FUNCTION


detect_fn = tf.saved_model.load(PATH_TO_SAVED_MODEL)
30

end_time = time.time()
elapsed_time = end_time - start_time
print('Done! Took {} seconds'.format(elapsed_time))

# LOAD LABEL MAP DATA FOR PLOTTING

category_index =
label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,

use_display_name=True)

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore') # Suppress Matplotlib warnings

def load_image_into_numpy_array(path):
"""Load an image from file into a numpy array.
Puts image into numpy array to feed into tensorflow graph.
Note that by convention we put it into a numpy array with shape
(height, width, channels), where channels=3 for RGB.
Args:
path: the file path to the image
Returns:
uint8 numpy array with shape (img_height, img_width, 3)
"""
return np.array(Image.open(path))

print('Running inference for {}... '.format(IMAGE_PATHS), end='')

image = cv2.imread(IMAGE_PATHS)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_expanded = np.expand_dims(image_rgb, axis=0)

# The input needs to be a tensor, convert it using `tf.convert_to_tensor`.


input_tensor = tf.convert_to_tensor(image)
# The model expects a batch of images, so add an axis with `tf.newaxis`.
input_tensor = input_tensor[tf.newaxis, ...]

# input_tensor = np.expand_dims(image_np, 0)
detections = detect_fn(input_tensor)
31

# All outputs are batches tensors.


# Convert to numpy arrays, and take index [0] to remove the batch
dimension.
# We're only interested in the first num_detections.
num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
for key, value in detections.items()}
detections['num_detections'] = num_detections

# detection_classes should be ints.


detections['detection_classes'] =
detections['detection_classes'].astype(np.int64)

image_with_detections = image.copy()

# SET MIN_SCORE_THRESH BASED ON YOU MINIMUM THRESHOLD FOR DETECTIONS


viz_utils.visualize_boxes_and_labels_on_image_array(
image_with_detections,
detections['detection_boxes'],
detections['detection_classes'],
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=200,
min_score_thresh=0.5,
agnostic_mode=False)

print('Done')
# DISPLAYS OUTPUT IMAGE
cv2_imshow(image_with_detections)
# CLOSES WINDOW ONCE KEY IS PRESSED
32

https://colab.research.google.com/drive/19ycUy5qIZKCO8tKy37f4zkUiHzgKs05I?usp=sharing#scrollTo=pNVwlSCq9pr1
33

Practical No :10
10) Write a program for object detection using pre-trained models to use object
detection.

!git clone https://github.com/pjreddie/darknet

cd darknet

ls

!make

!wget https://pjreddie.com/media/files/yolov3.weights

!./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg

!./darknet detect cfg/yolov3.cfg yolov3.weights data/yolo_test.jfif


34

from PIL import Image

img=Image.open("predictions.jpg")
img

https://github.com/nachi-hebbar/Object-Detection-Yolo-V3-Darknet/blob/main/YOLO_V3.ipynb

You might also like