Spaces:
Sleeping
Sleeping
File size: 1,050 Bytes
e0552b0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import numpy as np
import torch
from io import BytesIO
from typing import Any, Callable, Awaitable
def file_to_tensor(path: str) -> torch.Tensor:
return torch.from_numpy(np.fromfile(path, dtype=np.uint8).copy())
def tensor_to_bytes(tensor: torch.Tensor):
return BytesIO(tensor.cpu().numpy().tobytes())
type Emitter = Callable[[str, Any], Awaitable[None]]
async def noop_emitter(name: str, value: Any) -> None:
pass
def round_nested(value, precision):
if isinstance(value, list):
return [round_nested(v, precision) for v in value]
return round(float(value), precision)
def tensor_norm(t, precision=3):
# Tensor 또는 list 모두 허용
if isinstance(t, torch.Tensor):
x = t.detach().cpu().float()
elif isinstance(t, list):
x = torch.tensor(t, dtype=torch.float32)
else:
raise TypeError()
x = torch.nan_to_num(x)
x_min = x.min()
x_max = x.max()
normalized = (x - x_min) / (x_max - x_min + 1e-8)
return round_nested(normalized.tolist(), precision)
|