forked from SiddhanthHegde/You-Need-to-Pay-More-Attention
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
101 lines (88 loc) · 3.1 KB
/
utils.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
import os
from shutil import copy2
import time
import torch
def move_data(start,troll,not_troll):
for img_name in os.listdir(start):
src = os.path.join(start,img_name)
if img_name.startswith('N'):
copy2(src,not_troll)
else:
copy2(src,troll)
def split_data(start,train,val,split):
for i, img_name in enumerate(os.listdir(start)):
src = os.path.join(start,img_name)
if i < split:
copy2(src,val)
else:
copy2(src,train)
def epoch_time(start_time,end_time):
elapsed_time = end_time - start_time
elapsed_mins = int(elapsed_time/60)
elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
return elapsed_mins,elapsed_secs
def train_epoch(model,data_loader,loss_fn,optimizer,device,scheduler,n_examples):
model = model.train()
losses = []
correct_predictions = 0
for idx, data in enumerate(data_loader):
input_ids = data['input_ids'].to(device)
attention_mask = data['attention_mask'].to(device)
labels = data['label'].to(device)
labelsviewed = labels.view(labels.shape[0],1)
image = data['image'].to(device)
outputs = model(
input_ids=input_ids,
attention_mask=attention_mask,
img=image
)
preds = [0 if x < 0.5 else 1 for x in outputs]
preds = torch.tensor(preds).to(device)
loss = loss_fn(outputs,labelsviewed)
correct_predictions += torch.sum(preds == labels)
losses.append(loss.item())
loss.backward()
nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()
scheduler.step()
optimizer.zero_grad()
return correct_predictions.double() / n_examples, np.mean(losses)
def eval_model(model, data_loader, loss_fn, device, n_examples):
model = model.eval()
losses = []
correct_predictions = 0
with torch.no_grad():
for d in data_loader:
input_ids = d["input_ids"].to(device)
attention_mask = d["attention_mask"].to(device)
labels = d["label"].to(device)
labelsviewed = labels.view(labels.shape[0],1)
image = d['image'].to(device)
outputs = model(
input_ids=input_ids,
attention_mask=attention_mask,
img=image
)
preds = [0 if x < 0.5 else 1 for x in outputs]
preds = torch.tensor(preds).to(device)
loss = loss_fn(outputs, labelsviewed)
correct_predictions += torch.sum(preds == labels)
losses.append(loss.item())
return correct_predictions.double() / n_examples, np.mean(losses)
def get_predictions(model,data_loader, device):
model = model.eval()
f_preds = []
with torch.no_grad():
for d in data_loader:
input_ids = d["input_ids"].to(device)
attention_mask = d["attention_mask"].to(device)
image = d['image'].to(device)
outputs = model(
input_ids=input_ids,
attention_mask=attention_mask,
img=image
)
preds = ['Non-troll' if x < 0.5 else 'Troll' for x in outputs]
for j in preds:
f_preds.append(j)
return f_preds