0% found this document useful (0 votes)
17 views

Code

This is machine learning code model

Uploaded by

melato thapelo
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Code

This is machine learning code model

Uploaded by

melato thapelo
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

# Import nessecary libraries

import os
import numpy as np
import torch
import torch.nn as nn
from torchvision.transforms import transforms
from torch.utils.data import DataLoader
from torch.optim import Adam
import torchvision
import pathlib
import glob
import matplotlib
import matplotlib.pyplot as plt

# Define the transformation pipeline


transformer = transforms.Compose([
transforms.Resize((200, 200)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]) # Standard deviation
and mean of RGB
])

# Check if GPU is available


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

# Dataset directories
train_dir = r'c:\Users\User\Downloads\Weather\dataset2\dataset2\train'
test_dir = r'c:\Users\User\Downloads\Weather\dataset2\dataset2\test'

# Create data loaders


train_loader = DataLoader(
torchvision.datasets.ImageFolder(train_dir, transform=transformer),
batch_size=180, shuffle=True
)
test_loader = DataLoader(
torchvision.datasets.ImageFolder(test_dir, transform=transformer),
batch_size=25, shuffle=True
)

# Get the class labels


root = pathlib.Path(train_dir)
classes = sorted([j.name.split('/')[-1] for j in root.iterdir()])

# Define the WeatherCovNet class


class WeatherCovNet(nn.Module):
def __init__(self, num_classes):
super(WeatherCovNet, self).__init__()
# Convolutional layers
self.conv1 = nn.Conv2d(in_channels=3, out_channels=9, kernel_size=3,
stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(num_features=9)
self.relu1 = nn.ReLU()
self.pool = nn.MaxPool2d(kernel_size=2)
self.conv2 = nn.Conv2d(in_channels=9, out_channels=18, kernel_size=3,
stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(num_features=18)
self.relu2 = nn.ReLU()
self.conv3 = nn.Conv2d(in_channels=18,out_channels=27, kernel_size=3,
stride=1, padding=1)
self.bn3 = nn.BatchNorm2d(num_features=27)
self.relu3 = nn.ReLU()
# Fully connected layers
self.fc = nn.Linear(in_features=27 * 100 * 100, out_features=num_classes)

def forward(self, input):


# Forward pass
output = self.conv1(input)
output = self.bn1(output)
output = self.relu1(output)
output = self.pool(output)
output = self.conv2(output)
output = self.bn2(output)
output = self.relu2(output)
output = self.conv3(output)
output = self.bn3(output)
output = self.relu3(output)
output = output.view(-1, 27 * 100 * 100)
output = self.fc(output)
return output

# Create the model


num_classes = len(classes)
Model = WeatherCovNet(num_classes).to(device)

# Define the loss function and optimizer


optimizer = Adam(Model.parameters(), lr=0.001, weight_decay=0.0001)
loss_function = nn.CrossEntropyLoss()

# Training loop
num_epochs = 10
best_accuracy = 0.0
# Lists to store training and testing accuracy values
train_accuracy_history = []
test_accuracy_history = []
# Calculate the size of training and testing images
train_count = len(glob.glob(train_dir + '/**/*.jpg'))
test_count = len(glob.glob(test_dir + '/**/*.jpg'))

for epoch in range(num_epochs):


Model.train() # Set the model to training mode
training_loss = 0.0
training_accuracy = 0.0

for images, labels in train_loader:


optimizer.zero_grad() # Zero the gradients
outputs = Model(images) # Forward pass
loss = loss_function(outputs, labels) # Compute the loss
loss.backward() # Backward pass
optimizer.step() # Update the weights

training_loss += loss.item() * images.size(0)


_, prediction = torch.max(outputs.data, 1)
training_accuracy += int(torch.sum(prediction == labels.data))

training_accuracy /= train_count
training_loss /= train_count
train_accuracy_history.append(training_accuracy)

# Evaluation on testing dataset


Model.eval() # Set the model to evaluation mode
test_accuracy = 0.0

for images, labels in test_loader:


outputs = Model(images)
_, prediction = torch.max(outputs.data, 1)
test_accuracy += int(torch.sum(prediction == labels.data))

test_accuracy /= test_count
test_accuracy_history.append(test_accuracy)
print(f"Epoch [{epoch+1}/{num_epochs}], Train Loss: {training_loss:.4f}, Train
Accuracy: {training_accuracy:.4f}, Test Accuracy: {test_accuracy:.4f}")

torch.save(Model.state_dict(), 'best_checkpoint.model')
# Plotting the accuracy
epochs = range(1, num_epochs+1)

plt.figure(figsize=(10, 5))
plt.plot(epochs, train_accuracy_history, label='Training Accuracy', marker='o')
plt.plot(epochs, test_accuracy_history, label='Testing Accuracy', marker='o')
plt.title('Training and Testing Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.xticks(epochs)
plt.legend()
plt.grid(True)
plt.show()

# Load the best model checkpoint


Model.load_state_dict(torch.load('best_checkpoint.model'))
Model.eval() # Set the model to evaluation mode

# Get a single batch of images and labels from the test dataset
images, labels = next(iter(test_loader))

# Make predictions on the batch of images


with torch.no_grad():
predictions = Model(images)

# Choose a random index to select a single image from the batch


index = np.random.randint(0, len(images))

# Get the original image and its corresponding label


original_image = images[index].cpu().numpy()
original_label = labels[index].item()

# Get the predicted label from the model's output


predicted_label = torch.argmax(predictions[index]).item()
predicted_image = predictions[index].cpu().numpy()

# Plot the original and predicted images, along with their labels
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(np.transpose(original_image, (1, 2, 0)))
plt.title(f'Original: {classes[original_label]}')
# Since predicted_image is not the actual image data, plot the original image again
plt.subplot(1, 2, 2)
plt.imshow(np.transpose(original_image, (1, 2, 0)))
plt.title(f'Predicted: {classes[predicted_label]}')

plt.tight_layout()
plt.show()

You might also like