-
Notifications
You must be signed in to change notification settings - Fork 5
/
train.py
36 lines (27 loc) · 1.06 KB
/
train.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
import torch
from torch.autograd import Variable
from utils import *
import csv
import os
from pycm import *
def train(epoch, train_loader, net, optim, criterion):
print('train at epoch {}'.format(epoch))
net.train()
losses = AverageMeter()
accuracies = AverageMeter()
for i, (imgs_s1, imgs_s2, labels) in enumerate(train_loader):
imgs_s1 = Variable(imgs_s1.cuda())
imgs_s2 = Variable(imgs_s2.cuda())
labels = Variable(labels.cuda())
logits, _, _ = net(imgs_s1, imgs_s2, is_training=True)
optim.zero_grad()
loss = criterion(logits, labels)
loss.backward()
optim.step()
acc = accuracy(logits, labels)
losses.update(loss.item(), logits.size(0))
accuracies.update(acc, logits.size(0))
if (i%50==0 and i!=0) or i+1==len(train_loader):
print ('Train: Epoch[{}]:{}/{} Loss:{:.4f} Accu:{:.2f}%'.\
format(epoch, i, len(train_loader), float(losses.avg), float(accuracies.avg)*100))
return accuracies.avg