Spaces:
Running on Zero
Running on Zero
Download models/tta.py from akashch1512/SingleViewHeigthEstimation: direct link, hf CLI and curl.
- Browser
- Download file 4.59 kB
-
https://huggingface.co/spaces/akashch1512/SingleViewHeigthEstimation/resolve/main/models/tta.py
- Command line
-
hf download hf://spaces/akashch1512/SingleViewHeigthEstimation/models/tta.py
-
curl -L -o tta.py https://huggingface.co/spaces/akashch1512/SingleViewHeigthEstimation/resolve/main/models/tta.py
4.59 kB
| """Test-time augmentation: the 8 elements of D4, optionally x scales. | |
| Nadir imagery has no canonical up, so flips and 90-degree rotations are exact | |
| symmetries of the task — averaging over them is free accuracy. v2 implemented | |
| this and then never ran it: the process crashed before the final eval, so no TTA | |
| number was ever produced. | |
| """ | |
| from __future__ import annotations | |
| import torch | |
| import torch.nn.functional as F | |
| _D4 = [(k, f) for f in (False, True) for k in range(4)] | |
| def _apply(x, k, flip): | |
| x = torch.rot90(x, k, dims=(-2, -1)) | |
| return torch.flip(x, dims=(-1,)) if flip else x | |
| def _invert(x, k, flip): | |
| if flip: | |
| x = torch.flip(x, dims=(-1,)) | |
| return torch.rot90(x, -k, dims=(-2, -1)) | |
| def tta_predict(model, image, scales=(1.0,), key: str = "fused", | |
| patch: int = 16, amp_dtype=None) -> torch.Tensor: | |
| h, w = image.shape[-2:] | |
| acc = torch.zeros((image.shape[0], 1, h, w), device=image.device, dtype=torch.float32) | |
| n = 0 | |
| for s in scales: | |
| if abs(s - 1.0) < 1e-6: | |
| img_s = image | |
| else: | |
| nh = max(patch, int(round(h * s / patch)) * patch) | |
| nw = max(patch, int(round(w * s / patch)) * patch) | |
| img_s = F.interpolate(image, size=(nh, nw), mode="bilinear", align_corners=False) | |
| for k, flip in _D4: | |
| ctx = (torch.autocast("cuda", dtype=amp_dtype) | |
| if (amp_dtype is not None and image.is_cuda) | |
| else torch.autocast("cpu", enabled=False)) | |
| with ctx: | |
| pred = model(_apply(img_s, k, flip))[key] | |
| pred = _invert(pred.float(), k, flip) | |
| if pred.shape[-2:] != (h, w): | |
| pred = F.interpolate(pred, size=(h, w), mode="bilinear", align_corners=False) | |
| acc += pred | |
| n += 1 | |
| return acc / max(1, n) | |
| def n_views(scales=(1.0,), d4: bool = True) -> int: | |
| """Forward passes per tile under `tta_predict_parallel`.""" | |
| return (len(_D4) if d4 else 1) * max(1, len(scales)) | |
| def tta_predict_parallel(forward, image, scales=(1.0,), patch: int = 16, | |
| d4: bool = True): | |
| """`tta_predict`, but every D4 view of the batch goes through the net *together*. | |
| The sequential version runs 8 small forwards per batch, which leaves a large | |
| GPU mostly idle. A square tile keeps its shape under rot90/flip, so the 8 | |
| views stack along the batch axis and `forward` sees one (8B, 3, S, S) batch | |
| per scale; it is free to split that into whatever fits in memory. | |
| `forward(x) -> (pred, std, seg)`. Average first and second moments across | |
| views so the returned spread includes both bin uncertainty and TTA disagreement. | |
| """ | |
| b, _, h, w = image.shape | |
| transforms = _D4 if d4 else [(0, False)] | |
| acc = torch.zeros((b, 1, h, w), device=image.device, dtype=torch.float32) | |
| acc_m2 = torch.zeros_like(acc) | |
| seg_id, n = None, 0 | |
| for s in scales: | |
| if abs(s - 1.0) < 1e-6: | |
| img_s = image | |
| else: | |
| nh = max(patch, int(round(h * s / patch)) * patch) | |
| nw = max(patch, int(round(w * s / patch)) * patch) | |
| img_s = F.interpolate(image, size=(nh, nw), mode="bilinear", align_corners=False) | |
| views = torch.cat([_apply(img_s, k, flip) for k, flip in transforms]) | |
| pred, pred_std, seg = forward(views) | |
| for i, (k, flip) in enumerate(transforms): | |
| p = _invert(pred[i * b:(i + 1) * b], k, flip) | |
| p_slice = pred[i * b:(i + 1) * b] | |
| m2_slice = p_slice.square() | |
| if pred_std is not None: | |
| m2_slice = m2_slice + pred_std[i * b:(i + 1) * b].square() | |
| m2 = _invert(m2_slice, k, flip) | |
| if p.shape[-2:] != (h, w): | |
| p = F.interpolate(p, size=(h, w), mode="bilinear", align_corners=False) | |
| m2 = F.interpolate(m2, size=(h, w), mode="bilinear", align_corners=False) | |
| acc += p | |
| acc_m2 += m2 | |
| n += 1 | |
| # Prefer the native identity labels; scaled-only inference uses its | |
| # identity labels returned to the target grid with nearest sampling. | |
| if seg is not None and (seg_id is None or img_s is image): | |
| seg_id = seg[:b] | |
| if seg_id.shape[-2:] != (h, w): | |
| seg_id = F.interpolate(seg_id[:, None].float(), size=(h, w), | |
| mode="nearest")[:, 0].to(seg.dtype) | |
| mean = acc / max(1, n) | |
| std = (acc_m2 / max(1, n) - mean.square()).clamp_min(0).sqrt() | |
| return mean, std, seg_id | |