PyTorch Integration (Python)

Pytorch is a Machine Learning framework that allows you to train Neural Networks. To take advantage of this the Darwin SDK does allow for some integrations with Pytorch.

In this chapter we will learn:

  • How to do custom transformations
  • How to do a Resize

Custom transformations

To define a custom transformation you use the ClassificationDataset class which takes a transformation class and the dataset path as paramters.

import numpy as np
from darwin.torch.dataset import ClassificationDataset
from pathlib import Path
import torch

dataset_path = Path('/home/user/.darwin/datasets/my-team-slug/my-dataset-slug')

# Class where you can define your custom transformations
class ToTensor():
    def __call__(self, img, target=None):
        return torch.from_numpy(np.array(img)), target


ds = ClassificationDataset(
    transform=ToTensor(),
    dataset_path=dataset_path
)
img, ann = ds[0]

print(type(img))

Resize

Following is an example how to resize an image. To this effect we use the InstanceSegmentationDataset class, which takes as parameters a transformation class and the dataset path.

from torch.nn.functional import interpolate
import numpy as np
from darwin.torch.dataset import InstanceSegmentationDataset
from pathlib import Path
import torch

dataset_path = Path('/home/pedro/.darwin/datasets/andreas-team/cars')

class Resize():
    def __init__(self, size):
        self.size = size

    def __call__(self, img, target=None):
        # resize image
        img = img.resize(self.size)
        if target:
            # resize target mask
            target['masks'] = interpolate(
                target['masks'].unsqueeze(0), size=self.size).squeeze(0)
        if not target:
            return img
        return img, target


ds = InstanceSegmentationDataset(
    transform=Resize(size=(224, 224)),
    dataset_path=dataset_path
)
img, ann = ds[0]
print(img.size)
print(ann['masks'].shape)