-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
61 lines (54 loc) · 1.72 KB
/
Copy pathutils.py
File metadata and controls
61 lines (54 loc) · 1.72 KB
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
'''
Software utilities go here
'''
#Converts a 2D matrix into a compressed representation
import numpy as np
def compress(matrix):
dataVector = []
indexVector = [0]
zerosCount = 0
for row in matrix:
for element in row:
#We store at maximum 15 contiguous zeros
#This allows us to use 4 bits for each element in compressed vector
if abs(element) > 0.1 or zerosCount >= 15:
indexVector.append(zerosCount)
dataVector.append(element)
indexVector[0] += 1
zerosCount = 0
else:
zerosCount += 1
return dataVector, indexVector
def compressMultiple(matrixlist):
dataVector = []
indexVector = [0]
zerosCount = 0
for matrix in matrixlist:
for row in matrix:
for element in row:
if element > 0 or zerosCount > 15:
indexVector.append(zerosCount)
dataVector.append(element)
indexVector[0] += 1
zerosCount = 0
else:
zerosCount += 1
return dataVector, indexVector
def convolve(activations, weights):
a = np.array(activations)
w = np.array(weights)
a = np.pad(a, 1, mode = 'constant')
m, n = w.shape
y, x = a.shape
y = y - m + 1
x = x - m + 1
new_image = np.zeros((y,x))
for i in range(y):
for j in range(x):
new_image[i][j] = np.sum(a[i:i+m, j:j+m]*w)
return new_image
def convolveMultiple(activations, weight_list):
result = np.zeros((len(activations), len(activations[0])))
for weights in weight_list:
result += convolve(activations, weights)
return result