1.
Demonstration and implementation of Shallow architecture using Python,
keyboard_arrow_down TensorFlow and Keras
i) Google Colaboratory - Cloning GitHub repository, Upload Data, Importing Kaggle's dataset, Basic
File operations
ii) Implementing Perceptron
Perceptrons were one of the first algorithms discovered in the field of AI. Its big significance was that it raised the hopes and expectations for
the field of neural networks. It is a machine learning algorithm that uses a single node or neuron to predict a class label for a row of data. It is a
type of neural network model and is considered one of the simplest types.
The Perceptron algorithm consists of four main components:
Input values
Weights and bias
Net sum
Activation function
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
keyboard_arrow_down Creating the dataset
Here we generate a simulated dataset using scikit-learn's make_classification function. This creates a dataset with 2 input features and
binary target labels.
features, targets = make_classification(n_samples = 20, n_features = 2, n_informative = 1, n_redundant = 0, n_clusters_per_class = 1, ra
df = pd.DataFrame(data=features, columns=['x1', 'x2'])
df['targets'] = targets
df.head()
x1 x2 targets
0 -0.887629 0.784959 1
1 -0.012665 1.141704 1
2 -0.191836 0.984424 1
3 -0.267888 -0.330421 0
4 -0.935769 -1.883225 0
Next steps: Generate code with df
toggle_off View recommended plots
features.shape
(20, 2)
targets.shape
(20,)
np.bincount(targets)
array([10, 10])
keyboard_arrow_down Visualizing the dataset
We can visualize the dataset by plotting the two input features colored by the target class. This gives us a sense of how linearly separable the
data is. We can see there is an approximate linear decision boundary that separates the two classes.
plt.plot(
features[targets == 0, 0],
features[targets == 0, 1],
marker = 'P',
markersize = 10,
linestyle = '',
label = 'Class 0'
plt.plot(
features[targets == 1, 0],
features[targets == 1, 1],
marker = '^',
markersize = 10,
linestyle = '',
label = 'Class 1'
)
plt.legend(loc = 2)
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.xlabel("Feature $x_1$", fontsize=12)
plt.ylabel("Feature $x_2$", fontsize=12)
plt.grid()
plt.show()
keyboard_arrow_down Implementing a Perceptron
We can now implement the perceptron algorithm in Python. The perceptron contains weights and bias parameters that can be updated during
training.
class Perceptron:
def __init__(self, num_features):
self.num_features = num_features
self.weights = [0.0 for _ in range(num_features)]
self.bias = 0
ppn = Perceptron(num_features = 2)
ppn.weights
[0.0, 0.0]
ppn.bias
Double-click (or enter) to edit
keyboard_arrow_down Implementing the forward function
The forward pass computes the weighted sum of the inputs and bias. An activation function thresholds this sum to produce a binary 0/1
prediction
class Perceptron:
def __init__(self, num_features):
self.num_features = num_features
self.weights = [0.0 for _ in range(num_features)]
self.bias = 0
def forward(self, x):
weighted_sum_z = self.bias
for i, _ in enumerate(self.weights):
weighted_sum_z += x[i] * self.weights[i]
if weighted_sum_z > 0:
prediction = 1
else:
prediction = 0
return prediction
import numpy as np
class Perceptron:
def __init__(self, num_features):
self.num_features = num_features
self.weights = [0.0 for _ in range(num_features)]
self.bias = 0
def forward(self, x):
weighted_sum_z = self.bias
for i, _ in enumerate(self.weights):
weighted_sum_z += x[i] * self.weights[i]
if weighted_sum_z > 0:
prediction = 1
else:
prediction = 0
return prediction
def train(self, X, y, epochs=1000, learning_rate=0.1):
for epoch in range(epochs):
total_error = 0
for i in range(len(X)):
prediction = self.forward(X[i])
error = y[i] - prediction
total_error += error
# Update weights and bias
self.weights = [w + learning_rate * error * x_i for w, x_i in zip(self.weights, X[i])]
self.bias += learning_rate * error
# Calculate accuracy
accuracy = self.evaluate(X, y)
print(f"Epoch {epoch + 1}/{epochs}, Total Error: {total_error}, Accuracy: {accuracy}")
def evaluate(self, X, y):
correct_predictions = 0
for i in range(len(X)):
prediction = self.forward(X[i])
if prediction == y[i]:
correct_predictions += 1
accuracy = correct_predictions / len(X)
return accuracy
# Sample data
X_perceptron = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y_perceptron = np.array([0, 1, 1, 1])
# Create and train Perceptron model
perceptron_model = Perceptron(num_features=2)
perceptron_model.train(X_perceptron, y_perceptron, epochs=10, learning_rate=0.1)
# Evaluate the model
accuracy_perceptron = perceptron_model.evaluate(X_perceptron, y_perceptron)
print(f"Perceptron Accuracy: {accuracy perceptron}")
print(f Perceptron Accuracy: {accuracy_perceptron} )
output Epoch 1/10, Total Error: 1, Accuracy: 0.75
Epoch 2/10, Total Error: 0, Accuracy: 0.75
Epoch 3/10, Total Error: -1, Accuracy: 1.0
Epoch 4/10, Total Error: 0, Accuracy: 1.0
Epoch 5/10, Total Error: 0, Accuracy: 1.0
Epoch 6/10, Total Error: 0, Accuracy: 1.0
Epoch 7/10, Total Error: 0, Accuracy: 1.0
Epoch 8/10, Total Error: 0, Accuracy: 1.0
Epoch 9/10, Total Error: 0, Accuracy: 1.0
Epoch 10/10, Total Error: 0, Accuracy: 1.0
Perceptron Accuracy: 1.0
iii) Digit Classification: Neural network to classify MNIST dataset
In this notebook, we create a Multilayer Perceptron (MLP) model of the MNIST dataset.
Multilayer Perceptrons (MLPs) usually mean fully connected networks, that is, each neuron in one layer is connected to all neurons in the
next layer. The "fully-connectedness" of these networks makes them prone to overfitting the data.
These MLP models are also referred to as either deep feedforward networks or feedforward neural networks. MLPs are common in
simple logistic and linear regression problems.
So, the objective is to create a neural network for identifying numbers based on handwritten digits. For example, when the input to the
network is an image of a handwritten number 8, the corresponding prediction must also be the digit 8.
To both train and validate a neural network, there must be a sufficiently large dataset of handwritten digits.
The Modified National Institute of Standards and Technology dataset or MNIST dataset for short, is often considered as the Hello World!
of deep learning and is a suitable dataset for handwritten digit classification.
MNIST is used to explain and validate deep learning theories because the 70,000 samples it contains are small, yet sufficiently rich in
information (MNIST dataset is described later).
keyboard_arrow_down 2. Import necessary libraries
# This Python 3 environment comes with many helpful analytics libraries installed
# For example, here's several helpful packages to load in
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt # plotting library
%matplotlib inline
from keras.models import Sequential
from keras.layers import Dense , Activation, Dropout
from keras.optimizers import Adam ,RMSprop
from keras import backend as K
# Any results you write to the current directory are saved as output.
keyboard_arrow_down MNIST dataset
MNIST is a collection of handwritten digits ranging from the number 0 to 9.
It has a training set of 60,000 images, and 10,000 test images that are classified into corresponding categories or labels.
To use the MNIST dataset in Keras, an API is provided to download and extract images and labels automatically.
The following Keras code shows how to access MNIST dataset, plot 25 random samples, and count the number of labels for train and test
datasets.
# import dataset
from keras.datasets import mnist
# load dataset
(x_train, y_train),(x_test, y_test) = mnist.load_data()
# count the number of unique train labels
unique, counts = np.unique(y_train, return_counts=True)
print("Train labels: ", dict(zip(unique, counts)))
# count the number of unique test labels
unique, counts = np.unique(y_test, return_counts=True)
print("\nTest labels: ", dict(zip(unique, counts)))
Train labels: {0: 5923, 1: 6742, 2: 5958, 3: 6131, 4: 5842, 5: 5421, 6: 5918, 7: 6265, 8: 5851, 9: 5949}
Test labels: {0: 980, 1: 1135, 2: 1032, 3: 1010, 4: 982, 5: 892, 6: 958, 7: 1028, 8: 974, 9: 1009}
keyboard_arrow_down Data visualization
The following code will help to sample the 25 random MNIST digits and visualize them.
indexes = np.random.randint(0, x_train.shape[0], size=25)
images = x_train[indexes]
labels = y_train[indexes]
# plot the 25 mnist digits
plt.figure(figsize=(5,5))
for i in range(len(indexes)):
plt.subplot(5, 5, i + 1)
image = images[i]
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.show()
plt.savefig("mnist-samples.png")
plt.close('all')
2. Basic implementation of a deep Learning models in PyTorch and Tensor Flow. Tune
keyboard_arrow_down its performance by adding additional layers provided by the library
keyboard_arrow_down Basic Deep Learning Model in PyTorch:
import torch
import torch.nn as nn
import torch.optim as optim
# Define a simple neural network class
class SimpleNet(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(SimpleNet, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, output_size)
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
return x
# Sample data
input_size = 10
hidden_size = 20
output_size = 5
batch_size = 32
epochs = 10 # Define the number of epochs
# Generate random input and labels (for demonstration purposes)
inputs = torch.randn(batch_size, input_size)
labels = torch.randint(0, output_size, (batch_size,))
# Create an instance of the model
model_pytorch = SimpleNet(input_size, hidden_size, output_size)
# Define loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model_pytorch.parameters(), lr=0.01)
# Training loop
for epoch in range(epochs):
# Forward pass
outputs = model_pytorch(inputs)
loss = criterion(outputs, labels)
# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Save or use the trained model for inference
keyboard_arrow_down Enhancing PyTorch Model by Adding Layers:
# Enhanced PyTorch Model
class EnhancedNet(nn.Module):
def __init__(self, input_size, hidden_size1, hidden_size2, output_size):
super(EnhancedNet, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size1)
self.relu1 = nn.ReLU()
self.fc2 = nn.Linear(hidden_size1, hidden_size2)
self.relu2 = nn.ReLU()
self.fc3 = nn.Linear(hidden_size2, output_size)
def forward(self, x):
x = self.fc1(x)
x = self.relu1(x)
x = self.fc2(x)
x = self.relu2(x)
x = self.fc3(x)
return x
# Create an instance of the enhanced model
model_pytorch_enhanced = EnhancedNet(input_size, 50, 30, output_size)
# Training loop (similar to the previous one)
keyboard_arrow_down Basic Deep Learning Model in TensorFlow:
# Save the trained model
model_tf.save("my_model.h5")
# Load the saved model for inference
loaded_model = tf.keras.models.load_model("my_model.h5")
# Example of using the loaded model for inference (replace with your actual input)
inference_input = tf.random.normal((1, input_size))
inference_result = loaded_model.predict(inference_input)
print("Inference Result:", inference_result)
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file vi
saving_api.save_model(
1/1 [==============================] - 0s 201ms/step
Inference Result: [[0.06069971 0.07249957 0.09608455 0.10964236 0.66107386]]
keyboard_arrow_down Enhancing TensorFlow Model by Adding Layers:
# Save the trained enhanced model
model_tf_enhanced.save("my_enhanced_model.h5")
# Load the saved enhanced model for inference
loaded_enhanced_model = tf.keras.models.load_model("my_enhanced_model.h5")
# Example of using the loaded enhanced model for inference (replace with your actual input)
inference_input = tf.random.normal((1, input_size))
inference_result = loaded_enhanced_model.predict(inference_input)
print("Inference Result (Enhanced Model):", inference_result)
1/1 [==============================] - 0s 46ms/step
Inference Result (Enhanced Model): [[0.17575717 0.29464447 0.15700005 0.24500619 0.12759209]]
3.mplement custom operations in PyTorch by using deep learning via gradient
keyboard_arrow_down descent; recursive chain rule (backpropagation); bias-variance tradeoff, regularization;
output units: linear, softmax; hidden units: tanh, RELU
let's create a simple custom operation using the recursive chain rule for backpropagation, involving a hidden layer with Tanh activation and a
linear output layer. We'll also discuss bias-variance tradeoff and regularization
import torch
import torch.nn as nn
import torch.optim as optim
# Custom Operation: Recursive Chain Rule
class CustomOperationFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input):
ctx.save_for_backward(input)
return input.tanh()
@staticmethod
def backward(ctx, grad_output):
input, = ctx.saved_tensors
grad_input = grad_output * (1 - input.tanh()**2)
return grad_input
# Custom Neural Network Model
class CustomNet(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(CustomNet, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.activation = CustomOperationFunction.apply
self.fc2 = nn.Linear(hidden_size, output_size)
def forward(self, x):
x = self.fc1(x)
x = self.activation(x)
x = self.fc2(x)
return x
# Sample data
input_size = 10
hidden_size = 20
output_size = 5
batch_size = 32
# Create an instance of the custom model
model_pytorch_custom = CustomNet(input_size, hidden_size, output_size)
# Define loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model_pytorch_custom.parameters(), lr=0.01)
# Training loop
epochs = 10
for epoch in range(epochs):
# Forward pass
outputs = model_pytorch_custom(inputs)
loss = criterion(outputs, labels)
# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Print loss for monitoring training progress
print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item()}')
# Save or use the trained model for inference
Epoch [1/10], Loss: 1.566769003868103
Epoch [2/10], Loss: 1.5645911693572998
Epoch [3/10], Loss: 1.5624265670776367
Epoch [4/10], Loss: 1.560274600982666
Epoch [5/10], Loss: 1.5581356287002563
Epoch [6/10], Loss: 1.5560091733932495
Epoch [7/10], Loss: 1.5538952350616455
Epoch [8/10], Loss: 1.5517940521240234
Epoch [9/10], Loss: 1.549704909324646
Epoch [10/10], Loss: 1.5476276874542236
The model will have a hidden layer with ReLU activation, and we'll discuss bias-variance tradeoff and regularization. Additionally, the output
layer will use softmax activation.
import torch
import torch.nn as nn
import torch.optim as optim
# Custom Operation: Recursive Chain Rule with ReLU Activation
class CustomOperationFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input):
ctx.save_for_backward(input)
return input.relu()
@staticmethod
def backward(ctx, grad_output):
input, = ctx.saved_tensors
grad_input = grad_output * (input > 0).float()
return grad_input
# Custom Neural Network Model with ReLU Activation
class CustomNet(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(CustomNet, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.activation = CustomOperationFunction.apply
self.fc2 = nn.Linear(hidden_size, output_size)
def forward(self, x):
x = self.fc1(x)
x = self.activation(x)
x = self.fc2(x)
return x
# Sample data
input_size = 10
hidden_size = 20
output_size = 5
batch_size = 32
# Create an instance of the custom model
model_pytorch_custom = CustomNet(input_size, hidden_size, output_size)
# Define loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model_pytorch_custom.parameters(), lr=0.01)
# Training loop
epochs = 10
for epoch in range(epochs):
# Forward pass
outputs = model_pytorch_custom(inputs)
loss = criterion(outputs, labels)
# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Print loss for monitoring training progress
print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item()}')
# Save or use the trained model for inference
Epoch [1/10], Loss: 1.6587367057800293
Epoch [2/10], Loss: 1.6560542583465576
Epoch [3/10], Loss: 1.6533976793289185
Epoch [4/10], Loss: 1.6507666110992432
Epoch [5/10], Loss: 1.6481602191925049
Epoch [6/10], Loss: 1.6455782651901245
Epoch [7/10], Loss: 1.6430206298828125
Epoch [8/10], Loss: 1.640486240386963
Epoch [9/10], Loss: 1.6379752159118652
Epoch [10/10], Loss: 1.6354867219924927
The model will have a hidden layer with Tanh activation, and we'll discuss bias-variance tradeoff and regularization. Additionally, the output layer
will use softmax activation.
import torch
import torch.nn as nn
import torch.optim as optim
# Custom Operation: Recursive Chain Rule with Tanh Activation
class CustomOperationFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input):
ctx.save_for_backward(input)
return input.tanh()
@staticmethod
def backward(ctx, grad_output):
input, = ctx.saved_tensors
grad_input = grad_output * (1 - input.tanh()**2)
return grad_input
# Custom Neural Network Model with Tanh Activation
class CustomNet(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(CustomNet, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.activation = CustomOperationFunction.apply
self.fc2 = nn.Linear(hidden_size, output_size)
def forward(self, x):
x = self.fc1(x)
x = self.activation(x)
x = self.fc2(x)
return x
# Sample data
input_size = 10
hidden_size = 20
output_size = 5
batch_size = 32
# Create an instance of the custom model
model_pytorch_custom = CustomNet(input_size, hidden_size, output_size)
# Define loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model_pytorch_custom.parameters(), lr=0.01)
# Training loop
epochs = 10
for epoch in range(epochs):
# Forward pass
outputs = model_pytorch_custom(inputs)
loss = criterion(outputs, labels)
# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Print loss for monitoring training progress
print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item()}')
# Save or use the trained model for inference
Epoch [1/10], Loss: 1.6401724815368652
Epoch [2/10], Loss: 1.6379419565200806
Epoch [3/10], Loss: 1.6357260942459106
Epoch [4/10], Loss: 1.6335244178771973
Epoch [5/10], Loss: 1.6313371658325195
Epoch [6/10], Loss: 1.6291640996932983
Epoch [7/10], Loss: 1.6270047426223755
Epoch [8/10], Loss: 1.624859094619751
Epoch [9/10], Loss: 1.6227271556854248
Epoch [10/10], Loss: 1.6206086874008179
The model will have a hidden layer with ReLU activation, and we'll discuss bias-variance tradeoff and regularization. The output layer will use a
linear activation
import torch
import torch.nn as nn
import torch.optim as optim
# Custom Operation: Recursive Chain Rule with ReLU Activation
class CustomOperationFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input):
ctx.save_for_backward(input)
return input.relu()
@staticmethod
def backward(ctx, grad_output):
input, = ctx.saved_tensors
grad_input = grad_output * (input > 0).float()
return grad_input
# Custom Neural Network Model with ReLU Activation
class CustomNet(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(CustomNet, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.activation = CustomOperationFunction.apply
self.fc2 = nn.Linear(hidden_size, output_size)
def forward(self, x):
x = self.fc1(x)
x = self.activation(x)
x = self.fc2(x)
return x
# Sample data
input_size = 10
hidden_size = 20
output_size = 5
batch_size = 32
# Create an instance of the custom model
model_pytorch_custom = CustomNet(input_size, hidden_size, output_size)
# Define loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model_pytorch_custom.parameters(), lr=0.01)
# Assuming you have your actual input data and labels
# Replace this with your data
inputs = torch.randn(batch_size, input_size)
labels = torch.randint(0, output_size, (batch_size,))
# Training loop
epochs = 10
for epoch in range(epochs):
# Forward pass
outputs = model_pytorch_custom(inputs)
loss = criterion(outputs, labels)
# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Print loss for monitoring training progress
print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item()}')
# Save or use the trained model for inference
Epoch [1/10], Loss: 1.6439942121505737
Epoch [2/10], Loss: 1.642916202545166
Epoch [3/10], Loss: 1.641843557357788
Epoch [4/10], Loss: 1.6407759189605713
Epoch [5/10], Loss: 1.639709234237671
Epoch [6/10], Loss: 1.638649582862854
Epoch [7/10], Loss: 1.6375948190689087
Epoch [8/10], Loss: 1.6365455389022827
Epoch [9/10], Loss: 1.6355013847351074
Epoch [10/10], Loss: 1.6344619989395142
keyboard_arrow_down 4.Implement a simple CNN starting from filtering, Convolution and pooling operations
and arithmetic of these with Visualization in PyTorch and Tensorflow.
PyTorch Implementation:
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
# Load and preprocess the CIFAR-10 dataset
transform = transforms.Compose([
transforms.ToTensor(),
])
train_dataset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=4, shuffle=True)
# Define a simple CNN model
class SimpleCNN(nn.Module):
def __init__(self):
super(SimpleCNN, self).__init__()
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1)
self.relu = nn.ReLU()
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
def forward(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.pool(x)
return x
# Instantiate the model
model_pytorch = SimpleCNN()
# Visualize some intermediate results
data_iter = iter(train_loader)
images, _ = data_iter.__next__()
# Plot original image
plt.imshow(torchvision.utils.make_grid(images).numpy().transpose(1, 2, 0))
plt.title('Original Image')
plt.show()
# Plot a specific channel from the filtered and pooled image
output_pytorch = model_pytorch(images)
channel_to_visualize = 0 # Adjust this value based on the number of channels in your output
channel_image = output_pytorch[0, channel_to_visualize].detach().numpy()
plt.imshow(channel_image, cmap='gray') # Assuming a grayscale channel
plt.title(f'Filtered and Pooled Channel {channel_to_visualize} (PyTorch)')
plt.show()
Files already downloaded and verified
TensorFlow Implementation:
import tensorflow as tf
from tensorflow.keras import layers, models, datasets
import matplotlib.pyplot as plt
# Load and preprocess the CIFAR-10 dataset
(train_images, _), (_, _) = datasets.cifar10.load_data()
train_images = train_images.astype('float32') / 255.0
# Define a simple CNN model
model_tf = models.Sequential([
layers.Conv2D(16, (3, 3), padding='same', input_shape=(32, 32, 3)),
layers.Activation('relu'),
layers.MaxPooling2D(pool_size=(2, 2)),
])
# Visualize some intermediate results
# Plot original image
plt.imshow(tf.keras.preprocessing.image.array_to_img(train_images[0]))
plt.title('Original Image')
plt.show()
# Plot the first channel of the filtered and pooled image
output_tf = model_tf.predict(tf.expand_dims(train_images[0], axis=0))
channel_to_visualize = 0 # Adjust this value based on the number of channels in your output
channel_image = output_tf[0, :, :, channel_to_visualize]
plt.imshow(channel_image, cmap='gray') # Assuming a grayscale channel
plt.title(f'Filtered and Pooled Channel {channel_to_visualize} (TensorFlow)')
plt.show()
1/1 [==============================] - 0s 80ms/step
ConvNet Architectures: Implement a famous convNet architectures - AlexNet, ZFNet,
keyboard_arrow_down VGG, C3D, GoogLeNet, ResNet, MobileNet-v1.
implementation of AlexNet using TensorFlow.
import tensorflow as tf
from tensorflow.keras import layers, models
def alexnet_model(input_shape=(224, 224, 3), num_classes=1000):
model = models.Sequential()
# Layer 1
model.add(layers.Conv2D(96, (11, 11), strides=(4, 4), activation='relu', input_shape=input_shape))
model.add(layers.MaxPooling2D((3, 3), strides=(2, 2)))
# Layer 2
model.add(layers.Conv2D(256, (5, 5), padding='same', activation='relu'))
model.add(layers.MaxPooling2D((3, 3), strides=(2, 2)))
# Layer 3
model.add(layers.Conv2D(384, (3, 3), padding='same', activation='relu'))
# Layer 4
model.add(layers.Conv2D(384, (3, 3), padding='same', activation='relu'))
# Layer 5
model.add(layers.Conv2D(256, (3, 3), padding='same', activation='relu'))
model.add(layers.MaxPooling2D((3, 3), strides=(2, 2)))
# Flatten
model.add(layers.Flatten())
# Fully Connected layers
model.add(layers.Dense(4096, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(4096, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(num_classes, activation='softmax'))
return model
# Create AlexNet model
alexnet = alexnet_model()
# Display model summary
alexnet.summary()
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_2 (Conv2D) (None, 54, 54, 96) 34944
max_pooling2d_2 (MaxPoolin (None, 26, 26, 96) 0
g2D)
conv2d_3 (Conv2D) (None, 26, 26, 256) 614656
max_pooling2d_3 (MaxPoolin (None, 12, 12, 256) 0
g2D)
conv2d_4 (Conv2D) (None, 12, 12, 384) 885120
conv2d_5 (Conv2D) (None, 12, 12, 384) 1327488
conv2d_6 (Conv2D) (None, 12, 12, 256) 884992
max_pooling2d_4 (MaxPoolin (None, 5, 5, 256) 0
g2D)
flatten (Flatten) (None, 6400) 0
dense (Dense) (None, 4096) 26218496
dropout (Dropout) (None, 4096) 0
dense_1 (Dense) (None, 4096) 16781312
dropout_1 (Dropout) (None, 4096) 0
dense_2 (Dense) (None, 1000) 4097000
=================================================================
Total params: 50844008 (193.95 MB)
Trainable params: 50844008 (193.95 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
implementation of ZFNet using TensorFlow:
import tensorflow as tf
from tensorflow.keras import layers, models
def zfnet_model(input_shape=(224, 224, 3), num_classes=1000):
model = models.Sequential()
# Layer 1
model.add(layers.Conv2D(96, (7, 7), strides=(2, 2), activation='relu', input_shape=input_shape))
model.add(layers.MaxPooling2D((3, 3), strides=(2, 2)))
# Layer 2
model.add(layers.Conv2D(256, (5, 5), padding='same', activation='relu'))
model.add(layers.MaxPooling2D((3, 3), strides=(2, 2)))
# Layer 3
model.add(layers.Conv2D(384, (3, 3), padding='same', activation='relu'))
model.add(layers.Conv2D(384, (3, 3), padding='same', activation='relu'))
model.add(layers.Conv2D(256, (3, 3), padding='same', activation='relu'))
model.add(layers.MaxPooling2D((3, 3), strides=(2, 2)))
# Flatten
model.add(layers.Flatten())
# Fully Connected layers
model.add(layers.Dense(4096, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(4096, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(num_classes, activation='softmax'))
return model
# Create ZFNet model
zfnet = zfnet_model()
# Display model summary
zfnet.summary()
Model: "sequential_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_7 (Conv2D) (None, 109, 109, 96) 14208
max_pooling2d_5 (MaxPoolin (None, 54, 54, 96) 0
g2D)
conv2d_8 (Conv2D) (None, 54, 54, 256) 614656
max_pooling2d_6 (MaxPoolin (None, 26, 26, 256) 0
g2D)
conv2d_9 (Conv2D) (None, 26, 26, 384) 885120
conv2d_10 (Conv2D) (None, 26, 26, 384) 1327488
conv2d_11 (Conv2D) (None, 26, 26, 256) 884992
max_pooling2d_7 (MaxPoolin (None, 12, 12, 256) 0
g2D)
flatten_1 (Flatten) (None, 36864) 0
dense_3 (Dense) (None, 4096) 150999040
dropout_2 (Dropout) (None, 4096) 0
dense_4 (Dense) (None, 4096) 16781312
dropout_3 (Dropout) (None, 4096) 0
dense_5 (Dense) (None, 1000) 4097000
=================================================================
Total params: 175603816 (669.88 MB)
Trainable params: 175603816 (669.88 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
implementation of the VGG16 architecture using TensorFlow:
import tensorflow as tf
from tensorflow.keras import layers, models
def vgg16_model(input_shape=(224, 224, 3), num_classes=1000):
model = models.Sequential()
# Block 1
model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=input_shape))
model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
# Block 2
model.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
# Block 3
model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
# Block 4
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
# Block 5
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
# Flatten
model.add(layers.Flatten())
# Fully Connected layers
model.add(layers.Dense(4096, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(4096, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(num_classes, activation='softmax'))
return model
# Create VGG16 model
vgg16 = vgg16_model()
# Display model summary
vgg16.summary()
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_12 (Conv2D) (None, 224, 224, 64) 1792
conv2d_13 (Conv2D) (None, 224, 224, 64) 36928
max_pooling2d_8 (MaxPoolin (None, 112, 112, 64) 0
g2D)
conv2d_14 (Conv2D) (None, 112, 112, 128) 73856
conv2d_15 (Conv2D) (None, 112, 112, 128) 147584
max_pooling2d_9 (MaxPoolin (None, 56, 56, 128) 0
g2D)
conv2d_16 (Conv2D) (None, 56, 56, 256) 295168
conv2d_17 (Conv2D) (None, 56, 56, 256) 590080
conv2d_18 (Conv2D) (None, 56, 56, 256) 590080
max_pooling2d_10 (MaxPooli (None, 28, 28, 256) 0
ng2D)
conv2d_19 (Conv2D) (None, 28, 28, 512) 1180160
conv2d_20 (Conv2D) (None, 28, 28, 512) 2359808
conv2d_21 (Conv2D) (None, 28, 28, 512) 2359808
max_pooling2d_11 (MaxPooli (None, 14, 14, 512) 0
ng2D)
conv2d_22 (Conv2D) (None, 14, 14, 512) 2359808
conv2d_23 (Conv2D) (None, 14, 14, 512) 2359808
conv2d_24 (Conv2D) (None, 14, 14, 512) 2359808
max_pooling2d_12 (MaxPooli (None, 7, 7, 512) 0
ng2D)
flatten_2 (Flatten) (None, 25088) 0
dense_6 (Dense) (None, 4096) 102764544
dropout_4 (Dropout) (None, 4096) 0
dense_7 (Dense) (None, 4096) 16781312
dropout_5 (Dropout) (None, 4096) 0
dense_8 (Dense) (None, 1000) 4097000
=================================================================
implementation of GoogLeNet using TensorFlow
import tensorflow as tf
from tensorflow.keras import layers, models
def inception_module(x, filters):
conv1x1_1 = layers.Conv2D(filters[0], (1, 1), padding='same', activation='relu')(x)
conv1x1_3 = layers.Conv2D(filters[1], (1, 1), padding='same', activation='relu')(x)
conv3x3 = layers.Conv2D(filters[2], (3, 3), padding='same', activation='relu')(conv1x1_3)
conv1x1_5 = layers.Conv2D(filters[3], (1, 1), padding='same', activation='relu')(x)
conv5x5 = layers.Conv2D(filters[4], (5, 5), padding='same', activation='relu')(conv1x1_5)
maxpool = layers.MaxPooling2D((3, 3), strides=(1, 1), padding='same')(x)
conv1x1_maxpool = layers.Conv2D(filters[5], (1, 1), padding='same', activation='relu')(maxpool)
inception = layers.Concatenate(axis=-1)([conv1x1_1, conv3x3, conv5x5, conv1x1_maxpool])
return inception
def googlenet_model(input_shape=(224, 224, 3), num_classes=1000):
inputs = layers.Input(shape=input_shape)
# Initial convolutions
x = layers.Conv2D(64, (7, 7), strides=(2, 2), padding='same', activation='relu')(inputs)
x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
x = layers.BatchNormalization()(x)
# Inception modules
x = inception_module(x, [64, 128, 128, 32, 32, 32])
x = inception_module(x, [128, 192, 96, 64, 64, 64])
x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
x = inception_module(x, [192, 208, 96, 64, 64, 64])
x = inception_module(x, [160, 224, 112, 64, 64, 64])
x = inception_module(x, [128, 256, 128, 64, 64, 64])
x = inception_module(x, [112, 288, 144, 64, 64, 64])
x = inception_module(x, [256, 320, 160, 128, 128, 128])
x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
# Fully connected layers
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(1024, activation='relu')(x)
x = layers.Dropout(0.4)(x)
x = layers.Dense(num_classes, activation='softmax')(x)
model = models.Model(inputs, x)
return model
# Create GoogLeNet model
googlenet = googlenet_model()
# Display model summary
googlenet.summary()
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 224, 224, 3)] 0 []
conv2d_25 (Conv2D) (None, 112, 112, 64) 9472 ['input_1[0][0]']
max_pooling2d_13 (MaxPooli (None, 56, 56, 64) 0 ['conv2d_25[0][0]']
ng2D)
batch_normalization (Batch (None, 56, 56, 64) 256 ['max_pooling2d_13[0][0]']
Normalization)
conv2d_27 (Conv2D) (None, 56, 56, 128) 8320 ['batch_normalization[0][0]']
conv2d_29 (Conv2D) (None, 56, 56, 32) 2080 ['batch_normalization[0][0]']
max_pooling2d_14 (MaxPooli (None, 56, 56, 64) 0 ['batch_normalization[0][0]']
ng2D)
conv2d_26 (Conv2D) (None, 56, 56, 64) 4160 ['batch_normalization[0][0]']
conv2d_28 (Conv2D) (None, 56, 56, 128) 147584 ['conv2d_27[0][0]']
conv2d_30 (Conv2D) (None, 56, 56, 32) 25632 ['conv2d_29[0][0]']
conv2d_31 (Conv2D) (None, 56, 56, 32) 2080 ['max_pooling2d_14[0][0]']
concatenate (Concatenate) (None, 56, 56, 256) 0 ['conv2d_26[0][0]',
'conv2d_28[0][0]',
'conv2d_30[0][0]',
'conv2d_31[0][0]']
conv2d_33 (Conv2D) (None, 56, 56, 192) 49344 ['concatenate[0][0]']
conv2d_35 (Conv2D) (None, 56, 56, 64) 16448 ['concatenate[0][0]']
max_pooling2d_15 (MaxPooli (None, 56, 56, 256) 0 ['concatenate[0][0]']
ng2D)
conv2d_32 (Conv2D) (None, 56, 56, 128) 32896 ['concatenate[0][0]']
conv2d_34 (Conv2D) (None, 56, 56, 96) 165984 ['conv2d_33[0][0]']
conv2d_36 (Conv2D) (None, 56, 56, 64) 102464 ['conv2d_35[0][0]']
conv2d_37 (Conv2D) (None, 56, 56, 64) 16448 ['max_pooling2d_15[0][0]']
concatenate_1 (Concatenate (None, 56, 56, 352) 0 ['conv2d_32[0][0]',
) 'conv2d_34[0][0]',
'conv2d_36[0][0]',
'conv2d_37[0][0]']
max_pooling2d_16 (MaxPooli (None, 28, 28, 352) 0 ['concatenate_1[0][0]']
ng2D)
conv2d_39 (Conv2D) (None, 28, 28, 208) 73424 ['max_pooling2d_16[0][0]']
keyboard_arrow_down Convolution Neural Network application using TensorFlow and Keras,
i) Classification of MNIST Dataset using CNN
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
# Load and preprocess the MNIST dataset
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1)).astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1)).astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
# Define the CNN model
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
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, activation='softmax'))
# Compile the model
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Train the model
model.fit(train_images, train_labels, epochs=5, batch_size=64, validation_split=0.2)
# Evaluate the model on the test set
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f'Test accuracy: {test_acc}')
# Make predictions
predictions = model.predict(test_images[:5])
print('Predictions:')
print(predictions.argmax(axis=1))
print('Actual Labels:')
print(test_labels[:5].argmax(axis=1))
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11490434/11490434 [==============================] - 0s 0us/step
Epoch 1/5
750/750 [==============================] - 46s 60ms/step - loss: 0.2270 - accuracy: 0.9290 - val_loss: 0.0625 - val_accuracy: 0.9808
Epoch 2/5
750/750 [==============================] - 40s 54ms/step - loss: 0.0566 - accuracy: 0.9823 - val_loss: 0.0447 - val_accuracy: 0.9868
Epoch 3/5
750/750 [==============================] - 40s 53ms/step - loss: 0.0401 - accuracy: 0.9873 - val_loss: 0.0401 - val_accuracy: 0.9882
Epoch 4/5
750/750 [==============================] - 40s 53ms/step - loss: 0.0312 - accuracy: 0.9904 - val_loss: 0.0512 - val_accuracy: 0.9858
Epoch 5/5
750/750 [==============================] - 44s 58ms/step - loss: 0.0261 - accuracy: 0.9915 - val_loss: 0.0436 - val_accuracy: 0.9869
313/313 [==============================] - 4s 12ms/step - loss: 0.0322 - accuracy: 0.9895
Test accuracy: 0.9894999861717224
1/1 [==============================] - 0s 82ms/step
Predictions:
[7 2 1 0 4]
Actual Labels:
[7 2 1 0 4]
keyboard_arrow_down ii) Face recognition using CNN
filename = "../input/opencv-facial-recognition-lbph/yalefaces/test/subject03.glasses.gif"
pixels = plt.imread(filename)
rgb_pixels = np.stack((pixels, pixels, pixels), axis=2)
print(rgb_pixels.shape)
plt.imshow(pixels)
plt.show()
(243, 320, 3)
keyboard_arrow_down 2) MTCNN
detector = MTCNN()
results = detector.detect_faces(rgb_pixels)
results
KMP_FOREIGN_THREADS_THREADPRIVATE=true
KMP_FORKJOIN_BARRIER='2,2'
KMP_FORKJOIN_BARRIER_PATTERN='hyper,hyper'
KMP_GTID_MODE=3
KMP_HANDLE_SIGNALS=false
KMP_HOT_TEAMS_MAX_LEVEL=1
KMP_HOT_TEAMS_MODE=0
KMP_INIT_AT_FORK=true
KMP_LIBRARY=throughput
KMP_LOCK_KIND=queuing
KMP_MALLOC_POOL_INCR=1M
KMP_NUM_LOCKS_IN_BLOCK=1
KMP_PLAIN_BARRIER='2,2'
KMP_PLAIN_BARRIER_PATTERN='hyper,hyper'
KMP_REDUCTION_BARRIER='1,1'
KMP_REDUCTION_BARRIER_PATTERN='hyper,hyper'
KMP_SCHEDULE='static,balanced;guided,iterative'
KMP_SETTINGS=true
KMP_SPIN_BACKOFF_PARAMS='4096,100'
KMP_STACKOFFSET=64
KMP_STACKPAD=0
KMP_STACKSIZE=8M
KMP_STORAGE_MAP=false
KMP_TASKING=2
KMP_TASKLOOP_MIN_TASKS=0
KMP_TASK_STEALING_CONSTRAINT=1
KMP_TEAMS_THREAD_LIMIT=4
KMP_TOPOLOGY_METHOD=all
KMP_USE_YIELD=1
KMP_VERSION=false
KMP_WARNINGS=false
OMP_AFFINITY_FORMAT='OMP: pid %P tid %i thread %n bound to OS proc set {%A}'
OMP_ALLOCATOR=omp_default_mem_alloc
OMP_CANCELLATION=false
OMP_DEFAULT_DEVICE=0
OMP_DISPLAY_AFFINITY=false
OMP_DISPLAY_ENV=false
OMP_DYNAMIC=false
OMP_MAX_ACTIVE_LEVELS=1
OMP_MAX_TASK_PRIORITY=0
OMP_NESTED: deprecated; max-active-levels-var=1
OMP_NUM_THREADS: value is not defined
OMP_PLACES: value is not defined
OMP_PROC_BIND='intel'
OMP_SCHEDULE='static'
OMP_STACKSIZE=8M
OMP_TARGET_OFFLOAD=DEFAULT
OMP_THREAD_LIMIT=2147483647
OMP_WAIT_POLICY=PASSIVE
KMP_AFFINITY='verbose,warnings,respect,granularity=fine,compact,1,0'
[{'box': [115, 90, 110, 134],
'confidence': 0.9997938275337219,
'keypoints': {'left_eye': (147, 140),
'right_eye': (198, 138),
'nose': (174, 169),
'mouth_left': (151, 192),
'mouth_right': (195, 193)}}]
def draw_image_with_boxes(data, result_list):
plt.imshow(data)
ax = plt.gca()
for result in result_list:
x, y, width, height = result['box']
rect = Rectangle((x, y), width, height, fill=False, color='red')
ax.add_patch(rect)
plt.show()
draw_image_with_boxes(rgb_pixels, results)
keyboard_arrow_down 3) Extract and normalise the face pixels
def extract_face_from_file(filename, required_size=(160, 160)):
image = Image.open(filename)
return extract_face(image, required_size)
def extract_face(image, required_size=(160, 160)):
image = image.convert('RGB')
pixels = np.asarray(image)
results = detector.detect_faces(pixels)
x1, y1, width, height = results[0]['box']
x1, y1 = abs(x1), abs(y1)
x2, y2 = x1 + width, y1 + height
face = pixels[y1:y2, x1:x2]
image = Image.fromarray(face)
image = image.resize(required_size)
face_array = np.asarray(image)
gray_face = cv2.cvtColor(face_array, cv2.COLOR_BGR2GRAY)
return gray_face
detector = MTCNN()
face_pixels = extract_face_from_file("../input/opencv-facial-recognition-lbph/yalefaces/test/subject03.glasses.gif")
plt.imshow(face_pixels)
<matplotlib.image.AxesImage at 0x782d6d6b1610>
keyboard_arrow_down 2.Data set 〜 135 train datas and 30 test datas
def list_files(directory, contains):
return list(f for f in listdir(directory) if contains in f)
i = 1
faces = list()
for filename in tqdm(list_files(DIRECTORY_train, "subject")[0:16]):
# path
path = DIRECTORY_train + filename
# get face
face = extract_face_from_file(path)
# plot
plt.subplot(4, 4, i)
plt.axis('off')
plt.imshow(face)
faces.append(face)
i += 1
plt.show()
100%|██████████| 16/16 [00:13<00:00, 1.14it/s]
filenames = pd.DataFrame(list_files(DIRECTORY_train, "subject"))
df_train = filenames[0].str.split(".", expand=True)
df_train["filename"] = filenames
df_train = df_train.rename(columns = {0:"subject", 1:"category"})
df_train['subject'] = df_train.subject.str.replace('subject' , '')
df_train.apply(pd.to_numeric, errors='coerce').dropna()
df_train['subject'] = pd.to_numeric(df_train["subject"])
df_train
subject category 2 filename
0 15 happy gif subject15.happy.gif
1 13 noglasses gif subject13.noglasses.gif
2 1 normal gif subject01.normal.gif
3 15 surprised gif subject15.surprised.gif
4 14 wink gif subject14.wink.gif
... ... ... ... ...
130 5 sad gif subject05.sad.gif
131 3 surprised gif subject03.surprised.gif
132 4 sad gif subject04.sad.gif
133 7 normal gif subject07.normal.gif
134 2 surprised gif subject02.surprised.gif
135 rows × 4 columns
filenames2 = pd.DataFrame(list_files(DIRECTORY_test, "subject"))
df_test = filenames2[0].str.split(".", expand=True)
df_test["filename"] = filenames2
df_test = df_test.rename(columns = {0:"subject", 1:"category"})
df_test['subject'] = df_test.subject.str.replace('subject' , '')
df_test.apply(pd.to_numeric, errors='coerce').dropna()
df_test['subject'] = pd.to_numeric(df_test["subject"])
df_test
subject category 2 filename
0 3 glasses gif subject03.glasses.gif
1 12 normal gif subject12.normal.gif
2 2 leftlight gif subject02.leftlight.gif
3 13 sad gif subject13.sad.gif
4 6 leftlight gif subject06.leftlight.gif
5 11 glasses gif subject11.glasses.gif
6 2 centerlight gif subject02.centerlight.gif
7 14 sad gif subject14.sad.gif
8 14 normal gif subject14.normal.gif
9 4 surprised gif subject04.surprised.gif
10 1 happy gif subject01.happy.gif
11 9 rightlight gif subject09.rightlight.gif
12 15 rightlight gif subject15.rightlight.gif
13 9 sad gif subject09.sad.gif
14 15 sad gif subject15.sad.gif
15 7 happy gif subject07.happy.gif
16 12 rightlight gif subject12.rightlight.gif
17 5 surprised gif subject05.surprised.gif
18 4 leftlight gif subject04.leftlight.gif
19 10 sad gif subject10.sad.gif
20 6 happy gif subject06.happy.gif
21 8 rightlight gif subject08.rightlight.gif
22 13 sleepy gif subject13.sleepy.gif
23 3 leftlight gif subject03.leftlight.gif
24 5 sleepy gif subject05.sleepy.gif
25 10 centerlight gif subject10.centerlight.gif
26 8 normal gif subject08.normal.gif
27 1 gif None subject01.gif
28 7 leftlight gif subject07.leftlight.gif
29 11 happy gif subject11.happy.gif
x_train=df_train.loc[:,['category','filename']]
x_test=df_test.loc[:,['category','filename']]
y_train=df_train.loc[:,['subject']]
y_test=df_test.loc[:,['subject']]
y_train=y_train.to_numpy()
y_test=y_test.to_numpy()
y_train = y_train.tolist()
y_test = y_test.tolist()
detector = MTCNN()
def load_dataset1(dataset):
faces = list()
for filename in tqdm(dataset["filename"]):
path = DIRECTORY_train + filename
# get face
face = extract_face_from_file(path)
faces.append(face)
return np.asarray(faces)
detector = MTCNN()
def load_dataset2(dataset):
faces = list()
for filename in tqdm(dataset["filename"]):
path = DIRECTORY_test + filename
# get face
x_test =face
load_dataset2(x_test)
= extract_face_from_file(path)
x_train = load_dataset1(x_train)
faces.append(face)
return np.asarray(faces)
print(x_test.shape)
print(x_train.shape)
100%|██████████| 30/30 [00:25<00:00, 1.19it/s]
91%|█████████ | 123/135 [01:40<00:09, 1.25it/s]
keyboard_arrow_down 3. Convolutional Neural Network Model
TRAINING_DATA_DIRECTORY = "data/train"
TESTING_DATA_DIRECTORY = "data/test"
NUM_CLASSES = 15
EPOCHS = 25
BATCH_SIZE = 20
NUMBER_OF_TRAINING_IMAGES = 135
NUMBER_OF_TESTING_IMAGES = 30
IMAGE_HEIGHT = 160
IMAGE_WIDTH = 160
import os
def save_keras_dataset(setname, dataset, labels, per_class):
data = sorted(list(zip(labels, dataset)), key=lambda x: x[0])
j = 0
for label, gray_img in tqdm(data):
j = (j% per_class) + 1
directory = f"data/{setname}/class_{label}/"
if not os.path.exists(directory):
os.makedirs(directory)
cv2.imwrite(f"{directory}class_{label}_{j}.png",gray_img)
import shutil
shutil.rmtree(r'data', ignore_errors=True)
# Save datasets
save_keras_dataset("test", x_test, y_test, 3)
save_keras_dataset("train", x_train, y_train, 8)
from tensorflow.keras.preprocessing.image import ImageDataGenerator
def data_generator():
return ImageDataGenerator(
keyboard_arrow_down 8. Text processing, Language Modeling using RNN
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, SimpleRNN, Dense
# Sample text data
text_data = [
"The quick brown fox jumps over the lazy dog",
"A quick brown dog jumps over the lazy fox",
"The lazy fox jumps over the quick brown dog"
]
# Define parameters for text processing
num_words = 50 # Number of most frequent words to keep
max_len = 10 # Maximum sequence length
# Tokenize the text data
tokenizer = Tokenizer(num_words=num_words)
tokenizer.fit_on_texts(text_data)
sequences = tokenizer.texts_to_sequences(text_data)
# Pad sequences to ensure uniform length
padded_sequences = pad_sequences(sequences, maxlen=max_len, padding='pre')
# Prepare input-output pairs for language modeling
X = padded_sequences[:, :-1] # Input sequence (remove last token)
y = padded_sequences[:, 1:] # Output sequence (shifted by one)
# Define the RNN model
model = Sequential([
Embedding(input_dim=num_words, output_dim=10, input_length=max_len-1),
SimpleRNN(units=32, return_sequences=True),
Dense(num_words, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')
# Train the model
model.fit(X, y, epochs=50, verbose=1)
# Generate text using the trained model
# Generate text using the trained model
def generate_text(seed_text, max_length=10):
for _ in range(max_length):
# Tokenize the seed text
tokenized_text = tokenizer.texts_to_sequences([seed_text])[0]
# Pad the sequence
padded_sequence = pad_sequences([tokenized_text], maxlen=max_len-1, padding='pre')
# Predict the next word
predicted_index = np.argmax(model.predict(padded_sequence), axis=-1)[0][0]
# Convert the index to word
predicted_word = tokenizer.index_word.get(predicted_index, '')
if predicted_word:
seed_text += ' ' + predicted_word
else:
break
return seed_text
# Generate text starting from a seed
seed_text = "The quick brown"
generated_text = generate_text(seed_text)
print("Generated Text:", generated_text)
Epoch 32/50
1/1 [==============================] - 0s 14ms/step - loss: 3.1028
Epoch 33/50
1/1 [==============================] - 0s 14ms/step - loss: 3.0771
Epoch 34/50
1/1 [==============================] - 0s 14ms/step - loss: 3.0519
Epoch 35/50
1/1 [==============================] - 0s 14ms/step - loss: 3.0271
Epoch 36/50
1/1 [==============================] - 0s 12ms/step - loss: 3.0026
Epoch 37/50
1/1 [==============================] - 0s 12ms/step - loss: 2.9783
Epoch 38/50
1/1 [==============================] - 0s 12ms/step - loss: 2.9541
Epoch 39/50
1/1 [==============================] - 0s 13ms/step - loss: 2.9301
Epoch 40/50
1/1 [==============================] - 0s 16ms/step - loss: 2.9061
Epoch 41/50
1/1 [==============================] - 0s 11ms/step - loss: 2.8822
Epoch 42/50
1/1 [==============================] - 0s 13ms/step - loss: 2.8583
Epoch 43/50
1/1 [==============================] - 0s 15ms/step - loss: 2.8346
Epoch 44/50
1/1 [==============================] - 0s 12ms/step - loss: 2.8111
Epoch 45/50
1/1 [==============================] - 0s 13ms/step - loss: 2.7878
Epoch 46/50
1/1 [==============================] - 0s 13ms/step - loss: 2.7647
Epoch 47/50
1/1 [==============================] - 0s 14ms/step - loss: 2.7419
Epoch 48/50
1/1 [==============================] - 0s 13ms/step - loss: 2.7196
Epoch 49/50
1/1 [==============================] - 0s 12ms/step - loss: 2.6977
Epoch 50/50
1/1 [==============================] - 0s 14ms/step - loss: 2.6763
1/1 [==============================] - 0s 192ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 25ms/step
Generated Text: The quick brown the the the the the the fox brown fox fox
keyboard_arrow_down 9. Time Series Prediction using RNN
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense
import matplotlib.pyplot as plt
# Function to generate time series data
def generate_time_series(n_steps):
freq1, freq2, offsets1, offsets2 = np.random.rand(4, 1)
time = np.linspace(0, 1, n_steps)
series = 0.5 * np.sin((time - offsets1) * (freq1 * 10 + 10)) # First sine wave
series += 0.2 * np.sin((time - offsets2) * (freq2 * 20 + 20)) # Second sine wave
series += 0.1 * (np.random.rand(n_steps) - 0.5) # Noise
return series[..., np.newaxis].astype(np.float32)
# Generate training and validation data
n_steps = 50
series = generate_time_series(10000)
X_train, y_train = series[:7000, :n_steps], series[:7000, -1]
X_valid, y_valid = series[7000:9000, :n_steps], series[7000:9000, -1]
X_test, y_test = series[9000:, :n_steps], series[9000:, -1]
# Plot the time series data
plt.plot(series)
plt.title('Time Series Data')
plt.xlabel('Time')
plt.ylabel('Value')
plt.show()
# Define the RNN model
model = Sequential([
SimpleRNN(20, input_shape=[None, 1]),
Dense(1)
])
# Compile the model
model.compile(optimizer='adam', loss='mse')
# Train the model
history = model.fit(X_train, y_train, epochs=20, validation_data=(X_valid, y_valid))
# Evaluate the model
mse_test = model.evaluate(X_test, y_test)
print("Test MSE:", mse_test)
# Plot training and validation loss
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.show()
output
Epoch 1/20
219/219 [==============================] - 3s 6ms/step - loss: 0.1242 - val_loss: 0.0010
Epoch 2/20
219/219 [==============================] - 1s 6ms/step - loss: 2.5556e-04 - val_loss: 1.0640e-05
Epoch 3/20
219/219 [==============================] - 1s 4ms/step - loss: 1.4771e-05 - val_loss: 1.0516e-05
Epoch 4/20
219/219 [==============================] - 1s 3ms/step - loss: 1.4698e-05 - val_loss: 1.0569e-05
Epoch 5/20
219/219 [==============================] - 1s 3ms/step - loss: 1.4588e-05 - val_loss: 1.1668e-05
Epoch 6/20
219/219 [==============================] - 1s 3ms/step - loss: 1.4546e-05 - val_loss: 1.0685e-05
Epoch 7/20
219/219 [==============================] - 1s 3ms/step - loss: 1.4416e-05 - val_loss: 1.0099e-05
Epoch 8/20
219/219 [==============================] - 1s 3ms/step - loss: 1.4256e-05 - val_loss: 1.0089e-05
Epoch 9/20
219/219 [==============================] - 1s 3ms/step - loss: 1.4095e-05 - val_loss: 1.0385e-05
Epoch 10/20
219/219 [==============================] - 1s 3ms/step - loss: 1.4024e-05 - val_loss: 1.0446e-05
Epoch 11/20
219/219 [==============================] - 1s 3ms/step - loss: 1.3683e-05 - val_loss: 9.8510e-06
Epoch 12/20
219/219 [==============================] - 1s 3ms/step - loss: 1.3568e-05 - val_loss: 9.1702e-06
Epoch 13/20
219/219 [==============================] - 1s 3ms/step - loss: 1.3355e-05 - val_loss: 9.5895e-06
Epoch 14/20
219/219 [==============================] - 1s 3ms/step - loss: 1.3283e-05 - val_loss: 9.6184e-06
Epoch 15/20
219/219 [==============================] - 1s 3ms/step - loss: 1.3050e-05 - val_loss: 1.0971e-05
Epoch 16/20
219/219 [==============================] - 1s 3ms/step - loss: 1.2653e-05 - val_loss: 9.2719e-06
Epoch 17/20
219/219 [==============================] - 1s 3ms/step - loss: 1.2574e-05 - val_loss: 8.5916e-06
Epoch 18/20
219/219 [==============================] - 1s 4ms/step - loss: 1.2296e-05 - val_loss: 1.0373e-05
Epoch 19/20
219/219 [==============================] - 1s 5ms/step - loss: 1.2138e-05 - val_loss: 8.4920e-06
Epoch 20/20
219/219 [==============================] - 1s 5ms/step - loss: 1.1929e-05 - val_loss: 8.3111e-06
32/32 [==============================] - 0s 2ms/step - loss: 1.1224e-05
Test MSE: 1.1224380614294205e-05
keyboard_arrow_down 10. Sentiment Analysis using LSTM
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.datasets import imdb
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import Tokenizer
# Load the dataset
file_path = '/content/drive/MyDrive/IMDB Dataset.csv'
df = pd.read_csv(file_path, nrows=30000)
# Define parameters for preprocessing
num_words = 10000 # Number of most frequent words to keep
max_len = 100 # Maximum sequence length
# Perform train-test split
X_train_text, X_test_text, y_train, y_test = train_test_split(df['review'], df['sentiment'], test_size=0.2, random_state=42)
# Preprocess the text data
tokenizer = Tokenizer(num_words=num_words)
tokenizer.fit_on_texts(X_train_text)
X_train = tokenizer.texts_to_sequences(X_train_text)
X_test = tokenizer.texts_to_sequences(X_test_text)
X_train = pad_sequences(X_train, maxlen=max_len)
X_test = pad_sequences(X_test, maxlen=max_len)
# Encode the target labels
label_encoder = LabelEncoder()
y_train = label_encoder.fit_transform(y_train)
y_test = label_encoder.transform(y_test)
# Define the LSTM model
model = Sequential([
Embedding(input_dim=num_words, output_dim=128, input_length=max_len),
LSTM(units=64, dropout=0.2, recurrent_dropout=0.2),
Dense(units=1, activation='sigmoid')
])
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(X_train, y_train, batch_size=64, epochs=3, validation_data=(X_test, y_test))
# Evaluate the model on test data
loss, accuracy = model.evaluate(X_test, y_test)
print(f'Test Loss: {loss:.4f}, Test Accuracy: {accuracy:.4f}')
output Epoch 1/3
375/375 [==============================] - 75s 195ms/step - loss: 0.4085 - accuracy: 0.8100 - val_loss: 0.3385 - val_accuracy: 0.85
Epoch 2/3
375/375 [==============================] - 71s 190ms/step - loss: 0.2624 - accuracy: 0.8944 - val_loss: 0.3361 - val_accuracy: 0.85
Epoch 3/3
375/375 [==============================] - 69s 184ms/step - loss: 0.1891 - accuracy: 0.9295 - val_loss: 0.4028 - val_accuracy: 0.85
188/188 [==============================] - 4s 20ms/step - loss: 0.4028 - accuracy: 0.8500
Test Loss: 0.4028, Test Accuracy: 0.8500
keyboard_arrow_down Testing on custom data.
# Preprocess custom inputs
def preprocess_input(text):
# Tokenize the text
tokenized_text = tokenizer.texts_to_sequences([text])
# Pad sequences
padded_text = pad_sequences(tokenized_text, maxlen=max_len)
return padded_text
# Example custom input
custom_input = "This project is great, It feels nice to work on it."
# Preprocess the custom input
preprocessed_input = preprocess_input(custom_input)
# Predict sentiment
prediction = model.predict(preprocessed_input)
# Convert prediction to sentiment label
sentiment_label = "Positive" if prediction[0][0] > 0.5 else "Negative"
print("Custom Input:", custom_input)
print("Predicted Sentiment:", sentiment_label)
1/1 [==============================] - 0s 311ms/step
Custom Input: This project is great, It feels nice to work on it.
Predicted Sentiment: Positive
keyboard_arrow_down 11. Image geretation using GAN
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Reshape
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.optimizers import Adam
# Load MNIST dataset
(X_train, _), (_, _) = mnist.load_data()
# Normalize and reshape data
X_train = (X_train.astype(np.float32) - 127.5) / 127.5
X_train = np.expand_dims(X_train, axis=-1)
# Define generator model
generator = Sequential()
generator.add(Dense(256, input_shape=(100,), activation='relu'))
generator.add(Dense(512, activation='relu'))
generator.add(Dense(28*28*1, activation='tanh'))
generator.add(Reshape((28, 28, 1)))
# Define discriminator model
discriminator = Sequential()
discriminator.add(Flatten(input_shape=(28, 28, 1)))
discriminator.add(Dense(512, activation='relu'))
discriminator.add(Dense(256, activation='relu'))
discriminator.add(Dense(1, activation='sigmoid'))
# Compile discriminator
discriminator.compile(loss='binary_crossentropy', optimizer=Adam(learning_rate=0.0002, beta_1=0.5), metrics=['accuracy'])
# Combine generator and discriminator into a GAN model
discriminator.trainable = False
gan = Sequential([generator, discriminator])
gan.compile(loss='binary_crossentropy', optimizer=Adam(learning_rate=0.0002, beta_1=0.5))
# Training loop
batch_size = 64
epochs = 30000
sample_interval = 200
half_batch = batch_size // 2
for epoch in range(epochs):
# Train discriminator
idx = np.random.randint(0, X_train.shape[0], half_batch)
real_images = X_train[idx]
noise = np.random.normal(0, 1, (half_batch, 100))
fake_images = generator.predict(noise)
d_loss_real = discriminator.train_on_batch(real_images, np.ones((half_batch, 1)))
d_loss_fake = discriminator.train_on_batch(fake_images, np.zeros((half_batch, 1)))
d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
# Train generator
noise = np.random.normal(0, 1, (batch_size, 100))
valid_y = np.array([1] * batch_size)
g_loss = gan.train_on_batch(noise, valid_y)
# Print progress
if epoch % sample_interval == 0:
print(f"Epoch {epoch}, [D loss: {d_loss[0]}, acc.: {100 * d_loss[1]}%], [G loss: {g_loss}]")
# Save generated images
r, c = 5, 5
noise = np.random.normal(0, 1, (r * c, 100))
gen_imgs = generator.predict(noise)
gen_imgs = 0.5 * gen_imgs + 0.5 # Rescale images 0 - 1
fig, axs = plt.subplots(r, c)
cnt = 0
for i in range(r):
for j in range(c):
axs[i,j].imshow(gen_imgs[cnt, :, :, 0], cmap='gray')
axs[i,j].axis('off')
cnt += 1
plt.show()
output Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11490434/11490434 [==============================] - 0s 0us/step
1/1 [==============================] - 1s 771ms/step
Epoch 0, [D loss: 0.6993211209774017, acc.: 45.3125%], [G loss: 0.7240753173828125]
1/1 [==============================] - 0s 55ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 18ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 17ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 18ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 18ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 17ms/step
1/1 [==============================] - 0s 18ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 28ms/step
1/1 [==============================] - 0s 32ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 27ms/step