-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_eval.py
51 lines (40 loc) · 1.54 KB
/
train_eval.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
import torch
from tqdm import tqdm
from sklearn.metrics import classification_report
# Normal training step with the poisoning dataset
def train(model, data_loader, criterion, optimizer):
"""
Function for model training step
"""
running_loss = 0
model.train()
for step, (batch_img, batch_label) in enumerate(tqdm(data_loader)):
optimizer.zero_grad() # Set gradients to zero
output = model(batch_img) # Forward pass
loss = criterion(output, batch_label)
loss.backward() # Backpropagation
optimizer.step() # Update weights
running_loss += loss
return running_loss
# Simple evaluation with the addition of a classification report with precision and recall
def eval(model, test_loader, batch_size=64, report=True):
"""
Simple evaluation with the addition of a classification report.
"""
ret = 0
preds = []
gt = []
with torch.no_grad():
model.eval()
for step, (batch_img, batch_label) in enumerate(test_loader):
output = model(batch_img)
label_predict = torch.argmax(output, dim=1)
preds.append(label_predict)
batch_label = torch.argmax(batch_label, dim=1)
gt.append(batch_label)
ret += torch.sum(batch_label == label_predict)
if report:
gt = torch.cat(gt, 0)
preds = torch.cat(preds, 0)
print(classification_report(gt.cpu(), preds.cpu()))
return int(ret) / (step * batch_size)