-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmaster.py
executable file
·165 lines (124 loc) · 4.89 KB
/
master.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# --------------------------------------------------------
# Master File
# Written by berkan
# Contact: [email protected]
# --------------------------------------------------------
from easydict import EasyDict as edict
import subprocess, os
import pickle
import operator
__C = edict()
cfg = __C
tmpFileName = 'tmpfile'
datasetList = {0:"data/aPaY", 1:"data/AwA"}
sideInformationList = {0: "GloVe", 1: "word2vec"}
networkModelList = {0: "CNN_M2K", 1: "GoogleNet"}
lossType = {0: "predicateBased", 1: "imageBased", 2: "combined"}
DATASET = datasetList[1]
languageModel = sideInformationList[0]
networkModel = networkModelList[0]
applyLossType = lossType[1]
applyCrossValidation = False
saveWordVectors = False
def prepareData(mainPath, stopIter = 0, overfittingThreshold = 0):
__C = edict()
if DATASET == datasetList[0]:
__C.SAVE_FILE = 'aPaYResults.txt'
__C.VISUAL_DATA = 'visual/aPaY/'
__C.LEARNED_MODEL_PATH = 'models/'+applyLossType+'/aPaY/'
__C.WORD_VECTORS = 'wordVectors/aPaY/'
__C.LOG_FILE = 'aPaYLog.txt'
elif DATASET == datasetList[1]:
__C.SAVE_FILE = 'AwAResults.txt'
__C.VISUAL_DATA = 'visual/AwA/'
__C.WORD_VECTORS = 'wordVectors/AwA/'
__C.LEARNED_MODEL_PATH = 'models/'+applyLossType+'/AwA/'
__C.LOG_FILE = 'AwALog.txt'
elif DATASET == datasetList[2]:
__C.SAVE_FILE = 'CUBResults.txt'
__C.VISUAL_DATA = 'visual/CUB/'
__C.LEARNED_MODEL_PATH = 'models/'+applyLossType+'/CUB/'
__C.WORD_VECTORS = 'wordVectors/CUB/'
__C.LOG_FILE = 'CUBLog.txt'
else:
pass
if applyCrossValidation == False:
__C.SAVE_MODEL = True
else:
__C.SAVE_MODEL = False
__C.NUM_EPOCH = 1000000
__C.NUM_HIDDEN = [100]#[100, 200, 300, 400, 500]
__C.VERBOSE = True
__C.PERTURBED_EXAMPLES = False
__C.PERTURBED_EXAMPLE_CORRLEVEL = 5
__C.MAX_BATCH_SIZE = 64
__C.NUMBER_OF_FOLD = 2
__C.PLOT_ACC_PER_N_ITER = 100
__C.OVERFITTING_THRESHOLD = overfittingThreshold
__C.CV_PATH = DATASET + '/cv_data/'
__C.TMP_FILENAME = tmpFileName
if stopIter == 0:
__C.STOP_ITER = __C.NUM_EPOCH
else:
__C.STOP_ITER = stopIter
__C.TRAIN_CLASS_PATH = mainPath +'/'+languageModel+ '/trainClasses.mat'
__C.TEST_CLASS_PATH = mainPath +'/'+languageModel+ '/testClasses.mat'
__C.ATTRIBUTE_VECTOR_PATH = mainPath +'/'+languageModel+ '/attributeVectors.mat'
__C.PREDICATE_MATRIX_PATH = mainPath + '/predicateMatrix.mat'
__C.ATTR_CLASSIFIER_RESULTS_PATH = mainPath +'/'+networkModel+ '/attClassifierResults.mat'
__C.GROUND_TRUTH_LABELS = mainPath + '/groundTruthLabels.mat'
__C.TRAIN_IMAGE_LABELS = mainPath + '/trainImageLabels.mat'
__C.TRAIN_SCORES = mainPath +'/'+networkModel+'/trainScores.mat'
return __C
cfg = __C
if __name__ == '__main__':
try:
os.remove(os.getcwd() + '/' + tmpFileName)
except:
pass
if applyCrossValidation:
__C = prepareData(DATASET, 30000,0)
try:
os.remove(os.getcwd() + '/' + __C.LOG_FILE)
except:
pass
for currHidden in __C.NUM_HIDDEN:
for currFold in xrange(__C.NUMBER_OF_FOLD):
__C = prepareData(__C.CV_PATH+str(currFold+1), 30000,0)
__C.CURR_HIDDEN = currHidden
with open('objs.pickle', 'w') as f:
pickle.dump(__C, f)
subprocess.call('python zsl.py', shell=True)
with open(__C.LOG_FILE, "r") as ins:
cvAccr = {}
for line in ins:
currentAccuracy = float(line.split(',')[2].split(':')[1])
currentKey = line.split(',')[0]+' '+line.split(',')[1]
if currentKey in cvAccr:
if cvAccr[currentKey] == 0:
cvAccr[currentKey] = currentAccuracy
else:
cvAccr[currentKey] = (cvAccr[currentKey] + currentAccuracy)/2
else:
cvAccr[currentKey] = currentAccuracy
print max(cvAccr.iteritems(), key=operator.itemgetter(1))[0]
else:
__C = prepareData(DATASET, 26000, 0)
try:
os.remove(os.getcwd() + '/' + __C.LOG_FILE)
except:
pass
for currHidden in __C.NUM_HIDDEN:
__C.CURR_HIDDEN = currHidden
with open('objs.pickle', 'w') as f:
pickle.dump(__C, f)
subprocess.call('python zsl.py', shell=True)
file = open(__C.SAVE_FILE, 'a')
resultList = [float(line.rstrip('\n')) for line in open(__C.TMP_FILENAME)]
os.remove(os.getcwd() + '/' + __C.TMP_FILENAME)
file.write('ACCURACY: '+ str(resultList)+'\n')
file.write('NUM_HIDDEN: '+ str(currHidden)+'\n')
file.write('###############\n')
file.close()