An error occurred while loading the file. Please try again.
-
Benedetti Paola authored7e6f4737
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
194
195
196
197
198
199
200
import sys
import os
import tensorflow as tf
import numpy as np
from sklearn.metrics import accuracy_score, cohen_kappa_score, f1_score
from sklearn.metrics import precision_recall_fscore_support
from sklearn.ensemble import RandomForestClassifier
#Format values matrix from (n,22*13) shape to (n,22,13)
def getRNNFormat(X, n_timestamps):
#print X.shape
new_X = []
for row in X:
new_X.append( np.split(row, n_timestamps) )
return np.array(new_X)
#Format labels matrix from (n,1) shape to (n,3)
def getRNNFormatLabel(Y):
vals = np.unique(np.array(Y))
sorted(vals)
hash_val = {}
for el in vals:
hash_val[el] = len(hash_val.keys())
new_Y = []
for el in Y:
t = np.zeros(len(vals))
t[hash_val[el]] = 1.0
new_Y.append(t)
return np.array(new_Y)
#Get i-th batches of values set X and labels set Y
def getBatch(X, Y, i, batchsz):
start_id = i*batchsz
end_id = min( (i+1) * batchsz, X.shape[0])
batch_x = X[start_id:end_id]
batch_y = Y[start_id:end_id]
return batch_x, batch_y
def getLabelFormat(Y):
vals = np.unique(np.array(Y))
sorted(vals)
hash_val = {}
for el in vals:
hash_val[el] = len(hash_val.keys())
new_Y = []
for el in Y:
t = np.zeros(len(vals))
t[hash_val[el]] = 1.0
new_Y.append(t)
return np.array(new_Y)
#MAIN
#Model parameters
nunits = 1024
batchsz = 64
hm_epochs = 400
n_levels_lstm = 1
#dropout = 0.2
#Data INformation
n_timestamps = 34
n_dims = 16
patch_window = 25
n_channels = 5
itr = int( sys.argv[1] )
p_split = 100*float( sys.argv[2] )
choice = sys.argv[3]
path_in_val = sys.argv[4]
path_in_gt = sys.argv[5]
g_path = './dataset/TS/25x25/'
#declare paths
#g_path = "./dataset/NDVI/15x15/"
net_path = g_path+"%s_%d_%d/"%(choice, p_split, batchsz)
path_in_dir = g_path+'%s_%d_%d/modelTT%d/'%( choice, p_split, batchsz, itr ) #model path
path_in_model = path_in_dir+'model-%d.meta'%itr #model file
#path_in_x = path_in_val+'test_x%d_%d.npy'%(itr,p_split) #values
#path_in_y = path_in_gt+'test_y%d_%d.npy'%(itr,p_split) #gt
ftest_acc = open(net_path+"test_accuracy.txt", "a")
ftest_fscore = open(net_path+"test_fscore.txt","a")
ftest_precisions = open(net_path+"test_prec_classes.txt","a")
ftest_recall = open(net_path+"test_rec_classes.txt","a")
ftest_scores = open(net_path+"test_fscore_classes.txt","a")
graph_var = open(net_path+"graph_var.txt","a")
tf.reset_default_graph()
with tf.Session() as sess:
model_saver = tf.train.import_meta_graph(path_in_model)
model_saver.restore(sess, tf.train.latest_checkpoint(path_in_dir))
print "Model Restored"
writer = tf.summary.FileWriter( path_in_dir+'histogram_example', sess.graph )
print "Graph Wrote"
writer.close()
graph = tf.get_default_graph()
#for n in tf.global_variables():
# print n
#for op in graph.get_operations():
# print op.values()
# graph_var.write(str(op.values())+"\n")
#graph_var.close()
#exit()
x = graph.get_tensor_by_name("x_rnn:0")
y = graph.get_tensor_by_name("y:0")
#in densenet
'''
if 'AttentionII' in choice:
p = graph.get_tensor_by_name("dense_37/BiasAdd:0")
print "ATTENTION II"
else:
p = graph.get_tensor_by_name("dense_3/BiasAdd:0")
print "ATTENTION I"
'''
#p = graph.get_tensor_by_name("prediction/BiasAdd:0")
p = graph.get_tensor_by_name("prediction/BiasAdd:0")
learning_rate = graph.get_tensor_by_name("learning_rate:0")
is_training_ph = graph.get_tensor_by_name("is_training:0")
dropout = graph.get_tensor_by_name("drop_rate:0")
#load ds
test_x = np.load(path_in_val+'test_x%d_%d.npy'%(itr,p_split))
test_y = np.load(path_in_gt+'test_y%d_%d.npy'%(itr,p_split))
#format data
test_x = getRNNFormat( test_x, n_timestamps)
test_y = getLabelFormat( test_y )
#EVAL TEST
testAcc = 0
testFscore = 0
testFArray = np.array([])
pred_test = []
feat_test = []
iter_test = len( test_x )/batchsz
if len( test_y )%batchsz != 0:
iter_test+=1
cl = np.zeros( len( test_y ) )
for i_batch in range( iter_test ):
batch_x, batch_y = getBatch( test_x, cl, i_batch, batchsz )
pred_temp = sess.run( p , feed_dict={ x:batch_x,
is_training_ph:False,
dropout:1.0,
learning_rate:0.0002} )
for el in pred_temp:
pred_test.append( np.argmax(el) )
#grounf_truth test
gt_test = []
for el in test_y:
gt_test.append( np.argmax( el ) )
pred_test = pred_test[ 0:len( gt_test )]
testAcc = accuracy_score( gt_test, pred_test )
testFscore = f1_score( gt_test, pred_test, average='weighted' )
var_prec, var_rec, var_fsc, _ = precision_recall_fscore_support( gt_test, pred_test )
print "Test Accuracy: %f\nTest Fscore:%f \nTest Fxclassses: %s\nTest Pxclasses:%s\nTest Rxclasses:%s\n" %( testAcc, testFscore, var_fsc, var_prec, var_rec )
ftest_acc.write("%f\n"%testAcc)
ftest_fscore.write("%f\n"%testFscore)
for el in var_rec:
ftest_recall.write("%f\t"%el)
for el1 in var_prec:
ftest_precisions.write("%f\t"%el1)
for el2 in var_fsc:
ftest_scores.write("%f\t"%el2)
ftest_recall.write("\n")
ftest_precisions.write("\n")
ftest_scores.write("\n")
#ftest.write("%f\n%f\n%s\n%s\n%s\n"%( testAcc, testFscore, var_fsc, var_prec, var_rec ))
ftest_acc.close()
ftest_fscore.close()
ftest_recall.close()
ftest_precisions.close()
ftest_fscore.close()