0% found this document useful (0 votes)
15 views7 pages

Notebook - Agave Plant Maturation Model Inference and Testing

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

Notebook - Agave Plant Maturation Model Inference and Testing

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

In [ ]: # Agave Plant Maturation Model Inference and Testing

# Import necessary libraries


import os
import torch
import torch.nn as nn
from torchvision import transforms, models
from torch.utils.data import Dataset, DataLoader
from PIL import Image
from sklearn.metrics import confusion_matrix, classification_report
import matplotlib.pyplot as plt
import seaborn as sns

In [ ]: # Define the AgaveDataset class


class AgaveDataset(Dataset):
def __init__(self, root_dir, transform=None):
self.root_dir = root_dir
self.transform = transform
self.classes = ['stage_1', 'stage_2', 'stage_3', 'stage_4', 'stage_5
self.class_to_idx = {cls_name: i for i, cls_name in enumerate(self.c
self.images = self._load_images()

def _load_images(self):
images = []
for cls_name in self.classes:
class_dir = os.path.join(self.root_dir, cls_name)
for img_name in os.listdir(class_dir):
images.append((os.path.join(class_dir, img_name), self.class
return images

def __len__(self):
return len(self.images)

def __getitem__(self, idx):


img_path, label = self.images[idx]
image = Image.open(img_path).convert('RGB')

if self.transform:
image = self.transform(image)

return image, label

In [ ]: # Define data transforms


val_test_transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.22
])

# Create test Dataset and Data Loader


test_dataset = AgaveDataset('path/to/test/data', transform=val_test_transfor
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False, num_wor
In [ ]: # Load the quantized model
model = models.resnet18()
model.fc = nn.Linear(model.fc.in_features, 5)

# Load the quantized model's state_dict


model.load_state_dict(torch.load('agave_classifier_quantized.pth', map_locat

In [ ]: # Function to evaluate the model on the test set


def evaluate_model(model, test_loader):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model.eval()

all_preds = []
all_labels = []

with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
_, predicted = outputs.max(1)

all_preds.extend(predicted.cpu().numpy())
all_labels.extend(labels.cpu().numpy())

return all_preds, all_labels

In [ ]: # Perform evaluation
test_preds, test_labels = evaluate_model(model, test_loader)

# Print classification report


print(classification_report(test_labels, test_preds, target_names=['Stage 1'

# Plot confusion matrix


cm = confusion_matrix(test_labels, test_preds)
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=['Stage 1', '
plt.xlabel('Predicted')
plt.ylabel('True')
plt.title('Confusion Matrix')
plt.show()
In [ ]: # Install necessary libraries
!pip install pytube opencv-python torch torchvision

# Import necessary libraries


import os
import cv2
from pytube import YouTube

# Define YouTube URLs


video_urls = [
'https://youtu.be/r25bnZYSo5w', # Stage 1
'https://youtu.be/i9BFmzZRp0Q', # Stage 2
'https://youtu.be/yPjm33XauJ0', # Stage 3
'https://youtu.be/lfsdWoXTE7Q', # Stage 4
'https://youtu.be/tbCWQKeCcBA' # Stage 5
]

# Function to download YouTube videos


def download_video(url, output_path):
yt = YouTube(url)
stream = yt.streams.filter(file_extension='mp4').first()
stream.download(output_path)
return os.path.join(output_path, stream.default_filename)

# Download videos
video_dir = 'videos'
os.makedirs(video_dir, exist_ok=True)
video_paths = [download_video(url, video_dir) for url in video_urls]

# Function to extract frames from a video


def extract_frames(video_path, output_dir, interval=30):
os.makedirs(output_dir, exist_ok=True)
vidcap = cv2.VideoCapture(video_path)
success, image = vidcap.read()
count = 0
frame_count = 0
while success:
if count % interval == 0:
frame_path = os.path.join(output_dir, f"frame{frame_count}.jpg")
cv2.imwrite(frame_path, image)
frame_count += 1
success, image = vidcap.read()
count += 1

# Extract frames from each video


for i, video_path in enumerate(video_paths):
stage_dir = os.path.join('frames', f'stage_{i+1}')
extract_frames(video_path, stage_dir)

In [ ]: # Import necessary libraries


import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, models
from PIL import Image
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, classification_report
import seaborn as sns

# Define the AgaveDataset class


class AgaveDataset(Dataset):
def __init__(self, root_dir, transform=None):
self.root_dir = root_dir
self.transform = transform
self.classes = ['not_ready', 'ready_for_harvest']
self.class_to_idx = {cls_name: i for i, cls_name in enumerate(self.c
self.images = self._load_images()

def _load_images(self):
images = []
for cls_name in self.classes:
class_dir = os.path.join(self.root_dir, cls_name)
for img_name in os.listdir(class_dir):
images.append((os.path.join(class_dir, img_name), self.class
return images

def __len__(self):
return len(self.images)

def __getitem__(self, idx):


img_path, label = self.images[idx]
image = Image.open(img_path).convert('RGB')

if self.transform:
image = self.transform(image)

return image, label

# Define data transforms


train_transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.22
])

val_test_transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.22
])

# Create Datasets and Data Loaders


train_dataset = AgaveDataset('path/to/train/data', transform=train_transform
val_dataset = AgaveDataset('path/to/val/data', transform=val_test_transform)
test_dataset = AgaveDataset('path/to/test/data', transform=val_test_transfor

train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True, num_wo


val_loader = DataLoader(val_dataset, batch_size=32, shuffle=False, num_worke
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False, num_wor

In [ ]: # Load pre-trained ResNet18 and modify for binary classification


model = models.resnet18(pretrained=True)
model.fc = nn.Linear(model.fc.in_features, 2)

In [ ]: # Define loss function and optimizer


criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

In [ ]: # Training function
def train_model(model, criterion, optimizer, train_loader, val_loader, num_e
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

train_losses, train_accs, val_losses, val_accs = [], [], [], []

for epoch in range(num_epochs):


# Training phase
model.train()
running_loss = 0.0
correct = 0
total = 0

for inputs, labels in train_loader:


inputs, labels = inputs.to(device), labels.to(device)

optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()

running_loss += loss.item()
_, predicted = outputs.max(1)
total += labels.size(0)
correct += predicted.eq(labels).sum().item()

train_loss = running_loss / len(train_loader)


train_acc = correct / total
train_losses.append(train_loss)
train_accs.append(train_acc)

# Validation phase
model.eval()
val_loss = 0.0
correct = 0
total = 0

with torch.no_grad():
for inputs, labels in val_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
loss = criterion(outputs, labels)
val_loss += loss.item()
_, predicted = outputs.max(1)
total += labels.size(0)
correct += predicted.eq(labels).sum().item()

val_loss /= len(val_loader)
val_acc = correct / total
val_losses.append(val_loss)
val_accs.append(val_acc)

print(f'Epoch {epoch+1}/{num_epochs}:')
print(f'Train Loss: {train_loss:.4f}, Train Acc: {train_acc:.4f}')
print(f'Val Loss: {val_loss:.4f}, Val Acc: {val_acc:.4f}')

return model, train_losses, train_accs, val_losses, val_accs

model, train_losses, train_accs, val_losses, val_accs = train_model(model, c

In [ ]: # Plot training and validation curves


plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(train_losses, label='Train Loss')
plt.plot(val_losses, label='Val Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.title('Training and Validation Loss')

plt.subplot(1, 2, 2)
plt.plot(train_accs, label='Train Acc')
plt.plot(val_accs, label='Val Acc')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.title('Training and Validation Accuracy')
plt.show()

In [ ]: # Evaluate the model on the test set


def evaluate_model(model, test_loader):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model.eval()

all_preds = []
all_labels = []

with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
_, predicted = outputs.max(1)

all_preds.extend(predicted.cpu().numpy())
all_labels.extend(labels.cpu().numpy())
return all_preds, all_labels

test_preds, test_labels = evaluate_model(model, test_loader)

print(classification_report(test_labels, test_preds, target_names=['Not Read

cm = confusion_matrix(test_labels, test_preds)
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.xlabel('Predicted')
plt.ylabel('True')
plt.title('Confusion Matrix')
plt.show()

In [ ]: # Save the model


torch.save(model.state_dict(), 'agave_classifier.pth')

# Quantize the model


model.eval()
quantized_model = torch.quantization.quantize_dynamic(
model, {nn.Linear}, dtype=torch.qint8
)

# Save the quantized model


torch.save(quantized_model.state_dict(), 'agave_classifier_quantized.pth')

print("Model training, evaluation, and quantization complete!")

You might also like