-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.py
More file actions
33 lines (25 loc) · 958 Bytes
/
utils.py
File metadata and controls
33 lines (25 loc) · 958 Bytes
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
import numpy as np
# That very smart code was taken from http://stackoverflow.com/questions/30109068/implement-matlabs-im2col-sliding-in-python/30110497
def im2col(array, size, stride=1, padding=0):
# Add padding to our array
padded_array = np.pad(
array,
((0, 0), (0, 0), (padding, padding), (padding, padding)),
mode='constant'
)
# Get the shape of our newly made array
H, W = np.shape(padded_array)
# Get the extent
extent = H - size + 1
# Start index
start_idx = np.arange(size)[:, None] * H + np.arange(size)
offset_idx = np.arange(extent)[:, None] * H + np.arange(extent)
return np.take(
array,
np.ravel(start_idx)[:, None] + np.ravel(offset_idx)[::stride]
)
def cifar_rgb_to_grayscale(image):
red = np.reshape(image[0:1024], (32, 32))
green = np.reshape(image[1024:2048], (32, 32))
blue = np.reshape(image[2048:3072], (32, 32))
return 0.2989 * red + 0.5870 * green + 0.1140 * blue