ttach

raw JSON →
0.0.3 verified Mon Apr 27 auth: no python maintenance

Image test time augmentation for PyTorch. Wraps models to apply TTA transforms (flips, rotations, scaling) and aggregate predictions. Current version 0.0.3, last release April 2021. No recent updates; library is in maintenance mode.

pip install ttach
error AttributeError: module 'ttach' has no attribute 'Compose'
cause Importing the module incorrectly (e.g., 'import ttach') and then using 'ttach.Compose' without importing Compose directly.
fix
Use 'from ttach import Compose' or 'import ttach' then 'tta = ttach; tta.Compose' (but direct import is recommended).
error module 'torch' has no attribute 'Tensor'
cause Running TTA on a model that expects different input shapes or on non-tensor data.
fix
Ensure input is a torch.Tensor of appropriate shape (e.g., (batch, channels, height, width) for images).
deprecated The library has not been updated since April 2021. It may not work with newer PyTorch versions without issues.
fix Consider alternatives like kornia's kornia.augmentation.TTA or torchvision's test-time augmentation utilities.
gotcha The 'd4' transform alias was fixed in v0.0.2 to remove duplicated augmentations. If using older version, you might get unintended duplicates.
fix Upgrade to >=0.0.2 or manually define the D4 transform using Compose.
breaking Keypoints support was added in v0.0.3. If you rely on keypoints, ensure version >=0.0.3 or use a workaround.
fix Upgrade to 0.0.3 or handle keypoints manually.

Basic TTA example: wrap a model with horizontal flip, 90° rotation, and scaling. Merge predictions by mean.

import torch
import ttach as tta

model = torch.nn.Linear(10, 2)  # dummy model
model.eval()

transforms = tta.Compose([
    tta.HorizontalFlip(),
    tta.Rotate90(angles=[0, 90]),
    tta.Scale(scales=[1.0, 1.5]),
])

tta_model = tta.SegmentationTTAWrapper(model, transforms, merge_mode='mean')

input_tensor = torch.randn(1, 10)
output = tta_model(input_tensor)
print(output.shape)