-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultimodal_G-MODELs.py
376 lines (296 loc) · 14.7 KB
/
multimodal_G-MODELs.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import pandas as pd
import numpy as np
from IPython.display import HTML
from tqdm import tqdm
from Utils import *
import torch
from scipy.spatial.distance import cosine
from collections import OrderedDict
import pickle
from ast import literal_eval
import warnings
warnings.filterwarnings("ignore")
print("loading model...")
from transformers import AutoTokenizer, BertModel
model = BertModel.from_pretrained('bert-base-multilingual-cased', output_hidden_states = True)
tokenizer = AutoTokenizer.from_pretrained("bert-base-multilingual-cased")
#___________________________________LOAD DATASET___________________________________
# FROM ORIGINAL DATASET
"""
train_df = pd.read_excel('./Data/training.xls')
label_df_path = "./Data/all_misogyny.xlsx"
train_df = train_df.merge(pd.read_excel(label_df_path)[['meme', 'NOTmisogynous']], left_on='file_name', right_on='meme').drop_duplicates().reset_index()
train_df = get_dataset_labels(train_df)
#___________________________________COMPUTE SCORES___________________________________
train_df['sentences'] = train_df['original_text'].apply(lambda x : split_sentence(x))
train_df['sentences'] = train_df['sentences'].apply(lambda x :adjust_split(x))
train_df['sentences'] = train_df['sentences'].apply(lambda x : apply_lemmatization(x))
train_df['lemmi_text']= train_df['sentences'].apply(lambda x: ' '.join(x))
# ESCLUDO GLI ULTIMI 1000 DA USARE COME TEST
test_df = train_df[9000:]
train_df = train_df[:9000]
train_df['tokens'] = ''
context_embeddings = []
context_tokens = []
for index, row in tqdm(train_df.iterrows()):
tokenized_text, list_token_embeddings = text_to_emb(row.lemmi_text, tokenizer, model)
#print(tokenized_text)
train_df.loc[index,'tokens'] = str(tokenized_text)
# make ordered dictionary to keep track of the position of each word
tokens = OrderedDict()
# loop over tokens in sensitive sentence
for token in tokenized_text[1:-1]:
# keep track of position of word and whether it occurs multiple times
if token in tokens:
tokens[token] += 1
else:
tokens[token] = 1
# compute the position of the current token
token_indices = [i for i, t in enumerate(tokenized_text) if t == token]
current_index = token_indices[tokens[token]-1]
# get the corresponding embedding
token_vec = list_token_embeddings[current_index]
# save values
context_tokens.append(token)
context_embeddings.append(token_vec)
# Save embeddings and tokens to a file
with open('embeddings_and_tokens.pkl', 'wb') as f:
pickle.dump((context_embeddings, context_tokens), f)
print("Data has been saved successfully.")
train_df.to_csv("processed_MAMI_TrainOnly.csv", sep='\t', index=False)
test_df.to_csv("processed_MAMI_TestOnly.csv", sep='\t', index=False)
"""
#LOAD PREPROCESSED
print("loading preprocessed data...")
# Load embeddings and tokens from a file
with open('embeddings_and_tokens.pkl', 'rb') as f:
context_embeddings, context_tokens = pickle.load(f)
print("Data has been loaded successfully.")
tags_df = pd.read_excel('./Data/training.xls')
tags_df_path = "./Data/clarifai_train.csv"
tags_df = tags_df.merge(pd.read_csv(tags_df_path), left_on='file_name', right_on='id').drop_duplicates().reset_index()
tags_df = tags_df[['file_name','Text Transcription', 'clarifai']]
tags_df['clarifai'] = tags_df['clarifai'].apply(lambda x: literal_eval(x))
tags_df['clarifai'] = tags_df['clarifai'].apply(lambda x: ['tag_'+str(element) for element in x])
train_df = pd.read_csv("processed_MAMI_TrainOnly.csv", sep='\t')
test_df = pd.read_csv("processed_MAMI_TestOnly.csv", sep='\t')
#___________________________________TOKEN SELECTION___________________________________
# Convert entire column to a list (saved as str)
train_df['tokens'] = train_df['tokens'].apply(lambda x: literal_eval(x))
valid_tokens = elements_appearing_more_than_10_times(flatten_list(train_df.tokens.values))
valid_tokens = valid_tokens + ['tag_'+str(element).lower() for element in ['Animal', 'Broom', 'Car', 'Cartoon', 'Cat', 'Dog', 'Child', 'Crockery', 'Dishwasher', 'Kitchen', 'KitchenUtensil', 'Man', 'Woman', 'Nudity']]
# Create a new column for each unique token, with values of 1 or 0 depending on whether the token is in the "tokens" list for that row
train_df['tokens'] = train_df['tokens'].apply(lambda x: [str(element).lower() for element in x])
train_df['tokens']= train_df['tokens'].apply(lambda x: clean_tokens(x))
train_df['clarifai'] = tags_df['clarifai'][:len(train_df)]
#train_df['tokens']= train_df.apply(lambda x: x.tokens+x.clarifai, axis=1)
train_df = tokens_to_columns(valid_tokens, train_df).copy()
#___________________________________COMPUTE SCORES___________________________________
"""
plot_scores = pd.DataFrame(columns=['token', 'Agreement', 'Hate'])
agreement_df = pd.concat([train_df.loc[train_df['soft_label_1']==1], train_df.loc[train_df['soft_label_0']==1]])
for x in valid_tokens:
#compute p(Agreement|t)
#if there is only one value it's 0
if len(agreement_df[x].value_counts()) == 1 and 0 in list(agreement_df[x].values):
p1=0
else:
p1 = agreement_df[x].value_counts()[1]/train_df[x].value_counts()[1]
#compute p(Hate|t)
#if there is only one value it's 0
if len(train_df.loc[train_df['misogynous']==1][x].value_counts()) == 1 and 0 in list(train_df.loc[train_df['misogynous']==1][x].values):
p2=0
else:
p2 = train_df.loc[train_df['misogynous']==1][x].value_counts()[1]/train_df[x].value_counts()[1]
#plot_scores=plot_scores.append({'token':x, 'Agreement':p1, 'Hate':p2 },ignore_index=True)
plot_scores = pd.concat([plot_scores, pd.DataFrame([{'token': x, 'Agreement': p1, 'Hate': p2}])], ignore_index=True)
plot_scores['Agreement_coordinate'] = plot_scores['Agreement'].apply(lambda x: x-(1-x))
plot_scores['Hate_coordinate'] = plot_scores['Hate'].apply(lambda x: x-(1-x))
plot_scores['occurrences'] = plot_scores['token'].apply(lambda x: train_df[x].value_counts()[1])
plot_scores.to_csv('final_scores_trainOnly.csv', sep='\t', index=False)
"""
#___________________________________LOAD SCORES___________________________________
print("loading scores...")
tags_scores = pd.read_csv('final_scores_tags_trainOnly.csv', sep='\t')
tags_scores['token']= 'tag_'+tags_scores['token']
#plot_scores= pd.concat([pd.read_csv('final_scores_trainOnly.csv', sep='\t'), tags_scores], ignore_index=True)
plot_scores= pd.read_csv('final_scores_trainOnly.csv', sep='\t')
tokens_df_10 = plot_scores[plot_scores.occurrences >= 10]
#tokens_df = plot_scores
# concateno dopo in modo da avere tutti i tag (non solo quelli con almeno 10 occorrenze)
tokens_df_10= pd.concat([tokens_df_10, tags_scores], ignore_index=True)
dev_df = train_df[8000:]
#___________________________________BASELINE NO-ESTIMATION___________________________________
#___________________________________G-MODELs TOKEN ESTIMATION___________________________________
# media pesata classica. Prima era la media dei termini moltiplicati per la distanza
print("computing context tokens...")
context_embeddings_mean = []
context_tokens_mean = []
for tk in set([str(element).lower() for element in context_tokens]):
indexes = find_token_indices(context_tokens, tk)
context_tokens_mean.append(tk)
context_embeddings_mean.append(torch.mean(torch.stack(retrieve_elements(context_embeddings, indexes)) , dim=0))
print(len(context_tokens_mean))
print("computing thresholds on dev for G-Models...")
#thresholds per vicinato
best_n_somma = 0
best_n_media = 0
best_n_mediana = 0
best_n_min = 0
#thresholds per predizione
best_t_somma = 0
best_t_media = 0
best_t_mediana = 0
best_t_min = 0
#performances
best_f1_somma = 0
best_f1_media = 0
best_f1_mediana = 0
best_f1_min = 0
for threshold in [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]:
pred_somma = []
pred_tutti_verdi = []
pred_media = []
pred_mediana = []
for _, row in tqdm(dev_df.iterrows()):
colors_agreement, _ = get_all_colors(row['tokens']+row['clarifai'], tokens_df_10)
if 'NA' in colors_agreement:
similar_words, distances_df, new_words = find_similar_words(' '.join(row['tokens']), find_NA_indices(colors_agreement), tokenizer, context_tokens_mean,context_embeddings_mean, model, tokens_df_10)
for i in range(0, len(new_words)):
if list(distances_df.loc[(distances_df['new_token']==new_words[i])& (distances_df['distance']>=threshold)]['distance']):
stimated_coordinate = np.average(
list(distances_df.loc[(distances_df['new_token']==new_words[i]) & (distances_df['distance']>=threshold)]['Agreement_coordinate']),
weights=list(distances_df.loc[(distances_df['new_token']==new_words[i])& (distances_df['distance']>=threshold)]['distance']))
colors_agreement[find_NA_indices(colors_agreement)[0]]=stimated_coordinate
else: #if there isn't any word above the threshold
colors_agreement[find_NA_indices(colors_agreement)[0]]=0
#tolgo gli zero:
colors_agreement = [i for i in colors_agreement if i != 0]
if colors_agreement:
pred_somma.append(sum(colors_agreement))
pred_media.append(np.mean(colors_agreement))
pred_mediana.append(np.median(colors_agreement))
pred_tutti_verdi.append(min(colors_agreement))
else:
pred_somma.append(0)
pred_media.append(0)
pred_mediana.append(0)
pred_tutti_verdi.append(0)
if threshold_estimation(pred_somma, dev_df, 'disagreement')[1] > best_f1_somma:
best_t_somma, best_f1_somma = threshold_estimation(pred_somma, dev_df, 'disagreement')
best_n_somma = threshold
if threshold_estimation(pred_media, dev_df, 'disagreement')[1] > best_f1_media:
best_t_media, best_f1_media = threshold_estimation(pred_media, dev_df, 'disagreement')
best_n_media = threshold
if threshold_estimation(pred_mediana, dev_df, 'disagreement')[1] > best_f1_mediana:
best_t_mediana, best_f1_mediana = threshold_estimation(pred_mediana, dev_df, 'disagreement')
best_n_mediana = threshold
if threshold_estimation(pred_tutti_verdi, dev_df, 'disagreement')[1] > best_f1_min:
best_t_min, best_f1_min = threshold_estimation(pred_tutti_verdi, dev_df, 'disagreement')
best_n_min = threshold
print(" ___th: "+str(threshold)+"___")
print('best_t_somma ' + str(best_t_somma))
print('best_f1_somma ' + str(best_f1_somma))
print('best_n_somma ' + str(best_n_somma))
print('best_t_media ' + str(best_t_media))
print('best_f1_media ' + str(best_f1_media))
print('best_n_media ' + str(best_n_media))
print('best_t_mediana ' + str(best_t_mediana))
print('best_f1_mediana ' + str(best_f1_mediana))
print('best_n_mediana ' + str(best_n_mediana))
print('best_t_min ' + str(best_t_min))
print('best_f1_min ' + str(best_f1_min))
print('best_n_min ' + str(best_n_min))
"""
best_t_somma = 3.1
best_f1_somma = 0.5447475245646648
best_n_somma = 0.8
best_t_media = 0.2
best_f1_media = 0
best_n_media = 0.8
best_t_mediana = 0.2
best_f1_mediana = 0.5005273723248613
best_n_mediana = 0.8
best_t_min = -0.1
best_f1_min = 0.6057218415674961
best_n_min = 0.8
"""
"""
#senza zeri, th valutato tra 0.5 e 0.7
best_t_somma 3.4
best_f1_somma 0.541311553030303
best_n_somma 0.7
best_t_media 0.2
best_f1_media 0.5173944005979602
best_n_media 0.5
best_t_mediana 0.2
best_f1_mediana 0.42401905267313406
best_n_mediana 0.7
best_t_min -0.1
best_f1_min 0.6025296237728867
best_n_min 0.5
"""
"""
best_t_somma = 3.6
best_f1_somma= 0.5497666396103896
best_n_somma =0.8
best_t_media =0
best_f1_media =0
best_n_media =0
best_t_mediana= 0.2
best_f1_mediana= 0.47432315475810155
best_n_mediana =0.8
best_t_min= -0.1
best_f1_min= 0.6057218415674961
best_n_min =0.8
"""
#_______________________Performances on Test____________________
test_df['tokens'] = ''
for index, row in tqdm(test_df.iterrows()):
test_df.loc[index,'tokens'] = str(clean_tokens(text_to_emb(row.lemmi_text, tokenizer, model)[0]))
test_df['tokens'] = test_df['tokens'].apply(lambda x: literal_eval(x))
test_df['tokens'] = test_df['tokens'].apply(lambda x: [str(element).lower() for element in x])
train_df['tokens']= train_df['tokens'].apply(lambda x: clean_tokens(x))
test_df['clarifai'] = list(tags_df['clarifai'][len(train_df):] )
#test_df['tokens']= test_df.apply(lambda x: x.tokens+x.clarifai, axis=1)
for threshold in set([best_n_somma, best_n_media, best_n_mediana, best_n_min]):
pred_somma = []
pred_tutti_verdi = []
pred_media = []
pred_mediana = []
for _, row in tqdm(test_df.iterrows()):
colors_agreement, _ = get_all_colors(row['tokens']+row['clarifai'], tokens_df_10)
if 'NA' in colors_agreement:
similar_words, distances_df, new_words = find_similar_words(' '.join(row['tokens']), find_NA_indices(colors_agreement), tokenizer, context_tokens_mean,context_embeddings_mean, model, tokens_df_10)
for i in range(0, len(new_words)):
if list(distances_df.loc[(distances_df['new_token']==new_words[i])& (distances_df['distance']>=threshold)]['distance']):
stimated_coordinate = np.average(
list(distances_df.loc[(distances_df['new_token']==new_words[i]) & (distances_df['distance']>=threshold)]['Agreement_coordinate']),
weights=list(distances_df.loc[(distances_df['new_token']==new_words[i])& (distances_df['distance']>=threshold)]['distance']))
colors_agreement[find_NA_indices(colors_agreement)[0]]=stimated_coordinate
else: #if there isn't any word above the threshold
colors_agreement[find_NA_indices(colors_agreement)[0]]=0
#tolgo gli zero:
colors_agreement = [i for i in colors_agreement if i != 0]
if colors_agreement:
pred_somma.append(sum(colors_agreement))
pred_media.append(np.mean(colors_agreement))
pred_mediana.append(np.median(colors_agreement))
pred_tutti_verdi.append(min(colors_agreement))
else:
pred_somma.append(0)
pred_media.append(0)
pred_mediana.append(0)
pred_tutti_verdi.append(0)
if threshold == best_n_somma:
print('SOMMA')
print(classification_report(test_df['disagreement'], [int(i>=best_t_somma) for i in pred_somma] ))
if threshold == best_n_media:
print('MEDIA')
print(classification_report(test_df['disagreement'], [int(i>=best_t_media) for i in pred_media] ))
if threshold == best_n_mediana:
print('MEDIANA')
print(classification_report(test_df['disagreement'], [int(i>=best_t_mediana) for i in pred_mediana] ))
if threshold == best_n_min:
print('MIN')
print(classification_report(test_df['disagreement'], [int(i>=best_t_min) for i in pred_tutti_verdi] ))