-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.py
188 lines (149 loc) · 6.23 KB
/
utils.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
import numpy as np
import os
import json
from keras.preprocessing import image
from sklearn.metrics import confusion_matrix
def average_over_positive_values(a):
positives = a[a != -1]
avgs = np.average(positives)
stds = np.std(positives)
return avgs, stds
def average_over_positive_values_of_2d_array(a):
positives = [a[a[:, 0] != -1, 0], a[a[:, 1] != -1, 1]]
avgs = np.average(positives, axis=1)
stds = np.std(positives, axis=1)
return avgs, stds
def average_of_gradient_metrics(a):
avgs = np.zeros(a.shape[1])
stds = np.zeros(a.shape[1])
for i in range(a.shape[1]):
positives = a[a[:, i] != -1, i]
avgs[i] = np.average(positives)
stds[i] = np.std(positives)
return avgs, stds
def average_of_gradient_metrics_of_2d_array(a):
avgs = np.zeros((a.shape[1], 2))
stds = np.zeros((a.shape[1], 2))
for i in range(a.shape[1]):
positives = [a[a[:, i, 0] != -1, i, 0], a[a[:, i, 1] != -1, i, 1]]
avgs[i] = np.average(positives, axis=1)
stds[i] = np.std(positives, axis=1)
return avgs, stds
def wigthed_average(value, count):
return np.sum(value[value != -1] * count[value != -1]) / np.sum(count[value != -1])
def wigthed_average_for_gradient_metrics(value, count):
avgs = np.zeros(7)
for i in range(value.shape[1]):
avgs[i] = np.sum(value[value[:, i] != -1, i] * count[value[:, i] != -1]) / np.sum(count[value[:, i] != -1])
return avgs
def average_over_gradient_metrics(a):
avgs = np.average(a, axis=0)
stds = np.std(a, axis=0)
return avgs, stds
def wigthed_average_over_gradient_metrics(value, count):
avgs = np.zeros(value.shape[1])
for i in range(value.shape[1]):
metric = value[:, i]
avgs[i] = np.sum(metric[metric != -1] * count[metric != -1]) / np.sum(count[metric != -1])
return avgs
def false_alarm_rate(y_true, y_pred):
TP, TN, FP, FN = classification_scores(y_true, y_pred)
if FP + TN == 0:
return -1
else:
return FP / (FP + TN)
def classification_scores(y_true, y_pred):
CM = confusion_matrix(y_true, y_pred)
if CM.shape[0] <= 1:
return (0, 0, 0, 0)
TN = CM[0][0]
FN = CM[1][0]
TP = CM[1][1]
FP = CM[0][1]
return (TP, TN, FP, FN)
def imagenet_to_keras_mapping(id):
# The class with label 0 does not exist in original ImageNet dataset
imagenetmapping = ["non-existence index"]
kerasmapping = []
f = open(os.path.join(os.path.dirname(__file__), 'map_clsloc.txt'), "r")
for i in range(1000):
imagenetmapping.append(f.readline().split(' ')[0])
with open(os.path.join(os.path.dirname(__file__), 'imagenet_class_index.json')) as json_file:
data = json.load(json_file)
for i in range(1000):
kerasmapping.append(data[str(i)][0])
class_code = imagenetmapping[id]
keraslabel = kerasmapping.index(class_code)
return keraslabel
def keras_to_imagenet_mapping(id):
# The class with label 0 does not exist in original ImageNet dataset
imagenetmapping = ["non-existence index"]
kerasmapping = []
f = open(os.path.join(os.path.dirname(__file__), 'map_clsloc.txt'), "r")
for i in range(1000):
imagenetmapping.append(f.readline().split(' ')[0])
with open(os.path.join(os.path.dirname(__file__), 'imagenet_class_index.json')) as json_file:
data = json.load(json_file)
for i in range(1000):
kerasmapping.append(data[str(i)][0])
class_code = kerasmapping[id]
keraslabel = imagenetmapping.index(class_code)
return keraslabel
def load_Data_with_keras_id(keras_class_id, target_size=(299, 299), imagenet_path='imagenet/'):
imagenet_class = keras_to_imagenet_mapping(keras_class_id)
train_path = imagenet_path + 'train/' + str(imagenet_class) + '/'
test_path = imagenet_path + 'val/' + str(imagenet_class) + '/'
img_list = []
for folder, subs, files in os.walk(train_path):
for file in files:
filename = folder + "/" + file
img = image.load_img(filename, target_size=target_size)
img = image.img_to_array(img)
img_list.append(img)
if len(img_list) == 0:
print('Error: The train folder in the imagenet path is empty (' + train_path + ')')
exit()
x_train = np.array(img_list)
img_list = []
for folder, subs, files in os.walk(test_path):
for file in files:
filename = folder + "/" + file
img = image.load_img(filename, target_size=target_size)
img = image.img_to_array(img)
img_list.append(img)
if len(img_list) == 0:
print('Error: The test folder in the imagenet path is empty (' + test_path + ')')
exit()
x_test = np.array(img_list)
y_train = np.ones(x_train.shape[0]) * keras_class_id
y_test = np.ones(x_test.shape[0]) * keras_class_id
return (x_train, y_train), (x_test, y_test), imagenet_class
def load_Data_with_imagenet_id(imagenet_class_id, target_size=(299, 299), imagenet_path='imagenet/'):
keras_class_id = imagenet_to_keras_mapping(imagenet_class_id)
train_path = imagenet_path + 'train/' + str(imagenet_class_id) + '/'
test_path = imagenet_path + 'val/' + str(imagenet_class_id) + '/'
img_list = []
for folder, subs, files in os.walk(train_path):
for file in files:
filename = folder + "/" + file
img = image.load_img(filename, target_size=target_size)
img = image.img_to_array(img)
img_list.append(img)
if len(img_list) == 0:
print('Error: The train folder in the imagenet path is empty (' + train_path + ')')
exit()
x_train = np.array(img_list)
img_list = []
for folder, subs, files in os.walk(test_path):
for file in files:
filename = folder + "/" + file
img = image.load_img(filename, target_size=target_size)
img = image.img_to_array(img)
img_list.append(img)
if len(img_list) == 0:
print('Error: The test folder in the imagenet path is empty (' + test_path + ')')
exit()
x_test = np.array(img_list)
y_train = np.ones(x_train.shape[0]) * keras_class_id
y_test = np.ones(x_test.shape[0]) * keras_class_id
return (x_train, y_train), (x_test, y_test), keras_class_id