-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
51 lines (39 loc) · 1.31 KB
/
utils.py
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
from math import cos, pi
import torch
from torchvision import transforms
from PIL import Image
import torch.nn as nn
import cv2
import numpy as np
import torch.nn.functional as F
def torch_img_to_np(img):
return img.detach().cpu().numpy().transpose(0, 2, 3, 1)
def torch_img_to_np2(img):
img = img.detach().cpu().numpy()
# img = img * np.array([0.229, 0.224, 0.225]).reshape(1,-1,1,1)
# img = img + np.array([0.485, 0.456, 0.406]).reshape(1,-1,1,1)
img = img * np.array([0.5, 0.5, 0.5]).reshape(1,-1,1,1)
img = img + np.array([0.5, 0.5, 0.5]).reshape(1,-1,1,1)
img = img.transpose(0, 2, 3, 1)
img = img * 255.0
img = np.clip(img, 0, 255).astype(np.uint8)[:, :, :, [2, 1, 0]]
return img
def _fix_image(image):
if image.max() < 30.:
image = image * 255.
image = np.clip(image, 0, 255).astype(np.uint8)[:, :, :, [2, 1, 0]]
return image
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count