-
Notifications
You must be signed in to change notification settings - Fork 0
/
ensemble.py
85 lines (68 loc) · 2.88 KB
/
ensemble.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
import os
import sys
import json
from ast import literal_eval
from posterior import generate_submit
models = ["xception", "inception", "vgg", "resnet"]
statusesPath = os.environ["PROJECT_DIR"] + "/statuses"
imagesPath = os.environ["PROJECT_DIR"] + "/dataset"
#bestModel = {model: getBestModel(model) for model in models}
#def getBestModel(model):
# prefix = "transferlearning_train_ "
# i = 0
# minValidationError = 1.
# bestModel = 0
# while os.path.isfile(statusesPath + prefix + i + ".status"):
# with open(statusesPath + prefix + i + ".status", encoding='utf-8') as data_file:
# data = json.loads(data_file.read())
# if data["validation_error"] < minValidationError:
# minValidationError = data["validation_error"]
# bestModel = i
# i += 1
# return bestModel
def getTestDistribution(test_path):
trueImages = len([name for name in os.listdir(test_path + '/merger/') if os.path.isfile(test_path + '/merger/' + name)])
falseImages = len([name for name in os.listdir(test_path + '/noninteracting/') if os.path.isfile(test_path + '/noninteracting/' + name)])
totalImages = trueImages + falseImages
print(trueImages, falseImages, totalImages)
return trueImages/totalImages, falseImages/totalImages, totalImages
def ensemblePredictions():
perFilePredictions = {}
for model in models:
# with open(statusesPath + "/" + model + "/probPreds") as f:
modelPredictions = (literal_eval(f) for f in open(statusesPath + "/" + model + "/probPreds"))
for p in modelPredictions:
for pred in p:
if pred[0] not in perFilePredictions:
perFilePredictions[pred[0]] = pred[1]
else:
perFilePredictions[pred[0]] = [x + y for x, y in zip(perFilePredictions[pred[0]], pred[1])]
ensembledPredictions = {}
for filename, predictions in perFilePredictions.items():
ensembledPredictions[filename] = [x/len(models) for x in predictions]
return ensembledPredictions
def applyPosterior(ensembledPredictions):
weightedPredictions = {}
for key, value in ensembledPredictions.items():
weightedPredictions[key] = [value[0]*mergerProportion, value[1]* nonMergerProportion]
return weightedPredictions
mergerProportion, nonMergerProportion, totalImages = getTestDistribution(imagesPath + "/test")
bestModel = {
"vgg": 2,
"inception": 1,
"xception": 7,
"resnet": 2
}
ensembledPredictions = ensemblePredictions()
if len(sys.argv) > 1 and sys.argv[1] == "withPosterior":
ensembledPredictions = generate_submit(list(ensembledPredictions.values()), list(ensembledPredictions.keys()), 'posterior.test')
correct = 0
for key, value in ensembledPredictions.items():
if ("merger" in key and value == "merger") or ("noninteracting" in key and value == "noninteracting"):
correct += 1
else:
print(key,value)
if len(sys.argv) > 1 and sys.argv[1] == "withPosterior":
print("Accuracy of Ensemble Model with Posterior: ", correct/totalImages)
else:
print("Accuracy of Ensemble Model: ", correct/totalImages)