# TTAch
Image Test Time Augmentation with PyTorch!
Similar to what Data Augmentation is doing to the training set, the purpose of Test Time Augmentation is to perform random modifications to the test images. Thus, instead of showing the regular, “clean” images, only once to the trained model, we will show it the augmented images several times. We will then average the predictions of each corresponding image and take that as our final guess [[1](https://towardsdatascience.com/test-time-augmentation-tta-and-how-to-perform-it-with-keras-4ac19b67fb4d)].
```
Input
| # input batch of images
/ / /|\ \ \ # apply augmentations (flips, rotation, scale, etc.)
| | | | | | | # pass augmented batches through model
| | | | | | | # reverse transformations for each batch of masks/labels
\ \ \ / / / # merge predictions (mean, max, gmean, etc.)
| # output batch of masks/labels
Output
```
## Table of Contents
1. [Quick Start](#quick-start)
2. [Transforms](#transforms)
3. [Aliases](#aliases)
4. [Merge modes](#merge-modes)
5. [Installation](#installation)
## Quick start
##### Segmentation model wrapping:
```python
import ttach as tta
tta_model = tta.SegmentationTTAWrapper(model, tta.aliases.d4_transform(), merge_mode='mean')
```
##### Classification model wrapping:
```python
tta_model = tta.ClassificationTTAWrapper(model, tta.aliases.five_crop_transform())
```
##### Keypoints model wrapping:
```python
tta_model = tta.KeypointsTTAWrapper(model, tta.aliases.flip_transform(), scaled=True)
```
**Note**: the model must return keypoints in the format `torch([x1, y1, ..., xn, yn])`
## Advanced Examples
##### Custom transform:
```python
# defined 2 * 2 * 3 * 3 = 36 augmentations !
transforms = tta.Compose(
[
tta.HorizontalFlip(),
tta.Rotate90(angles=[0, 180]),
tta.Scale(scales=[1, 2, 4]),
tta.Multiply(factors=[0.9, 1, 1.1]),
]
)
tta_model = tta.SegmentationTTAWrapper(model, transforms)
```
##### Custom model (multi-input / multi-output)
```python
# Example how to process ONE batch on images with TTA
# Here `image`/`mask` are 4D tensors (B, C, H, W), `label` is 2D tensor (B, N)
for transformer in transforms: # custom transforms or e.g. tta.aliases.d4_transform()
# augment image
augmented_image = transformer.augment_image(image)
# pass to model
model_output = model(augmented_image, another_input_data)
# reverse augmentation for mask and label
deaug_mask = transformer.deaugment_mask(model_output['mask'])
deaug_label = transformer.deaugment_label(model_output['label'])
# save results
labels.append(deaug_mask)
masks.append(deaug_label)
# reduce results as you want, e.g mean/max/min
label = mean(labels)
mask = mean(masks)
```
## Transforms
| Transform | Parameters | Values |
|----------------|:-------------------------:|:---------------------------------:|
| HorizontalFlip | - | - |
| VerticalFlip | - | - |
| Rotate90 | angles | List\[0, 90, 180, 270] |
| Scale | scales<br>interpolation | List\[float]<br>"nearest"/"linear"|
| Resize | sizes<br>original_size<br>interpolation | List\[Tuple\[int, int]]<br>Tuple\[int,int]<br>"nearest"/"linear"|
| Add | values | List\[float] |
| Multiply | factors | List\[float] |
| FiveCrops | crop_height<br>crop_width | int<br>int |
## Aliases
- flip_transform (horizontal + vertical flips)
- hflip_transform (horizontal flip)
- d4_transform (flips + rotation 0, 90, 180, 270)
- multiscale_transform (scale transform, take scales as input parameter)
- five_crop_transform (corner crops + center crop)
- ten_crop_transform (five crops + five crops on horizontal flip)
## Merge modes
- mean
- gmean (geometric mean)
- sum
- max
- min
- tsharpen ([temperature sharpen](https://www.kaggle.com/c/severstal-steel-defect-detection/discussion/107716#latest-624046) with t=0.5)
## Installation
PyPI:
```bash
$ pip install ttach
```
Source:
```bash
$ pip install git+https://github.com/qubvel/ttach
```
## Run tests
```bash
docker build -f Dockerfile.dev -t ttach:dev . && docker run --rm ttach:dev pytest -p no:cacheprovider
```
Raw data
{
"_id": null,
"home_page": "https://github.com/qubvel/ttach",
"name": "ttach",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.0.0",
"maintainer_email": "",
"keywords": "",
"author": "Pavel Yakubovskiy",
"author_email": "qubvel@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/91/5d/4c49e0eca4206bc25eff4ba89cee51b781466e2e3aad2f1057fd5d2634be/ttach-0.0.3.tar.gz",
"platform": "",
"description": "\n# TTAch\nImage Test Time Augmentation with PyTorch!\n\nSimilar to what Data Augmentation is doing to the training set, the purpose of Test Time Augmentation is to perform random modifications to the test images. Thus, instead of showing the regular, \u201cclean\u201d images, only once to the trained model, we will show it the augmented images several times. We will then average the predictions of each corresponding image and take that as our final guess [[1](https://towardsdatascience.com/test-time-augmentation-tta-and-how-to-perform-it-with-keras-4ac19b67fb4d)]. \n```\n Input\n | # input batch of images \n / / /|\\ \\ \\ # apply augmentations (flips, rotation, scale, etc.)\n | | | | | | | # pass augmented batches through model\n | | | | | | | # reverse transformations for each batch of masks/labels\n \\ \\ \\ / / / # merge predictions (mean, max, gmean, etc.)\n | # output batch of masks/labels\n Output\n```\n## Table of Contents\n1. [Quick Start](#quick-start)\n2. [Transforms](#transforms)\n3. [Aliases](#aliases)\n4. [Merge modes](#merge-modes)\n5. [Installation](#installation)\n\n## Quick start\n\n##### Segmentation model wrapping:\n```python\nimport ttach as tta\ntta_model = tta.SegmentationTTAWrapper(model, tta.aliases.d4_transform(), merge_mode='mean')\n```\n##### Classification model wrapping:\n```python\ntta_model = tta.ClassificationTTAWrapper(model, tta.aliases.five_crop_transform())\n```\n\n##### Keypoints model wrapping:\n```python\ntta_model = tta.KeypointsTTAWrapper(model, tta.aliases.flip_transform(), scaled=True)\n```\n**Note**: the model must return keypoints in the format `torch([x1, y1, ..., xn, yn])`\n\n## Advanced Examples\n##### Custom transform:\n```python\n# defined 2 * 2 * 3 * 3 = 36 augmentations !\ntransforms = tta.Compose(\n [\n tta.HorizontalFlip(),\n tta.Rotate90(angles=[0, 180]),\n tta.Scale(scales=[1, 2, 4]),\n tta.Multiply(factors=[0.9, 1, 1.1]), \n ]\n)\n\ntta_model = tta.SegmentationTTAWrapper(model, transforms)\n```\n##### Custom model (multi-input / multi-output)\n```python\n# Example how to process ONE batch on images with TTA\n# Here `image`/`mask` are 4D tensors (B, C, H, W), `label` is 2D tensor (B, N)\n\nfor transformer in transforms: # custom transforms or e.g. tta.aliases.d4_transform() \n\n # augment image\n augmented_image = transformer.augment_image(image)\n\n # pass to model\n model_output = model(augmented_image, another_input_data)\n\n # reverse augmentation for mask and label\n deaug_mask = transformer.deaugment_mask(model_output['mask'])\n deaug_label = transformer.deaugment_label(model_output['label'])\n\n # save results\n labels.append(deaug_mask)\n masks.append(deaug_label)\n\n# reduce results as you want, e.g mean/max/min\nlabel = mean(labels)\nmask = mean(masks)\n```\n\n## Transforms\n\n| Transform | Parameters | Values |\n|----------------|:-------------------------:|:---------------------------------:|\n| HorizontalFlip | - | - |\n| VerticalFlip | - | - |\n| Rotate90 | angles | List\\[0, 90, 180, 270] |\n| Scale | scales<br>interpolation | List\\[float]<br>\"nearest\"/\"linear\"|\n| Resize | sizes<br>original_size<br>interpolation | List\\[Tuple\\[int, int]]<br>Tuple\\[int,int]<br>\"nearest\"/\"linear\"|\n| Add | values | List\\[float] |\n| Multiply | factors | List\\[float] |\n| FiveCrops | crop_height<br>crop_width | int<br>int |\n\n## Aliases\n\n - flip_transform (horizontal + vertical flips)\n - hflip_transform (horizontal flip)\n - d4_transform (flips + rotation 0, 90, 180, 270)\n - multiscale_transform (scale transform, take scales as input parameter)\n - five_crop_transform (corner crops + center crop)\n - ten_crop_transform (five crops + five crops on horizontal flip)\n\n## Merge modes\n - mean\n - gmean (geometric mean)\n - sum\n - max\n - min\n - tsharpen ([temperature sharpen](https://www.kaggle.com/c/severstal-steel-defect-detection/discussion/107716#latest-624046) with t=0.5)\n\n## Installation\nPyPI:\n```bash\n$ pip install ttach\n```\nSource:\n```bash\n$ pip install git+https://github.com/qubvel/ttach\n```\n\n## Run tests\n\n```bash\ndocker build -f Dockerfile.dev -t ttach:dev . && docker run --rm ttach:dev pytest -p no:cacheprovider\n```\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Images test time augmentation with PyTorch.",
"version": "0.0.3",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8da3ee48a184a185c1897c582c72240c2c8a0d0aeb5f8051a71d4e4cd930c52d",
"md5": "227e7146fa53a6876527ed3121a87994",
"sha256": "7000bb4334f856b0c79a341df386c92f1c76faf091043cc3cd7f541d2149faf8"
},
"downloads": -1,
"filename": "ttach-0.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "227e7146fa53a6876527ed3121a87994",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.0.0",
"size": 9839,
"upload_time": "2020-07-09T14:44:08",
"upload_time_iso_8601": "2020-07-09T14:44:08.006458Z",
"url": "https://files.pythonhosted.org/packages/8d/a3/ee48a184a185c1897c582c72240c2c8a0d0aeb5f8051a71d4e4cd930c52d/ttach-0.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "915d4c49e0eca4206bc25eff4ba89cee51b781466e2e3aad2f1057fd5d2634be",
"md5": "4b1cc1a69a01fbd221dfd965e0859026",
"sha256": "120c4dd881feb0e9c8dd63b154f2655891c3e20689b68a94d162bfd5557bcb48"
},
"downloads": -1,
"filename": "ttach-0.0.3.tar.gz",
"has_sig": false,
"md5_digest": "4b1cc1a69a01fbd221dfd965e0859026",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.0.0",
"size": 9600,
"upload_time": "2020-07-09T14:44:09",
"upload_time_iso_8601": "2020-07-09T14:44:09.035300Z",
"url": "https://files.pythonhosted.org/packages/91/5d/4c49e0eca4206bc25eff4ba89cee51b781466e2e3aad2f1057fd5d2634be/ttach-0.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2020-07-09 14:44:09",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "qubvel",
"github_project": "ttach",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "ttach"
}