-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix hiera init with num_classes=0, fix weight tag names for sbb2 hier…
…a/vit weights, add LayerScale/LayerScale2d to layers
- Loading branch information
Showing
4 changed files
with
49 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import torch | ||
from torch import nn | ||
|
||
|
||
class LayerScale(nn.Module): | ||
""" LayerScale on tensors with channels in last-dim. | ||
""" | ||
def __init__( | ||
self, | ||
dim: int, | ||
init_values: float = 1e-5, | ||
inplace: bool = False, | ||
) -> None: | ||
super().__init__() | ||
self.inplace = inplace | ||
self.gamma = nn.Parameter(init_values * torch.ones(dim)) | ||
|
||
def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
return x.mul_(self.gamma) if self.inplace else x * self.gamma | ||
|
||
|
||
class LayerScale2d(nn.Module): | ||
""" LayerScale for tensors with torch 2D NCHW layout. | ||
""" | ||
def __init__( | ||
self, | ||
dim: int, | ||
init_values: float = 1e-5, | ||
inplace: bool = False, | ||
): | ||
super().__init__() | ||
self.inplace = inplace | ||
self.gamma = nn.Parameter(init_values * torch.ones(dim)) | ||
|
||
def forward(self, x): | ||
gamma = self.gamma.view(1, -1, 1, 1) | ||
return x.mul_(gamma) if self.inplace else x * gamma | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters