{"id":13578080,"url":"https://github.com/qubvel/ttach","last_synced_at":"2025-04-14T08:17:29.589Z","repository":{"id":44965368,"uuid":"212140860","full_name":"qubvel/ttach","owner":"qubvel","description":"Image Test Time Augmentation with PyTorch!","archived":false,"fork":false,"pushed_at":"2023-07-28T04:17:42.000Z","size":53,"stargazers_count":997,"open_issues_count":18,"forks_count":67,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-14T08:17:17.778Z","etag":null,"topics":["augmentation","classification","computer-vision","deep-learning","keypoint-detection","pytorch","segmentation","test-time-augmentation","tta","tta-wrapper"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/qubvel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-10-01T16:08:55.000Z","updated_at":"2025-04-14T06:19:10.000Z","dependencies_parsed_at":"2024-01-26T04:47:14.919Z","dependency_job_id":null,"html_url":"https://github.com/qubvel/ttach","commit_stats":{"total_commits":20,"total_committers":3,"mean_commits":6.666666666666667,"dds":0.09999999999999998,"last_synced_commit":"94e579e59a21cbdfbb4f5790502e648008ecf64e"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qubvel%2Fttach","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qubvel%2Fttach/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qubvel%2Fttach/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qubvel%2Fttach/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qubvel","download_url":"https://codeload.github.com/qubvel/ttach/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248843999,"owners_count":21170499,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["augmentation","classification","computer-vision","deep-learning","keypoint-detection","pytorch","segmentation","test-time-augmentation","tta","tta-wrapper"],"created_at":"2024-08-01T15:01:27.109Z","updated_at":"2025-04-14T08:17:29.548Z","avatar_url":"https://github.com/qubvel.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# 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, “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)].  \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 [[docstring](ttach/wrappers.py#L8)]:\n```python\nimport ttach as tta\ntta_model = tta.SegmentationTTAWrapper(model, tta.aliases.d4_transform(), merge_mode='mean')\n```\n#####  Classification model wrapping [[docstring](ttach/wrappers.py#L52)]:\n```python\ntta_model = tta.ClassificationTTAWrapper(model, tta.aliases.five_crop_transform())\n```\n\n#####  Keypoints model wrapping [[docstring](ttach/wrappers.py#L96)]:\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\u003cbr\u003einterpolation   | List\\[float]\u003cbr\u003e\"nearest\"/\"linear\"|\n| Resize         | sizes\u003cbr\u003eoriginal_size\u003cbr\u003einterpolation   | List\\[Tuple\\[int, int]]\u003cbr\u003eTuple\\[int,int]\u003cbr\u003e\"nearest\"/\"linear\"|\n| Add            | values                    | List\\[float]                      |\n| Multiply       | factors                   | List\\[float]                      |\n| FiveCrops      | crop_height\u003cbr\u003ecrop_width | int\u003cbr\u003eint                        |\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 . \u0026\u0026 docker run --rm ttach:dev pytest -p no:cacheprovider\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqubvel%2Fttach","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqubvel%2Fttach","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqubvel%2Fttach/lists"}