KR23 DL Lab Record
KR23 DL Lab Record
Aim: Design Single unit perceptron for classification of iris dataset without using predefined models.
Software Required: Google Co Lab, Jupyter notebook, Kaggle .
Theory:
Mr. Frank Rosenblatt invented the perceptron model as a binary classifier which contains three main
components. These are as follows:
Code:
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
iris=pd.read_csv("iris.csv")
import seaborn as sns
import matplotlib.pyplot as plt
Output:
iris['species'].unique()
Output: array(['setosa', 'versicolor', 'virginica'], dtype=object)
iris.groupby('species').size()
Output:
species
setosa 50
versicolor 50
virginica 50
dtype: int64
#iris = load_iris()
iris = load_iris()
X = iris.data[:100, :2] # Use only two features and two classes (Setosa and Versicolor)
y = iris.target[:100]
# Convert labels to -1 and 1
y = np.where(y == 0, -1, 1)
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize weights and bias
weights = np.zeros(X_train.shape[1])
bias = 0
learning_rate = 0.1
epochs = 10
# Perceptron training
for epoch in range(epochs):
for i in range(X_train.shape[0]):
linear_output = np.dot(X_train[i], weights) + bias
y_pred = np.where(linear_output > 0, 1, -1)
Output:1.0
Result:
Experiment 2: Design, train and test the MLP for tabular data and verify various
activation functions and optimizers tensorflow.
Aim: Design, train and test the MLP for tabular data and verify various activation
functions and optimizers tensorflow.
Theory:
A very common neural network architecture is to stack the neurons in layers on top of each other, called
hidden layers. Each layer has n number of neurons. Layers are connected to each other by weight
connections.
Hidden Layer
It is the heart of all Artificial neural networks. This layer comprises all computations of the neural
network. The edges of the hidden layer have weights multiplied by the node values. This layer uses the
activation function. There can be one or two hidden layers in the model. Several hidden layer nodes
should be accurate as few nodes in the hidden layer make the model unable to work efficiently with
complex data. More nodes will result in an overfitting problem.
Output Layer
This layer gives the estimated output of the Neural Network as shown in fig 7.d. The number of nodes in
the output layer depends on the type of problem. For a single targeted variable, use one node. N
classification problem, ANN uses N nodes in the output layer.
Code:
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
data=pd.read_csv("/kaggle/input/diabetes-dataset/diabetes.csv")
data
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam, SGD, RMSprop
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
results = {}
# Plotting
plt.figure(figsize=(10, 6))
for activation in activation_functions:
plt.plot([optimizer for (act, optimizer) in results.keys() if act == activation],
[results[(activation, optimizer)] for optimizer in optimizers],
label=f'{activation} activation')
Output:
Output:
Activation: relu, Optimizer: sgd, MSE: 2831.2041
To eliminate NaN (Not a Number) values in the MSE when using the ReLU activation function with the SGD
optimizer, the following changes can be made:
Reduce Learning Rate: A high learning rate can cause issues with convergence, especially with the ReLU
activation function, leading to NaN values. Lowering the learning rate for the SGD optimizer can help stabilize
training.
Initialize Weights Properly: Proper weight initialization (like He initialization) can also help prevent issues with
ReLU activation
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, BatchNormalization
from tensorflow.keras.optimizers import Adam, SGD, RMSprop, Adagrad
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
results = {}
# Plotting
plt.figure(figsize=(10, 6))
for activation in activation_functions:
plt.plot([optimizer for (act, optimizer) in results.keys() if act == activation],
[results[(activation, optimizer)] for optimizer in optimizers],
label=f'{activation} activation')
Output
Activation: relu, Optimizer: adam, MSE: 2956.9028
Activation: relu, Optimizer: sgd, MSE: 3147.7812
Activation: relu, Optimizer: rmsprop, MSE: 2907.7595
Activation: relu, Optimizer: Adagrad, MSE: 23059.0273
Activation: tanh, Optimizer: adam, MSE: 14791.1758
Activation: tanh, Optimizer: sgd, MSE: 2860.1355
Activation: tanh, Optimizer: rmsprop, MSE: 15218.6572
Activation: tanh, Optimizer: Adagrad, MSE: 25984.4414
Activation: sigmoid, Optimizer: adam, MSE: 20165.0508
Activation: sigmoid, Optimizer: sgd, MSE: 2957.6797
Activation: sigmoid, Optimizer: rmsprop, MSE: 16949.9629
Activation: sigmoid, Optimizer: Adagrad, MSE: 25664.1484
Result:
Experiment 3: Design and implement to classify 32x32 images using MLP using
tensorflow/keras and check the accuracy.
Aim: Design and implement to classify 32x32 images using MLP using tensorflow/keras
and check the accuracy.
Theory:
A very common neural network architecture is to stack the neurons in layers on top of each other, called
hidden layers. Each layer has n number of neurons. Layers are connected to each other by weight
connections.
Hidden Layer
It is the heart of all Artificial neural networks. This layer comprises all computations of the neural
network. The edges of the hidden layer have weights multiplied by the node values. This layer uses the
activation function. There can be one or two hidden layers in the model. Several hidden layer nodes
should be accurate as few nodes in the hidden layer make the model unable to work efficiently with
complex data. More nodes will result in an overfitting problem.
Output Layer
This layer gives the estimated output of the Neural Network as shown in fig 7.d. The number of nodes in
the output layer depends on the type of problem. For a single targeted variable, use one node. N
classification problem, ANN uses N nodes in the output layer.
Code:
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
# Load the CIFAR-10 dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# Normalize the images to a range of 0 to 1
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
model.summary()
OUTPUT-
# Evaluate the model
test_loss, test_accuracy = model.evaluate(x_test, y_test)
print(f'Test accuracy: {test_accuracy:.4f}')
# Plot training & validation accuracy
import matplotlib.pyplot as plt
plt.plot(history.history['accuracy'], label='train_accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
Output:
Result:
Experiment 4: Design and implement a CNN model to classify multi category JPG
images with tensorflow /keras and check accuracy. Predict labels for new images.
Aim: Design and implement a CNN model to classify multi category JPG images with tensorflow /keras
and check accuracy. Predict labels for new images.
Theory:
Code:
import tensorflow as tf
# CIFAR-10 Image Classification using CNN
# Step 1: Import Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
(x_train, y_train), (x_test, y_test) = datasets.cifar10.load_data()
print(f"x_train shape: {x_train.shape}")
print(f"y_train shape: {y_train.shape}")
print(f"x_test shape: {x_test.shape}")
print(f"y_test shape: {y_test.shape}")
Output:
x_train shape: (50000, 32, 32, 3)
y_train shape: (50000, 1)
x_test shape: (10000, 32, 32, 3)
y_test shape: (10000, 1)
plt.subplots_adjust(hspace=0.4)
plt.figure()
plt.imshow(x_train[12])
plt.colorbar()
# Step 4: Build the CNN Model
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax') # 10 output units for the 10 classes
])
# View the model summary
model.summary()
Output:
# Step 5: Compile the Model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',metrics=['accuracy'])
Output:
OUTPUT:
313/313 - 1s - 4ms/step - accuracy: 0.6379 - loss: 1.0576
# Loss plot
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.title('Loss Over Epochs')
plt.show()
OUTPUT:
import numpy as np
# Function to predict and display an image from test data
def predict_image(index):
img = x_test[index]
prediction = model.predict(np.expand_dims(img, axis=0))
predicted_class = np.argmax(prediction)
plt.imshow(img)
plt.title(f"Predicted: {class_names[predicted_class]},
Actual:↪{class_names[y_test[index][0]]}")
plt.show()
# Predict an example image from test set
predict_image(1)
OUTPUT:
layers.Flatten(),
layers.Dense(256, activation='relu'),
layers.Dense(10, activation='softmax')
])
# View the model summary
model.summary()
OUTPUT:
plt.show()
Result:
Experiment 5: : Design and implement a CNN model to classify multi category tiff
images with tensorflow /keras
Aim: Design and implement a CNN model to classify multi category tiff images with tensorflow
/keras and check the accuracy. Check whether your model is overfit / underfit / perfect fit and apply
the techniques to avoid overfit and underfit like regulizers, dropouts etc.
Code:
import os
import numpy as np
from tensorflow.keras.datasets import cifar10
from PIL import Image
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense,
Dropout
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import os
Step 2: Define Data Directories and Data Generator Assuming that the .tiff images are saved in folders like
cifar10_tiff/train and cifar10_tiff/test, with subdirectories for each class (e.g., airplane, automobile, etc.).
# Set paths
train_dir = 'cifar10_tiff/train'
test_dir = 'cifar10_tiff/test'
Step 3: Build the CNN Model Define a CNN model suitable for classifying the CIFAR-10 images.
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
MaxPooling2D((2, 2)),
Dropout(0.25),
Flatten(),
Dense(256, activation='relu'),
Dropout(0.5),
Dense(10, activation='softmax') # 10 classes for CIFAR-10
])
history =
model.fit(train_data,validation_data=validation_data,epochs=10,callbacks=[early_stopping])
Step 5: Evaluate the Model Check model performance on the test dataset and plot accuracy and loss.
# Plot loss
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.title('Training and Validation Loss')
plt.show()
OUTPUT:
Step 6: Make Predictions (Optional) You can use the trained model to make predictions on individual images
or batches of images.
# Get predictions
predictions = model.predict(test_data)
predicted_classes = tf.argmax(predictions, axis=1)
Result:
Experiment 6: Implement a CNN architecture (LeNet, Alexnet, VGG, etc) model to
classify multi category Satellite images with tensorflow / keras and check the
accuracy
Aim: Implement a CNN architecture (LeNet, Alexnet, VGG, etc) model to classify multi
category Satellite images with tensorflow / keras and check the accuracy. Check whether your
model isoverfit / underfit / perfect fit and apply the techniques to avoid overfit and underfit.
Theory:
In 1998, Lecun et al. introduced a pioneering CNN called LeNet-5. The architecture is composed of five
weight layers, and hence the name LeNet-5: three convolutional layers and two fully connected layers.
We refer to the convolutional and fully connected layers as weight layers because they contain trainable
weights as opposed to pooling layers that don’t contain any weights.
LeNet architecture:
AlexNet was the winner of the ILSVRC image classification competition in 2012. Krizhevsky et al. created
the neural network architecture and trained it on 1.2 million high-resolution images into 1,000 different
classes of the ImageNet dataset. AlexNet has a lot of similarities to LeNet but is much deeper (more
hidden layers) and bigger (more filters per layer). They have similar building blocks: a series of
convolutional and pooling layers stacked on top of each other followed by fully connected layers and a
softmax. We’ve seen that LeNet has around 61,000 parameters, whereas AlexNet has about 60 million
parameters and 650,000 neurons, which gives it a larger learning capacity to understand more complex
features.
Code:
import os
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers, models
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
# Define paths
data_folder_path = 'C:\\Users\\Gove\\Desktop\\archive (1) (1)\\data'
categories = ['cloudy', 'desert', 'green_area', 'water']
output:
def create_lenet():
model = models.Sequential()
# Define the input shape explicitly using Input
model.add(Input(shape=(IMG_SIZE[0], IMG_SIZE[1], 3)))
def create_alexnet():
model = models.Sequential()
# First convolutional layer
model.add(layers.Conv2D(96, (11, 11), strides=4, activation='relu',
input_shape=(IMG_SIZE[0], IMG_SIZE[1], 3)))
model.add(layers.MaxPooling2D(pool_size=(2, 2), strides=2)) #
Reduced pool size
return model
output:
Epoch 10/10
# Evaluate LeNet
lenet_test_loss, lenet_test_acc = lenet_model.evaluate(X_test, y_test)
print(f"LeNet Test Accuracy: {lenet_test_acc:.4f}")
# Evaluate AlexNet
alexnet_test_loss, alexnet_test_acc = alexnet_model.evaluate(X_test,
y_test)
print(f"AlexNet Test Accuracy: {alexnet_test_acc:.4f}")
output:
def plot_history(history, title):
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.title(title)
plt.legend(loc='lower right')
plt.show()
plot_history(lenet_history, "LeNet Training")
plot_history(alexnet_history, "AlexNet Training")
Result:
Experiment 7: Implement ResNet model to classify multi category medical images
with tensorflow / keras and check the accuracy
Aim: Implement ResNet model to classify multi category medical images with tensorflow /
keras and check the accuracy. Check whether your model is overfit / underfit / perfect fit and
apply the techniques to avoid overfit and underfit.
Theory:
The Residual Neural Network (ResNet) was developed in 2015 by a group from the Microsoft Research
team. They introduced a novel residual module architecture with skip connections. The network also
features heavy batch normalization for the hidden layers. This technique allowed the team to train very
deep neural networks with 50, 101, and 152 weight layers while still having lower complexity than
smaller networks like VGGNet (19 layers)
Code:
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
# Install necessary libraries
!pip install tensorflow matplotlib
# Import libraries
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
from tensorflow.keras.layers import GlobalAveragePooling2D, Dropout, Dense
import matplotlib.pyplot as plt
# Define ResNet-18
def ResNet18(input_shape=(224, 224, 3), num_classes=2):
base_model = tf.keras.applications.ResNet50(
include_top=False,
weights='imagenet',
input_shape=input_shape
)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(0.5)(x)
outputs = Dense(num_classes, activation='softmax')(x)
model = models.Model(inputs=base_model.input, outputs=outputs)
return model
# Load dataset
data_gen = ImageDataGenerator(
rescale=1.0 / 255,
rotation_range=15,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1,
shear_range=0.1,
horizontal_flip=True,
validation_split=0.2
)
train_gen = data_gen.flow_from_directory(
'/kaggle/input/chest-xray-pneumonia/chest_xray/train',
target_size=(224, 224),
batch_size=32,
class_mode='categorical',
subset='training'
)
val_gen = data_gen.flow_from_directory(
'/kaggle/input/chest-xray-pneumonia/chest_xray/train',
target_size=(224, 224),
batch_size=32,
class_mode='categorical',
subset='validation'
)
test_gen = data_gen.flow_from_directory(
'/kaggle/input/chest-xray-pneumonia/chest_xray/test',
target_size=(224, 224),
batch_size=32,
class_mode='categorical'
)
model.compile(
optimizer=Adam(learning_rate=1e-4),
loss='categorical_crossentropy',
metrics=['accuracy']
)
history = model.fit(
train_gen,
validation_data=val_gen,
epochs=7,
callbacks=callbacks
)
plot_history(history)
Output:
Result:
Experiment 8: Implement an image classification model using transfer learning
techniques and check accuracy
Aim: Implement an image classification model using transfer learning techniques and check
accuracy.Tune the required hyperparameters.
Theory:
Transfer learning is the transfer of the knowledge (feature maps) that the network has acquired from one
task, where we have a large amount of data, to a new task where data is not abundantly available.
• It is generally used where a neural network model is first trained on a problem similar to the
problem that is being solved.
• One or more layers from the trained model are then used in a new model trained on the problem of
interest.
• In transfer learning, we first train a base network on a base dataset and task, and then we
repurpose the learned features, or transfer them to a second target network to be trained on a
target dataset and task. This process will tend to work if the features are general, meaning
suitable to both base and target tasks, instead of specific to the base task.
Code:
import tensorflow as tf
from tensorflow.keras import layers, models, optimizers
from tensorflow.keras.applications import VGG16
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.preprocessing import image
from tensorflow.keras.utils import to_categorical
import numpy as np
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
# Convert labels to one-hot encoding
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
base_model = VGG16(weights='imagenet', include_top=False,
input_shape=(32, 32,3))
# Freeze the layers of the pre-trained model to avoid retraining them
base_model.trainable = False
model = models.Sequential([
base_model, # Add the pre-trained base VGG16 model
layers.Flatten(),
layers.Dense(256, activation='relu'),
layers.Dropout(0.5), # Dropout to avoid overfitting
layers.Dense(10, activation='softmax') # 10 output classes for CIFAR-10
])
model.compile(optimizer=optimizers.Adam(learning_rate=0.0001), #
Small␣learning rate
loss='categorical_crossentropy',
metrics=['accuracy'])
Output:
Epoch 25/25
391/391 ━━━━━━━━━━━━━━━━━━━━ 10s 17ms/step - accuracy: 0.5940 - loss: 1.1730 -
val_accuracy: 0.5885 - val_loss: 1.1709
output
output:
Result:
Experiment 9: Implement R-CNN model for object detection. Check with Fast and
Faster R-CNN models.
Aim: Implement R-CNN model for object detection. Check with Fast and Faster R-CNN models.
Software Required: Google Co Lab, Jupyter notebook, Kaggle .
Theory:
R-CNN (Region-based Convolutional Neural Network) is a deep learning framework for object
detection that combines region proposal methods with convolutional neural networks. It works by
first using selective search to generate potential object regions (region proposals) from an input
image. Each proposal is then resized and passed through a CNN to extract features, which are
subsequently fed into a classifier (such as SVM) to identify the object category. Additionally, a
regression model refines the bounding box coordinates for more accurate localization. While
effective, R-CNN is computationally expensive due to its multi-step process, requiring separate
training for the CNN, classifier, and regression model, as well as extensive computation for every
region proposal.
Code:
# Extract predictions
boxes = predictions[0]['boxes'] # Bounding boxes for detected objects
labels = predictions[0]['labels'] # Class labels for detected objects
scores = predictions[0]['scores'] # Confidence scores for detected
objects
OUTPUT:
# Install the necessary libraries
!pip install torch torchvision
# Extract predictions
boxes = predictions[0]['boxes'] # Bounding boxes for detected objects
labels = predictions[0]['labels'] # Class labels for detected objects
scores = predictions[0]['scores'] # Confidence scores for detected
objects
Result:
Experiment 10: Implement a model to mask various categories with Semantic
Segmentation
Theory:
Semantic segmentation is a computer vision technique that uses deep learning to label each pixel in
an image with a class:
How it works: Semantic segmentation uses image classification models to label pixels based on their
semantic features, such as color, placement, or contrast. The result is a colorized map of the image,
where each pixel color represents a different class label.
Code:
import numpy as np
import tensorflow as tf
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.datasets import cifar10
# Downsampling
c1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
p1 = MaxPooling2D((2, 2))(c1)
# Bottleneck
c3 = Conv2D(128, (3, 3), activation='relu', padding='same')(p2)
# Upsampling
u1 = UpSampling2D((2, 2))(c3)
m1 = concatenate([u1, c2])
c4 = Conv2D(64, (3, 3), activation='relu', padding='same')(m1)
u2 = UpSampling2D((2, 2))(c4)
m2 = concatenate([u2, c1])
c5 = Conv2D(32, (3, 3), activation='relu', padding='same')(m2)
OUTPUT:
Epoch 10/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 8s 5ms/step - accuracy:
0.9981 - loss: 0.0060 - val_accuracy: 0.9990 - val_loss: 0.0049
pred = model.predict(x_test[:5])
plt.subplot(1, 3, 2)
plt.title("Ground Truth Mask")
plt.imshow(y_test_seg[i].squeeze(), cmap='gray')
plt.subplot(1, 3, 3)
plt.title("Predicted Mask")
plt.imshow(pred[i].squeeze(), cmap='gray')
plt.show()
OUTPUT:
Result:
Experiment 11: Implement an Autoencoder to de-noise image.
Theory:
Autoencoders are a specialized class of algorithms that can learn efficient representations of input
data with no need for labels. It is a class of artificial neural networks designed for unsupervised
learning. Learning to compress and effectively represent input data without specific labels is the
essential principle of an automatic decoder. This is accomplished using a two-fold structure that
consists of an encoder and a decoder. The encoder transforms the input data into a reduced-
dimensional representation, which is often referred to as “latent space” or “encoding”. From that
representation, a decoder rebuilds the initial input. For the network to gain meaningful patterns in
data, a process of encoding and decoding facilitates the definition of essential features.
Code:
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv2D, Conv2DTranspose,
MaxPooling2D, UpSampling2D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
# Decoder
x = Conv2DTranspose(128, (3, 3), activation='relu',
padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2DTranspose(64, (3, 3), activation='relu',
padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2DTranspose(1, (3, 3), activation='sigmoid',
padding='same')(x)
return autoencoder
# Visualize results
def visualize_denoising_results(noisy_images, denoised_images,
clean_images, num_images=10):
plt.figure(figsize=(20, 4))
for i in range(num_images):
# Noisy image
ax = plt.subplot(3, num_images, i + 1)
plt.imshow(noisy_images[i].reshape(28, 28), cmap="gray")
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
if i == 0:
ax.set_title('Noisy Images')
# Denoised image
ax = plt.subplot(3, num_images, i + 1 + num_images)
plt.imshow(denoised_images[i].reshape(28, 28), cmap="gray")
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
if i == 0:
ax.set_title('Denoised Images')
# Original image
ax = plt.subplot(3, num_images, i + 1 + num_images * 2)
plt.imshow(clean_images[i].reshape(28, 28), cmap="gray")
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
if i == 0:
ax.set_title('Original Images')
plt.show()
Output:
Result:
Experiment 12: Implement a GAN application to convert images.
Theory: Generative Adversarial Networks (GANs) are a powerful class of neural networks that are
used for an unsupervised learning. GANs are made up of two neural networks, a discriminator
and a generator. Generative Adversarial Networks (GANs) can be broken down into three parts:
• Generative: To learn a generative model, which describes how data is generated in terms of a
probabilistic model.
• Adversarial: The word adversarial refers to setting one thing up against another. This means
that, in the context of GANs, the generative result is compared with the actual images in the
data set. A mechanism known as a discriminator is used to apply a model that attempts to
distinguish between real and fake images.
• Networks: Use deep neural networks as artificial intelligence (AI) algorithms for training
purposes.
Code:
import tensorflow as tf
from tensorflow.keras import layers
import matplotlib.pyplot as plt
# Build Generator
def build_generator():
model = tf.keras.Sequential([
layers.Input(shape=(32, 32, 1)),
layers.Conv2D(64, kernel_size=4, strides=2, padding="same"),
layers.LeakyReLU(),
layers.Conv2D(128, kernel_size=4, strides=2, padding="same"),
layers.BatchNormalization(),
layers.LeakyReLU(),
layers.Conv2DTranspose(128, kernel_size=4, strides=2,
padding="same"),
layers.BatchNormalization(),
layers.ReLU(),
layers.Conv2DTranspose(3, kernel_size=4, strides=2,
padding="same", activation='tanh')
])
return model
# Build Discriminator
def build_discriminator():
model = tf.keras.Sequential([
layers.Input(shape=(32, 32, 3)),
layers.Conv2D(64, kernel_size=4, strides=2, padding="same"),
layers.LeakyReLU(),
layers.Conv2D(128, kernel_size=4, strides=2, padding="same"),
layers.BatchNormalization(),
layers.LeakyReLU(),
layers.Flatten(),
layers.Dense(1, activation='sigmoid')
])
return model
# Loss Functions
def discriminator_loss(real_output, fake_output):
real_loss = tf.keras.losses.BinaryCrossentropy(from_logits=True)(
tf.ones_like(real_output), real_output)
fake_loss = tf.keras.losses.BinaryCrossentropy(from_logits=True)(
tf.zeros_like(fake_output), fake_output)
return real_loss + fake_loss
def generator_loss(fake_output):
return tf.keras.losses.BinaryCrossentropy(from_logits=True)(
tf.ones_like(fake_output), fake_output)
# Optimizers
generator_optimizer = tf.keras.optimizers.Adam(1e-4)
discriminator_optimizer = tf.keras.optimizers.Adam(1e-4)
# Models
generator = build_generator()
discriminator = build_discriminator()
# Training Step
@tf.function
def train_step(real_images, gray_images):
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
generated_images = generator(gray_images, training=True)
real_output = discriminator(real_images, training=True)
fake_output = discriminator(generated_images, training=True)
gen_loss = generator_loss(fake_output)
disc_loss = discriminator_loss(real_output, fake_output)
gradients_of_generator = gen_tape.gradient(gen_loss,
generator.trainable_variables)
gradients_of_discriminator = disc_tape.gradient(disc_loss,
discriminator.trainable_variables)
generator_optimizer.apply_gradients(zip(gradients_of_generator,
generator.trainable_variables))
discriminator_optimizer.apply_gradients(zip(gradients_of_discrimina
tor, discriminator.trainable_variables))
return gen_loss, disc_loss
# Training Function
def train(dataset, epochs):
for epoch in range(epochs):
for real_images, gray_images in dataset:
gen_loss, disc_loss = train_step(real_images, gray_images)
print(f"Epoch {epoch+1}, Gen Loss: {gen_loss.numpy()}, Disc
Loss: {disc_loss.numpy()}")
# Dataset Preparation
(x_train, _), (_, _) = tf.keras.datasets.cifar10.load_data()
x_train = x_train.astype('float32') / 127.5 - 1
x_train_gray = tf.image.rgb_to_grayscale(x_train)
BATCH_SIZE = 64
BUFFER_SIZE = 10000
dataset = tf.data.Dataset.from_tensor_slices((x_train, x_train_gray))
dataset = dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE)
# Visualization
def generate_and_show(generator, gray_images, real_images):
generated_images = generator(gray_images, training=False)
plt.figure(figsize=(10, 5))
for i in range(5):
# Grayscale input
plt.subplot(3, 5, i+1)
plt.imshow(tf.squeeze(gray_images[i]) * 0.5 + 0.5, cmap='gray')
plt.axis('off')
# Generated image
plt.subplot(3, 5, i+6)
plt.imshow((generated_images[i] * 0.5 + 0.5).numpy())
plt.axis('off')
# Real image
plt.subplot(3, 5, i+11)
plt.imshow((real_images[i] * 0.5 + 0.5).numpy())
plt.axis('off')
plt.show()
Result: