-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
208 lines (160 loc) · 6.32 KB
/
main.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
import os
import gc
import numpy as np
import pandas as pd
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from torchvision.io import read_image
from PIL import Image
from tqdm import tqdm
from torchmetrics.classification import BinaryAUROC, BinaryF1Score, BinaryAccuracy
import wandb
from pytorch_lightning import seed_everything
import clip
from hatememe.config import CFG
from hatememe.dataset import HMDataset
from hatememe.architecture import HMMLP
# from logging import log, basicConfig
from hatememe.logger import log
# basicConfig(level=20)
seed_everything(CFG.seed, workers=True)
cfg = CFG()
print("Torch version:", torch.__version__)
from hatememe.dataset import HMDataset
eager_transform = cfg.eager_transform
train_dataset = HMDataset(
cfg.images_path,
os.path.join(cfg.annotations_path,'train_updated.jsonl'),
image_transform=cfg.image_transform,
text_transform=cfg.text_transform,
eager_transform=eager_transform,
add_memotion=cfg.add_memotion,
)
val_dataset = HMDataset(
cfg.images_path,
os.path.join(cfg.annotations_path,'dev_unseen.jsonl'),
image_transform=cfg.image_transform,
text_transform=cfg.text_transform,
eager_transform=eager_transform
)
test_dataset = HMDataset(
cfg.images_path,
os.path.join(cfg.annotations_path,'test_unseen.jsonl'),
image_transform=cfg.image_transform,
text_transform=cfg.text_transform,
eager_transform=eager_transform
)
def convert_models_to_fp32(model):
for p in model.parameters():
p.data = p.data.float()
if p.grad is not None:
p.grad.data = p.grad.data.float()
device = torch.device(cfg.device if torch.cuda.is_available() else "cpu")
net = HMMLP()
print(net)
convert_models_to_fp32(net)
num_workers=30
train_dataloader = DataLoader(train_dataset, batch_size=cfg.batch_size, shuffle=True, num_workers=num_workers)
val_dataloader = DataLoader(val_dataset, batch_size=cfg.batch_size, shuffle=False, num_workers=num_workers)
test_dataloader = DataLoader(test_dataset, batch_size=cfg.batch_size, shuffle=False, num_workers=num_workers)
net = net.to(device)
criterion = nn.BCEWithLogitsLoss()
optimizer = torch.optim.AdamW(net.parameters(), lr=cfg.learning_rate, weight_decay=cfg.weight_decay)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, len(train_dataloader)*cfg.epochs)
scaler = torch.cuda.amp.GradScaler()
best_auroc = 0
best_acc = 0
for epoch in range(cfg.epochs):
running_loss = 0
for i, data in enumerate(tqdm(train_dataloader)):
net.train()
images, texts, labels = data
images, texts, labels = images.to(device), texts.to(device), labels.to(device)
optimizer.zero_grad()
with torch.cuda.amp.autocast():
output = net(images, texts)
loss = criterion(output.squeeze(), labels.squeeze())
scaler.scale(loss).backward()
# convert_models_to_fp32(net.base_model)
scaler.step(optimizer)
scaler.update()
# clip.model.convert_weights(net.base_model)
running_loss+=loss.item()
# torch.cuda.empty_cache()
# gc.collect()
if i % cfg.log_every == (cfg.log_every - 1):
print(
f"[Epoch {epoch + 1}, step {i+1:3d}] loss: {running_loss/cfg.log_every:.5f}"
)
log({"epoch":epoch, "train_loss":running_loss/cfg.log_every})
running_loss = 0.0
wandb.watch(net)
scheduler.step()
net.eval()
running_loss = 0.0
total_preds = 0
preds_all_val = torch.tensor([]).cuda()
labels_all_val = torch.tensor([]).cuda()
for i, data in enumerate(tqdm(val_dataloader), 0):
images, texts, labels = data
images = images.to(device)
texts = texts.to(device)
labels = labels.squeeze().to(device)
with torch.cuda.amp.autocast():
output = net(images, texts)
loss = criterion(output.squeeze(), labels.squeeze())
# loss = criterion(output.squeeze(), labels)
running_loss += loss.item()
# Accumulate all predictions and labels
preds_all_val = torch.cat((preds_all_val, torch.sigmoid(output).squeeze()))
labels_all_val = torch.cat((labels_all_val, labels))
auroc = BinaryAUROC().to(device)
auroc_score = auroc(preds_all_val, labels_all_val.int())
accuracy = BinaryAccuracy().to(device)
accuracy_score = accuracy(preds_all_val, labels_all_val)
f1 = BinaryF1Score().to(device)
f1_score = f1(preds_all_val, labels_all_val.int())
if auroc_score > best_auroc:
torch.save(net.state_dict(), cfg.model_path)
best_auroc = auroc_score
best_acc = accuracy_score
best_f1_score = f1_score
print(
f"[Epoch {epoch +1}, step {i+1:3d}] val loss: {running_loss/i+1:.5f} accuracy: "
f"{accuracy_score} auroc: {auroc_score} f1_score: {f1_score}"
)
log({"val_loss":running_loss/i+1, "val_f1_score":f1_score, "val_accuracy":accuracy_score, "val_auroc":auroc_score})
log({"val_loss":running_loss/i+1, "val_f1_score":best_f1_score, "val_accuracy":best_acc, "val_auroc":best_auroc})
torch.cuda.empty_cache()
gc.collect()
del net
###################
# Testing loop
###################
# Load the best model
net = HMMLP()
net.load_state_dict(torch.load(cfg.model_path))
net = net.to(device)
net.eval()
preds_all_val = torch.tensor([]).cuda()
labels_all_val = torch.tensor([]).cuda()
for i, data in enumerate(tqdm(test_dataloader), 0):
images, texts, labels = data
images = images.to(device)
texts = texts.to(device)
labels = labels.squeeze().to(device)
with torch.cuda.amp.autocast():
output = net(images, texts)
# loss = criterion(output.squeeze(), labels.squeeze())
preds_all_val = torch.cat((preds_all_val, torch.sigmoid(output).squeeze()))
labels_all_val = torch.cat((labels_all_val, labels))
wandb.watch(net)
auroc = BinaryAUROC().to(device)
auroc_score = auroc(preds_all_val, labels_all_val.int())
accuracy = BinaryAccuracy().to(device)
accuracy_score = accuracy(preds_all_val, labels_all_val)
f1 = BinaryF1Score().to(device)
f1_score = f1(preds_all_val, labels_all_val.int())
log({"test_f1_score":f1_score, "test_accuracy":accuracy_score, "test_auroc":auroc_score})