0% found this document useful (0 votes)
42 views4 pages

Code

This is machine learning code model

Uploaded by

melato thapelo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views4 pages

Code

This is machine learning code model

Uploaded by

melato thapelo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# Import nessecary libraries

import os
import numpy as np
import torch
import [Link] as nn
from [Link] import transforms
from [Link] import DataLoader
from [Link] import Adam
import torchvision
import pathlib
import glob
import matplotlib
import [Link] as plt

# Define the transformation pipeline


transformer = [Link]([
[Link]((200, 200)),
[Link](),
[Link](),
[Link]([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]) # Standard deviation
and mean of RGB
])

# Check if GPU is available


device = [Link]('cuda' if [Link].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(
[Link](train_dir, transform=transformer),
batch_size=180, shuffle=True
)
test_loader = DataLoader(
[Link](test_dir, transform=transformer),
batch_size=25, shuffle=True
)

# Get the class labels


root = [Link](train_dir)
classes = sorted([[Link]('/')[-1] for j in [Link]()])

# Define the WeatherCovNet class


class WeatherCovNet([Link]):
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 = [Link]()
[Link] = 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 = [Link]()
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 = [Link]()
# Fully connected layers
[Link] = [Link](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 = [Link](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 = [Link](-1, 27 * 100 * 100)
output = [Link](output)
return output

# Create the model


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

# Define the loss function and optimizer


optimizer = Adam([Link](), lr=0.001, weight_decay=0.0001)
loss_function = [Link]()

# 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([Link](train_dir + '/**/*.jpg'))
test_count = len([Link](test_dir + '/**/*.jpg'))

for epoch in range(num_epochs):


[Link]() # 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
[Link]() # Backward pass
[Link]() # Update the weights

training_loss += [Link]() * [Link](0)


_, prediction = [Link]([Link], 1)
training_accuracy += int([Link](prediction == [Link]))

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

# Evaluation on testing dataset


[Link]() # Set the model to evaluation mode
test_accuracy = 0.0

for images, labels in test_loader:


outputs = Model(images)
_, prediction = [Link]([Link], 1)
test_accuracy += int([Link](prediction == [Link]))

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}")

[Link](Model.state_dict(), 'best_checkpoint.model')
# Plotting the accuracy
epochs = range(1, num_epochs+1)

[Link](figsize=(10, 5))
[Link](epochs, train_accuracy_history, label='Training Accuracy', marker='o')
[Link](epochs, test_accuracy_history, label='Testing Accuracy', marker='o')
[Link]('Training and Testing Accuracy')
[Link]('Epoch')
[Link]('Accuracy')
[Link](epochs)
[Link]()
[Link](True)
[Link]()

# Load the best model checkpoint


Model.load_state_dict([Link]('best_checkpoint.model'))
[Link]() # 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 = [Link](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 = [Link](predictions[index]).item()
predicted_image = predictions[index].cpu().numpy()

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

plt.tight_layout()
[Link]()

You might also like