Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ShagReza authored Oct 16, 2023
0 parents commit a873842
Show file tree
Hide file tree
Showing 22 changed files with 1,299 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ComputeCosineScore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 8 09:43:57 2020
@author: user
"""

import numpy as np
import scipy.io
from utils import saveplot,load_data

def ComputeCosineScore(x_test):

D2 = scipy.io.loadmat('test_ivecs.mat')
D2['Results']['ivectors'][0,0]=np.transpose(x_test)
scipy.io.savemat('test_ivecs_filtered.mat', D2)


l = [0 , 0.0001 , .001 , .01 , 0.1 , 0.2,0.3,0.4,0.5,0.75,1]
dic=load_data('test_ivecs_filtered.npz' )
scores_cosine=saveplot(dic , l , 'test-all.scp','ALL' , 'Ivector_raw' , 'Cosine' , 'log.txt','\t',1)
return scores_cosine
36 changes: 36 additions & 0 deletions ComputeCosineScore_OnTrainSet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 8 10:14:04 2020
@author: user
"""
import numpy as np

def cosinedis(v1,v2):
dot_product = np.dot(v1, v2)
norm_a = np.linalg.norm(v1)
norm_b = np.linalg.norm(v2)
return dot_product / (norm_a * norm_b)



def ComputeCosineScore_OnTrainSet(FusionTrails_positive,FusionTrails_negative):
y_pred=[]
data=FusionTrails_positive
for i in range(len(data)):
file1 =data[i][0:150]
file2= data[i][150:300]
res = cosinedis(file1,file2)
y_pred.append(res)
scores_cosine_positive=y_pred

y_pred=[]
data=FusionTrails_negative
for i in range(len(data)):
file1 =data[i][0:150]
file2= data[i][150:300]
res = cosinedis(file1,file2)
y_pred.append(res)
scores_cosine_negative=y_pred

return scores_cosine_positive,scores_cosine_negative
60 changes: 60 additions & 0 deletions ComputeGuassianScores_OnTrainSet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 8 12:30:31 2020
@author: user
"""
from sklearn.mixture import GaussianMixture as Gaussian
import numpy as np

def ComputeGuassianScores_OnTrainSet(FusionTrails_positive,FusionTrails_negative):

NegativePairs=np.load('NegativePairs.npy')
PositivePairs=np.load('PositivePairs.npy')

#8: Train Gaussian models
GaussainPositive=Gaussian(n_components=1,covariance_type='full')
GaussainNegative=Gaussian(n_components=1,covariance_type='full')
GaussainPositive.fit(PositivePairs)
GaussainNegative.fit(NegativePairs)
cov_P=GaussainPositive.covariances_
mean_P=GaussainPositive.means_
cov_N=GaussainNegative.covariances_
mean_N=GaussainNegative.means_


cov_N = np.reshape(cov_N, (300,300))
cov_P = np.reshape(cov_P, (300,300))
InvCovP=np.linalg.inv(cov_P)
InvCovN=np.linalg.inv(cov_N)
#-------------------------------------------
#9: Compute Scores
x_test=FusionTrails_positive
NumTest=len(x_test)
scores=[]
for i in range(NumTest):
A1=x_test[i][0:150]
A2=x_test[i][150:300]
Pairs=np.append(A1,A2)
Pairs = np.reshape(Pairs, (1,300))
s=-1*(Pairs-mean_P).dot(InvCovP).dot(np.transpose(Pairs-mean_P))+(Pairs-mean_N).dot(InvCovN).dot(np.transpose(Pairs-mean_N))
scores.append(s[0][0])
scores_guassian_positive=scores

x_test=FusionTrails_negative
NumTest=len(x_test)
scores=[]
for i in range(NumTest):
A1=x_test[i][0:150]
A2=x_test[i][150:300]
Pairs=np.append(A1,A2)
Pairs = np.reshape(Pairs, (1,300))
s=-1*(Pairs-mean_P).dot(InvCovP).dot(np.transpose(Pairs-mean_P))+(Pairs-mean_N).dot(InvCovN).dot(np.transpose(Pairs-mean_N))
scores.append(s[0][0])
scores_guassian_negative=scores

np.save('InvCovP',InvCovP)
np.save('InvCovN',InvCovN)
np.save('mean_P',mean_P)
np.save('mean_N',mean_N)
return scores_guassian_positive,scores_guassian_negative
40 changes: 40 additions & 0 deletions ComputeMopldaScores_OnTrainSet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

import numpy as np
import matlab.engine
eng = matlab.engine.start_matlab()




def ComputeMopldaScores_OnTrainSet(FusionTrails_positive,FusionTrails_negative):


x_test=FusionTrails_positive
NumTest=len(x_test)
scores=[]
for i in range(NumTest):
print('positive trail',i)
A1=x_test[i][0:250]
A2=x_test[i][250:500]
A1matlab = matlab.double(A1.tolist())
A2matlab = matlab.double(A2.tolist())
s= eng.score_moplda_trials(A1matlab,A2matlab,nargout=1)
scores.append(s)
scores_Moplda_positive=scores


x_test=FusionTrails_negative
NumTest=len(x_test)
scores=[]
for i in range(NumTest):
print('negative trail',i)
A1=x_test[i][0:250]
A2=x_test[i][250:500]
A1matlab = matlab.double(A1.tolist())
A2matlab = matlab.double(A2.tolist())
s = eng.score_moplda_trials(A1matlab,A2matlab,nargout=1)
scores.append(s)
scores_Moplda_negative=scores

eng.quit()
return scores_Moplda_positive,scores_Moplda_negative
52 changes: 52 additions & 0 deletions ComputePldaScores_OnTrainSet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 18 08:48:43 2020
@author: user
"""

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 8 12:30:31 2020
@author: user
"""
import numpy as np
import matlab.engine
eng = matlab.engine.start_matlab()




def ComputePldaScores_OnTrainSet(FusionTrails_positive,FusionTrails_negative):


x_test=FusionTrails_positive
NumTest=len(x_test)
scores=[]
for i in range(NumTest):
print('positive trail',i)
A1=x_test[i][0:150]
A2=x_test[i][150:300]
A1matlab = matlab.double(A1.tolist())
A2matlab = matlab.double(A2.tolist())
s= eng.score_gplda_trials(A1matlab,A2matlab,nargout=1)
scores.append(s)
scores_Plda_positive=scores


x_test=FusionTrails_negative
NumTest=len(x_test)
scores=[]
for i in range(NumTest):
print('negative trail',i)
A1=x_test[i][0:150]
A2=x_test[i][150:300]
A1matlab = matlab.double(A1.tolist())
A2matlab = matlab.double(A2.tolist())
s = eng.score_gplda_trials(A1matlab,A2matlab,nargout=1)
scores.append(s)
scores_Plda_negative=scores

eng.quit()
return scores_Plda_positive,scores_Plda_negative
102 changes: 102 additions & 0 deletions ExtractFusionTrails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 1 10:06:47 2020
@author: user
"""


#---------------------------------------------------------------------------
import pandas as pd
import numpy as np
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LdaSciKit
import scipy.io
#from sklearn.LpLda import LinearDiscriminantAnalysis as LpLdaSciKit
from sklearn.mixture import GaussianMixture as Gaussian
import itertools, random
#---------------------------------------------------------------------------


#---------------------------------------------------------------------------
#usage: alist = select(10,2)
def ListOfPositivePairs(size, pair_size):
g =itertools.combinations(range(size),pair_size)
alist = list(g)
random.seed(4)
random.shuffle(alist)
return alist

def ListOfNegativePairs(list1, list2):
alist = list(itertools.product(list1, list2))
random.seed(4)
random.shuffle(alist)
return alist
#---------------------------------------------------------------------------





def ExtractFusionTrails(x_train,label_train,dim_lda):
TrailDim=2*dim_lda
#------------------------------------
#positive trails:
vectors=x_train
labels=label_train
unique_labels = np.unique(label_train)
PositivePairs=np.empty((1,TrailDim))
Pairs=np.empty((1,TrailDim))
for label in unique_labels:
vecs = [vectors[i] for i in range(len(vectors)) if labels[i] == label]
print(label, len(vecs))
a=len(vecs)
alist = ListOfPositivePairs(a,2)

if len(alist)>0:
L=25
if len(alist)<L:
L=len(alist)
for x in range(L):
A1=vecs[alist[x][0]]
A2=vecs[alist[x][1]]
Pairs=np.append(A1,A2)
Pairs = np.reshape(Pairs, (1,TrailDim))
PositivePairs=np.append(PositivePairs,Pairs,axis=0)

PositivePairs=np.delete(PositivePairs,0,0)
FusionTrails_positive=PositivePairs
#np.save('FusionTrails_positive',FusionTrails_positive)
#PositivePairs=np.load('PositivePairs.npy')
#-----------------------------------
# Negative trails:
NegativePairs=np.empty((1,TrailDim))
Pairs=np.empty((1,TrailDim))
for label in unique_labels:
print(label)
vecs = [vectors[i] for i in range(len(vectors)) if labels[i] == label]
vecs_not = [vectors[i] for i in range(len(vectors)) if labels[i] != label]

#alist = ListOfNegativePairs( list(range(len(vecs))) , list(range(len(vecs_not))) #time consuming

A=list(range(len(vecs_not)))
random.seed(4)
random.shuffle(A)
AA=A[0:100]
alist = ListOfNegativePairs( list(range(len(vecs))) , AA )

if len(alist)>0:
L=20
if len(alist)<L:
L=len(alist)
for x in range(L):
A1=vecs[alist[x][0]]
A2=vecs_not[alist[x][1]]
Pairs=np.append(A1,A2)
Pairs = np.reshape(Pairs, (1,TrailDim))
NegativePairs=np.append(NegativePairs,Pairs,axis=0)
NegativePairs=np.delete(NegativePairs,0,0)
FusionTrails_negative=NegativePairs
#np.save('FusionTrails_negative',FusionTrails_negative)
#NegativePairs=np.load('NegativePairs.npy')

return FusionTrails_negative , FusionTrails_positive
Loading

0 comments on commit a873842

Please sign in to comment.