-
Notifications
You must be signed in to change notification settings - Fork 89
/
utils.py
77 lines (56 loc) · 1.89 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
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
import cv2
def plot(samples, X_dim, channel):
fig = plt.figure(figsize=(4, 4))
gs = gridspec.GridSpec(4, 4)
gs.update(wspace=0.05, hspace=0.05)
dim1 = int(np.sqrt(X_dim))
samples = (samples + 1) / 2
for i, sample in enumerate(samples):
ax = plt.subplot(gs[i])
plt.axis('off')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_aspect('equal')
if channel == 1:
plt.imshow(sample.reshape([dim1, dim1]), cmap=plt.get_cmap('gray'))
else:
plt.imshow(sample.reshape([dim1, dim1, channel]))
return fig
def pixel_range(img):
vmin, vmax = np.min(img), np.max(img)
if vmin * vmax >= 0:
return (vmin, vmax)
else:
if -vmin > vmax:
vmax = -vmin
else:
vmin = -vmax
return (vmin, vmax)
def translate(img, x, y):
"""
Translates the given image.
:param x: distance to translate in the positive x-direction
:param y: distance to translate in the positive y-direction
:returns: the translated image as an numpy array
"""
M = np.float32([[1, 0, x], [0, 1, y]])
return cv2.warpAffine(img.reshape(28,28), M, (28, 28)).reshape(1,784)
def find_roi(img, ksize, coords):
"""
Finds the feature with the largest relevance scores.
:param img: the image to find the feature with the largest relevance score
:param ksize: the size of the sliding window
:param coords: the coordinates to ignore
:returns: the coordinate of the feature with the largest relevance score. If the window size is larger than 1X1, function returns the position of the leftmost pixel.
"""
size = np.shape(img)
temp = np.copy(img)
for coord in coords:
temp[coord[0]:coord[0]+ksize[0], coord[1]:coord[1]+ksize[1]] = -np.infty
r = size[0] - ksize[0] + 1
c = size[1] - ksize[1] + 1
pool = [np.sum(temp[i:i+ksize[0], j:j+ksize[1]]) for i in range(r) for j in range(c)]
return (np.argmax(pool) // c, np.argmax(pool) % c)