forked from kevinzakka/recurrent-visual-attention
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
139 lines (107 loc) · 3.34 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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)
def bounding_box(x, y, size, color="w"):
x = int(x - (size / 2))
y = int(y - (size / 2))
rect = patches.Rectangle(
(x, y), size, size, linewidth=1, edgecolor=color, fill=False
)
return rect
# https://github.com/pytorch/examples/blob/master/imagenet/main.py
class AverageMeter:
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 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 plot_images(images, gd_truth):
images = images.squeeze()
assert len(images) == len(gd_truth) == 9
# Create figure with sub-plots.
fig, axes = plt.subplots(3, 3)
for i, ax in enumerate(axes.flat):
# plot the image
ax.imshow(images[i], cmap="Greys_r")
xlabel = "{}".format(gd_truth[i])
ax.set_xlabel(xlabel)
ax.set_xticks([])
ax.set_yticks([])
plt.show()
def prepare_dirs(config):
for path in [config.data_dir, config.ckpt_dir, config.logs_dir]:
if not os.path.exists(path):
os.makedirs(path)
def save_config(config):
model_name = "ram_{}_{}x{}_{}".format(
config.num_glimpses, config.patch_size, config.patch_size, config.glimpse_scale
)
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)