MCPcopy Create free account
hub / github.com/apple/ml-sharp / MeanStdNormalizer

Class MeanStdNormalizer

src/sharp/models/normalizers.py:15–38  ·  view source on GitHub ↗

Normalizing image input by mean and std.

Source from the content-addressed store, hash-verified

13
14
15class MeanStdNormalizer(nn.Module):
16 """Normalizing image input by mean and std."""
17
18 mean: torch.Tensor
19 std_inv: torch.Tensor
20
21 def __init__(
22 self,
23 mean: Union[Sequence[float], torch.Tensor],
24 std: Union[Sequence[float], torch.Tensor],
25 ):
26 """Initialize MeanStdNormalizer."""
27 super(MeanStdNormalizer, self).__init__()
28 if not isinstance(mean, torch.Tensor):
29 mean = torch.as_tensor(mean).view(-1, 1, 1)
30 if not isinstance(std, torch.Tensor):
31 std = torch.as_tensor(std).view(-1, 1, 1)
32 self.register_buffer("mean", mean)
33 # We use inverse std to use a multiplication which is better supported by the hardware
34 self.register_buffer("std_inv", 1.0 / std)
35
36 def forward(self, image: torch.Tensor) -> torch.Tensor:
37 """Apply mean and std normalization over input image."""
38 return (image - self.mean) * self.std_inv
39
40
41class AffineRangeNormalizer(nn.Module):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected