def val(data_loader, net, criterion): losses = tools.AverageMeter() acc = tools.AverageMeter() net.eval() loop = len(data_loader) for i, (images, labels) in enumerate(data_loader): images = Variable(images.view(-1, 14951)).cuda() labels = Variable(labels).cuda() outputs = net(images) loss = criterion(outputs, labels) prec1, prec1 = tools.Accuracy(outputs, labels, topk=(1, 1)) losses.update(loss.data[0], images.size(0)) acc.update(prec1[0], images.size(0)) #if (i + 1) % 10 == 0: print('Step [%d], Loss: %.4f' % (i + 1, loss.data[0])) print('Accuracy : ', (prec1.cpu().data.numpy()[0])) print('Accuracy (All): ', acc.avg.cpu().data.numpy()[0]) print('Losses (All): ', losses.avg) print() return acc.avg
def val(data_loader, net, criterion, scheduler, epoch): net.train(False) losses = tools.AverageMeter() acc = tools.AverageMeter() net.eval() loop = len(data_loader) for i, (images, labels) in enumerate(data_loader): #images, labels = data_loader[i] # Convert torch tensor to Variable images = Variable(images.view(-1, 14951)).cuda() labels = Variable(labels).cuda() outputs = net(images) loss = criterion(outputs, labels) prec1, prec1 = tools.Accuracy(outputs, labels, topk=(1, 1)) losses.update(loss.data[0], images.size(0)) acc.update(prec1[0], images.size(0)) if (i + 1) % 100 == 0: print('Epoch [%d/%d], Step [%d/%d], Loss: %.4f' % (epoch + 1, num_epochs, i + 1, len(val_dataset) // batch_size, loss.data[0])) print('Accuracy : ', (prec1.cpu().data.numpy()[0])) print('Accuracy (All): ', acc.avg.cpu().data.numpy()[0]) print('Losses (All): ', losses.avg) print() return acc.avg
def train(train_loader, model, criterion, optimizer, epoch): batch_time = tools.AverageMeter() losses = tools.AverageMeter() acc = tools.AverageMeter() end = time.time() for i, (img, label, og_label, key, linenumber) in enumerate(train_loader): target = label.cuda(async=True) img = Variable(img).cuda() label = Variable(label).cuda() output = model(img) loss = criterion(output,label) optimizer.zero_grad() loss.backward() optimizer.step() # measure accuracy and record loss prec1, prec1 = tools.Accuracy(output.data, target, topk=(1, 1)) losses.update(loss.data[0], img.size(0)) acc.update(prec1[0], img.size(0)) # measure elapsed time batch_time.update(time.time() - end) end = time.time() if i % 50 == 0: print('\tEpoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' 'Accuracy {acc.val:.3f} ({acc.avg:.3f})'.format( epoch, i, len(train_loader), batch_time=batch_time, loss=losses, acc=acc)) return acc.avg
def train(data_loader, net, criterion, optimizer, scheduler, epoch): losses = tools.AverageMeter() acc = tools.AverageMeter() scheduler.step() net.train(True) loop = len(data_loader) for i, (images, labels) in enumerate(data_loader): # Convert torch tensor to Variable images = Variable(images.view(-1, 14951)).cuda() labels = Variable(labels).cuda() # Forward + Backward + Optimize optimizer.zero_grad() # zero the gradient buffer outputs = net(images) loss = criterion(outputs, labels) loss.backward() optimizer.step() prec1, prec1 = tools.Accuracy(outputs, labels, topk=(1, 1)) losses.update(loss.data[0], images.size(0)) acc.update(prec1[0], images.size(0)) if (i + 1) % 100 == 0: print('Epoch [%d/%d], Step [%d/%d], Loss: %.4f' % (epoch + 1, num_epochs, i + 1, len(train_dataset) // batch_size, loss.data[0])) print('Accuracy : ', (prec1.cpu().data.numpy()[0])) print('Accuracy (All): ', acc.avg.cpu().data.numpy()[0]) print('Losses (All): ', losses.avg) print()
def val(val_loader, model, criterion): batch_time = tools.AverageMeter() losses = tools.AverageMeter() acc = tools.AverageMeter() end = time.time() model.eval() for i, (img, label) in enumerate(val_loader): target = label.cuda(async=True) img = Variable(img).cuda() label = Variable(label).cuda() output = model(img) loss = criterion(output,label) # measure accuracy and record loss prec1, prec1 = tools.Accuracy(output.data, target, topk=(1, 1)) losses.update(loss.data[0], img.size(0)) acc.update(prec1[0], img.size(0)) # measure elapsed time batch_time.update(time.time() - end) end = time.time() if i % 100 == 0: print('\tVal: [{0}/{1}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' 'Accuracy {acc.val:.3f} ({acc.avg:.3f})'.format( i, len(val_loader), batch_time=batch_time, loss=losses, acc=acc)) print(' * Accuracy {acc.avg:.3f}'.format(acc=acc)) return acc.avg