ARB Security Solutions, LLC.

Post Model Training To Microsoft Teams Channel Using PyTorch

Well hello there Imports. This script performs the training loop for the defined number of epochs. It trains the model on the CIFAR10 dataset and calculates the training and testing accuracy at each epoch. It also sends a progress update message to a Microsoft Teams channel using a Microsoft Teams card created with the pymsteams library. After each epoch, the trained model checkpoint is saved and uploaded to the SharePoint Online document library. The final trained model is saved and uploaded as well, so fancy.

import os
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from shareplum import Site
from shareplum import Office365
from pymsteams import connectorcard

# SharePoint Online credentials and site information
username = 'your_username'
password = 'your_password'
site_url = 'https://your_sharepoint_site_url'
document_library = 'Documents'

# Microsoft Teams webhook URL
teams_webhook_url = 'https://your_teams_webhook_url'

# Define the model architecture
class CustomModel(nn.Module):
def __init__(self):
super(CustomModel, self).__init__()
# Your model architecture goes here

def forward(self, x):
# Your forward pass logic goes here
return x

# Main script
if __name__ == '__main__':
# SharePoint Online connection
with Office365(site_url, username=username, password=password) as auth:
site = Site(site_url, auth=auth)
folder = site.Folder(f'{document_library}/DeepLearning')

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

# Data augmentation and normalization
transform = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32, padding=4),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
])

# Load CIFAR10 dataset
train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
test_dataset = datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)

# Data loaders
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=128, shuffle=True, num_workers=2)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=128, shuffle=False, num_workers=2)

# Model initialization
model = CustomModel().to(device)

# Loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

# Training loop
num_epochs = 10
for epoch in range(num_epochs):
# Training
model.train()
train_loss = 0.0
train_correct = 0
train_total = 0
for inputs, labels in train_loader:
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()

# Forward pass
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()

# Update training metrics
train_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
train_total += labels.size(0)
train_correct += (predicted == labels).sum().item()

# Calculate training accuracy
train_accuracy = 100.0 * train_correct / train_total

# Evaluation
model.eval()
test_loss = 0.0
test_correct = 0
test_total = 0
with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
loss = criterion(outputs, labels)

# Update testing metrics
test_loss += loss.item()
_,

predicted = torch.argmax(outputs.data, 1)
                    test_total += labels.size(0)
                    test_correct += (predicted == labels).sum().item()

            # Calculate testing accuracy
            test_accuracy = 100.0 * test_correct / test_total

            # Send progress update to Microsoft Teams
            message = f'Epoch [{epoch+1}/{num_epochs}], ' \
                      f'Train Loss: {train_loss:.4f}, ' \
                      f'Train Accuracy: {train_accuracy:.2f}%, ' \
                      f'Test Loss: {test_loss:.4f}, ' \
                      f'Test Accuracy: {test_accuracy:.2f}%'

            # Create a Microsoft Teams card
            card = connectorcard.ConnectorCard(webhookurl=teams_webhook_url)
            card.title('Training Update')
            card.text(message)
            card.send()

            print(message)

            # Save the trained model checkpoint
            checkpoint_filename = f'checkpoint_epoch_{epoch}.pth'
            torch.save(model.state_dict(), checkpoint_filename)
            with open(checkpoint_filename, 'rb') as checkpoint_file:
                folder.upload_file(checkpoint_file, os.path.basename(checkpoint_filename))

        # Save the final trained model
        model_filename = 'trained_model.pth'
        torch.save(model.state_dict(), model_filename)
        with open(model_filename, 'rb') as model_file:
            folder.upload_file(model_file, os.path.basename(model_filename))

    print("Model and checkpoints uploaded to SharePoint Online.")

Comments are closed.