-
Notifications
You must be signed in to change notification settings - Fork 8
/
model.py
374 lines (316 loc) · 14.9 KB
/
model.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 os
import logging
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import torchvision
import pytorch_lightning as pl
from pytorch_lightning.callbacks import Callback
from pytorch_lightning.callbacks.early_stopping import EarlyStopping
from sentence_transformers import SentenceTransformer
import transformers
NUM_CLASSES = 2
BATCH_SIZE = 32
LEARNING_RATE = 1e-4
DROPOUT_P = 0.1
MODALITY = "text-image"
DATA_PATH = "./data"
PL_ASSETS_PATH = "./lightning_logs"
IMAGES_DIR = os.path.join(DATA_PATH, "images")
IMAGE_EXTENSION = ".jpg"
RESNET_OUT_DIM = 2048
SENTENCE_TRANSFORMER_EMBEDDING_DIM = 768
losses = []
logging.basicConfig(level=logging.INFO) # DEBUG, INFO, WARNING, ERROR, CRITICAL
print("CUDA available:", torch.cuda.is_available())
class JointTextImageModel(nn.Module):
def __init__(
self,
num_classes,
loss_fn,
text_module,
image_module,
text_feature_dim,
image_feature_dim,
fusion_output_size,
dropout_p,
hidden_size=512,
):
super(JointTextImageModel, self).__init__()
self.text_module = text_module
self.image_module = image_module
self.fusion = torch.nn.Linear(in_features=(text_feature_dim + image_feature_dim),
out_features=fusion_output_size)
# self.fc = torch.nn.Linear(in_features=fusion_output_size, out_features=num_classes)
self.fc1 = torch.nn.Linear(in_features=fusion_output_size, out_features=hidden_size)
self.fc2 = torch.nn.Linear(in_features=hidden_size, out_features=num_classes)
self.loss_fn = loss_fn
self.dropout = torch.nn.Dropout(dropout_p)
def forward(self, text, image, label):
text_features = torch.nn.functional.relu(self.text_module(text))
image_features = torch.nn.functional.relu(self.image_module(image))
combined = torch.cat([text_features, image_features], dim=1)
fused = self.dropout(
torch.nn.functional.relu(self.fusion(combined))) # TODO add dropout
# logits = self.fc(fused)
hidden = torch.nn.functional.relu(self.fc1(fused))
logits = self.fc2(hidden)
# nn.CrossEntropyLoss expects raw logits as model output, NOT torch.nn.functional.softmax(logits, dim=1)
# https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html
pred = logits
loss = self.loss_fn(pred, label)
return (pred, loss)
@classmethod
def build_image_transform(cls, 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
class MultimodalFakeNewsDetectionModel(pl.LightningModule):
def __init__(self, hparams=None):
super(MultimodalFakeNewsDetectionModel, self).__init__()
if hparams:
# Cannot reassign self.hparams in pl.LightningModule; must use update()
# https://github.com/PyTorchLightning/pytorch-lightning/discussions/7525
self.hparams.update(hparams)
self.embedding_dim = self.hparams.get("embedding_dim", 768)
self.text_feature_dim = self.hparams.get("text_feature_dim", 300)
self.image_feature_dim = self.hparams.get("image_feature_dim", self.text_feature_dim)
self.model = self._build_model()
# Required for pl.LightningModule
def forward(self, text, image, label):
# pl.Lightning convention: forward() defines prediction for inference
return self.model(text, image, label)
# Required for pl.LightningModule
def training_step(self, batch, batch_idx):
global losses
# pl.Lightning convention: training_step() defines prediction and
# accompanying loss for training, independent of forward()
text, image, label = batch["text"], batch["image"], batch["label"]
pred, loss = self.model(text, image, label)
self.log("train_loss", loss, on_step=True, on_epoch=True, prog_bar=True, logger=True)
print(loss.item())
losses.append(loss.item())
return loss
# Optional for pl.LightningModule
def training_step_end(self, batch_parts):
"""
Aggregates results when training using a strategy that splits data
from each batch across GPUs (e.g. data parallel)
Note that training_step returns a loss, thus batch_parts returns a list
of 2 loss values (since there are 2 GPUs being used)
"""
return sum(batch_parts) / len(batch_parts)
# Optional for pl.LightningModule
def test_step(self, batch, batch_idx):
text, image, label = batch["text"], batch["image"], batch["label"]
pred, loss = self.model(text, image, label)
pred_label = torch.argmax(pred, dim=1)
accuracy = torch.sum(pred_label == label).item() / (len(label) * 1.0)
output = {
'test_loss': loss,
'test_acc': torch.tensor(accuracy).cuda()
}
print(loss.item(), output['test_acc'])
return output
# Optional for pl.LightningModule
def test_epoch_end(self, outputs):
avg_loss = torch.stack([x["test_loss"] for x in outputs]).mean()
avg_accuracy = torch.stack([x["test_acc"] for x in outputs]).mean()
logs = {
'test_loss': avg_loss,
'test_acc': avg_accuracy
}
# pl.LightningModule has some issues displaying the results automatically
# As a workaround, we can store the result logs as an attribute of the
# class instance and display them manually at the end of testing
# https://github.com/PyTorchLightning/pytorch-lightning/issues/1088
self.test_results = logs
return {
'avg_test_loss': avg_loss,
'avg_test_acc': avg_accuracy,
'log': logs,
'progress_bar': logs
}
# Required for pl.LightningModule
def configure_optimizers(self):
optimizer = torch.optim.Adam(self.parameters(), lr=LEARNING_RATE)
# optimizer = torch.optim.SGD(self.parameters(), lr=LEARNING_RATE, momentum=0.9)
return optimizer
def _build_model(self):
text_module = torch.nn.Linear(
in_features=self.embedding_dim, out_features=self.text_feature_dim)
image_module = torchvision.models.resnet152(pretrained=True)
# Overwrite last layer to get features (rather than classification)
image_module.fc = torch.nn.Linear(
in_features=RESNET_OUT_DIM, out_features=self.image_feature_dim)
return JointTextImageModel(
num_classes=self.hparams.get("num_classes", NUM_CLASSES),
loss_fn=torch.nn.CrossEntropyLoss(),
text_module=text_module,
image_module=image_module,
text_feature_dim=self.text_feature_dim,
image_feature_dim=self.image_feature_dim,
fusion_output_size=self.hparams.get("fusion_output_size", 512),
dropout_p=self.hparams.get("dropout_p", DROPOUT_P)
)
class JointTextImageDialogueModel(nn.Module):
def __init__(
self,
num_classes,
loss_fn,
text_module,
image_module,
dialogue_module,
text_feature_dim,
image_feature_dim,
dialogue_feature_dim,
fusion_output_size,
dropout_p,
hidden_size=512,
):
super(JointTextImageDialogueModel, self).__init__()
self.text_module = text_module
self.image_module = image_module
self.dialogue_module = dialogue_module
self.fusion = torch.nn.Linear(in_features=(text_feature_dim + image_feature_dim + dialogue_feature_dim),
out_features=fusion_output_size)
# self.fc = torch.nn.Linear(in_features=fusion_output_size, out_features=num_classes)
self.fc1 = torch.nn.Linear(in_features=fusion_output_size, out_features=hidden_size) # trial
self.fc2 = torch.nn.Linear(in_features=hidden_size, out_features=num_classes) # trial
self.loss_fn = loss_fn
self.dropout = torch.nn.Dropout(dropout_p)
def forward(self, text, image, dialogue, label):
text_features = torch.nn.functional.relu(self.text_module(text))
image_features = torch.nn.functional.relu(self.image_module(image))
dialogue_features = torch.nn.functional.relu(self.dialogue_module(dialogue))
# print(text_features.size(), image_features.size()) # torch.Size([32, 300]) torch.Size([16, 300])
combined = torch.cat([text_features, image_features, dialogue_features], dim=1)
fused = self.dropout(
torch.nn.functional.relu(self.fusion(combined)))
# logits = self.fc(fused)
hidden = torch.nn.functional.relu(self.fc1(fused)) # trial
logits = self.fc2(hidden) # trial
# pred = torch.nn.functional.softmax(logits, dim=1)
pred = logits # nn.CrossEntropyLoss expects raw logits as model output # https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html
loss = self.loss_fn(pred, label)
return (pred, loss)
@classmethod
def build_image_transform(cls, 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
class MultimodalFakeNewsDetectionModelWithDialogue(pl.LightningModule):
def __init__(self, hparams=None):
super(MultimodalFakeNewsDetectionModelWithDialogue, self).__init__()
if hparams:
self.hparams.update(hparams) # https://github.com/PyTorchLightning/pytorch-lightning/discussions/7525
self.embedding_dim = self.hparams.get("embedding_dim", 768)
self.text_feature_dim = self.hparams.get("text_feature_dim", 300)
self.image_feature_dim = self.hparams.get("image_feature_dim", self.text_feature_dim)
self.dialogue_feature_dim = self.hparams.get("dialogue_feature_dim", self.text_feature_dim)
self.model = self._build_model()
# Required for pl.LightningModule
def forward(self, text, image, dialogue, label):
# pl.Lightning convention: forward() defines prediction for inference
return self.model(text, image, dialogue, label)
# Required for pl.LightningModule
def training_step(self, batch, batch_idx):
global losses
# pl.Lightning convention: training_step() defines prediction and
# accompanying loss for training, independent of forward()
text, image, dialogue, label = batch["text"], batch["image"], batch["dialogue"], batch["label"]
pred, loss = self.model(text, image, dialogue, label)
self.log("train_loss", loss, on_step=True, on_epoch=True, prog_bar=True, logger=True)
print(loss.item())
losses.append(loss.item())
return loss
# Optional for pl.LightningModule
def training_step_end(self, batch_parts):
"""
Aggregates results when training using a strategy that splits data
from each batch across GPUs (e.g. data parallel)
Note that training_step returns a loss, thus batch_parts returns a list
of 2 loss values (since there are 2 GPUs being used)
"""
return sum(batch_parts) / len(batch_parts)
# Optional for pl.LightningModule
def test_step(self, batch, batch_idx):
text, image, dialogue, label = batch["text"], batch["image"], batch["dialogue"], batch["label"]
pred, loss = self.model(text, image, dialogue, label)
pred_label = torch.argmax(pred, dim=1)
accuracy = torch.sum(pred_label == label).item() / (len(label) * 1.0)
output = {
'test_loss': loss,
'test_acc': torch.tensor(accuracy).cuda()
}
print(loss.item(), output['test_acc'])
return output
# Optional for pl.LightningModule
def test_epoch_end(self, outputs):
avg_loss = torch.stack([x["test_loss"] for x in outputs]).mean()
avg_accuracy = torch.stack([x["test_acc"] for x in outputs]).mean()
logs = {
'test_loss': avg_loss,
'test_acc': avg_accuracy
}
# pl.LightningModule has some issues displaying the results automatically
# As a workaround, we can store the result logs as an attribute of the
# class instance and display them manually at the end of testing
# https://github.com/PyTorchLightning/pytorch-lightning/issues/1088
self.test_results = logs
return {
'avg_test_loss': avg_loss,
'avg_test_acc': avg_accuracy,
'log': logs,
'progress_bar': logs
}
# Required for pl.LightningModule
def configure_optimizers(self):
optimizer = torch.optim.Adam(self.parameters(), lr=LEARNING_RATE)
# optimizer = torch.optim.SGD(self.parameters(), lr=LEARNING_RATE, momentum=0.9)
return optimizer
def _build_model(self):
text_module = torch.nn.Linear(
in_features=self.embedding_dim, out_features=self.text_feature_dim)
image_module = torchvision.models.resnet152(pretrained=True)
# Overwrite last layer to get features (rather than classification)
image_module.fc = torch.nn.Linear(
in_features=RESNET_OUT_DIM, out_features=self.image_feature_dim)
dialogue_module = torch.nn.Linear(
in_features=self.embedding_dim, out_features=self.dialogue_feature_dim)
return JointTextImageDialogueModel(
num_classes=self.hparams.get("num_classes", NUM_CLASSES),
loss_fn=torch.nn.CrossEntropyLoss(),
text_module=text_module,
image_module=image_module,
dialogue_module=dialogue_module,
text_feature_dim=self.text_feature_dim,
image_feature_dim=self.image_feature_dim,
dialogue_feature_dim=self.dialogue_feature_dim,
fusion_output_size=self.hparams.get("fusion_output_size", 512),
dropout_p=self.hparams.get("dropout_p", DROPOUT_P)
)
class PrintCallback(Callback):
def on_train_start(self, trainer, pl_module):
print("Training started...")
def on_train_end(self, trainer, pl_module):
print("Training done...")
global losses
for loss_val in losses:
print(loss_val)