Applied Machine and Deep Learning
Applied Machine and Deep Learning
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
model.compile(optimizer=SGD(learning_rate=0.01),
loss="categorical_crossentropy", metrics=["accuracy"])
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
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
batch_size=256,
shuffle=True,
validation_data=(test_images_flat, test_images_flat))
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)
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
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
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
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
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")
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)
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)
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)
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.
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
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)
BUFFER_SIZE = 60000
BATCH_SIZE = 256
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
print(model.summary())
return model
generator = generator_model()
def discriminator_model():
model = tf.keras.Sequential()
model.add(Flatten())
model.add(Dense(1))
print(model.summary())
return model
In [ ]:
discriminator = discriminator_model()
20
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])
gen_loss = generator_loss(fake_output)
disc_loss = discriminator_loss(real_output, fake_output)
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))
import time
from IPython import display # A command shell for interactive computing in
Python.
23
# 4 - Print out the completed epoch no. and the time spent
print ('Time for epoch {} is {} sec'.format(epoch + 1, time.time()-
start))
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
# 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])
# 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.
import tensorflow as tf
print(tf.__version__)
pwd
cd /content/models/research
pwd
cd cocoapi/PythonAPI
!make
cp -r pycocotools /content/models/research
cd ..
cd ..
cp object_detection/packages/tf2/setup.py .
!python -m pip install
!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
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
/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
import time
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils
end_time = time.time()
elapsed_time = end_time - start_time
print('Done! Took {} seconds'.format(elapsed_time))
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))
image = cv2.imread(IMAGE_PATHS)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_expanded = np.expand_dims(image_rgb, axis=0)
# input_tensor = np.expand_dims(image_np, 0)
detections = detect_fn(input_tensor)
31
image_with_detections = image.copy()
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.
cd darknet
ls
!make
!wget https://pjreddie.com/media/files/yolov3.weights
img=Image.open("predictions.jpg")
img
https://github.com/nachi-hebbar/Object-Detection-Yolo-V3-Darknet/blob/main/YOLO_V3.ipynb