forked from csho33/bacteria-ID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining.py
56 lines (51 loc) · 1.82 KB
/
training.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
from torch.autograd import Variable
from torch import nn
import torch
import numpy as np
def run_epoch(epoch, model, dataloader, cuda, training=False, optimizer=None):
if training:
model.train()
else:
model.eval()
total_loss = 0
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(dataloader):
if cuda: inputs, targets = inputs.cuda(), targets.cuda()
inputs, targets = Variable(inputs), Variable(targets.long())
outputs = model(inputs)
loss = nn.CrossEntropyLoss()(outputs, targets)
if training:
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
if cuda:
correct += predicted.eq(targets.data).cpu().sum().item()
else:
correct += predicted.eq(targets.data).sum().item()
acc = 100 * correct / total
avg_loss = total_loss / total
return acc, avg_loss
def get_predictions(model, dataloader, cuda, get_probs=False):
preds = []
model.eval()
for batch_idx, (inputs, targets) in enumerate(dataloader):
if cuda: inputs, targets = inputs.cuda(), targets.cuda()
inputs, targets = Variable(inputs), Variable(targets.long())
outputs = model(inputs)
if get_probs:
probs = torch.nn.functional.softmax(outputs, dim=1)
if cuda: probs = probs.data.cpu().numpy()
else: probs = probs.data.numpy()
preds.append(probs)
else:
_, predicted = torch.max(outputs.data, 1)
if cuda: predicted = predicted.cpu()
preds += list(predicted.numpy().ravel())
if get_probs:
return np.vstack(preds)
else:
return np.array(preds)