-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier.py
314 lines (267 loc) · 11 KB
/
classifier.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import os
import sys
import warnings
import pickle
import tensorflow as tf
import numpy as np
import pandas as pd
import configparser as cp
from ekphrasis.classes.preprocessor import TextPreProcessor
from ekphrasis.classes.tokenizer import SocialTokenizer
from ekphrasis.dicts.emoticons import emoticons
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.models import load_model
from sklearn.metrics import confusion_matrix, precision_score, recall_score, accuracy_score, f1_score, roc_auc_score, \
classification_report, average_precision_score
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import label_binarize
from utilities.preprocessor import PreProcessor
from utilities.embed_extractor import EmbeddingsExtractor
from utilities.data_loader import get_embeddings, load_data, onehot_to_categories
from models.models import polus_te, polus_to
class Classifier:
def __init__(self, model_name, config_file, epochs=30, batch_size=16):
self.epochs = epochs
self.batch_size = batch_size
config = cp.ConfigParser()
config.read(config_file)
self.embedding_dim = config.getint(model_name, 'embeddings', fallback=300)
self.max_length = config.getint(model_name, 'max_length', fallback=50)
self.use_lsh = config.getboolean(model_name, 'use_lsh', fallback=False)
self.sim_threshold = config.getfloat(model_name, 'sim_threshold', fallback=1)
self.max_query_id = config.getint(model_name, 'max_query_id', fallback=3000)
self.emoji_count = config.getint(model_name, 'emoji_count', fallback=1018)
self.hashtag_count = config.getint(model_name, 'hashtag_count', fallback=0)
self.corpus = config.get(model_name, 'corpus', fallback='fasttext')
self.model_name = model_name
self.embeddings, self.word_indices = get_embeddings(corpus=self.corpus, dim=self.embedding_dim)
if self.model_name == 'polus_to':
self.model = polus_to(
self.embeddings, self.max_length, config)
elif self.model_name == 'polus_te':
self.model = polus_te(
self.embeddings, self.max_length, config)
else:
raise NotImplementedError(
'Unrecognized model ' + self.model_name + '. It should be one of [\'polus_to\', \'polus_te\']')
self.pipeline = Pipeline([('preprocess', PreProcessor(TextPreProcessor(
normalize=['url', 'email', 'percent', 'money', 'phone', 'user', 'time', 'url', 'date', 'number'],
include_tags={'hashtag', 'allcaps', 'elongated', 'repeated', 'emphasis', 'censored'},
fix_html=True,
segmenter="twitter",
corrector="twitter",
unpack_hashtags=True, # original hashtags are not included in embeddings dictionary, so unpack
unpack_contractions=True,
spell_correct_elong=False,
tokenizer=SocialTokenizer(lowercase=True).tokenize,
dicts=[emoticons]))), ('ext', EmbeddingsExtractor(word_indices=self.word_indices,
max_lengths=self.max_length,
add_tokens=True,
unk_policy="random"))])
def train(self, datafile, dedup, out_file):
"""
Train model
:param datafile:
:param dedup:
:param out_file:
:return:
"""
print(self.model.summary())
# text, sentiment, encoder_mapping
data = load_data(datafile, self.pipeline, split=True, dedup=dedup, test_size=0.2)
trainTextX, trainEmojisX, trainY, valTextX, valEmojisX, testY, class_weights, encoder_mapping = data
print("Train on size:{}".format(len(trainY)))
if self.model_name == 'polus_to':
trainX = [trainTextX]
valX = [valTextX]
elif self.model_name == 'polus_te':
trainX = [trainTextX, trainEmojisX]
valX = [valTextX, valEmojisX]
else:
trainX = None
valX = None
print("No model name: ", self.model_name)
callbacks = []
checkpoint = ModelCheckpoint(out_file+'_min_val_loss.h5', save_best_only=True, monitor='val_loss', mode='min')
callbacks.append(checkpoint)
checkpoint = ModelCheckpoint(out_file+'_max_val_acc.h5', save_best_only=True, monitor='val_accuracy', mode='max')
callbacks.append(checkpoint)
history = self.model.fit(
x=trainX, y=trainY,
validation_data=(valX, testY),
class_weight=class_weights,
epochs=self.epochs,
batch_size=self.batch_size,
callbacks=callbacks
)
hist_df = pd.DataFrame(history.history, columns=[['epochs', 'loss', 'accuracy', 'val_loss', 'val_accuracy']])
with open(f"model_history/{out_file.rsplit('/',1)[1].rsplit('.', 1)[0]}.csv", mode='w') as f:
hist_df.to_csv(f)
def retrain(self, datafile, dedup, in_file, out_file):
"""
Train model
:param datafile:
:param dedup:
:param in_file:
:param out_file:
:return:
"""
print(self.model.summary())
# text, sentiment, encoder_mapping
data = load_data(datafile, self.pipeline, split=True, dedup=dedup, test_size=0.2)
trainTextX, trainEmojisX, trainY, valTextX, valEmojisX, testY, class_weights, encoder_mapping = data
print("Re-Train on size:{}".format(len(trainY)))
if self.model_name == 'polus_to':
trainX = [trainTextX]
valX = [valTextX]
elif self.model_name == 'polus_te':
trainX = [trainTextX, trainEmojisX]
valX = [valTextX, valEmojisX]
else:
trainX = None
valX = None
print("No model name: ", self.model_name)
callbacks = []
checkpoint = ModelCheckpoint(out_file+'_min_val_loss.h5', save_best_only=True, monitor='val_loss', mode='min')
callbacks.append(checkpoint)
checkpoint = ModelCheckpoint(out_file+'_max_val_acc.h5', save_best_only=True, monitor='val_accuracy', mode='max')
callbacks.append(checkpoint)
print(f"Loading model ...")
loaded_model = load_model(in_file)
history = loaded_model.fit(
x=trainX, y=trainY,
validation_data=(valX, testY),
class_weight=class_weights,
epochs=self.epochs,
batch_size=self.batch_size,
callbacks=callbacks
)
hist_df = pd.DataFrame(history.history)
with open(f"model_history/{in_file.rsplit('/',1)[1].rsplit('.', 1)[0]}.csv", mode='w') as f:
hist_df.to_csv(f)
def test(self, datafile, infile):
"""
Test Model
:param datafile:
:param infile:
:return:
"""
# text, sentiment, encoder_mapping
data = load_data(datafile, self.pipeline, dedup=True)
TextX, EmojisX, y, encoder_mapping = data
if self.model_name == 'polus_to':
X = [TextX]
elif self.model_name == 'polus_te':
X = [TextX, EmojisX]
else:
X = None
print("No model name: ", self.model_name)
encoded_labels = onehot_to_categories(y)
encoded_classes = set(list(encoded_labels))
print(f"Encoded Labels:{encoded_classes}")
print(f"Labels:{set([encoder_mapping[l] for l in encoded_labels])}")
labels = encoded_labels
test_loss = []
test_accs = []
print(f"Loading model ...")
model_test = load_model(infile)
print(f"Testing ...")
score = model_test.evaluate(X, y, verbose=1, )
test_loss.append(score[0])
test_accs.append(score[1])
# Predicting the Test set results
y_pred = model_test.predict(X)
with open("pred_prob_distr.pickle", 'wb') as save:
pickle.dump(y_pred, save)
predictions = tf.argmax(y_pred, axis=1)
# Creating the Confusion Matrix (non-norm)
cm = confusion_matrix(labels, predictions)
# # Classification Report
cr = classification_report(labels, predictions, target_names=encoder_mapping.values())
# Scores
macro_precision = precision_score(labels, predictions, average='macro')
micro_precision = precision_score(labels, predictions, average='micro')
weighted_precision = precision_score(labels, predictions, average='weighted')
macro_recall = recall_score(labels, predictions, average='macro')
micro_recall = recall_score(labels, predictions, average='micro')
weighted_recall = recall_score(labels, predictions, average='weighted')
acc = accuracy_score(labels, predictions, )
f1_macro = f1_score(labels, predictions, average='macro')
f1_micro = f1_score(labels, predictions, average='micro')
f1_weighted = f1_score(labels, predictions, average='weighted')
one_vs_all_labels = label_binarize(labels, classes=[0, 1, 2])
one_vs_all_predictions = label_binarize(predictions, classes=[0, 1, 2])
roc_auc = roc_auc_score(one_vs_all_labels, one_vs_all_predictions)
auprc = average_precision_score(one_vs_all_labels, one_vs_all_predictions)
print('')
print('Testing samples: %i' % len(X[0]))
print('')
print('Metrics')
print('-' * 40)
print('F1 Macro: {0:0.2f}%'.format(100 * f1_macro))
print('ROC AUC: {0:0.2f}%'.format(100 * roc_auc))
print('AUPRC: {0:0.2f}%'.format(100 * auprc))
print('Accuracy: {0:0.2f}%'.format(100 * acc))
print('-' * 40)
print('F1 Micro: {0:0.2f}%'.format(100 * f1_micro))
print('F1 Weighted: : {0:0.2f}%'.format(100 * f1_weighted))
print('Macro Precision: {0:0.2f}%'.format(100 * macro_precision))
print('Micro Precision: {0:0.2f}%'.format(100 * micro_precision))
print('Weighted Precision: {0:0.2f}%'.format(100 * weighted_precision))
print('Macro Recall: {0:0.2f}%'.format(100 * macro_recall))
print('Micro Recall: {0:0.2f}%'.format(100 * micro_recall))
print('Weighted Recall: {0:0.2f}%'.format(100 * weighted_recall))
print('\n')
print('Confusion Matrix')
print('-' * 20)
print(cm)
print('\n')
print('Sums Matrix')
print('-' * 20)
print(cm.sum(axis=1))
print('\n')
print('Normalized Confusion Matrix')
print('-' * 40)
print(cm.astype('float') / cm.sum(axis=1)[:, np.newaxis])
print('\n')
print('Classification Report')
print('-' * 60)
print(cr)
return labels, cm, cr, acc, f1_macro, f1_micro, f1_weighted, roc_auc, auprc, macro_precision, micro_precision, weighted_precision, macro_recall, micro_recall, weighted_recall
def predict(self, filename, infile):
"""
Predict on new data from dataset
:param filename:
:param infile:
:return:
"""
stderr = sys.stderr
sys.stderr = open(os.devnull, 'w')
sys.stderr = stderr
warnings.filterwarnings("ignore")
data = load_data(filename, self.pipeline, predict=True)
if not data:
print("Nothing to predict !")
return [[]]
TextX, EmojisX, sid, encoder_mapping = data
if self.model_name == 'polus_to':
X = [TextX]
elif self.model_name == 'polus_te':
X = [TextX, EmojisX]
else:
X = None
print("No model name: ", self.model_name)
model_test = load_model(infile)
# Predicting results
y_pred = model_test.predict(X)
predictions = tf.argmax(y_pred, axis=1)
res = []
for i in range(len(sid)):
res.append([
str(sid[i]),
encoder_mapping[predictions[i].numpy()],
])
df = pd.DataFrame(data=res, columns=['id', 'predicted_sentiment'])
df.to_csv(filename+'.res.csv', encoding='utf-8', index=False)
print(f"Predictions saved at {filename+'.res.csv'}")
return res