-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandbox.py
91 lines (76 loc) · 3.95 KB
/
sandbox.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
import numpy as np
import math
from VGGNet import VGGNet
def convolution(inputM, coefficients,bias):
output = np.zeros( (inputM.shape[0], inputM.shape[1], coefficients.shape[3]) )
inputs = np.zeros( (inputM.shape[0] + 2, inputM.shape[1] + 2, inputM.shape[2]) )
inputs[ 1:inputM.shape[0]+1, 1:inputM.shape[0]+1, :] = inputM
for k in range(coefficients.shape[3]):
weights = coefficients[:, :, :, k]
for i in range(inputM.shape[0]):
for j in range(inputM.shape[1]):
output[i, j, k] = np.sum( weights * inputs[i:i+3, j:j+3,:])
return output + bias
def backpropagation(DeltaI, coefficients):
sideLength = DeltaI.shape[1]
prop = np.zeros( (sideLength, sideLength,coefficients.shape[2] ))
newImage = np.zeros((sideLength + 2, sideLength + 2, DeltaI.shape[2]))
newImage[1:sideLength + 1, 1:sideLength + 1,:] = DeltaI
for l in range(coefficients.shape[2]):
for k in range(DeltaI.shape[2]):
for i in range(sideLength):
for j in range(sideLength):
prop[i,j,l] += np.sum( newImage[i:i+3, j:j+3, k] * np.rot90(coefficients[:,:,l,k],2) )
return prop
# Different implementation using matrix multiplication
def newConvolution(inputM, coefficients, bias):
totalSize = coefficients.shape[0]*coefficients.shape[1]*coefficients.shape[2]
x = np.zeros((coefficients.shape[3], totalSize))
for i in range(coefficients.shape[3]):
x[i,:] = np.reshape(coefficients[:,:,:,i],(1,totalSize))
y = np.zeros((totalSize,inputM.shape[0] ** 2))
newImage = np.zeros((inputM.shape[0] + 2, inputM.shape[1] + 2,inputM.shape[2]))
newImage[1:inputM.shape[0] + 1, 1:inputM.shape[0]+ 1,:] = inputM
for i in range(inputM.shape[0]):
for j in range(inputM.shape[1]):
y[:,i*inputM.shape[0]+j] = np.reshape(newImage[i:i+3, j:j+3,:], (totalSize))
z = np.dot(x,y)
c = np.zeros((inputM.shape[0],inputM.shape[1],coefficients.shape[3]))
for i in range(coefficients.shape[3]):
c[:,:,i] = np.reshape(z[i,:],(inputM.shape[0], inputM.shape[0]))
return c + bias
def newBackProp(inputM,coefficients):
prop = np.zeros((inputM.shape[0], inputM.shape[0], coefficients.shape[2]))
totalSize = coefficients.shape[0]*coefficients.shape[1]
for layer in range(coefficients.shape[2]):
for m in range(inputM.shape[2]):
newSlice = np.zeros((inputM.shape[0] + 2, inputM.shape[1] + 2))
newSlice[1:inputM.shape[0] + 1, 1:inputM.shape[0] + 1] = inputM[:,:,m]
ASlice = np.zeros((totalSize, inputM.shape[0] ** 2))
for i in range(inputM.shape[0]):
for j in range(inputM.shape[1]):
ASlice[:,i*inputM.shape[0]+j] = np.ravel(newSlice[i:i+3, j:j+3])
weights = np.array(np.ravel(np.rot90(coefficients[:,:,layer,m],2)))
prop[:,:,layer] += np.reshape(np.dot(weights,ASlice),(inputM.shape[0],inputM.shape[1]))
return prop
if __name__ == '__main__':
config = {
"layers" : ['conv', 'conv', 'pool', 'conv', 'conv', 'pool', 'conv', 'conv', 'conv', 'conv', 'pool',
'conv', 'conv', 'conv', 'conv', 'pool', 'conv'],
"style_weights" : [1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
"content_weights": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
"pixel_mean" : [103.939, 116.779, 123.680]
}
numPool = 4
for i in range(17):
j = 16 - i
print "layer " + str(j) + ":"
if config["layers"][j] == "conv":
print config["style_weights"][j]
if config["style_weights"][j] > 0:
print "layer " + str(j - numPool + 1) + " is taken into content reconstruction"
if config['content_weights'][j] > 0:
print "layer " + str(j - numPool + 1) + " is taken into content reconstruction"
elif config["layers"][j] == "pool":
numPool = numPool- 1
print "============================================="