-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSEDE.py
193 lines (185 loc) · 10.5 KB
/
SEDE.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
import config
from imports import basename, argparse, os, shutil, join, np, exists, dirname
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='DNN debugger')
parser.add_argument('-a', '--action', help='supported actions: test, heatmap, cluster, assign, retrain',
required=False)
parser.add_argument('-m', '--modelName', help='pretrainedWeights.pth Path', required=False)
parser.add_argument('-o', '--outputPathX', help='Output path for saving the result', required=True)
parser.add_argument('-sF', '--scratchFlag', help='Number of Classes', required=False)
parser.add_argument('-n', '--ClusterModeX', help='ICD - WICD - S', required=False)
parser.add_argument('-cF', '--clustF', help='clustering Flag', required=False)
parser.add_argument('-dcF', '--drawCF', help='Exporting images Flag', required=False)
parser.add_argument('-aF', '--assignF', help='Exporting images Flag', required=False)
parser.add_argument('-daF', '--drawAssignF', help='Exporting images Flag', required=False)
parser.add_argument('-err', '--errorMarginPixels', help='error Margin Pixels', required=False)
parser.add_argument('-sub', '--faceSubSet', help='Subset of the face', required=False)
parser.add_argument('-tl', '--transfer', help='scratch/pretrained', required=False)
parser.add_argument('-rF', '--retrainF', help='HUDD, BL1, BL2', required=False)
parser.add_argument('-mode', '--retrainMode', help='HUDD, BL1, BL2', required=False)
parser.add_argument('-app', '--approach', help='A, B', required=False)
parser.add_argument('-exp1', '--expNumber', help='Number of retrainings', required=False)
parser.add_argument('-exp2', '--expNumber2', help='Number of retrainings', required=False)
parser.add_argument('-ep', '--epoch', help='Number of epochs', required=False)
parser.add_argument('-ass', '--assignMode', help='ICD - Centroid - Closest - SSE', required=False)
parser.add_argument('-bs', '--BagSize', help='ICD - Centroid - Closest - SSE', required=False)
parser.add_argument('-mc', '--maxClust', help='ICD - Centroid - Closest - SSE', required=False)
parser.add_argument('-ow', '--ow', help='overwrite flag', required=False)
parser.add_argument('-sel', '--select', help='layer selection mode', required=False)
parser.add_argument('-fld', '--FLD', help='FLD selection mode', required=False)
parser.add_argument('-wc', '--workersCount', help='FLD selection mode', required=False)
parser.add_argument('-batchS', '--batchSize', help='FLD selection mode', required=False)
parser.add_argument('-cleanF', '--cleanFlag', help='FLD selection mode', required=False)
parser.add_argument('-rcc', '--rccSource', help='FLD selection mode', required=False)
parser.add_argument('-numR', '--numRuns', help='FLD selection mode', required=False)
parser.add_argument('-rA', '--retrieveAccuracy', help='FLD selection mode', required=False)
parser.add_argument('-rq', '--RQ1A', help='FLD selection mode', required=False)
parser.add_argument('-rS', '--retrainSet', help='FLD selection mode', required=False)
parser.add_argument('-SEDE', '--SEDEmode', help='FLD selection mode', required=False)
parser.add_argument('-iee', '--ieeVersion', default="1", help='iee_sim1, iee_sim2', required=False)
parser.add_argument('-cls', '--clsNum', default="1", help='iee_sim1, iee_sim2', required=False)
parser.add_argument('-case', '--caseStudy', default="FLD", help='FLD, HPD-H, HPD-F', required=False)
args = parser.parse_args()
#if args.SEDEmode == "RQ1":
# import SEDE_RQ1
# SEDE_RQ1.doRQ(args.caseStudy, os.getcwd())
components = ["noseridge", "nose", "mouth", "rightbrow", "righteye", "lefteye", "leftbrow"]
if args.ieeVersion is not None:
if int(args.ieeVersion) == 1:
config.nVar = 13
elif int(args.ieeVersion) == 2:
config.nVar = 23
else:
print("WARNING: number of Variables in config.nVar is not set")
else:
print("WARNING: number of Variables in config.nVar is not set")
import Helper
if args.caseStudy == "HPD-H" or args.caseStudy == "HPD-F":
args.outputPathX = join(dirname(args.outputPathX), "HPD")
SEDE = Helper.Helper(outputPath=args.outputPathX, modelName=args.modelName, workersCount=args.workersCount,
batchSize=args.batchSize, metric="Euc", clustFlag=args.clustF, assignFlag=args.assignF,
retrainFlag=args.retrainF, retrainMode=args.retrainMode, retrainApproach=args.approach,
expNumber=args.expNumber, expNumber2=args.expNumber2, bagSize=args.BagSize,
clustMode=args.ClusterModeX, assMode=args.assignMode,
overWrite=args.ow, selectionMode=args.select, FLD=args.FLD, cleanFlag=args.cleanFlag,
RCC=args.rccSource, scratchFlag=args.scratchFlag, retrieveAccuracy=args.retrieveAccuracy,
RQ1A=False, retrainSet=args.retrainSet, drawClustFlag=args.drawCF, ieeVersion=args.ieeVersion, clustNum=args.clsNum)
#HUDD.updateCaseFile()
finalResultDict = {}
datasetName = basename(args.outputPathX)
TestSetCheck = False
if args.SEDEmode == "HUDD":
if datasetName == "FLD":
if args.faceSubSet is None:
maxSub = 0.0
for subset in components:
print(subset)
# ResultDict, _ = HUDD.KPNet(subset)
# HUDD.faceSubset = subset
# HUDD.updateCaseFile()
# HUDD.saveResult()
else:
print(args.faceSubSet)
ResultDict, _ = SEDE.KPNet(args.faceSubSet)
else:
ResultDict, _ = SEDE.AlexNet()
if args.numRuns is None:
if datasetName == "FLD":
ResultDict, _ = SEDE.KPNet(components[0])
SEDE.faceSubSet = components[0]
# HUDD.saveResult()
SEDE.retrainDNN()
else:
for x in range(0, int(args.numRuns)):
SEDE.retrainDNN()
elif args.SEDEmode == "SEDE":
import searchModule, assignModule
SEDE.RCC = "TR" #HPD-H
#self.RCC = "TR1" #HPD-F
SEDE.updateCaseFile()
SEDE.selectLayer()
print("Loading HM distance file for the selected layer.")
HMDistFile = join(str(SEDE.caseFile["filesPath"]), str(SEDE.caseFile["selectedLayer"]) + "HMDistance.xlsx")
clusterRadius, centroidHM, testHM = assignModule.getClusterData(SEDE.caseFile, HMDistFile)
#clusters = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] #HPD-H
#clusters = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] #FLD/HPD-F
clusters = [SEDE.clustNum]
popSize = [25, 25, 25]
nGen = [200, 100, 100]
searchModule.search(SEDE.caseFile, clusters, centroidHM, clusterRadius, popSize, nGen)
elif args.SEDEmode == "DeepJanus":
from DeepJanus import main
import assignModule
SEDE.RCC = "TR" #HPD-H
#self.RCC = "TR1" #HPD-F
SEDE.updateCaseFile()
SEDE.selectLayer()
print("Loading HM distance file for the selected layer.")
HMDistFile = join(str(SEDE.caseFile["filesPath"]), str(SEDE.caseFile["selectedLayer"]) + "HMDistance.xlsx")
clusterRadius, centroidHM, testHM = assignModule.getClusterData(SEDE.caseFile, HMDistFile)
#clusters = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] #HPD-H
#clusters = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] #FLD/HPD-F
clusters = [SEDE.clustNum]
main.runSEDE(SEDE.caseFile, clusters, centroidHM, clusterRadius)
elif args.SEDEmode == "RQ2":
import SEDE_RQ2, searchModule, assignModule
from imports import torch
SEDE.RCC = "TR" #HPD-H
#self.RCC = "TR1" #HPD-F
SEDE.updateCaseFile()
SEDE.selectLayer()
print("Loading HM distance file for the selected layer.")
HMDistFile = join(str(SEDE.caseFile["filesPath"]), str(SEDE.caseFile["selectedLayer"]) + "HMDistance.xlsx")
clusterRadius, centroidHM, testHM = assignModule.getClusterData(SEDE.caseFile, HMDistFile)
layerClust = join(SEDE.caseFile["filesPath"], "ClusterAnalysis_" + str(SEDE.caseFile["clustMode"]),
SEDE.caseFile["selectedLayer"] + ".pt")
clsData = torch.load(layerClust, map_location=torch.device('cpu'))
clusters = clsData['clusters']
#clusters = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] #HPD-H
#clusters = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] #FLD/HPD-F
SEDE.caseFile["caseStudy"] = args.caseStudy
SEDE_RQ2.RQ(centroidHM, SEDE_RQ2.getSEDE_imgs(clusters, SEDE.caseFile), SEDE.caseFile)
elif args.SEDEmode == "RQ3":
import SEDE_RQ3, searchModule, assignModule
SEDE.RCC = "TR" #HPD-H
#self.RCC = "TR1" #HPD-F
SEDE.updateCaseFile()
SEDE.selectLayer()
print("Loading HM distance file for the selected layer.")
HMDistFile = join(str(SEDE.caseFile["filesPath"]), str(SEDE.caseFile["selectedLayer"]) + "HMDistance.xlsx")
clusterRadius, centroidHM, testHM = assignModule.getClusterData(SEDE.caseFile, HMDistFile)
#clusters = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] #HPD-H
clusters = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] #FLD/HPD-F
SEDE_RQ3.plotRQ(SEDE.caseFile)
elif args.SEDEmode == "RQ4":
import SEDE_RQ4
SEDE.RCC = "TR"
SEDE.updateCaseFile()
SEDE_RQ4.evaluateResults(SEDE.caseFile)
import scipy.stats as stats
import numpy as np
while int(input("continue? 1: Yes, 2: No")) == 1:
n = input("correctly classified images: ")
total = input("total images: ")
ob_table = np.array([[2800, 1210], [int(total), int(n)]])
result = stats.chi2_contingency(ob_table, correction=False) # correction = False due to df=1
chisq, pvalue = result[:2]
print('chisq = {}, pvalue = {}'.format(chisq, pvalue))
result = stats.fisher_exact(ob_table)
oddsr, pvalue = result[:2]
print('fisher = {}, pvalue = {}'.format(oddsr, pvalue))
elif args.SEDEmode == "RQ5":
SEDE.selectLayer()
SEDE.retrainDNN()
elif args.SEDEmode == "testModel":
SEDE.saveResult()
elif args.SEDEmode == "generateHeatmaps":
SEDE.generateHeatmaps()
elif args.SEDEmode == "generateHMDists":
SEDE.generateHMDistances()
elif args.SEDEmode == "generateClusters":
SEDE.generateClusters()
SEDE.selectLayer()
elif args.SEDEmode == "assignImages":
SEDE.selectLayer()
SEDE.assignImages()