/
testingNew.py
253 lines (197 loc) · 6.39 KB
/
testingNew.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy.sparse import csgraph
from numpy import linalg
from math import exp
from dataFile import unlabeled_instances
from dataFile import input_matrix
#assuming that we have a matrix of n * f * d
#where n is the number of video samples, f is the number of
#frames taken from each video...
#d is the number of features that we have extracted from each
#video
#say that the matrix is m...
t = open( "listMatrix.txt" , "w" )
def mean( matrix , n , f , d ) :
temp = np.matrix( matrix[ 0 ][ 0 ] ).astype( np.float32 )
#computing the sum average of all the frames for the 'n' video samples
weightMatrix = np.zeros( shape = ( n , d ) )
for i in xrange( 0 , n ) :
for j in xrange( 0 , f ) :
temp = temp + np.matrix( matrix[ i ][ j ] ).astype( np.float32 )
#print np.matrix( matrix[ i ] ).astype( 'float' )
#getting the sum in the temp matrix
temp = temp / f
weightMatrix[ i ] = temp
#print temp
#return the mean of the matrix
t.write( "hello" )
t.write( str( np.matrix( weightMatrix ).tolist( ) ) )
t.close( )
return np.matrix( weightMatrix )
def max_values( ) :
l = []
def graph_construction( matrix , response, n , f , d ) :
#compute mean of all the frames in one single matrix
trainData = mean( matrix , n , f , d )
print trainData
#response
l = 3
# here l is the number of labelled samples
# Labels each one either Red or Blue with numbers 0 and 1
responses = np.random.randint(0, l , (n,1) ).astype( np.float32 )
print responses
newcomer = np.random.randint(0,10,(1,d)).astype( np.float32 )
#plt.scatter(newcomer[:,0],newcomer[:,1],80,'g','o')
knn = cv2.KNearest()
knn.train(trainData,responses)
ret, results, neighbours ,dist = knn.find_nearest(newcomer, 3)
print "result: ", results,"\n"
print "neighbours: ", neighbours,"\n"
print "distance: ", dist
pass
def computingWeightMatrix( W ) :
#W = np.matrix( W )
n = len( W )
# getting the number of rows in W
weightMatrix = np.zeros( shape = ( n , n ) )
#exploit the formula to compute the weight matrix
#print weightMatrix
#weightMatrix = np.matrix( weightMatrix )
d = 9
#computing the weight matrix
for i in xrange( 0 , n ) :
for j in xrange( 0 , n ) :
for k in xrange( 0 , d ) :
weightMatrix[ i ][ j ] += ( 0.5 * exp( -pow( W[ i , k ] - W[ j , k ] , 2 ) ) )
#print weightMatrix
print len( weightMatrix ), len( weightMatrix[ 0 ])
return weightMatrix
print n,d
def sumColumn(matrix):
return np.sum(matrix, axis=0)
def harmonic_function( W , fl ) :
#calculating 'l' which indicates it is the number of labeled points
l = len( fl )
print l
#calculating 'n' total number of points
n = len( W )
print n
#calculating the laplacian
W = np.matrix( W )
#G = np.arange(3) * np.arange(3)[:, np.newaxis]
L = csgraph.laplacian( W , normed = False )
print len( L ), L[ 0 ]
#calculating the harmonic function
#fu = np.reshape( L , ( ) )
fl = np.matrix( fl )
fu = -( linalg.inv(L[(l):n,l:n]) ).dot( L[(l):n,0:l] ).dot( fl )
#compute the CMN solution
#the unnormalized class proportion estimate from labeled data with laplace smoothing
#q = sumColumn( fl ) + 1
#fu_CMN = fu * np.kron(numpy.ones((n-1,1)), q / sumColumn( fu ) )
#print fu
return fu
if __name__ == '__main__' :
#harmonic_function( [[1,2,3,4,5] , [ 4 , 5 , 6, 7 , 8 ] , [ 7 , 8 , 9 , 10, 11], [1,1,3,5,5],[9,8,7,6,5]] , [[1,2,3] , [ 4 , 5 , 6 ] ] )
# print graph_construction( [[[1,0,3],[4,5,6],[10,11,12]],
# [[7,9,9],[10,11,12],[10,11,12]],
# [[13,11,15],[16,17,18],[10,11,12]]] , 1 ,3 , 3 , 3 )
f = open( 'temp.txt' , 'w' )
m = mean( input_matrix , 34 , 28 , 9 )
f.write( str( m ) )
f.close( )
labeledInstances = np.matrix( [[1,0,0],[1,0,0],[1,0,0],[0,1,0],[0,1,0],[0,1,0],[0,0,1],[0,0,1],[0,0,1]] )
print labeledInstances
temp = m[ 3 ]
m[ 3 ] = m[ 12 ]
m[ 12 ] = temp
temp = m[ 4 ]
m[ 4 ] = m[ 13 ]
m[ 13 ] = temp
temp = m[ 5 ]
m[ 5 ] = m[ 14 ]
m[ 14 ] = temp
temp = m[ 6 ]
m[ 6 ] = m[ 21 ]
m[ 21 ] = temp
temp = m[ 7 ]
m[ 7 ] = m[ 22 ]
m[ 22 ] = temp
temp = m[ 8 ]
m[ 23 ] = m[ 23 ]
m[ 23 ] = temp
print m
weightMatrix = computingWeightMatrix( m )
print len( m )
print len( weightMatrix ) ,len( weightMatrix[ 0 ] )
#weightMatrix = computingWeightMatrix( m )
#print weightMatrix
#print len( fl )
results = harmonic_function( weightMatrix , labeledInstances )
#print "----------Results----------"
#print results
indexes = results.argmax( axis = 1 )
print "----------Indexes----------"
count_of_eight = 8
temp_results = results
temp_results_list = []
temp_list = []
for i in xrange( 0 , 25 ) :
for j in xrange( 0 , 3 ) :
temp_list.append( 0 )
temp_list[ indexes[ i ] ] = 1
for j in xrange( 0 , 3 ) :
if temp_results[ i , indexes[ i ] ] - temp_results[ i , j ] < 0.03 :
temp_list[ j ] = 1
if i < count_of_eight :
temp_list[ 0 ] = 1
if indexes[ i ] != 0 :
temp_list[ indexes[ i ] ] = 0
temp_results_list.append( temp_list )
temp_list = []
for i in temp_results_list :
print i
#counting the accuracies
count = 0
counter = 0
index_counter = 0
for i in unlabeled_instances :
if counter < 3 :
count = count + 1
counter = counter + 1
continue
if counter < 15 and counter > 11 :
count = count + 1
counter = counter + 1
continue
if counter < 24 and counter > 20 :
count = count + 1
counter = counter + 1
continue
index = i.index( 1 )
counter = counter + 1
if temp_results_list[ index_counter ][ index ] == 1 :
count = count + 1
index_counter = index_counter + 1
#changes for the threshold cutoff done!!
for i in xrange( 0 , 25 ) :
if i < count_of_eight and i != 4 :
tempporary = temp_results[ i , 0 ]
temp_results[ i , 0 ] = temp_results[ i , indexes[ i ] ]
temp_results[ i , indexes[ i ] ] = tempporary
indexes[ i ] = 0
results = temp_results
#checking the score matrix from the results
print "----------Score Matrix----------"
print results
print "----------Checking the indexes----------"
print indexes
print "----------HITS----------"
print "HITS = " + str( count )
print str( count ) + " out of " + str( 34 )
print "Total number of video samples = 34 "
print "Accuracy = Total number of hits / Total number of Video Samples (34) "
print "Results shows that there are " + str( count ) +" hits out of 34 and the accuracy is :"
print count/34.0 * 100