Class 35 - CLASSIFICATION MODEL USING PYTORCH Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)
Class 35 - CLASSIFICATION MODEL USING PYTORCH
Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)
People will always discourage you, you have to keep moving forward.
Don't waste the opportunity ALLAH has given you, Every Day is a Blessing, Utilize it & Grow in your Life.
Now, you can learn anything that you want to learn. Everything is in your own hands now. Do your Hardwork & leave the rest upto ALLAH. ALLAH will create path for you.
Also train other's. Forget about time & money, while teaching to other's.
PyTorch is a framework to develop neural networks.
Classification:
Classification is a machine learning technique used to predict categorical or discrete target variables.
Types of classification problems: binary (two classes) and multi-class (more than two classes).
Data Preprocessing for Classification:
Data preprocessing is essential to transform raw data into a suitable format for machine learning algorithms.
It involves cleaning, transforming, and encoding the data.
Preprocessing improves model accuracy, handles missing values, and deals with categorical variables.
Selecting the Target Variable:
The target variable is the variable we want to predict/classify.
Recommended by LinkedIn
It represents the outcome or label we are interested in.
Choose a meaningful target variable based on the problem at hand.
Data Splitting: Train and Test Sets
Splitting data into training and testing sets is crucial for evaluating model performance.
The training set is used to build and train the classification model.
The testing set is used to evaluate the model on unseen data.
PyTorch will give you more control like customization.
With more control, you have to write more code.
.DropOut helps you to get rid of Neurons Dominance.
Also try the model for 50 Epcohs, change the learning rate and play with it.
LEARNING HAS NO AGE. YOU HAVE TO KEEP LEARNING. THAT'S THE REALITY TO KEEP MOVING FORWARD AND KEEP GROWING IN LIFE.
Google Colab Link:
#importing required libraries..
import torch
import numpy as np
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torchvision import datasets, transforms
from torch.utils.data.sampler import SubsetRandomSampler #for validation test
import matplotlib.pyplot as plt
%matplotlib inline
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# Assuming that we are on a CUDA machine, this should print a CUDA device:
print(device)
# Define a transform to convert images to tensors and normalize them
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,),)]) # mean and std have to be sequences (e.g., tuples),
# therefore we should add a comma after the values
# Load the data: train and test sets
trainset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data', download=True, train=True, transform=transform)
testset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data', download=True, train=False, transform=transform)
# Preparing for validation test
indices = list(range(len(trainset)))
np.random.shuffle(indices)
# To get 20% of the train set
split = int(np.floor(0.2 * len(trainset)))
train_sample = SubsetRandomSampler(indices[:split])
valid_sample = SubsetRandomSampler(indices[split:])
#Data Loader
trainloader = torch.utils.data.DataLoader(trainset, sampler=train_sample, batch_size=64)
validloader = torch.utils.data.DataLoader(trainset, sampler=valid_sample, batch_size=64)
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=True)
trainset
trainloader
# Create an iterator for the trainloader
dataiter = iter(trainloader)
print(dataiter)
# Get the next batch of data (images and labels)
images, labels = next(dataiter)
# Create a figure to display the images
fig = plt.figure(figsize=(15, 5))
# Loop through the first 20 images in the batch
for idx in np.arange(20):
# Create a subplot for each image
# xticks=[], yticks=[] is empty to print the images without any ticks around them
ax = fig.add_subplot(4, int(20/4), idx+1, xticks=[], yticks=[])
# Display the image using a grayscale colormap
ax.imshow(np.squeeze(images[idx]), cmap='gray')
# Set the title of the subplot to the corresponding label
# .item() gets the value contained in a Tensor
ax.set_title(labels[idx].item())
# Ensure that the subplots are properly laid out within the figure
fig.tight_layout()
# Define a custom classifier class that inherits from nn.Module
class Classifier(nn.Module):
def __init__(self):
super().__init__()
# Define the layers of the neural network
self.fc1 = nn.Linear(784, 120) # Fully connected layer 1 with 784 input features and 120 output features
self.fc2 = nn.Linear(120, 120) # Fully connected layer 2 with 120 input features and 120 output features
self.fc3 = nn.Linear(120, 10) # Fully connected layer 3 with 120 input features and 10 output features
self.dropout = nn.Dropout(0.2) # Dropout layer with a dropout probability of 0.2
def forward(self, x):
# Reshape the input tensor to have a flat vector shape
x = x.view(x.shape[0], -1)
# Apply the first fully connected layer with ReLU activation and dropout
x = self.dropout(F.relu(self.fc1(x)))
# Apply the second fully connected layer with ReLU activation and dropout
x = self.dropout(F.relu(self.fc2(x)))
# Apply the third fully connected layer with log softmax activation
x = F.log_softmax(self.fc3(x), dim=1)
return x
# Create an instance of the Classifier class to define the neural network model
model = Classifier()
# Define the loss function (Negative Log Likelihood Loss)
criterion = nn.NLLLoss()
# Define the optimizer (Stochastic Gradient Descent) and specify the learning rate
optimizer = optim.SGD(model.parameters(), lr=0.01)
# Initialize a variable with a high value to track the minimum validation loss
valid_loss_min = np.Inf
# Set the number of training epochs
epochs = 20
# Initialize a variable to count training steps
steps = 0
# Put the model in training mode
model.train()
# Create empty lists to store training and validation losses
train_losses, valid_losses = [], []
# Loop through the specified number of epochs for training
for e in range(epochs):
running_loss = 0
valid_loss = 0
# Train the model
for images, labels in trainloader:
optimizer.zero_grad() # Clear the gradients
log_ps = model(images) # Forward pass
loss = criterion(log_ps, labels) # Calculate the loss
loss.backward() # Backpropagate the gradients
optimizer.step() # Update the weights
running_loss += loss.item() * images.size(0)
# Validate the model
for images, labels in validloader:
log_ps = model(images) # Forward pass
loss = criterion(log_ps, labels) # Calculate the loss
valid_loss += loss.item() * images.size(0)
# Calculate average losses for training and validation
running_loss = running_loss / len(trainloader.sampler)
valid_loss = valid_loss / len(validloader.sampler)
# Append the losses to their respective lists
train_losses.append(running_loss)
valid_losses.append(valid_loss)
# Print training and validation loss for the current epoch
print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
e+1,
running_loss,
valid_loss
))
# Check if the current validation loss is lower than the minimum seen so far
if valid_loss <= valid_loss_min:
print('Validation loss decreased ({:.6f} --> {:.6f}). Saving Model ...'.format(valid_loss_min, valid_loss))
torch.save(model.state_dict(), 'model.pt') # Save the model's state
valid_loss_min = valid_loss # Update the minimum validation loss
plt.plot(train_losses, label='Train Loss')
plt.plot(valid_losses, label='Valid Loss')
plt.legend()
model.load_state_dict(torch.load('model.pt'))
#track the test loss
test_loss = 0
class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))
model.eval()
for images, labels in testloader:
#forword pass
output = model(images)
#calculate the loss
loss = criterion(output, labels)
#update the test loss
test_loss += loss.item()*images.size(0)
# convert output probabilities to predicted class
_, pred = torch.max(output, 1)
#compare predictions to the true labes
correct = np.squeeze(pred.eq(labels.data.view_as(pred)))
#calculate test accuracy for each object class
for i in range(len(labels)):
label = labels.data[i]
class_correct[label] += correct[i].item()
class_total[label] +=1
#calcaulate and prınt test loss
test_loss = test_loss/len(testloader.sampler)
print('Test Loss: {:.6f}\n'.format(test_loss))
for i in range(10):
if class_total[i] > 0:
print('Test Accuracy of %5s: %2d%% (%2d/%2d)'%
(str(i), 100 * class_correct[i]/class_total[i],
np.sum(class_correct[i]), np.sum(class_total[i])))
else:
print('Test Accuracy of %5s: N/A(no training examples)' % classes[i])
print('\nTest Accuracy (Overall): %2d%% (%2d/%2d)' % (
100. * np.sum(class_correct) / np.sum(class_total),
np.sum(class_correct), np.sum(class_total)))
# obtain one batch of test images
dataiter = iter(testloader)
images, labels = next(dataiter)
# get sample outputs
output = model(images)
# convert output probabilities to predicted class
_, preds = torch.max(output, 1)
# prep images for display
images = images.numpy()
# plot the images in the batch, along with predicted and true labels
fig = plt.figure(figsize=(25, 4))
for idx in np.arange(20):
ax = fig.add_subplot(2, int(20/2), idx+1, xticks=[], yticks=[])
ax.imshow(np.squeeze(images[idx]), cmap='gray')
ax.set_title("{} ({})".format(str(preds[idx].item()), str(labels[idx].item())),
color=("green" if preds[idx]==labels[idx] else "red"))
#AI #artificialintelligence #datascience #irfanmalik #drsheraz #xevensolutions #pytorch #classification #models #hamzanadeem