-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathutils.py
123 lines (100 loc) · 3.15 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
import json
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
def denormalize(T, coords):
return (0.5 * ((coords + 1.0) * T))
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
def accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def resize_array(x, size):
# 3D and 4D tensors allowed only
assert x.ndim in [3, 4], "Only 3D and 4D Tensors allowed!"
# 4D Tensor
if x.ndim == 4:
res = []
for i in range(x.shape[0]):
img = array2img(x[i])
img = img.resize((size, size))
img = np.asarray(img, dtype='float32')
img = np.expand_dims(img, axis=0)
img /= 255.0
res.append(img)
res = np.concatenate(res)
res = np.expand_dims(res, axis=1)
return res
# 3D Tensor
img = array2img(x)
img = img.resize((size, size))
res = np.asarray(img, dtype='float32')
res = np.expand_dims(res, axis=0)
res /= 255.0
return res
def img2array(data_path, desired_size=None, expand=False, view=False):
"""
Util function for loading RGB image into a numpy array.
Returns array of shape (1, H, W, C).
"""
img = Image.open(data_path)
img = img.convert('RGB')
if desired_size:
img = img.resize((desired_size[1], desired_size[0]))
if view:
img.show()
x = np.asarray(img, dtype='float32')
if expand:
x = np.expand_dims(x, axis=0)
x /= 255.0
return x
def array2img(x):
"""
Util function for converting anumpy array to a PIL img.
Returns PIL RGB img.
"""
x = np.asarray(x)
x = x + max(-np.min(x), 0)
x_max = np.max(x)
if x_max != 0:
x /= x_max
x *= 255
return Image.fromarray(x.astype('uint8'), 'RGB')
def prepare_dirs(config):
for path in [config.ckpt_dir, config.logs_dir]:
if not os.path.exists(path):
os.makedirs(path)
def save_config(config):
model_name = config.save_name
filename = model_name + '_params.json'
param_path = os.path.join(config.ckpt_dir, filename)
print("[*] Model Checkpoint Dir: {}".format(config.ckpt_dir))
print("[*] Param Path: {}".format(param_path))
with open(param_path, 'w') as fp:
json.dump(config.__dict__, fp, indent=4, sort_keys=True)