-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
67 lines (51 loc) · 1.64 KB
/
Copy pathutils.py
File metadata and controls
67 lines (51 loc) · 1.64 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import torch
from skimage.io import imread
from logger import log
import numpy as np
def compute_mean_std(
feats: torch.Tensor, eps=1e-8, infer=False
) -> torch.Tensor:
assert (
len(feats.shape) == 4
), "feature map should be 4-dimensional of the form N,C,H,W!"
# * Doing this to support ONNX.js inference.
if infer:
n = 1
c = 512 # * fixed for vgg19
else:
n, c, _, _ = feats.shape
feats = feats.view(n, c, -1)
mean = torch.mean(feats, dim=-1).view(n, c, 1, 1)
std = torch.std(feats, dim=-1).view(n, c, 1, 1) + eps
return mean, std
def inv_normz(img):
std = torch.Tensor([0.229, 0.224, 0.225]).reshape(-1, 1, 1).to(img.device)
mean = (
torch.Tensor([0.485, 0.456, 0.406]).reshape(-1, 1, 1).to(img.device)
)
out = torch.clamp(img * std + mean, 0, 1)
return out
def normz(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]):
if isinstance(img, np.ndarray):
img = torch.tensor(img)
mean = torch.tensor(mean).to(img.device)
std = torch.tensor(std).to(img.device)
if mean.ndim == 1:
mean = mean.view(-1, 1, 1)
if std.ndim == 1:
std = std.view(-1, 1, 1)
return (img - mean) / std
def img_loader(path: str):
img = imread(path)
return img
def resolve_device(device: str = "auto"):
if device.lower() in ["auto", "cpu", "cuda"]:
if device == "auto":
return "cuda" if torch.cuda.is_available() else "cpu"
else:
return device
else:
log.warn(
f"{device} should be one of [auto, cpu, cuda]! Defaulting to cpu."
)
return "cpu"