ARB Security Solutions, LLC.

SharePoint Online and PyTorch for Image Classification

This example shows how to use Python to connect to SharePoint Online, extract image data from a list, load the data into a custom PyTorch dataset, train a simple CNN model using the dataset, and use the trained model to predict the label for a new test image.

# Import required libraries
import requests
import io
from PIL import Image
import torch
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader
from torch import nn, optim
import numpy as np
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext

# Set up SharePoint Online connection
url = 'https://yourcompany.sharepoint.com/sites/sitename'
username = '[email protected]'
password = 'yourpassword'
auth_context = AuthenticationContext(url)
auth_context.acquire_token_for_user(username, password)
ctx = ClientContext(url, auth_context)

# Get image URLs from a SharePoint Online list
list_name = 'ImageList'
image_list = ctx.web.lists.get_by_title(list_name)
image_items = image_list.items
ctx.load(image_items)
ctx.execute_query()

# Extract image URLs and labels from the list
image_urls = []
labels = []
for item in image_items:
    image_urls.append(item.ImageUrl)
    labels.append(item.Label)

# Define a custom dataset to load data into PyTorch
class ImageDataset(Dataset):
    def __init__(self, image_urls, labels, transform=None):
        self.image_urls = image_urls
        self.labels = labels
        self.transform = transform

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

    def __getitem__(self, index):
        url = self.image_urls[index]
        response = requests.get(url)
        image = Image.open(io.BytesIO(response.content))
        if self.transform:
            image = self.transform(image)
        label = self.labels[index]
        return image, label

# Define the transforms to be applied to the images
transform = transforms.Compose([transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])

# Create an instance of your dataset and dataloader
dataset = ImageDataset(image_urls, labels, transform=transform)
dataloader = DataLoader(dataset, batch_size=4, shuffle=True)

# Define your model architecture
model = nn.Sequential(
    nn.Conv2d(3, 16, 3, padding=1),
    nn.ReLU(),
    nn.MaxPool2d(2),
    nn.Conv2d(16, 32, 3, padding=1),
    nn.ReLU(),
    nn.MaxPool2d(2),
    nn.Conv2d(32, 64, 3, padding=1),
    nn.ReLU(),
    nn.MaxPool2d(2),
    nn.Flatten(),
    nn.Linear(64*28*28, 256),
    nn.ReLU(),
    nn.Linear(256, 10)
)

# Define your loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Train your model
for epoch in range(10):
    running_loss = 0.0
    for i, batch in enumerate(dataloader):
        optimizer.zero_grad()
        images, labels = batch
        outputs = model(images)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()
    print('Epoch %d loss: %.3f' % (epoch + 1, running_loss / len(dataset)))

# Use your trained model for prediction
test_image_url = 'https://imageurl.com/testimage.jpg'
response = requests.get(test_image_url)
test_image = Image.open(io.BytesIO(response.content))
test_image_tensor = transform(test_image).unsqueeze(0)
with torch.no_grad(): output = model(test_image_tensor)
predicted_label = torch.argmax(output).item()
print('The predicted label is:', predicted_label)

Comments are closed.