-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradientDescent.py
175 lines (113 loc) · 3.62 KB
/
gradientDescent.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import numpy as np
import image as im
import matching as mt
import imageUtility as ut
import transforms as tn
import matplotlib.pyplot as plt
def openImages(imgRange,imgDir,imgParams,imgSet):
images = []
for i in imgRange:
images.append(im.image(imgDir[imgSet]['dir'] + str(i) + imgDir[imgSet]['ext'],imgParams))
return images
def computeOutliers(imageSet,imgParams):
print 'Computing Outliers'
print imgParams
print '\n'
for img in imageSet:
img.computeKP(imgParams)
img1 = imageSet[0]
inlierSum = 0
outlierSum = 0
for img2 in imageSet[1:]:
F = tn.fundamental(img1,img2,imgParams)
inlierSum += F.inlierCount()
outlierSum += F.outlierCount()
img1 = img2
return outlierSum
nOctaveLayers = 3
contrastThreshold = 0.03
edgeThreshold = 9
sigma = 1.6
imgDir = {'glacier':{'dir':'/home/dennis/Documents/View3D/images/glacier/','ext':'.JPG'},
'wbnp':{'dir':'/home/dennis/Documents/View3D/images/wbnp/','ext':'.JPG'},
'desk':{'dir':'/home/dennis/Documents/View3D/images/cathedral/','ext':'.JPG'}
}
imgRange = range(1,9)
imgParams = {'scale':0.15,
'kp':'sift',
'nOctaveLayers':3,
'contrastThreshold':0.04,#Threshold for
'edgeThreshold':10,#2x2 hessian to remove edges that are not corners. The threshold is the ratio between eigenvalues
'sigma':1.7
}
params = {'nOctaveLayers':{'step':1,'bdy':6},
'contrastThreshold':{'step':0.01,'bdy':0.09},
'edgeThreshold':{'step':1,'bdy':16},
'sigma':{'step':0.1,'bdy':2.1}
}
imageSet = openImages(imgRange,imgDir,imgParams,'glacier')
minima = False
ptResults = {'scale':[],
'kp':[],
'nOctaveLayers':[],
'contrastThreshold':[],
'edgeThreshold':[],
'sigma':[],
'y':[]
}
while not minima:
print 'Computing y'
y = computeOutliers(imageSet,imgParams)
for parameter in params.keys():
ptResults[parameter].append(imgParams[parameter])
ptResults['y'].append(y)
for parameter in params.keys():
plt.plot(ptResults[parameter],ptResults['y'])
plt.title(parameter)
plt.show()
print y
print '\n'
gradient = {}
for parameter in params.keys():
print parameter
gradParams = {key: value for key, value in imgParams.items()}
gradient[parameter] = {}
gradParams[parameter] = imgParams[parameter] - params[parameter]['step']
if gradParams[parameter] > 0:
a = computeOutliers(imageSet,gradParams)
print a
gradient[parameter]['left'] = (a - y)
else:
gradient[parameter]['left'] = 0
gradParams[parameter] = imgParams[parameter] + params[parameter]['step']
if gradParams[parameter] < params[parameter]['bdy']:
a = computeOutliers(imageSet,gradParams)
print a
gradient[parameter]['right'] = (a - y)
else:
gradient[parameter]['right'] = 0
print 'gradient'
print gradient
minima = True
for parameter in gradient.keys():
print parameter + ' gradient'
rgrad = gradient[parameter]['right']
print rgrad
lgrad = gradient[parameter]['left']
print lgrad
if lgrad < rgrad and lgrad < 0:
print 'lgrad'
imgParams[parameter] = imgParams[parameter] - params[parameter]['step']
minima = False
elif rgrad < lgrad and rgrad < 0:
print 'rgrad'
imgParams[parameter] = imgParams[parameter] + params[parameter]['step']
minima = False
print 'end of loop'
outfile = open('results.txt','w')
for line in ptResults:
outfile.write(line[0])
outfile.write('\n')
outfile.write(line[1])
outfile.write('\n')
outfile.close()