Spaces:
Running on Zero
Running on Zero
Download fab/models/modules/drop_path.py from hugging-apps/fab-body-estimation: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/spaces/hugging-apps/fab-body-estimation/resolve/main/fab/models/modules/drop_path.py
- Command line
-
hf download hf://spaces/hugging-apps/fab-body-estimation/fab/models/modules/drop_path.py
-
curl -L -o drop_path.py https://huggingface.co/spaces/hugging-apps/fab-body-estimation/resolve/main/fab/models/modules/drop_path.py
1.63 kB
| # Copyright (c) Meta Platforms, Inc. and affiliates. | |
| # SPDX-License-Identifier: LicenseRef-SAM-License | |
| # From SAM 3D Body (https://github.com/facebookresearch/sam-3d-body), modified for FAB; licence: third_party_licenses/SAM-License.txt | |
| import torch | |
| import torch.nn as nn | |
| def drop_path( | |
| x: torch.Tensor, drop_prob: float = 0.0, training: bool = False | |
| ) -> torch.Tensor: | |
| """Drop paths (Stochastic Depth) per sample (when applied in main path of | |
| residual blocks). | |
| We follow the implementation | |
| https://github.com/rwightman/pytorch-image-models/blob/a2727c1bf78ba0d7b5727f5f95e37fb7f8866b1f/timm/models/layers/drop.py # noqa: E501 | |
| """ | |
| if not training: | |
| return x | |
| keep_prob = 1 - drop_prob | |
| # handle tensors with different dimensions, not just 4D tensors. | |
| shape = (x.shape[0],) + (1,) * (x.ndim - 1) | |
| random_tensor = keep_prob + torch.rand(shape, dtype=x.dtype, device=x.device) | |
| output = x.div(keep_prob) * random_tensor.floor() | |
| return output | |
| class DropPath(nn.Module): | |
| """Drop paths (Stochastic Depth) per sample (when applied in main path of | |
| residual blocks). | |
| We follow the implementation | |
| https://github.com/rwightman/pytorch-image-models/blob/a2727c1bf78ba0d7b5727f5f95e37fb7f8866b1f/timm/models/layers/drop.py # noqa: E501 | |
| Args: | |
| drop_prob (float): Probability of the path to be zeroed. Default: 0.1 | |
| """ | |
| def __init__(self, drop_prob: float = 0.1): | |
| super().__init__() | |
| self.drop_prob = drop_prob | |
| def forward(self, x: torch.Tensor) -> torch.Tensor: | |
| return drop_path(x, self.drop_prob, self.training) | |