-
Notifications
You must be signed in to change notification settings - Fork 0
/
hateful_memes.py
587 lines (516 loc) · 19.9 KB
/
hateful_memes.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
import json
import logging
from pathlib import Path
import random
import tarfile
import tempfile
import warnings
import fasttext
import torchvision
import torch
from PIL import Image
import argparse
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pandas_path # Path style access for pandas
from tqdm import tqdm
parser = argparse.ArgumentParser(
prog = 'hateful_memes',
description = 'Train multimodal architecture and generate adversarial attacks and then retrains'
)
parser.add_argument('--load','-l',action='store_true',help="loads a model and run attack scripts" )
args = parser.parse_args()
class HatefulMemesDataset(torch.utils.data.Dataset):
"""Uses jsonl data to preprocess and serve
dictionary of multimodal tensors for model input.
"""
def __init__(
self,
data_path,
img_dir,
image_transform,
text_transform,
balance=False,
dev_limit=None,
random_state=0,
):
self.samples_frame = pd.read_json(
data_path, lines=True
)
self.dev_limit = dev_limit
if balance:
neg = self.samples_frame[
self.samples_frame.label.eq(0)
]
pos = self.samples_frame[
self.samples_frame.label.eq(1)
]
self.samples_frame = pd.concat(
[
neg.sample(
pos.shape[0],
random_state=random_state
),
pos
]
)
if self.dev_limit:
if self.samples_frame.shape[0] > self.dev_limit:
self.samples_frame = self.samples_frame.sample(
dev_limit, random_state=random_state
)
self.samples_frame = self.samples_frame.reset_index(
drop=True
)
self.samples_frame.img = self.samples_frame.apply(
lambda row: (img_dir / row.img), axis=1
)
# https://github.com/drivendataorg/pandas-path
# print(self.samples_frame.img)
# if not self.samples_frame.img.path.exists().all():
# raise FileNotFoundError
# if not self.samples_frame.img.path.is_file().all():
# raise TypeError
self.image_transform = image_transform
self.text_transform = text_transform
def __len__(self):
"""This method is called when you do len(instance)
for an instance of this class.
"""
return len(self.samples_frame)
def __getitem__(self, idx):
"""This method is called when you do instance[key]
for an instance of this class.
"""
if torch.is_tensor(idx):
idx = idx.tolist()
img_id = self.samples_frame.loc[idx, "id"]
image = Image.open(
self.samples_frame.loc[idx, "img"]
).convert("RGB")
image = self.image_transform(image)
text = torch.Tensor(
# self.text_transform.wv[self.samples_frame.loc[idx, "text"]]
self.text_transform.get_sentence_vector(
self.samples_frame.loc[idx, "text"]
)
).squeeze()
if "label" in self.samples_frame.columns:
label = torch.Tensor(
[self.samples_frame.loc[idx, "label"]]
).long().squeeze()
sample = {
"id": img_id,
"image": image,
"text": text,
"label": label
}
else:
sample = {
"id": img_id,
"image": image,
"text": text
}
return sample
class LanguageAndVisionConcat(torch.nn.Module):
def __init__(
self,
num_classes,
loss_fn,
language_module,
vision_module,
language_feature_dim,
vision_feature_dim,
fusion_output_size,
dropout_p,
):
super(LanguageAndVisionConcat, self).__init__()
self.language_module = language_module
self.vision_module = vision_module
self.fusion = torch.nn.Linear(
in_features=(language_feature_dim + vision_feature_dim),
out_features=fusion_output_size
)
self.fc = torch.nn.Linear(
in_features=fusion_output_size,
out_features=num_classes
)
self.loss_fn = loss_fn
self.dropout = torch.nn.Dropout(dropout_p)
def forward(self, text, image, label=None):
text_features = torch.nn.functional.relu(
self.language_module(text)
)
image_features = torch.nn.functional.relu(
self.vision_module(image)
)
combined = torch.cat(
[text_features, image_features], dim=1
)
fused = self.dropout(
torch.nn.functional.relu(
self.fusion(combined)
)
)
logits = self.fc(fused)
pred = torch.nn.functional.softmax(logits)
loss = (
self.loss_fn(pred, label)
if label is not None else label
)
return (pred, loss)
import pytorch_lightning as pl
import warnings
import logging
# for the purposes of this post, we'll filter
# much of the lovely logging info from our LightningModule
warnings.filterwarnings("ignore")
logging.getLogger().setLevel(logging.WARNING)
class HatefulMemesModel(pl.LightningModule):
def __init__(self, hparams):
for data_key in ["train_path", "dev_path", "img_dir",]:
# ok, there's one for-loop but it doesn't count
if data_key not in hparams.keys():
raise KeyError(
f"{data_key} is a required hparam in this model"
)
super(HatefulMemesModel, self).__init__()
self.save_hyperparameters()
for key in hparams.keys():
self.hparams[key]=hparams[key]
# assign some hparams that get used in multiple places
self.embedding_dim = self.hparams.get("embedding_dim", 300)
self.language_feature_dim = self.hparams.get(
"language_feature_dim", 300
)
self.vision_feature_dim = self.hparams.get(
# balance language and vision features by default
"vision_feature_dim", self.language_feature_dim
)
self.output_path = Path(
self.hparams.get("output_path", "model-outputs")
)
self.output_path.mkdir(exist_ok=True)
# instantiate transforms, datasets
self.text_transform = self._build_text_transform()
self.image_transform = self._build_image_transform()
self.train_dataset = self._build_dataset("train_path")
self.dev_dataset = self._build_dataset("dev_path")
# set up model and training
self.model = self._build_model()
self.trainer_params = self._get_trainer_params()
## Required LightningModule Methods (when validating) ##
def forward(self, text, image, label=None):
return self.model(text, image, label)
def training_step(self, batch, batch_nb):
preds, loss = self.forward(
text=batch["text"],
image=batch["image"],
label=batch["label"]
)
self.log("train_loss", loss, on_step=False, on_epoch=True, prog_bar=True, logger=True)
return {"loss": loss}
def training_epoch_end(self, outputs):
pass
def test_step(self, batch, batch_nb):
preds, loss = self.forward(
text=batch["text"],
image=batch["image"],
label=batch["label"]
)
self.log("test_loss", loss, on_step=True, on_epoch=True, prog_bar=True, logger=True)
return {"test_loss": loss}
def test_epoch_end(self, outputs):
self.trainer.progress_bar_callback.main_progress_bar.write(
f"Epoch {self.trainer.current_epoch} test loss={self.trainer.progress_bar_dict['loss']}")
def validation_step(self, batch, batch_nb):
preds, loss = self.eval().forward(
text=batch["text"],
image=batch["image"],
label=batch["label"]
)
self.log("val_loss",loss,on_step=True,on_epoch=True, prog_bar=True, logger=True)
return {"batch_val_loss": loss}
def validation_epoch_end(self, outputs):
pass
def configure_optimizers(self):
optimizer = torch.optim.AdamW(
self.model.parameters(),
lr=self.hparams.get("lr", 0.001)
)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer
)
return {
"optimizer": optimizer,
"lr_scheduler": {
"scheduler": scheduler,
"monitor": "val_loss",
"strict": False
# If "monitor" references validation metrics, then "frequency" should be set to a
# multiple of "trainer.check_val_every_n_epoch".
},
}
def train_dataloader(self):
return torch.utils.data.DataLoader(
self.train_dataset,
shuffle=True,
batch_size=self.hparams.get("batch_size", 4),
num_workers=self.hparams.get("num_workers", 16)
)
def val_dataloader(self):
return torch.utils.data.DataLoader(
self.dev_dataset,
shuffle=False,
batch_size=self.hparams.get("batch_size", 4),
num_workers=self.hparams.get("num_workers", 16)
)
## Convenience Methods ##
def fit(self):
self._set_seed(self.hparams.get("random_state", 42))
self.trainer = pl.Trainer(**self.trainer_params)
self.trainer.fit(self)
def _set_seed(self, seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
def _build_text_transform(self):
with tempfile.NamedTemporaryFile() as ft_training_data:
ft_path = Path(ft_training_data.name)
with ft_path.open("w") as ft:
training_data = [
json.loads(line)["text"] + "/n"
for line in open(
self.hparams.get("train_path")
).read().splitlines()
]
for line in training_data:
ft.write(line + "\n")
language_transform = fasttext.train_unsupervised(
str(ft_path),
model=self.hparams.get("fasttext_model", "cbow"),
dim=self.embedding_dim
)
return language_transform
def _build_image_transform(self):
image_dim = self.hparams.get("image_dim", 224)
image_transform = torchvision.transforms.Compose(
[
torchvision.transforms.Resize(
size=(image_dim, image_dim)
),
torchvision.transforms.ToTensor(),
# all torchvision models expect the same
# normalization mean and std
# https://pytorch.org/docs/stable/torchvision/models.html
torchvision.transforms.Normalize(
mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225)
),
]
)
return image_transform
def _build_dataset(self, dataset_key):
return HatefulMemesDataset(
data_path=self.hparams.get(dataset_key, dataset_key),
img_dir=self.hparams.get("img_dir"),
image_transform=self.image_transform,
text_transform=self.text_transform,
# limit training samples only
dev_limit=(
self.hparams.get("dev_limit", None)
if "train" in str(dataset_key) else None
),
balance=True if "train" in str(dataset_key) else False,
)
def _build_model(self):
# we're going to pass the outputs of our text
# transform through an additional trainable layer
# rather than fine-tuning the transform
language_module = torch.nn.Linear(
in_features=self.embedding_dim,
out_features=self.language_feature_dim
)
# easiest way to get features rather than
# classification is to overwrite last layer
# with an identity transformation, we'll reduce
# dimension using a Linear layer, resnet is 2048 out
vision_module = torchvision.models.resnet152(
pretrained=True
)
for param in vision_module.parameters():
param.requires_grad = False
vision_module.fc = torch.nn.Linear(
in_features=2048,
out_features=self.vision_feature_dim
)
return LanguageAndVisionConcat(
num_classes=self.hparams.get("num_classes", 2),
loss_fn=torch.nn.CrossEntropyLoss(),
language_module=language_module,
vision_module=vision_module,
language_feature_dim=self.language_feature_dim,
vision_feature_dim=self.vision_feature_dim,
fusion_output_size=self.hparams.get(
"fusion_output_size", 512
),
dropout_p=self.hparams.get("dropout_p", 0.1),
)
def _get_trainer_params(self):
checkpoint_callback = pl.callbacks.ModelCheckpoint(
# filepath=self.output_path,
dirpath=self.output_path,
monitor=self.hparams.get(
"checkpoint_monitor", "val_loss"
),
mode=self.hparams.get(
"checkpoint_monitor_mode", "min"
),
verbose=self.hparams.get("verbose", True)
)
early_stop_callback = pl.callbacks.EarlyStopping(
monitor=self.hparams.get(
"early_stop_monitor", "val_loss"
),
min_delta=self.hparams.get(
"early_stop_min_delta", 0.001
),
patience=self.hparams.get(
"early_stop_patience", 6
),
verbose=self.hparams.get("verbose", True),
)
trainer_params = {
# "checkpoint_callback": checkpoint_callback,
"callbacks": [early_stop_callback,checkpoint_callback],
"default_root_dir": self.output_path,
"accumulate_grad_batches": self.hparams.get(
"accumulate_grad_batches", 1
),
"accelerator":"gpu",
"devices":1,
"gpus": self.hparams.get("n_gpu", 1),
"max_epochs": self.hparams.get("max_epochs", 100),
"gradient_clip_val": self.hparams.get(
"gradient_clip_value", 1
),
}
return trainer_params
@torch.no_grad()
def make_submission_frame(self, test_path):
test_dataset = self._build_dataset(test_path)
submission_frame = pd.DataFrame(
index=test_dataset.samples_frame.id,
columns=["proba", "label"]
)
test_dataloader = torch.utils.data.DataLoader(
test_dataset,
shuffle=False,
batch_size=self.hparams.get("batch_size", 4),
num_workers=self.hparams.get("num_workers", 16))
for batch in tqdm(test_dataloader, total=len(test_dataloader)):
preds, _ = self.model.eval().to("cpu")(
batch["text"], batch["image"]
)
submission_frame.loc[batch["id"], "proba"] = preds[:, 1]
submission_frame.loc[batch["id"], "label"] = preds.argmax(dim=1)
submission_frame.proba = submission_frame.proba.astype(float)
submission_frame.label = submission_frame.label.astype(int)
return submission_frame
def fgsm_attack(model, loss, test_path, eps) :
attack_success = 0
original_success = 0
test_dataset = model._build_dataset(test_path)
test_frame = pd.DataFrame(
index=test_dataset.samples_frame.id,
columns=["proba", "label"]
)
test_dataloader = torch.utils.data.DataLoader(
test_dataset,
shuffle=False,
batch_size= 4,
num_workers= 16)
for batch in tqdm(test_dataloader, total=len(test_dataloader)):
batch["image"], batch["text"], batch["label"] = batch["image"].to("cuda"), batch["text"].to("cuda"), batch["label"].to("cuda")
batch["image"].requires_grad = True
preds, _ = model.eval().to("cuda")(batch["text"], batch["image"])
# preds = preds.max(1, keepdim=True)[1]
preds = torch.nn.functional.softmax(preds)
model.zero_grad()
# print(preds, batch["label"])
l = torch.nn.CrossEntropyLoss()
output = l(preds,batch["label"]).to("cuda")
output.backward()
batch_attack_images = batch["image"] + eps*batch["image"].grad.sign()
batch_attack_images = torch.clamp(batch_attack_images, 0, 1)
perturbed_preds, _ = model.eval().to("cuda")(batch["text"], batch_attack_images)
perturbed_preds = perturbed_preds.max(1, keepdim=True)[1]
for i, p in enumerate(preds.max(1, keepdim=True)[1]):
if p == batch["label"][i]:
original_success += 1
if perturbed_preds[i] != batch["label"][i]:
attack_success += 1
return ((attack_success/len(test_dataloader))*100)
data_dir = Path.cwd().parent / "ml-cybersec" / "datasets" / "hateful_memes" / "defaults" / "annotations"
print(data_dir)
img_tar_path = data_dir / "img.tar.gz"
train_path = data_dir / "train.jsonl"
dev_path = data_dir / "test_unseen.jsonl"
test_path = data_dir / "test_unseen.jsonl"
train_samples_frame = pd.read_json(train_path, lines=True)
train_samples_frame.label.value_counts()
hparams = {
# Required hparams
"train_path": train_path,
"dev_path": dev_path,
"img_dir": data_dir,
# Optional hparams
"embedding_dim": 150,
"language_feature_dim": 300,
"vision_feature_dim": 300,
"fusion_output_size": 256,
"output_path": "model-outputs",
"dev_limit": None,
"lr": 0.00005,
"max_epochs": 30,
"n_gpu": 1,
"batch_size": 4,
# allows us to "simulate" having larger batches
"accumulate_grad_batches": 16,
"early_stop_patience": 6,
}
if args.load:
load_path = Path.cwd()/hparams.get("output_path")/"epoch=10-step=1045.ckpt"
isExist = Path(load_path).is_file()
if not isExist:
raise Exception(f"No file exists at the path{load_path}, maybe you are missing saved model. Try training by running the script without load flag")
hateful_memes_model = HatefulMemesModel.load_from_checkpoint(load_path)
# TODO: Implement image perturbation attacks here
# Implement text perturbation attacks here
# Implement adversarial retraining here
eps = 50/255
loss = torch.nn.CrossEntropyLoss
print("Starting")
print(fgsm_attack(hateful_memes_model, loss, test_path, eps))
else:
hateful_memes_model = HatefulMemesModel(hparams=hparams)
hateful_memes_model.fit()
checkpoints = list(Path("model-outputs/lightning_logs/version_4/checkpoints").glob("*.ckpt"))
submission = hateful_memes_model.make_submission_frame(test_path)
submission.head()
submission.to_csv(("model-outputs/submission.csv"), index=True)
columns = ["id", "label"]
df = pd.read_csv("./model-outputs/submission.csv", usecols=columns)
import json
data = pd.read_json(test_path, lines=True)
accuracy = 0
row,col = df.shape
comp = []
for csv_row in range(row):
if(df.label[csv_row] == data.label[csv_row]):
accuracy+=1
comp.append(csv_row)
print("Accuracy: %.2f%%"%((accuracy/1000) * 100) )