-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
70 lines (49 loc) · 1.99 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
from __future__ import print_function
import torch
from torchvision import datasets, transforms
import torch.nn as nn
import torch.nn.functional as F
from tqdm import tqdm
def GetCorrectPredCount(pPrediction, pLabels):
return pPrediction.argmax(dim=1).eq(pLabels).sum().item()
def train(model, device, train_loader, optimizer, criterion, train_acc, train_losses):
model.train()
pbar = tqdm(train_loader)
train_loss = 0
correct = 0
processed = 0
for batch_idx, (data, target) in enumerate(pbar):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
# Predict
pred = model(data)
# Calculate loss
loss = criterion(pred, target)
train_loss += loss.item()
# Backpropagation
loss.backward()
optimizer.step()
correct += GetCorrectPredCount(pred, target)
processed += len(data)
pbar.set_description(desc=f'Train: Loss={loss.item():0.4f} Batch_id={batch_idx} Accuracy={100 * correct / processed:0.2f}')
train_acc.append(100 * correct / processed)
train_losses.append(train_loss / len(train_loader))
# Print accuracy after each epoch
print('Train set: Accuracy: {:.2f}%'.format(train_acc[-1]))
return train_acc, train_losses
def test(model, device, test_loader, criterion, test_acc, test_losses):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for batch_idx, (data, target) in enumerate(test_loader):
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += criterion(output, target).item() # sum up batch loss
correct += GetCorrectPredCount(output, target)
test_loss /= len(test_loader.dataset)
test_acc.append(100. * correct / len(test_loader.dataset))
test_losses.append(test_loss)
# Print accuracy after each epoch
print('Test set: Accuracy: {:.2f}%'.format(test_acc[-1]))
return test_acc, test_losses