rsp-ml 0.0.149
Machine Learning
License: MIT
Keywords: python, Machine Learning

# RSProduction MachineLearning

This project provides some usefull machine learning functionality.

# Table of Contents

- [1 dataset](#1-dataset)
  - [1.1 HMDB51 : torch.utils.data.dataset.Dataset](#11-hmdb51--torchutilsdatadatasetdataset)
    - [1.1.1 \_\_init\_\_](#111-\_\_init\_\_)
  - [1.2 Kinetics : torch.utils.data.dataset.Dataset](#12-kinetics--torchutilsdatadatasetdataset)
    - [1.2.1 \_\_init\_\_](#121-\_\_init\_\_)
  - [1.3 TUCHRI : torch.utils.data.dataset.Dataset](#13-tuchri--torchutilsdatadatasetdataset)
    - [1.3.1 \_\_init\_\_](#131-\_\_init\_\_)
    - [1.3.2 get\_uniform\_sampler](#132-get\_uniform\_sampler)
    - [1.3.3 load\_backgrounds](#133-load\_backgrounds)
  - [1.4 TUCRID : torch.utils.data.dataset.Dataset](#14-tucrid--torchutilsdatadatasetdataset)
    - [1.4.1 \_\_init\_\_](#141-\_\_init\_\_)
    - [1.4.2 get\_uniform\_sampler](#142-get\_uniform\_sampler)
    - [1.4.3 load\_backgrounds](#143-load\_backgrounds)
  - [1.5 UCF101 : torch.utils.data.dataset.Dataset](#15-ucf101--torchutilsdatadatasetdataset)
    - [1.5.1 \_\_init\_\_](#151-\_\_init\_\_)
  - [1.6 UTKinectAction3D : torch.utils.data.dataset.Dataset](#16-utkinectaction3d--torchutilsdatadatasetdataset)
    - [1.6.1 \_\_init\_\_](#161-\_\_init\_\_)
- [2 metrics](#2-metrics)
  - [2.1 AUROC](#21-auroc)
  - [2.2 F1\_Score](#22-f1\_score)
  - [2.3 FN](#23-fn)
  - [2.4 FP](#24-fp)
  - [2.5 FPR](#25-fpr)
  - [2.6 ROC](#26-roc)
  - [2.7 TN](#27-tn)
  - [2.8 TP](#28-tp)
  - [2.9 TPR](#29-tpr)
  - [2.10 confusion\_matrix](#210-confusion\_matrix)
  - [2.11 plot\_ROC](#211-plot\_roc)
  - [2.12 plot\_confusion\_matrix](#212-plot\_confusion\_matrix)
  - [2.13 precision](#213-precision)
  - [2.14 recall](#214-recall)
  - [2.15 top\_10\_accuracy](#215-top\_10\_accuracy)
  - [2.16 top\_1\_accuracy](#216-top\_1\_accuracy)
  - [2.17 top\_2\_accuracy](#217-top\_2\_accuracy)
  - [2.18 top\_3\_accuracy](#218-top\_3\_accuracy)
  - [2.19 top\_5\_accuracy](#219-top\_5\_accuracy)
  - [2.20 top\_k\_accuracy](#220-top\_k\_accuracy)
- [3 model](#3-model)
  - [3.1 MODELS : enum.Enum](#31-models--enumenum)
  - [3.2 WEIGHTS : enum.Enum](#32-weights--enumenum)
  - [3.3 list\_model\_weights](#33-list\_model\_weights)
  - [3.4 load\_model](#34-load\_model)
  - [3.5 publish\_model](#35-publish\_model)
- [4 module](#4-module)
  - [4.1 MultiHeadSelfAttention : torch.nn.modules.module.Module](#41-multiheadselfattention--torchnnmodulesmodulemodule)
        - [4.1.1 \_wrapped\_call\_impl](#411-\_wrapped\_call\_impl)
    - [4.1.2 \_\_init\_\_](#412-\_\_init\_\_)
        - [4.1.3 \_apply](#413-\_apply)
        - [4.1.4 \_call\_impl](#414-\_call\_impl)
        - [4.1.5 \_get\_backward\_hooks](#415-\_get\_backward\_hooks)
        - [4.1.6 \_get\_backward\_pre\_hooks](#416-\_get\_backward\_pre\_hooks)
        - [4.1.7 \_get\_name](#417-\_get\_name)
        - [4.1.8 \_load\_from\_state\_dict](#418-\_load\_from\_state\_dict)
        - [4.1.9 \_maybe\_warn\_non\_full\_backward\_hook](#419-\_maybe\_warn\_non\_full\_backward\_hook)
        - [4.1.10 \_named\_members](#4110-\_named\_members)
        - [4.1.11 \_register\_load\_state\_dict\_pre\_hook](#4111-\_register\_load\_state\_dict\_pre\_hook)
        - [4.1.12 \_register\_state\_dict\_hook](#4112-\_register\_state\_dict\_hook)
        - [4.1.13 \_replicate\_for\_data\_parallel](#4113-\_replicate\_for\_data\_parallel)
        - [4.1.14 \_save\_to\_state\_dict](#4114-\_save\_to\_state\_dict)
        - [4.1.15 \_slow\_forward](#4115-\_slow\_forward)
        - [4.1.16 \_wrapped\_call\_impl](#4116-\_wrapped\_call\_impl)
        - [4.1.17 add\_module](#4117-add\_module)
        - [4.1.18 apply](#4118-apply)
        - [4.1.19 bfloat16](#4119-bfloat16)
        - [4.1.20 buffers](#4120-buffers)
- [  >>> # xdoctest: +SKIP("undefined vars")](#-->>>-#-xdoctest--+skip("undefined-vars"))
        - [4.1.21 children](#4121-children)
        - [4.1.22 compile](#4122-compile)
        - [4.1.23 cpu](#4123-cpu)
        - [4.1.24 cuda](#4124-cuda)
        - [4.1.25 double](#4125-double)
        - [4.1.26 eval](#4126-eval)
        - [4.1.27 extra\_repr](#4127-extra\_repr)
        - [4.1.28 float](#4128-float)
    - [4.1.29 forward](#4129-forward)
        - [4.1.30 get\_buffer](#4130-get\_buffer)
        - [4.1.31 get\_extra\_state](#4131-get\_extra\_state)
        - [4.1.32 get\_parameter](#4132-get\_parameter)
        - [4.1.33 get\_submodule](#4133-get\_submodule)
        - [4.1.34 half](#4134-half)
        - [4.1.35 ipu](#4135-ipu)
        - [4.1.36 load\_state\_dict](#4136-load\_state\_dict)
        - [4.1.37 modules](#4137-modules)
        - [4.1.38 mtia](#4138-mtia)
        - [4.1.39 named\_buffers](#4139-named\_buffers)
- [  >>> # xdoctest: +SKIP("undefined vars")](#-->>>-#-xdoctest--+skip("undefined-vars"))
        - [4.1.40 named\_children](#4140-named\_children)
- [  >>> # xdoctest: +SKIP("undefined vars")](#-->>>-#-xdoctest--+skip("undefined-vars"))
        - [4.1.41 named\_modules](#4141-named\_modules)
        - [4.1.42 named\_parameters](#4142-named\_parameters)
- [  >>> # xdoctest: +SKIP("undefined vars")](#-->>>-#-xdoctest--+skip("undefined-vars"))
        - [4.1.43 parameters](#4143-parameters)
- [  >>> # xdoctest: +SKIP("undefined vars")](#-->>>-#-xdoctest--+skip("undefined-vars"))
        - [4.1.44 register\_backward\_hook](#4144-register\_backward\_hook)
        - [4.1.45 register\_buffer](#4145-register\_buffer)
- [  >>> # xdoctest: +SKIP("undefined vars")](#-->>>-#-xdoctest--+skip("undefined-vars"))
        - [4.1.46 register\_forward\_hook](#4146-register\_forward\_hook)
        - [4.1.47 register\_forward\_pre\_hook](#4147-register\_forward\_pre\_hook)
        - [4.1.48 register\_full\_backward\_hook](#4148-register\_full\_backward\_hook)
        - [4.1.49 register\_full\_backward\_pre\_hook](#4149-register\_full\_backward\_pre\_hook)
        - [4.1.50 register\_load\_state\_dict\_post\_hook](#4150-register\_load\_state\_dict\_post\_hook)
        - [4.1.51 register\_load\_state\_dict\_pre\_hook](#4151-register\_load\_state\_dict\_pre\_hook)
- [  hook(module, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs) -> None  # noqa: B950](#--hook(module,-state_dict,-prefix,-local_metadata,-strict,-missing_keys,-unexpected_keys,-error_msgs)-->-none--#-noqa--b950)
        - [4.1.52 register\_module](#4152-register\_module)
        - [4.1.53 register\_parameter](#4153-register\_parameter)
        - [4.1.54 register\_state\_dict\_post\_hook](#4154-register\_state\_dict\_post\_hook)
        - [4.1.55 register\_state\_dict\_pre\_hook](#4155-register\_state\_dict\_pre\_hook)
        - [4.1.56 requires\_grad\_](#4156-requires\_grad\_)
        - [4.1.57 set\_extra\_state](#4157-set\_extra\_state)
        - [4.1.58 set\_submodule](#4158-set\_submodule)
        - [4.1.59 share\_memory](#4159-share\_memory)
        - [4.1.60 state\_dict](#4160-state\_dict)
- [  >>> # xdoctest: +SKIP("undefined vars")](#-->>>-#-xdoctest--+skip("undefined-vars"))
        - [4.1.61 to](#4161-to)
- [  >>> # xdoctest: +IGNORE_WANT("non-deterministic")](#-->>>-#-xdoctest--+ignore_want("non-deterministic"))
- [  >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA1)](#-->>>-#-xdoctest--+requires(env-torch_doctest_cuda1))
        - [4.1.62 to\_empty](#4162-to\_empty)
        - [4.1.63 train](#4163-train)
        - [4.1.64 type](#4164-type)
        - [4.1.65 xpu](#4165-xpu)
        - [4.1.66 zero\_grad](#4166-zero\_grad)
  - [4.2 SelfAttention : torch.nn.modules.module.Module](#42-selfattention--torchnnmodulesmodulemodule)
        - [4.2.1 \_wrapped\_call\_impl](#421-\_wrapped\_call\_impl)
    - [4.2.2 \_\_init\_\_](#422-\_\_init\_\_)
        - [4.2.3 \_apply](#423-\_apply)
        - [4.2.4 \_call\_impl](#424-\_call\_impl)
        - [4.2.5 \_get\_backward\_hooks](#425-\_get\_backward\_hooks)
        - [4.2.6 \_get\_backward\_pre\_hooks](#426-\_get\_backward\_pre\_hooks)
        - [4.2.7 \_get\_name](#427-\_get\_name)
        - [4.2.8 \_load\_from\_state\_dict](#428-\_load\_from\_state\_dict)
        - [4.2.9 \_maybe\_warn\_non\_full\_backward\_hook](#429-\_maybe\_warn\_non\_full\_backward\_hook)
        - [4.2.10 \_named\_members](#4210-\_named\_members)
        - [4.2.11 \_register\_load\_state\_dict\_pre\_hook](#4211-\_register\_load\_state\_dict\_pre\_hook)
        - [4.2.12 \_register\_state\_dict\_hook](#4212-\_register\_state\_dict\_hook)
        - [4.2.13 \_replicate\_for\_data\_parallel](#4213-\_replicate\_for\_data\_parallel)
        - [4.2.14 \_save\_to\_state\_dict](#4214-\_save\_to\_state\_dict)
        - [4.2.15 \_slow\_forward](#4215-\_slow\_forward)
        - [4.2.16 \_wrapped\_call\_impl](#4216-\_wrapped\_call\_impl)
        - [4.2.17 add\_module](#4217-add\_module)
        - [4.2.18 apply](#4218-apply)
        - [4.2.19 bfloat16](#4219-bfloat16)
        - [4.2.20 buffers](#4220-buffers)
- [  >>> # xdoctest: +SKIP("undefined vars")](#-->>>-#-xdoctest--+skip("undefined-vars"))
        - [4.2.21 children](#4221-children)
        - [4.2.22 compile](#4222-compile)
        - [4.2.23 cpu](#4223-cpu)
        - [4.2.24 cuda](#4224-cuda)
        - [4.2.25 double](#4225-double)
        - [4.2.26 eval](#4226-eval)
        - [4.2.27 extra\_repr](#4227-extra\_repr)
        - [4.2.28 float](#4228-float)
    - [4.2.29 forward](#4229-forward)
        - [4.2.30 get\_buffer](#4230-get\_buffer)
        - [4.2.31 get\_extra\_state](#4231-get\_extra\_state)
        - [4.2.32 get\_parameter](#4232-get\_parameter)
        - [4.2.33 get\_submodule](#4233-get\_submodule)
        - [4.2.34 half](#4234-half)
        - [4.2.35 ipu](#4235-ipu)
        - [4.2.36 load\_state\_dict](#4236-load\_state\_dict)
        - [4.2.37 modules](#4237-modules)
        - [4.2.38 mtia](#4238-mtia)
        - [4.2.39 named\_buffers](#4239-named\_buffers)
- [  >>> # xdoctest: +SKIP("undefined vars")](#-->>>-#-xdoctest--+skip("undefined-vars"))
        - [4.2.40 named\_children](#4240-named\_children)
- [  >>> # xdoctest: +SKIP("undefined vars")](#-->>>-#-xdoctest--+skip("undefined-vars"))
        - [4.2.41 named\_modules](#4241-named\_modules)
        - [4.2.42 named\_parameters](#4242-named\_parameters)
- [  >>> # xdoctest: +SKIP("undefined vars")](#-->>>-#-xdoctest--+skip("undefined-vars"))
        - [4.2.43 parameters](#4243-parameters)
- [  >>> # xdoctest: +SKIP("undefined vars")](#-->>>-#-xdoctest--+skip("undefined-vars"))
        - [4.2.44 register\_backward\_hook](#4244-register\_backward\_hook)
        - [4.2.45 register\_buffer](#4245-register\_buffer)
- [  >>> # xdoctest: +SKIP("undefined vars")](#-->>>-#-xdoctest--+skip("undefined-vars"))
        - [4.2.46 register\_forward\_hook](#4246-register\_forward\_hook)
        - [4.2.47 register\_forward\_pre\_hook](#4247-register\_forward\_pre\_hook)
        - [4.2.48 register\_full\_backward\_hook](#4248-register\_full\_backward\_hook)
        - [4.2.49 register\_full\_backward\_pre\_hook](#4249-register\_full\_backward\_pre\_hook)
        - [4.2.50 register\_load\_state\_dict\_post\_hook](#4250-register\_load\_state\_dict\_post\_hook)
        - [4.2.51 register\_load\_state\_dict\_pre\_hook](#4251-register\_load\_state\_dict\_pre\_hook)
- [  hook(module, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs) -> None  # noqa: B950](#--hook(module,-state_dict,-prefix,-local_metadata,-strict,-missing_keys,-unexpected_keys,-error_msgs)-->-none--#-noqa--b950)
        - [4.2.52 register\_module](#4252-register\_module)
        - [4.2.53 register\_parameter](#4253-register\_parameter)
        - [4.2.54 register\_state\_dict\_post\_hook](#4254-register\_state\_dict\_post\_hook)
        - [4.2.55 register\_state\_dict\_pre\_hook](#4255-register\_state\_dict\_pre\_hook)
        - [4.2.56 requires\_grad\_](#4256-requires\_grad\_)
        - [4.2.57 set\_extra\_state](#4257-set\_extra\_state)
        - [4.2.58 set\_submodule](#4258-set\_submodule)
        - [4.2.59 share\_memory](#4259-share\_memory)
        - [4.2.60 state\_dict](#4260-state\_dict)
- [  >>> # xdoctest: +SKIP("undefined vars")](#-->>>-#-xdoctest--+skip("undefined-vars"))
        - [4.2.61 to](#4261-to)
- [  >>> # xdoctest: +IGNORE_WANT("non-deterministic")](#-->>>-#-xdoctest--+ignore_want("non-deterministic"))
- [  >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA1)](#-->>>-#-xdoctest--+requires(env-torch_doctest_cuda1))
        - [4.2.62 to\_empty](#4262-to\_empty)
        - [4.2.63 train](#4263-train)
        - [4.2.64 type](#4264-type)
        - [4.2.65 xpu](#4265-xpu)
        - [4.2.66 zero\_grad](#4266-zero\_grad)
- [5 multi\_transforms](#5-multi\_transforms)
  - [5.1 BGR2GRAY : MultiTransform](#51-bgr2gray--multitransform)
    - [5.1.1 \_\_call\_\_](#511-\_\_call\_\_)
    - [5.1.2 \_\_init\_\_](#512-\_\_init\_\_)
  - [5.2 BGR2RGB : MultiTransform](#52-bgr2rgb--multitransform)
    - [5.2.1 \_\_call\_\_](#521-\_\_call\_\_)
    - [5.2.2 \_\_init\_\_](#522-\_\_init\_\_)
  - [5.3 Brightness : MultiTransform](#53-brightness--multitransform)
    - [5.3.1 \_\_call\_\_](#531-\_\_call\_\_)
    - [5.3.2 \_\_init\_\_](#532-\_\_init\_\_)
  - [5.4 CenterCrop : MultiTransform](#54-centercrop--multitransform)
    - [5.4.1 \_\_call\_\_](#541-\_\_call\_\_)
    - [5.4.2 \_\_init\_\_](#542-\_\_init\_\_)
  - [5.5 Color : MultiTransform](#55-color--multitransform)
    - [5.5.1 \_\_call\_\_](#551-\_\_call\_\_)
    - [5.5.2 \_\_init\_\_](#552-\_\_init\_\_)
  - [5.6 Compose : builtins.object](#56-compose--builtinsobject)
    - [5.6.1 \_\_call\_\_](#561-\_\_call\_\_)
    - [5.6.2 \_\_init\_\_](#562-\_\_init\_\_)
  - [5.7 GaussianNoise : MultiTransform](#57-gaussiannoise--multitransform)
    - [5.7.1 \_\_call\_\_](#571-\_\_call\_\_)
    - [5.7.2 \_\_init\_\_](#572-\_\_init\_\_)
  - [5.8 MultiTransform : builtins.object](#58-multitransform--builtinsobject)
    - [5.8.1 \_\_call\_\_](#581-\_\_call\_\_)
    - [5.8.2 \_\_init\_\_](#582-\_\_init\_\_)
  - [5.9 Normalize : MultiTransform](#59-normalize--multitransform)
    - [5.9.1 \_\_call\_\_](#591-\_\_call\_\_)
    - [5.9.2 \_\_init\_\_](#592-\_\_init\_\_)
  - [5.10 RGB2BGR : BGR2RGB](#510-rgb2bgr--bgr2rgb)
    - [5.10.1 \_\_call\_\_](#5101-\_\_call\_\_)
    - [5.10.2 \_\_init\_\_](#5102-\_\_init\_\_)
  - [5.11 RandomCrop : MultiTransform](#511-randomcrop--multitransform)
    - [5.11.1 \_\_call\_\_](#5111-\_\_call\_\_)
    - [5.11.2 \_\_init\_\_](#5112-\_\_init\_\_)
  - [5.12 RandomHorizontalFlip : MultiTransform](#512-randomhorizontalflip--multitransform)
    - [5.12.1 \_\_call\_\_](#5121-\_\_call\_\_)
    - [5.12.2 \_\_init\_\_](#5122-\_\_init\_\_)
  - [5.13 RandomVerticalFlip : MultiTransform](#513-randomverticalflip--multitransform)
    - [5.13.1 \_\_call\_\_](#5131-\_\_call\_\_)
    - [5.13.2 \_\_init\_\_](#5132-\_\_init\_\_)
  - [5.14 RemoveBackgroundAI : MultiTransform](#514-removebackgroundai--multitransform)
    - [5.14.1 \_\_call\_\_](#5141-\_\_call\_\_)
    - [5.14.2 \_\_init\_\_](#5142-\_\_init\_\_)
  - [5.15 ReplaceBackground : MultiTransform](#515-replacebackground--multitransform)
    - [5.15.1 \_\_call\_\_](#5151-\_\_call\_\_)
    - [5.15.2 \_\_init\_\_](#5152-\_\_init\_\_)
  - [5.16 Resize : MultiTransform](#516-resize--multitransform)
    - [5.16.1 \_\_call\_\_](#5161-\_\_call\_\_)
    - [5.16.2 \_\_init\_\_](#5162-\_\_init\_\_)
  - [5.17 Rotate : MultiTransform](#517-rotate--multitransform)
    - [5.17.1 \_\_call\_\_](#5171-\_\_call\_\_)
    - [5.17.2 \_\_init\_\_](#5172-\_\_init\_\_)
  - [5.18 Satturation : MultiTransform](#518-satturation--multitransform)
    - [5.18.1 \_\_call\_\_](#5181-\_\_call\_\_)
    - [5.18.2 \_\_init\_\_](#5182-\_\_init\_\_)
  - [5.19 Scale : MultiTransform](#519-scale--multitransform)
    - [5.19.1 \_\_call\_\_](#5191-\_\_call\_\_)
    - [5.19.2 \_\_init\_\_](#5192-\_\_init\_\_)
  - [5.20 Stack : MultiTransform](#520-stack--multitransform)
    - [5.20.1 \_\_call\_\_](#5201-\_\_call\_\_)
    - [5.20.2 \_\_init\_\_](#5202-\_\_init\_\_)
  - [5.21 ToCVImage : MultiTransform](#521-tocvimage--multitransform)
    - [5.21.1 \_\_call\_\_](#5211-\_\_call\_\_)
    - [5.21.2 \_\_init\_\_](#5212-\_\_init\_\_)
  - [5.22 ToNumpy : MultiTransform](#522-tonumpy--multitransform)
    - [5.22.1 \_\_call\_\_](#5221-\_\_call\_\_)
    - [5.22.2 \_\_init\_\_](#5222-\_\_init\_\_)
  - [5.23 ToPILImage : MultiTransform](#523-topilimage--multitransform)
    - [5.23.1 \_\_call\_\_](#5231-\_\_call\_\_)
    - [5.23.2 \_\_init\_\_](#5232-\_\_init\_\_)
  - [5.24 ToTensor : MultiTransform](#524-totensor--multitransform)
    - [5.24.1 \_\_call\_\_](#5241-\_\_call\_\_)
    - [5.24.2 \_\_init\_\_](#5242-\_\_init\_\_)
- [6 run](#6-run)
  - [6.1 Run : builtins.object](#61-run--builtinsobject)
    - [6.1.1 \_\_init\_\_](#611-\_\_init\_\_)
    - [6.1.2 append](#612-append)
    - [6.1.3 get\_avg](#613-get\_avg)
    - [6.1.4 get\_val](#614-get\_val)
    - [6.1.5 len](#615-len)
    - [6.1.6 load\_best\_state\_dict](#616-load\_best\_state\_dict)
    - [6.1.7 load\_state\_dict](#617-load\_state\_dict)
    - [6.1.8 pickle\_dump](#618-pickle\_dump)
    - [6.1.9 pickle\_load](#619-pickle\_load)
    - [6.1.10 plot](#6110-plot)
    - [6.1.11 recalculate\_moving\_average](#6111-recalculate\_moving\_average)
    - [6.1.12 save](#6112-save)
    - [6.1.13 save\_best\_state\_dict](#6113-save\_best\_state\_dict)
    - [6.1.14 save\_state\_dict](#6114-save\_state\_dict)
    - [6.1.15 train\_epoch](#6115-train\_epoch)
    - [6.1.16 validate\_epoch](#6116-validate\_epoch)




# 1 dataset

[TOC](#table-of-contents)



## 1.1 HMDB51 : torch.utils.data.dataset.Dataset

[TOC](#table-of-contents)

**Description**

Dataset class for HMDB51.

**Example**

```python
from rsp.ml.dataset import HMDB51
import rsp.ml.multi_transforms as multi_transforms
import cv2 as cv

transforms = multi_transforms.Compose([
    multi_transforms.Color(1.5, p=0.5),
    multi_transforms.Stack()
])
ds = HMDB51('train', fold=1, 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.1.1 \_\_init\_\_

[TOC](#table-of-contents)

**Description**

Initializes a new instance.

**Parameters**

| Name | Type | Description |
|------|------|-------------|
| split | str | Dataset split [train|val|test] |
| fold | int | Fold number. The dataset is split into 3 folds. If fold is None, all folds will be loaded. |
| cache_dir | str, default = None | Directory to store the downloaded files. If set to `None`, the default cache directory will be used |
| force_reload | bool, default = False | If set to `True`, the dataset will be reloaded |
| target_size | (int, int), default = (400, 400) | Size of the frames. The frames will be resized to this size. |
| 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. |
| verbose | bool, default = False | If set to `True`, the progress will be printed. |
## 1.2 Kinetics : torch.utils.data.dataset.Dataset

[TOC](#table-of-contents)

**Description**

Dataset class for the Kinetics dataset.

**Example**

```python
from rsp.ml.dataset import Kinetics

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

for X, T in ds:
    print(X)
```
### 1.2.1 \_\_init\_\_

[TOC](#table-of-contents)

**Description**

Initializes a new instance.

**Parameters**

| Name | Type | Description |
|------|------|-------------|
| split | str | Dataset split [train|val] |
| sequence_length | int, default = 60 | Length of the sequences |
| 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. |
| verbose | bool, default = True | If set to `True`, the progress and additional information will be printed. |
## 1.3 TUCHRI : torch.utils.data.dataset.Dataset

[TOC](#table-of-contents)

**Description**

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


### 1.3.1 \_\_init\_\_

[TOC](#table-of-contents)

**Description**

Initializes a new instance.

**Parameters**

| Name | Type | Description |
|------|------|-------------|
| phase | str | Dataset phase [train|val] |
| load_depth_data | bool, default = True | Load depth data |
| sequence_length | int, default = 30 | Length of the sequences |
| num_classes | int, default = 10 | Number of classes |
| 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.3.2 get\_uniform\_sampler

[TOC](#table-of-contents)

### 1.3.3 load\_backgrounds

[TOC](#table-of-contents)

**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. |
## 1.4 TUCRID : torch.utils.data.dataset.Dataset

[TOC](#table-of-contents)

**Description**

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

**Example**

```python
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.4.1 \_\_init\_\_

[TOC](#table-of-contents)

**Description**

Initializes a new instance.

**Parameters**

| Name | Type | Description |
|------|------|-------------|
| phase | str | Dataset phase [train|val] |
| load_depth_data | bool, default = True | Load depth data |
| sequence_length | int, default = 30 | Length of the sequences |
| num_classes | int, default = 10 | Number of classes |
| 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.4.2 get\_uniform\_sampler

[TOC](#table-of-contents)

### 1.4.3 load\_backgrounds

[TOC](#table-of-contents)

**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. |
## 1.5 UCF101 : torch.utils.data.dataset.Dataset

[TOC](#table-of-contents)

**Description**

An abstract class representing a :class:`Dataset`.

All datasets that represent a map from keys to data samples should subclass
it. All subclasses should overwrite :meth:`__getitem__`, supporting fetching a
data sample for a given key. Subclasses could also optionally overwrite
:meth:`__len__`, which is expected to return the size of the dataset by many
:class:`~torch.utils.data.Sampler` implementations and the default options
of :class:`~torch.utils.data.DataLoader`. Subclasses could also
optionally implement :meth:`__getitems__`, for speedup batched samples
loading. This method accepts list of indices of samples of batch and returns
list of samples.

.. note::
  :class:`~torch.utils.data.DataLoader` by default constructs an index
  sampler that yields integral indices.  To make it work with a map-style
  dataset with non-integral indices/keys, a custom sampler must be provided.

**Example**

```python
from rsp.ml.dataset import UCF101
import rsp.ml.multi_transforms as multi_transforms
import cv2 as cv

transforms = multi_transforms.Compose([
    multi_transforms.Color(1.5, p=0.5),
    multi_transforms.Stack()
])
ds = UCF101('train', fold=1, 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](#table-of-contents)

**Description**

Initializes a new instance.

**Parameters**

| Name | Type | Description |
|------|------|-------------|
| split | str | Dataset split [train|val|test] |
| fold | int | Fold number. The dataset is split into 3 folds. If fold is None, all folds will be loaded. |
| cache_dir | str, default = None | Directory to store the downloaded files. If set to `None`, the default cache directory will be used |
| force_reload | bool, default = False | If set to `True`, the dataset will be reloaded |
| target_size | (int, int), default = (400, 400) | Size of the frames. The frames will be resized to this size. |
| 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. |
| verbose | bool, default = False | If set to `True`, the progress will be printed. |
## 1.6 UTKinectAction3D : torch.utils.data.dataset.Dataset

[TOC](#table-of-contents)

**Description**

Dataset class for the UTKinectAction3D dataset.

Parameters
----------
split : str
    Dataset split [train|val]
cache_dir : str, default = None
    Directory to store the downloaded files. If set to `None`, the default cache directory will be used
force_reload : bool, default = False
    If set to `True`, the dataset will be reloaded
target_size : (int, int), default = (400, 400)
    Size of the frames. The frames will be resized to this size.
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.
verbose : bool, default = False
    If set to `True`, the progress will be printed.

**Example**

```python
from rsp.ml.dataset import UTKinectAction3D
import rsp.ml.multi_transforms as multi_transforms
import cv2 as cv

transforms = multi_transforms.Compose([
    multi_transforms.Color(1.5, p=0.5),
    multi_transforms.Stack()
])
ds = UTKinectAction3D('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.6.1 \_\_init\_\_

[TOC](#table-of-contents)

**Description**

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

# 2 metrics

[TOC](#table-of-contents)

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

## 2.1 AUROC

[TOC](#table-of-contents)

**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](#table-of-contents)

**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**

```python
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](#table-of-contents)

**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**

```python
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](#table-of-contents)

**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**

```python
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](#table-of-contents)

**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**

```python
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](#table-of-contents)

**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**

```python
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](#table-of-contents)

**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**

```python
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](#table-of-contents)

**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**

```python
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](#table-of-contents)

**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**

```python
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](#table-of-contents)

**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**

```python
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](#table-of-contents)

**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

![](documentation/image/ROC_AUC.jpg)
## 2.12 plot\_confusion\_matrix

[TOC](#table-of-contents)

**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

![](documentation/image/confusion_matrix.jpg)
## 2.13 precision

[TOC](#table-of-contents)

**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**

```python
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](#table-of-contents)

**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**

```python
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](#table-of-contents)

**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**

```python
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](#table-of-contents)

**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 : flo