Skip to main content

Machine Learning

Project description

RSProduction MachineLearning

This project provides some usefull machine learning functionality.

Table of Contents

1 dataset

TOC

1.1 Kinetics : torch.utils.data.dataset.Dataset

TOC

Description

Dataset class for the Kinetics dataset.

Example

from rsp.ml.dataset import Kinetics

ds = Kinetics(split='train', type=400)

for X, T in ds:
    print(X)

1.1.1 __init__

TOC

Description

Initializes a new instance.

Parameters

Name Type Description
split str Dataset split [train
type int, default = 400 Type of the kineticts dataset. Currently only 400 is supported.
frame_size (int, int), default = (400, 400) Size of the frames. The frames will be resized to this size.
transforms rsp.ml.multi_transforms.Compose = default = rsp.ml.multi_transforms.Compose([]) Transformations, that will be applied to each input sequence. See documentation of rsp.ml.multi_transforms for more details.
cache_dir str, default = None Directory to store the downloaded files. If set to None, the default cache directory will be used
num_threads int, default = 0 Number of threads to use for downloading the files.

1.2 ReplaceBackground : rsp.ml.multi_transforms.multi_transforms.MultiTransform

TOC

Description

Transformation for background replacement based on HSV values. ReplaceBackground is an abstract class. Please inherit!

Example

from rsp.ml.dataset import ReplaceBackgroundRGB
from rsp.ml.dataset import TUCRID

backgrounds = TUCRID.load_backgrounds()

1.2.1 __call__

TOC

Description

Applies the transformation to the input data.

1.2.2 __init__

TOC

Description

Initializes a new instance.

1.2.3 change_background

TOC

Description

Changes the background of the input image.

Parameters

Name Type Description
img np.array Input image
bg np.array Background image
mask np.array Mask

1.2.4 hsv_filter

TOC

Description

Filters the input image based on HSV values.

Parameters

Name Type Description
img np.array Input image
hmin int Minimum hue value
hmax int Maximum hue value
smin int Minimum saturation value
smax int Maximum saturation value
vmin int Minimum value value
vmax int Maximum value value
inverted bool Invert the mask

1.3 ReplaceBackgroundRGB : ReplaceBackground

TOC

Description

Transformation for background replacement based on HSV values. ReplaceBackgroundRGB is a concrete class for RGB images.

1.3.1 __call__

TOC

Description

Applies the transformation to the input data.

1.3.2 __init__

TOC

Description

Initializes a new instance.

Parameters

Name Type Description
backgrounds List[np.array] List of background images
hsv_filter List[tuple[int, int, int, int, int, int]] List of HSV filters
p float, default = 1. Probability of applying the transformation

1.3.3 change_background

TOC

Description

Changes the background of the input image.

Parameters

Name Type Description
img np.array Input image
bg np.array Background image
mask np.array Mask

1.3.4 hsv_filter

TOC

Description

Filters the input image based on HSV values.

Parameters

Name Type Description
img np.array Input image
hmin int Minimum hue value
hmax int Maximum hue value
smin int Minimum saturation value
smax int Maximum saturation value
vmin int Minimum value value
vmax int Maximum value value
inverted bool Invert the mask

1.4 ReplaceBackgroundRGBD : ReplaceBackground

TOC

Description

Transformation for background replacement based on HSV values. ReplaceBackgroundRGBD is a concrete class for RGBD images.

Parameters

backgrounds : List[np.array] List of background images hsv_filter : List[tuple[int, int, int, int, int, int]] List of HSV filters p : float, default = 1. Probability of applying the transformation rotate : float, default = 5 Maximum rotation angle max_scale : float, default = 2 Maximum scaling factor

1.4.1 __call__

TOC

Description

Applies the transformation to the input data.

1.4.2 __init__

TOC

Description

Initializes a new instance.

1.4.3 change_background

TOC

Description

Changes the background of the input image.

Parameters

Name Type Description
img np.array Input image
bg np.array Background image
mask np.array Mask

1.4.4 hsv_filter

TOC

Description

Filters the input image based on HSV values.

Parameters

Name Type Description
img np.array Input image
hmin int Minimum hue value
hmax int Maximum hue value
smin int Minimum saturation value
smax int Maximum saturation value
vmin int Minimum value value
vmax int Maximum value value
inverted bool Invert the mask

1.5 TUCRID : torch.utils.data.dataset.Dataset

TOC

Description

Dataset class for the Robot Interaction Dataset by University of Technology Chemnitz (TUCRID).

Example

from rsp.ml.dataset import TUCRID
from rsp.ml.dataset import ReplaceBackgroundRGBD
import rsp.ml.multi_transforms as multi_transforms
import cv2 as cv

backgrounds = TUCRID.load_backgrounds_color()
transforms = multi_transforms.Compose([
    ReplaceBackgroundRGBD(backgrounds),
    multi_transforms.Stack()
])

ds = TUCRID('train', transforms=transforms)

for X, T in ds:
  for x in X.permute(0, 2, 3, 1):
    img_color = x[:, :, :3].numpy()
    img_depth = x[:, :, 3].numpy()

    cv.imshow('color', img_color)
    cv.imshow('depth', img_depth)

    cv.waitKey(30)

1.5.1 __init__

TOC

Description

Initializes a new instance.

Parameters

Name Type Description
phase str Dataset phase [train
load_depth_data bool, default = True Load depth data
sequence_length int, default = 30 Length of the sequences
transforms rsp.ml.multi_transforms.Compose = default = rsp.ml.multi_transforms.Compose([]) Transformations, that will be applied to each input sequence. See documentation of rsp.ml.multi_transforms for more details.

1.5.2 load_backgrounds

TOC

Description

Loads the background images.

Parameters

Name Type Description
load_depth_data bool, default = True If set to True, the depth images will be loaded as well.

2 metrics

TOC

The module rsp.ml.metrics provides some functionality to quantify the quality of predictions.

2.1 AUROC

TOC

Description

Calculates the Area under the Receiver Operation Chracteristic Curve.

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values
num_thresholds int, default = 100 Number of thresholds to compute.

Returns

Receiver Operation Chracteristic Area under the Curve : float

2.2 F1_Score

TOC

Description

F1 Score. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values
threshold float All values that are greater than or equal to the threshold are considered a positive class.

Returns

F1 Score : float

Equations

$precision = \frac{TP}{TP + FP}$

$recall = \frac{TP}{TP + FN}$

$F_1 = \frac{2 \cdot precision \cdot recall}{precision + recall} = \frac{2 \cdot TP}{2 \cdot TP + FP + FN}$

Example

import rsp.ml.metrics as m

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

f1score = m.F1_Score(Y, T)

print(f1score) --> 0.5

2.3 FN

TOC

Description

False negatives. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values
threshold float All values that are greater than or equal to the threshold are considered a positive class.

Returns

False negatives : int

Example

import rsp.ml.metrics as m
import torch

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

fn = m.FN(Y, T)
print(fn) -> 1

2.4 FP

TOC

Description

False positives. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values
threshold float All values that are greater than or equal to the threshold are considered a positive class.

Returns

False positives : int

Example

import rsp.ml.metrics as m
import torch

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

fp = m.FP(Y, T)
print(fp) -> 1

2.5 FPR

TOC

Description

False positive rate. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values
threshold float All values that are greater than or equal to the threshold are considered a positive class.

Returns

False positive rate : float

Example

import rsp.ml.metrics as m
import torch

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

fpr = m.FPR(Y, T)
print(fpr) -> 0.08333333333333333

2.6 ROC

TOC

Description

Calculates the receiver operating characteristic: computes False Positive Rates and True positive Rates for num_thresholds aligned between 0 and 1

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values
num_thresholds int, default = 100 Number of thresholds to compute.

Returns

(False Positive Rates, True Positive Rates) for 100 different thresholds : (List[float], List[float])

Example

import rsp.ml.metrics as m
import torch
import torch.nn.functional as F

num_elements = 100000
num_classes = 7

T = []
for i in range(num_elements):
  true_class = torch.randint(0, num_classes, (1,))
  t = F.one_hot(true_class, num_classes=num_classes)
  T.append(t)
T = torch.cat(T)

dist = torch.normal(T.float(), 1.5)
Y = F.softmax(dist, dim = 1)
FPRs, TPRs = m.ROC(Y, T)

2.7 TN

TOC

Description

True negatives. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values
threshold float All values that are greater than or equal to the threshold are considered a positive class.

Returns

True negatives : int

Example

import rsp.ml.metrics as m
import torch

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

tn = m.TN(Y, T)
print(tn) -> 11

2.8 TP

TOC

Description

True positives. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values
threshold float All values that are greater than or equal to the threshold are considered a positive class.

Returns

True positives : int

Example

import rsp.ml.metrics as m
import torch

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

tp = m.TP(Y, T)
print(tp) -> 5

2.9 TPR

TOC

Description

True positive rate. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values
threshold float All values that are greater than or equal to the threshold are considered a positive class.

Returns

True positive rate : float

Example

import rsp.ml.metrics as m
import torch

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

tpr = m.TPR(Y, T)
print(tpr) -> 0.8333333333333334

2.10 confusion_matrix

TOC

Description

Calculates the confusion matrix. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values

Returns

Confusion matrix : torch.Tensor

Example

import rsp.ml.metrics as m
import torch

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

conf_mat = m.confusion_matrix(Y, T)
print(conf_mat) -> tensor([
  [1, 1, 0],
  [0, 2, 0],
  [0, 0, 2]
])

2.11 plot_ROC

TOC

Description

Plot the receiver operating characteristic.

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values
num_thresholds int, default = 100 Number of thresholds to compute.
title str, optional, default = 'Confusion Matrix' Title of the plot
class_curves bool, default = False Plot ROC curve for each class
labels str, optional, default = None Class labels -> automatic labeling C000, ..., CXXX if labels is None
plt_show bool, optional, default = False Set to True to show the plot
save_file_name str, optional, default = None If not None, the plot is saved under the specified save_file_name.

Returns

Image of the confusion matrix : np.array

2.12 plot_confusion_matrix

TOC

Description

Plot the confusion matrix

Parameters

Name Type Description
confusion_matrix torch.Tensor Confusion matrix
labels str, optional, default = None Class labels -> automatic labeling C000, ..., CXXX if labels is None
cmap str, optional, default = 'Blues' Seaborn cmap, see https://r02b.github.io/seaborn_palettes/
xlabel str, optional, default = 'Predicted label' X-Axis label
ylabel str, optional, default = 'True label' Y-Axis label
title str, optional, default = 'Confusion Matrix' Title of the plot
plt_show bool, optional, default = False Set to True to show the plot
save_file_name str, optional, default = None If not None, the plot is saved under the specified save_file_name.

Returns

Image of the confusion matrix : np.array

2.13 precision

TOC

Description

Precision. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values
threshold float All values that are greater than or equal to the threshold are considered a positive class.

Returns

Precision : float

Equations

$precision = \frac{TP}{TP + FP}$

Example

import rsp.ml.metrics as m
import torch

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

precision = m.precision(Y, T)
print(precision) -> 0.8333333333333334

2.14 recall

TOC

Description

Recall. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values
threshold float All values that are greater than or equal to the threshold are considered a positive class.

Returns

Recall : float

Equations

$recall = \frac{TP}{TP + FN}$

Example

import rsp.ml.metrics as m
import torch

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

recall = m.recall(Y, T)
print(recall) -> 0.8333333333333334

2.15 top_10_accuracy

TOC

Description

Top 10 accuracy. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values

Returns

Top 10 accuracy -> top k accuracy | k = 10 : float

Example

import rsp.ml.metrics as m

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

top_10_accuracy = m.top_10_accuracy(Y, T, k = 3)

print(top_10_accuracy) --> 1.0

2.16 top_1_accuracy

TOC

Description

Top 1 accuracy. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values

Returns

Top 1 accuracy -> top k accuracy | k = 1 : float

Example

import rsp.ml.metrics as m

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

top_1_accuracy = m.top_1_accuracy(Y, T, k = 3)

print(top_1_accuracy) --> 0.8333333333333334

2.17 top_2_accuracy

TOC

Description

Top 2 accuracy. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values

Returns

Top 2 accuracy -> top k accuracy | k = 2 : float

Example

import rsp.ml.metrics as m

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

top_2_accuracy = m.top_2_accuracy(Y, T, k = 3)

print(top_2_accuracy) --> 1.0

2.18 top_3_accuracy

TOC

Description

Top 3 accuracy. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values

Returns

Top 3 accuracy -> top k accuracy | k = 3 : float

Example

import rsp.ml.metrics as m

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

top_3_accuracy = m.top_3_accuracy(Y, T, k = 3)

print(top_3_accuracy) --> 1.0

2.19 top_5_accuracy

TOC

Description

Top 5 accuracy. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values

Returns

Top 5 accuracy -> top k accuracy | k = 5 : float

Example

import rsp.ml.metrics as m

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

top_5_accuracy = m.top_5_accuracy(Y, T, k = 3)

print(top_5_accuracy) --> 1.0

2.20 top_k_accuracy

TOC

Description

Top k accuracy. Expected input shape: (batch_size, num_classes)

Parameters

Name Type Description
Y torch.Tensor Prediction
T torch.Tensor True values

Returns

Top k accuracy : float

Example

import rsp.ml.metrics as m

Y = torch.tensor([
  [0.1, 0.1, 0.8],
  [0.03, 0.95, 0.02],
  [0.05, 0.9, 0.05],
  [0.01, 0.87, 0.12],
  [0.04, 0.03, 0.93],
  [0.94, 0.02, 0.06]
])
T = torch.tensor([
  [0, 0, 1],
  [1, 0, 0],
  [0, 1, 0],
  [0, 1, 0],
  [0, 0, 1],
  [1, 0, 0]
])

top_k_accuracy = m.top_k_accuracy(Y, T, k = 3)

print(top_k_accuracy) --> 1.0

3 model

TOC

The module rsp.ml.model provides some usefull functionality to store and load pytorch models.

3.1 MODELS : enum.Enum

TOC

Description

Create a collection of name/value pairs.

Example enumeration:

class Color(Enum): ... RED = 1 ... BLUE = 2 ... GREEN = 3

Access them by:

  • attribute access::

Color.RED <Color.RED: 1>

  • value lookup:

Color(1) <Color.RED: 1>

  • name lookup:

Color['RED'] <Color.RED: 1>

Enumerations can be iterated over, and know how many members they have:

len(Color) 3

list(Color) [<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes -- see the documentation for details.

3.2 WEIGHTS : enum.Enum

TOC

Description

Create a collection of name/value pairs.

Example enumeration:

class Color(Enum): ... RED = 1 ... BLUE = 2 ... GREEN = 3

Access them by:

  • attribute access::

Color.RED <Color.RED: 1>

  • value lookup:

Color(1) <Color.RED: 1>

  • name lookup:

Color['RED'] <Color.RED: 1>

Enumerations can be iterated over, and know how many members they have:

len(Color) 3

list(Color) [<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes -- see the documentation for details.

3.3 list_model_weights

TOC

Description

Lists all available weight files.

Returns

List of (MODEL:str, WEIGHT:str) : List[Tuple(str, str)]

Example

import rsp.ml.model as model

model_weight_files = model.list_model_weights()

3.4 load_model

TOC

Description

Loads a pretrained PyTorch model from HuggingFace.

Parameters

Name Type Description
model MODELS ID of the model
weights WEIGHTS ID of the weights

Returns

Pretrained PyTorch model : torch.nn.Module

Example

import rsp.ml.model as model

action_recognition_model = model.load_model(MODEL.TUCARC3D, WEIGHTS.TUCAR)

3.5 publish_model

TOC

4 multi_transforms

TOC

The module rsp.ml.multi_transforms is based on torchvision.transforms, which is made for single images. rsp.ml.multi_transforms extends this functionality by providing transformations for sequences of images, which could be usefull for video augmentation.

4.1 BGR2GRAY : MultiTransform

TOC

Description

Converts a sequence of BGR images to grayscale images.

4.1.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.1.2 __init__

TOC

Description

Initializes a new instance.

4.2 BGR2RGB : MultiTransform

TOC

Description

Converts sequence of BGR images to RGB images.

4.2.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.2.2 __init__

TOC

Description

Initializes a new instance.

4.3 Brightness : MultiTransform

TOC

Description

MultiTransform is an extension to keep the same transformation over a sequence of images instead of initializing a new transformation for every single image. It is inspired by torchvision.transforms and could be used for video augmentation. Use rsp.ml.multi_transforms.Composeto combine multiple image sequence transformations.

Note rsp.ml.multi_transforms.MultiTransform is a base class and should be inherited.

4.3.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.3.2 __init__

TOC

Description

Initializes a new instance.

4.4 CenterCrop : MultiTransform

TOC

Description

Crops Images at the center after upscaling them. Dimensions kept the same.

4.4.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.4.2 __init__

TOC

Description

Initializes a new instance.

Parameters

Name Type Description
max_scale float Images are scaled randomly between 1. and max_scale before cropping to original size.

4.5 Color : MultiTransform

TOC

Description

MultiTransform is an extension to keep the same transformation over a sequence of images instead of initializing a new transformation for every single image. It is inspired by torchvision.transforms and could be used for video augmentation. Use rsp.ml.multi_transforms.Composeto combine multiple image sequence transformations.

Note rsp.ml.multi_transforms.MultiTransform is a base class and should be inherited.

4.5.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.5.2 __init__

TOC

Description

Initializes a new instance.

4.6 Compose : builtins.object

TOC

Description

Composes several MultiTransforms together.

Example

import rsp.ml.multi_transforms as t

transforms = t.Compose([
  t.BGR2GRAY(),
  t.Scale(0.5)
])

4.6.1 __call__

TOC

Description

Call self as a function.

4.6.2 __init__

TOC

Description

Initializes a new instance.

Parameters

Name Type Description
children List[MultiTransform] List of MultiTransforms to compose.

4.7 GaussianNoise : MultiTransform

TOC

Description

MultiTransform is an extension to keep the same transformation over a sequence of images instead of initializing a new transformation for every single image. It is inspired by torchvision.transforms and could be used for video augmentation. Use rsp.ml.multi_transforms.Composeto combine multiple image sequence transformations.

Note rsp.ml.multi_transforms.MultiTransform is a base class and should be inherited.

4.7.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.7.2 __init__

TOC

Description

Initializes a new instance.

4.8 MultiTransform : builtins.object

TOC

Description

MultiTransform is an extension to keep the same transformation over a sequence of images instead of initializing a new transformation for every single image. It is inspired by torchvision.transforms and could be used for video augmentation. Use rsp.ml.multi_transforms.Composeto combine multiple image sequence transformations.

Note rsp.ml.multi_transforms.MultiTransform is a base class and should be inherited.

4.8.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.8.2 __init__

TOC

Description

Initializes a new instance.

4.9 Normalize : MultiTransform

TOC

Description

Normalize images with mean and standard deviation. Given mean: (mean[1],...,mean[n]) and std: (std[1],..,std[n]) for n channels, this transform will normalize each channel of the input torch.*Tensor i.e., output[channel] = (input[channel] - mean[channel]) / std[channel]

Based on torchvision.transforms.Normalize

4.9.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.9.2 __init__

TOC

Description

Initializes a new instance.

Parameters

Name Type Description
mean List[float] Sequence of means for each channel.
std List[float] Sequence of standard deviations for each channel.
inplace bool Set to True make this operation in-place.

4.10 RGB2BGR : BGR2RGB

TOC

Description

Converts sequence of RGB images to BGR images.

4.10.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.10.2 __init__

TOC

Description

Initializes a new instance.

4.11 RandomCrop : MultiTransform

TOC

Description

Crops Images at a random location after upscaling them. Dimensions kept the same.

4.11.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.11.2 __init__

TOC

Description

Initializes a new instance.

Parameters

Name Type Description
max_scale float Images are scaled randomly between 1. and max_scale before cropping to original size.

4.12 RandomHorizontalFlip : MultiTransform

TOC

Description

MultiTransform is an extension to keep the same transformation over a sequence of images instead of initializing a new transformation for every single image. It is inspired by torchvision.transforms and could be used for video augmentation. Use rsp.ml.multi_transforms.Composeto combine multiple image sequence transformations.

Note rsp.ml.multi_transforms.MultiTransform is a base class and should be inherited.

4.12.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.12.2 __init__

TOC

Description

Initializes a new instance.

4.13 RandomVerticalFlip : MultiTransform

TOC

Description

MultiTransform is an extension to keep the same transformation over a sequence of images instead of initializing a new transformation for every single image. It is inspired by torchvision.transforms and could be used for video augmentation. Use rsp.ml.multi_transforms.Composeto combine multiple image sequence transformations.

Note rsp.ml.multi_transforms.MultiTransform is a base class and should be inherited.

4.13.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.13.2 __init__

TOC

Description

Initializes a new instance.

4.14 Resize : MultiTransform

TOC

Description

MultiTransform is an extension to keep the same transformation over a sequence of images instead of initializing a new transformation for every single image. It is inspired by torchvision.transforms and could be used for video augmentation. Use rsp.ml.multi_transforms.Composeto combine multiple image sequence transformations.

Note rsp.ml.multi_transforms.MultiTransform is a base class and should be inherited.

4.14.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.14.2 __init__

TOC

Description

Initializes a new instance.

4.15 Rotate : MultiTransform

TOC

Description

Randomly rotates images.

Equations

$angle = -max_angle + 2 \cdot random() \cdot max_angle$

4.15.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.15.2 __init__

TOC

Description

Iitializes a new instance.

Parameters

Name Type Description
max_angle float Maximal rotation in degrees
auto_scale bool, default = True Image will be resized when auto scale is activated to avoid black margins.

4.16 Satturation : MultiTransform

TOC

Description

MultiTransform is an extension to keep the same transformation over a sequence of images instead of initializing a new transformation for every single image. It is inspired by torchvision.transforms and could be used for video augmentation. Use rsp.ml.multi_transforms.Composeto combine multiple image sequence transformations.

Note rsp.ml.multi_transforms.MultiTransform is a base class and should be inherited.

4.16.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.16.2 __init__

TOC

Description

Initializes a new instance.

4.17 Scale : MultiTransform

TOC

Description

MultiTransform is an extension to keep the same transformation over a sequence of images instead of initializing a new transformation for every single image. It is inspired by torchvision.transforms and could be used for video augmentation. Use rsp.ml.multi_transforms.Composeto combine multiple image sequence transformations.

Note rsp.ml.multi_transforms.MultiTransform is a base class and should be inherited.

4.17.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.17.2 __init__

TOC

Description

Initializes a new instance.

4.18 Stack : MultiTransform

TOC

Description

MultiTransform is an extension to keep the same transformation over a sequence of images instead of initializing a new transformation for every single image. It is inspired by torchvision.transforms and could be used for video augmentation. Use rsp.ml.multi_transforms.Composeto combine multiple image sequence transformations.

Note rsp.ml.multi_transforms.MultiTransform is a base class and should be inherited.

4.18.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.18.2 __init__

TOC

Description

Initializes a new instance.

4.19 ToCVImage : MultiTransform

TOC

Description

Converts a torch.Tensorto Open CV image by changing dimensions (d0, d1, d2) -> (d1, d2, d0) and converting torch.Tensor to numpy.

4.19.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.19.2 __init__

TOC

Description

Initializes a new instance.

4.20 ToNumpy : MultiTransform

TOC

Description

Converts a torch.Tensorto numpy

4.20.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.20.2 __init__

TOC

Description

Initializes a new instance.

4.21 ToPILImage : MultiTransform

TOC

Description

Converts sequence of images to sequence of PIL.Image.

4.21.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.21.2 __init__

TOC

Description

Initializes a new instance.

4.22 ToTensor : MultiTransform

TOC

Description

Converts a sequence of images to torch.Tensor.

4.22.1 __call__

TOC

Description

Call self as a function.

Parameters

Name Type Description
input torch.Tensor
List[PIL.Image]
List[numpy.array]
Sequence of images

4.22.2 __init__

TOC

Description

Initializes a new instance.

5 run

TOC

The module rsp.ml.run provides some tools for storing, loading and visualizing data during training of models using PyTorch.

5.1 Run : builtins.object

TOC

5.1.1 __init__

TOC

Description

Initialize self. See help(type(self)) for accurate signature.

5.1.2 append

TOC

5.1.3 get_avg

TOC

5.1.4 get_val

TOC

5.1.5 len

TOC

5.1.6 load_best_state_dict

TOC

5.1.7 load_state_dict

TOC

5.1.8 pickle_dump

TOC

5.1.9 pickle_load

TOC

5.1.10 plot

TOC

5.1.11 recalculate_moving_average

TOC

5.1.12 save

TOC

5.1.13 save_best_state_dict

TOC

5.1.14 save_state_dict

TOC

5.1.15 train_epoch

TOC

5.1.16 validate_epoch

TOC

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rsp_ml-0.0.93.tar.gz (43.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rsp_ml-0.0.93-py3-none-any.whl (31.3 kB view details)

Uploaded Python 3

File details

Details for the file rsp_ml-0.0.93.tar.gz.

File metadata

  • Download URL: rsp_ml-0.0.93.tar.gz
  • Upload date:
  • Size: 43.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.0

File hashes

Hashes for rsp_ml-0.0.93.tar.gz
Algorithm Hash digest
SHA256 96bb34ebcbdbc72d23c58155e0078cc94b73e43f1883dcc8ff3eff82659a2931
MD5 010e3fd7d926f9ca6935c6a064e10f4d
BLAKE2b-256 9059bb16175d0d5c83fe71018db6816662c906454aed68c72254320e5c84ab09

See more details on using hashes here.

File details

Details for the file rsp_ml-0.0.93-py3-none-any.whl.

File metadata

  • Download URL: rsp_ml-0.0.93-py3-none-any.whl
  • Upload date:
  • Size: 31.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.0

File hashes

Hashes for rsp_ml-0.0.93-py3-none-any.whl
Algorithm Hash digest
SHA256 fd6df35a90e11d313db633453dd6f36b3347b2fc20ceda472eb6a0d335cff604
MD5 bdc9270a235cec5e5207ffe2ded1aafc
BLAKE2b-256 f12812247ad1641e3e2927938f81b772b7648ef9807c440d97aa944ae2c40c66

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page