-
Notifications
You must be signed in to change notification settings - Fork 3
/
DemoTest.py
164 lines (121 loc) · 4.95 KB
/
DemoTest.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
__author__ = 'GongLi'
import random
import math
import numpy as np
from sklearn.svm import SVC
import Utility as util
from sklearn.metrics import confusion_matrix
import pylab as pl
def constructBaseKernels(kernel_type, kernel_params, D2):
baseKernels = []
for i in range(len(kernel_type)):
for j in range(len(kernel_params)):
type = kernel_type[i]
param = kernel_params[j]
if type == "rbf":
baseKernels.append(math.e **(- param * D2))
elif type == "lap":
baseKernels.append(math.e **(- (param * D2) ** (0.5)))
elif type == "id":
baseKernels.append(1.0 / ((param * D2) ** (0.5) + 1))
elif type == "isd":
baseKernels.append(1.0 / (param * D2 + 1))
return baseKernels
def getTrainVideos(num):
birthdayIndices = [i for i in range(195, 346, 1)]
paradeIndices = [i for i in range(346, 465, 1)]
picnicIndices = [ i for i in range(465, 550, 1)]
showIndices = [i for i in range(550, 750, 1)]
sportsIndices = [i for i in range(750, 1010, 1)]
weddingIndices = [i for i in range(1010, 1101, 1)]
trainIndices = []
trainIndices.append(random.sample(birthdayIndices, num))
trainIndices.append(random.sample(paradeIndices, num))
trainIndices.append(random.sample(picnicIndices, num))
trainIndices.append(random.sample(showIndices, num))
trainIndices.append(random.sample(sportsIndices, num))
trainIndices.append(random.sample(weddingIndices, num))
return trainIndices
def getTestVideos(trainIndices, num):
birthdayIndices = [i for i in range(195, 346, 1)]
paradeIndices = [i for i in range(346, 465, 1)]
picnicIndices = [ i for i in range(465, 550, 1)]
showIndices = [i for i in range(550, 750, 1)]
sportsIndices = [i for i in range(750, 1010, 1)]
weddingIndices = [i for i in range(1010, 1101, 1)]
testIndices = []
temp = birthdayIndices
for i in trainIndices[0]:
temp.remove(i)
testIndices.append(random.sample(temp, num))
temp = paradeIndices
for i in trainIndices[1]:
temp.remove(i)
testIndices.append(random.sample(temp, num))
temp = picnicIndices
for i in trainIndices[2]:
temp.remove(i)
testIndices.append(random.sample(temp, num))
temp = showIndices
for i in trainIndices[3]:
temp.remove(i)
testIndices.append(random.sample(temp, num))
temp = sportsIndices
for i in trainIndices[4]:
temp.remove(i)
testIndices.append(random.sample(temp, num))
temp = weddingIndices
for i in trainIndices[5]:
temp.remove(i)
testIndices.append(random.sample(temp, num))
return testIndices
def runSVM_T(distances, labels, targetTrainingIndice, targetTestingIndice):
temp = []
for i in targetTrainingIndice:
for j in i:
temp.append(j)
targetTrainingIndice = temp
temp = []
for i in targetTestingIndice:
for j in i:
temp.append(j)
targetTestingIndice = temp
# Construct base kernels
baseKernels = []
for i in range(len(distances)):
distance = distances[i]
distance = distance ** 2
trainingDistances = distance[np.ix_(targetTrainingIndice, targetTrainingIndice)]
# Define kernel parameters
gramma0 = 1.0 / np.mean(trainingDistances)
# kernel_params = [gramma0 *(2 ** index) for index in range(-3, 2, 1)]
kernel_params = []
kernel_params.append(gramma0)
# Construct base kernels & pre-learned classifier
baseKernel = constructBaseKernels(["rbf", "lap", "isd","id"], kernel_params, distance)
baseKernels += baseKernel
TrainingLabels = [labels[index] for index in targetTrainingIndice]
TestingLabels = [labels[index] for index in targetTestingIndice]
for m in range(len(baseKernels)):
baseKernel = baseKernels[m]
Ktrain = baseKernel[np.ix_(targetTrainingIndice, targetTrainingIndice)]
Ktest = baseKernel[np.ix_(targetTestingIndice , targetTrainingIndice)]
clf = SVC(kernel="precomputed")
clf.fit(Ktrain, TrainingLabels)
prediction = clf.predict(Ktest)
correct = sum(1.0 * (prediction == TestingLabels))
accuracy = correct / len(TestingLabels)
print str(accuracy)
# cm = confusion_matrix(TestingLabels, prediction)
# # Show confusion matrix
# pl.matshow(cm)
# pl.title('Confusion matrix')
# pl.show()
if __name__ == "__main__":
distance = util.loadObject("/Users/GongLi/PycharmProjects/DomainAdaption/Distances/voc2500/LevelZero/All/Normal/all_DistanceMatrix_Level0.pkl")
labels = util.loadObject("/Users/GongLi/PycharmProjects/DomainAdaption/Distances/voc2500/LevelZero/All/Normal/all_labels_Level0.pkl")
distances = []
distances.append(distance)
trainIndices = getTrainVideos(80)
testIndices = getTestVideos(trainIndices, 3)
runSVM_T(distances, labels, trainIndices, testIndices)