-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_model.py
282 lines (235 loc) · 11 KB
/
eval_model.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import os
import sys, getopt
import tensorflow as tf
import numpy as np
import pandas as pd
from utils.utils import load_object
from collections import Counter
from sklearn.metrics import accuracy_score,classification_report,f1_score
import math
from tqdm import tqdm
import json
import datetime
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
import wandb
from wandb_creds import wandb_creds,wandb_entity_value
os.environ["WANDB_API_KEY"] = wandb_creds()
wandb_entity = wandb_entity_value()
"""
Trains a prototype label distributional learner (LDL) which is a simple
neural model that jointly learns to model label distributions for
ground truth labels, items, and annotators. The resultant model also
acquires embeddings for items as well as for annotators.
Example use:
$ python evaluate_ldlnm.py --data_dir="../data/" --model_fname="../data/trained_model.ldlnm"
$ python evaluate_ldlnm.py --data_dir=./experimental_data/jobQ1_BOTH/ --model_fname=./experimental_data/jobQ1_BOTH/trained_model.ldlnm --split_name=dev --empirical_fname=./datasets/jobQ123_BOTH/processed/jobQ1_BOTH/jobQ1_BOTH_dev_AIL.csv
@author DisCo Authors
"""
def convert_to_majority_array(labels):
output = []
for label_set in labels:
label_set = np.array(label_set)
zero_ary = np.zeros(len(label_set))
max_index = np.argmax(label_set)
zero_ary[max_index] = 1
output.append(zero_ary)
return output
def convert_to_majority_index(labels):
output = []
for label_set in labels:
label_set = np.array(label_set)
max_index = np.argmax(label_set)
output.append(max_index)
return output
def KLdivergence(P, Q):
# from Q to P
# https://datascience.stackexchange.com/a/26318/30372
"""
Epsilon is used here to avoid conditional code for
checking that neither P nor Q is equal to 0.
"""
epsilon = 0.00001
P = P + epsilon
Q = Q + epsilon
return np.sum(P * np.log(P/Q))
def KL_PMI_empirical2pred(empirical_pcts, prediction_proba):
KLsum = []
for pair in zip(empirical_pcts, prediction_proba):
empirical_pct = pair[0]
prediction_pct = np.asarray(pair[1])
# KL = entropy(empirical_pct, prediction_pct)
# from prediction_pct to empirical_pct
KL = KLdivergence(empirical_pct, prediction_pct)
if (math.isnan(KL)):
KL = 0.0
KLsum.append(KL)
KL = np.mean(KLsum)
print('KL divergence: ', KL)
return KL
def generate_confusion_matrix(labels_test,labels_preds,folder_path):
label_dict = [x for x in range(len(labels_test[0]))]
y_test = convert_to_majority_index(labels_test)
y_pred = convert_to_majority_index(labels_preds)
cm = confusion_matrix(y_test, y_pred, labels=label_dict)
disp = ConfusionMatrixDisplay(confusion_matrix=cm,display_labels=label_dict)
disp.plot(include_values=True,cmap=plt.cm.Blues)
# fig, ax = plt.subplots(figsize=(20,20))
# disp.plot(ax=ax,include_values=True,cmap=plt.cm.Blues)
plt.title("DisCo - CM ")
plt.savefig(folder_path)
plt.close()
def measure_accuracy(y_test,y_pred):
total_items = len(y_test)
label_choices = len(y_test[0])
matched = 0
y_test = convert_to_majority_array(y_test)
y_pred = convert_to_majority_array(y_pred)
for y_test_item,y_pred_item in zip(y_test,y_pred):
matched += accuracy_score(y_test_item, y_pred_item)
accuracy = float(matched/total_items)
return accuracy
def measure_f1(y_test,y_pred):
y_test = convert_to_majority_index(y_test)
y_pred = convert_to_majority_index(y_pred)
precision = {}
recall = {}
f1_macro = f1_score(y_test, y_pred, average='macro')
f1_micro = f1_score(y_test, y_pred, average='micro')
f1_weighted = f1_score(y_test, y_pred, average='weighted')
results = classification_report(y_test, y_pred, digits=3,output_dict=True)
precision['macro'] = results['macro avg']['precision']
precision['weighted'] = results['weighted avg']['precision']
recall['macro'] = results['macro avg']['recall']
recall['weighted'] = results['weighted avg']['recall']
return f1_macro,f1_micro,f1_weighted,precision,recall
def write_model_logs_to_json(MODEL_LOG_DIR, results_dict, output_name):
with open(MODEL_LOG_DIR +"/"+ output_name + ".json", "a") as fp:
json.dump(results_dict, fp, sort_keys=True, indent=4,default=str)
def write_results_to_wandb(wandb_name,results,model_type,dataset):
import pdb
pdb.set_trace()
wandb.init(project=wandb_name, entity=wandb_entity,name=dataset)
wandb.config = {
"model": model_type,
"dataset": dataset
}
results_to_write = results
results_to_write["dataset"] = dataset
wandb.log(results_to_write)
def main():
options, remainder = getopt.getopt(sys.argv[1:], '', ["data_dir=","model_fname=","gpu_id=","split_name=","dataset_name=","wandb_name=","empirical_fname="])
# Collect arguments from argv
batch_size = 100
data_dir = None
model_fname = None
dataset_name = None
wandb_name = None
use_gpu = False
gpu_id = -1
for opt, arg in options:
if opt in ("--data_dir"):
data_dir = arg.strip()
elif opt in ("--model_fname"):
model_fname = arg.strip()
elif opt in ("--gpu_id"):
gpu_id = int(arg.strip())
use_gpu = True
elif opt in ("--split_name"):
split_name = arg.strip()
elif opt in ("--dataset_name"):
dataset_name = arg.strip()
elif opt in ("--wandb_name"):
wandb_name = arg.strip()
elif opt in ("--empirical_fname"):
empirical_fname = arg.strip()
mid = gpu_id
if use_gpu:
print(" > Using GPU ID {0}".format(mid))
os.environ["CUDA_VISIBLE_DEVICES"]="{0}".format(mid)
gpu_tag = '/GPU:0'
#tf.config.experimental.set_memory_growth(gpu, True)
else:
os.environ["CUDA_VISIBLE_DEVICES"]="-1"
gpu_tag = '/CPU:0'
################################################################################
# load in design matrices
################################################################################
Xi = np.load("{0}Xi_{1}.npy".format(data_dir,split_name), allow_pickle=True)
Yi = np.load("{0}Yi_{1}.npy".format(data_dir,split_name), allow_pickle=True)
Ya = np.load("{0}Ya_{1}.npy".format(data_dir,split_name), allow_pickle=True)
Y = np.load("{0}Y_{1}.npy".format(data_dir,split_name), allow_pickle=True)
I = np.load("{0}I_{1}.npy".format(data_dir,split_name), allow_pickle=True)
A = np.load("{0}A_{1}.npy".format(data_dir,split_name), allow_pickle=True)
raw_item_annotator_label = pd.read_csv(empirical_fname,names=['item','annotator','label','message']) #np.load("{0}A_{1}.npy".format(data_dir,split_name))
unique_dataitems = pd.unique(raw_item_annotator_label['item'])
n_i = np.max(I) + 1 # 2000 # number items
n_a = np.max(A) + 1 #50 # number annotators
yi_dim = Yi.shape[1]
ya_dim = Ya.shape[1]
y_dim = Y.shape[1]
print("Y.shape = ",Y.shape)
print("Yi.shape = ",Yi.shape)
print("Ya.shape = ",Ya.shape)
print("I.shape = ",I.shape)
print("A.shape = ",A.shape)
empirical_labels = []
predicitions = []
data_to_write = []
for unique_dataitem in tqdm(unique_dataitems):
item_index = raw_item_annotator_label.loc[raw_item_annotator_label['item'] == unique_dataitem].index[0] #there is a chance of multiple rows returned.
items = raw_item_annotator_label.loc[raw_item_annotator_label['item'] == unique_dataitem].head(1)
message = items['message'].values[0]
empirical_label = Yi[item_index]
empirical_labels.append(empirical_label)
################################################################################
# set up label distributional learning model
################################################################################
with tf.device(gpu_tag):
eps = 1e-7
model = load_object(model_fname)
model.drop_p = 0.0
# check model's accuracy on the dataset it was fit on
# y_ind = tf.cast(tf.argmax(tf.cast(Y,dtype=tf.float32),1),dtype=tf.int32)
# acc, L, _, _ = calc_stats(model, Xi, Yi, Ya, Y, A, I, batch_size)
# print(" Acc = {0} L = {1}".format(acc,L))
############################################################################
# choose particular sample form the dataset to see how to use model at test time
############################################################################
s_ptr = item_index #int( tf.argmax(comp) )
x_s = tf.cast(np.expand_dims(Xi[s_ptr,:],axis=0),dtype=tf.float32)
y_s = tf.cast(np.expand_dims(Y[s_ptr,:],axis=0),dtype=tf.float32)
y_lab = int(tf.argmax(y_s,axis=1))
yi_s = tf.cast(np.expand_dims(Yi[s_ptr,:],axis=0),dtype=tf.float32)
ya_s = tf.cast(np.expand_dims(Ya[s_ptr,:],axis=0),dtype=tf.float32)
i_s = I[s_ptr,:]
a_s = A[s_ptr,:]
py, _ = model.decode_y_ensemble(x_s)
yhat_set = tf.argmax(py,axis=1).numpy().tolist()
predicted_label = Counter(yhat_set)
total_labels = sum(predicted_label.values())
predicted_label_dist = [predicted_label[x]/total_labels for x in range(len(empirical_label))]
if len(predicted_label_dist) == len(empirical_label):
predicitions.append(predicted_label_dist)
row_to_write = {}
row_to_write['message'] = message
row_to_write['message_id'] = item_index
row_to_write['labels'] = predicted_label_dist
data_to_write.append(row_to_write)
else:
print("Label class mismatch")
sys.exit()
results = {}
print("Size of empirical label set {0}*{1} | Shape of predicted label set {2}*{3}".format(len(empirical_labels),len(empirical_labels[0]),len(predicitions),len(predicitions[0])))
results['KL'] = KL_PMI_empirical2pred(empirical_labels,predicitions)
results['accuracy'] = measure_accuracy(empirical_labels,predicitions)
generate_confusion_matrix(empirical_labels,predicitions,data_dir+"/cmatrix.pdf")
results['f1_macro'],results['f1_micro'],results['f1_weighted'],results['precision'],results['recall'] = measure_f1(empirical_labels,predicitions)
results['timestamp'] = datetime.datetime.now()
print("KL {0} | Accuracy {1} | F1 macro {2} | F1 micro {3} | F1 weighted {4} | Precision {5} | Recall {6}".format(results['KL'],results['accuracy'],results["f1_macro"], results['f1_micro'],results['f1_weighted'],results['precision'],results['recall']))
write_results_to_wandb(wandb_name,results,"DisCo",dataset_name)
pd.DataFrame(data_to_write).to_excel(data_dir+"/preds.xlsx")
if __name__== "__main__":
main()