-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultimodal_MODELs.py
287 lines (208 loc) · 10.7 KB
/
multimodal_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
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 DATA
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.csv', sep='\t', index=False)
#___________________________________LOAD SCORES___________________________________
print("loading scores...")
tags_scores = pd.read_csv('final_scores_tags.csv', sep='\t')
tags_scores['token']= 'tag_'+tags_scores['token']
#plot_scores= pd.concat([pd.read_csv('final_scores.csv', sep='\t'), tags_scores], ignore_index=True)
plot_scores= pd.read_csv('final_scores.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___________________________________
print("computing thresholds on dev for Models...")
#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
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:
indexes = find_NA_indices(colors_agreement)
for new_word_index in range(len(indexes)) :
colors_agreement[indexes[new_word_index]]=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')
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')
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')
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')
print('best_t_somma ' + str(best_t_somma))
print('best_f1_somma ' + str(best_f1_somma))
print('best_t_media ' + str(best_t_media))
print('best_f1_media ' + str(best_f1_media))
print('best_t_mediana ' + str(best_t_mediana))
print('best_f1_mediana ' + str(best_f1_mediana))
print('best_t_min ' + str(best_t_min))
print('best_f1_min ' + str(best_f1_min))
#_______________________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)
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:
indexes = find_NA_indices(colors_agreement)
for new_word_index in range(len(indexes)) :
#stimated_coordinate = new_tokens_df_dev.loc[new_tokens_df_dev['new_token']== list(row['tokens_list'])[indexes[new_word_index]],'coordinate'].values[0]
colors_agreement[indexes[new_word_index]]=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)
print('SOMMA')
print(classification_report(test_df['disagreement'], [int(i>=best_t_somma) for i in pred_somma] ))
print('MEDIA')
print(classification_report(test_df['disagreement'], [int(i>=best_t_media) for i in pred_media] ))
print('MEDIANA')
print(classification_report(test_df['disagreement'], [int(i>=best_t_mediana) for i in pred_mediana] ))
print('MIN')
print(classification_report(test_df['disagreement'], [int(i>=best_t_min) for i in pred_tutti_verdi] ))