-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep_2a_filter.py
277 lines (239 loc) · 9.54 KB
/
step_2a_filter.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
import constants
import fire
import model_wrappers
import numpy as np
import os
import pandas as pd
import torch
import utils
import yake
import nltk
nltk.download("punkt")
nltk.download("stopwords")
def extract_keywords(texts: list[str], kw_lens: list[int], top=256):
"""
texts - list[str]: collection of texts to extract the keywords from
kw_lens - list[int]: sequence of upper bounds on the length of the keywords
top - int
"""
# print(texts[141037]) has a nan
all_texts = " .\n".join([str(t) for t in texts])
stopwords_ = set(nltk.corpus.stopwords.words("english"))
all_keywords = []
for l in kw_lens:
kw_extractor = yake.KeywordExtractor(
lan="en", n=l, dedupLim=0.9, top=top, features=None
)
keywords = kw_extractor.extract_keywords(all_texts)
keywords = [keyword[0] for keyword in keywords]
all_keywords += keywords
all_keywords = [k.replace(".", "").strip() for k in all_keywords]
all_keywords = list(set(all_keywords) - stopwords_)
return all_keywords
def get_captions_keywords_per_cls(dataset, top=256, only_spurious=False, remake=False):
ending = "_only_spurious" if only_spurious else ""
kw_cache_dir = os.path.join(
constants.CACHE_PATH,
"keywords",
dataset,
)
os.makedirs(kw_cache_dir, exist_ok=True)
kw_cache_file = os.path.join(kw_cache_dir, f"keywords{ending}.npy")
if not remake and os.path.isfile(kw_cache_file):
kws_dict = np.load(kw_cache_file, allow_pickle=True).item()
class_keywords = [
kws_dict[i] for i in range(len(constants.DATASET_CLASSES[dataset]))
]
return class_keywords
captions_path = os.path.join(constants.CACHE_PATH, "captions", dataset, "train.txt")
if not os.path.exists(captions_path):
raise FileNotFoundError(captions_path)
f = open(captions_path, "r")
captions = f.read().split("\n")
f.close()
metadata_path = os.path.join(
constants.DATA_PATH,
constants.DATASET_DIR[dataset],
constants.METADATA_NAME[dataset],
)
mtd = pd.read_csv(metadata_path)
train_labels = mtd.y[mtd.split == constants.DATASET_SPLITS["train"]].to_list()
train_envs = mtd.a[mtd.split == constants.DATASET_SPLITS["train"]].to_list()
del mtd
class_keywords = []
if only_spurious:
for cls in range(len(constants.DATASET_CLASSES[dataset])):
class_captions = [
c
for c, l, env in zip(captions, train_labels, train_envs)
if l == cls and env == constants.DATASET_CLASS_BIAS[dataset][cls]
]
class_keywords.append(
extract_keywords(class_captions, kw_lens=[3, 5], top=top)
)
else:
for cls in range(len(constants.DATASET_CLASSES[dataset])):
class_captions = [c for c, l in zip(captions, train_labels) if l == cls]
class_keywords.append(
extract_keywords(class_captions, kw_lens=[3, 5], top=top)
)
kws_dict = {i: kws for i, kws in enumerate(class_keywords)}
np.save(kw_cache_file, kws_dict)
return class_keywords
def get_civilcomments_keywords_per_cls(top=256, only_spurious=False, remake=False):
dataset = "CivilComments"
ending = "_only_spurious" if only_spurious else ""
kw_cache_dir = os.path.join(
constants.CACHE_PATH,
"keywords",
dataset,
)
os.makedirs(kw_cache_dir, exist_ok=True)
kw_cache_file = os.path.join(kw_cache_dir, f"keywords{ending}.npy")
if not remake and os.path.isfile(kw_cache_file):
kws_dict = np.load(kw_cache_file, allow_pickle=True).item()
class_keywords = [
kws_dict[i] for i in range(len(constants.DATASET_CLASSES[dataset]))
]
return class_keywords
csv_path = os.path.join(
constants.DATA_PATH,
constants.DATASET_DIR["CivilComments"],
"civilcomments_coarse.csv",
)
metadata_path = os.path.join(
constants.DATA_PATH,
constants.DATASET_DIR["CivilComments"],
constants.METADATA_NAME["CivilComments"],
)
df = pd.read_csv(csv_path)
mtd = pd.read_csv(metadata_path)
texts = df.comment_text[mtd.split == constants.DATASET_SPLITS["train"]].to_numpy()
train_labels = mtd.y[mtd.split == constants.DATASET_SPLITS["train"]].to_numpy()
train_envs = mtd.a[mtd.split == constants.DATASET_SPLITS["train"]].to_numpy()
del mtd
class_keywords = []
if only_spurious:
for cls in range(len(constants.DATASET_CLASSES[dataset])):
class_texts = texts[
(train_labels == cls)
& (train_envs == constants.DATASET_CLASS_BIAS[dataset][cls])
]
class_keywords.append(
extract_keywords(class_texts, kw_lens=[3, 5], top=top)
)
else:
for cls in range(len(constants.DATASET_CLASSES[dataset])):
class_texts = texts[train_labels == cls]
class_keywords.append(
extract_keywords(class_texts, kw_lens=[3, 5], top=top)
)
kws_dict = {i: kws for i, kws in enumerate(class_keywords)}
np.save(kw_cache_file, kws_dict)
return class_keywords
def cache_clip_keywords_and_embeddings_per_cls(dataset: str, only_spurious=False):
""" """
class_keywords = get_captions_keywords_per_cls(
dataset, only_spurious=only_spurious, remake=False
)
all_keywords = list(set(sum(class_keywords, start=[])))
# remove class_instances
ending = "_only_spurious" if only_spurious else ""
processed_keywords_path = os.path.join(
constants.CACHE_PATH, "keywords", dataset, f"filtered_keywords{ending}.pt"
)
print(processed_keywords_path)
if os.path.isfile(processed_keywords_path):
saved_keywords = torch.load(processed_keywords_path)
clean_keywords = saved_keywords["clean"]
else:
llm_keywords, llm_raw_keywords = utils.remove_class_instances_llm(
all_keywords,
constants.DATASET_CLASSES_EXPLICIT[dataset],
model_id="meta-llama/Meta-Llama-3.1-8B-Instruct",
weights_dtype=torch.bfloat16,
device_map="cuda",
# to_remove=dataset + so,
)
_, compound_terms, class_attributes, _ = utils.remove_class_instances(
llm_keywords,
constants.DATASET_CLASSES[dataset],
constants.DATASET_CLASSES_WN[dataset],
)
clean_keywords = list(set(compound_terms + class_attributes))
kwd = {
"llm_raw": llm_raw_keywords,
"llm": llm_keywords,
"clean": clean_keywords,
}
torch.save(kwd, processed_keywords_path)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
clip_encoder = model_wrappers.CLIPTextEncoderWrapper()
keywords_embeddings = clip_encoder.encode_texts_batched(
clean_keywords, device=device
)
d = {
"keywords": clean_keywords,
"keywords_embeddings": keywords_embeddings,
}
output_path = os.path.join(constants.CACHE_PATH, "biases", dataset)
if not os.path.exists(output_path):
os.makedirs(output_path)
torch.save(
d, os.path.join(output_path, f"filtered_keywords_and_embeddings{ending}.pt")
)
def cache_civilcomments_keywords_and_embeddings_per_cls(only_spurious=False):
""" """
dataset = "CivilComments"
class_keywords = get_civilcomments_keywords_per_cls(
only_spurious=only_spurious, remake=False
)
all_keywords = list(set(sum(class_keywords, start=[])))
ending = "_only_spurious" if only_spurious else ""
processed_keywords_path = os.path.join(
constants.CACHE_PATH, "keywords", dataset, f"filtered_keywords{ending}.pt"
)
if os.path.isfile(processed_keywords_path):
saved_keywords = torch.load(processed_keywords_path)
clean_keywords = saved_keywords["clean"]
else:
llm_keywords, llm_raw_output = utils.remove_class_instances_llm(
all_keywords,
constants.DATASET_CLASSES_EXPLICIT[dataset],
model_id="meta-llama/Meta-Llama-3.1-8B-Instruct",
weights_dtype=torch.bfloat16,
device_map="cuda",
# to_remove=dataset + so
)
_, compound_terms, class_attributes, _ = utils.remove_class_instances(
llm_keywords,
constants.DATASET_CLASSES[dataset],
constants.DATASET_CLASSES_WN[dataset],
)
clean_keywords = list(set(compound_terms + class_attributes))
kwd = {
"llm_raw": llm_raw_output,
"llm": llm_keywords,
"clean": clean_keywords,
}
torch.save(kwd, processed_keywords_path)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
encoder = model_wrappers.SentenceEncoderWrapper()
keywords_embeddings = encoder.encode_texts_batched(clean_keywords, device=device)
d = {
"keywords": clean_keywords,
"keywords_embeddings": keywords_embeddings,
}
output_path = os.path.join(constants.CACHE_PATH, "biases", dataset)
if not os.path.exists(output_path):
os.makedirs(output_path)
torch.save(
d, os.path.join(output_path, f"filtered_keywords_and_embeddings{ending}.pt")
)
def cache_keywords_and_embeddings(dataset: str, only_spurious=False):
if dataset == "CivilComments":
cache_civilcomments_keywords_and_embeddings_per_cls(only_spurious)
else:
cache_clip_keywords_and_embeddings_per_cls(dataset, only_spurious)
if __name__ == "__main__":
fire.Fire(cache_keywords_and_embeddings)