MCPcopy Create free account
hub / github.com/MotrixLab/AiOS / normalize

Function normalize

detrsmpl/core/renderer/torch3d_renderer/utils.py:13–40  ·  view source on GitHub ↗

Normalize the tensor or array and convert dtype.

(value,
              origin_value_range=None,
              out_value_range=(0, 1),
              dtype=None,
              clip=False)

Source from the content-addressed store, hash-verified

11
12
13def normalize(value,
14 origin_value_range=None,
15 out_value_range=(0, 1),
16 dtype=None,
17 clip=False) -> Union[torch.Tensor, np.ndarray]:
18 """Normalize the tensor or array and convert dtype."""
19 if origin_value_range is not None:
20 value = (value - origin_value_range[0]) / (
21 origin_value_range[1] - origin_value_range[0] + 1e-9)
22
23 else:
24 value = (value - value.min()) / (value.max() - value.min())
25 value = value * (out_value_range[1] -
26 out_value_range[0]) + out_value_range[0]
27 if clip:
28 value = torch.clip(value,
29 min=out_value_range[0],
30 max=out_value_range[1])
31 if isinstance(value, torch.Tensor):
32 if dtype is not None:
33 return value.type(dtype)
34 else:
35 return value
36 elif isinstance(value, np.ndarray):
37 if dtype is not None:
38 return value.astype(dtype)
39 else:
40 return value
41
42
43def tensor2array(image: torch.Tensor) -> np.ndarray:

Callers 9

tensor2rgbaMethod · 0.85
tensor2arrayFunction · 0.85
array2tensorFunction · 0.85
tensor2rgbaMethod · 0.85
tensor2rgbaMethod · 0.85
_write_imagesMethod · 0.85
tensor2rgbaMethod · 0.85
tensor2rgbaMethod · 0.85
forwardMethod · 0.85

Calls 1

maxMethod · 0.80

Tested by

no test coverage detected