def test(logger, epoch): model.eval() test_loss = 0 meters = logger.reset_meters('val') begin = time.time() for batch_idx, data in enumerate(test_loader): batch_size = data['answer'].size(0) if args.cuda: question, image, target = data['question'].cuda( async=True), data['image'].float().cuda( async=True), data['answer'].cuda(async=True) else: question, image, target = data['question'], data['image'].float( ), data['answer'] question, image, target = Variable(question, volatile=True), Variable( image, volatile=True), Variable(target, volatile=True) # Compute output and loss output = model(question, image) loss = F.nll_loss(output, target).data[0] test_loss += loss.item() # sum up batch loss meters['loss'].update(loss.item(), n=batch_size) acc1, acc5 = utils.accuracy(output.data, target.data, topk=(1, 5)) meters['acc1'].update(acc1[0], n=batch_size) meters['acc5'].update(acc5[0], n=batch_size) tensorboard_writer.add_scalar("Test loss", loss.item(), epoch * len(test_loader) + batch_idx) tensorboard_writer.add_scalar("Test top1 acc", acc1[0], epoch * len(test_loader) + batch_idx) tensorboard_writer.add_scalar("Test top 5 acc ", acc5[0], epoch * len(test_loader) + batch_idx) meters['batch_time'].update(time.time() - begin, n=batch_size) test_loss /= len(test_loader.dataset) print( '\n Test set: Average loss: {:.4f}, Acc@1 {acc1.avg:.3f} Acc@5 {acc5.avg:.3f}' .format(test_loss, acc1=meters['acc1'], acc5=meters['acc5'])) logger.log_meters('val', n=epoch) return meters['acc1'].avg
def train(epoch, logger): begin = time.time() model.train() meters = logger.reset_meters('train') start = time.time() for batch_idx, data in enumerate(train_loader): batch_size = data['question'].size(0) # Measures the data loading time meters['data_time'].update(time.time() - begin, n=batch_size) if args.cuda: question, image, target = data['question'].cuda( ), data['image'].float().cuda(), data['answer'].cuda(async=True) else: question, image, target = data['question'], data['image'].float( ), data['answer'] question, image, target = Variable(question), Variable( image), Variable(target) # Compute output and loss output = model(question, image) if args.cuda: torch.cuda.synchronize() loss = F.nll_loss(output, target) # Log the loss meters['loss'].update(loss.data[0], n=batch_size) # Measure accuracy acc1, acc5 = utils.accuracy(output.data, target.data, topk=(1, 5)) meters['acc1'].update(acc1[0], n=batch_size) meters['acc5'].update(acc5[0], n=batch_size) #print(loss) optimizer.zero_grad() loss.backward() torch.nn.utils.clip_grad_norm(model.parameters(), 5) if args.cuda: torch.cuda.synchronize() # optimizer.step() #if args.cuda: # torch.cuda.synchronize() meters['batch_time'].update(time.time() - begin, n=batch_size) begin = time.time() optimizer.step() if batch_idx % args.log_interval == 0: print("Time since last print : {}".format(time.time() - start)) start = time.time() print('Epoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' 'Acc@1 {acc1.val:.3f} ({acc1.avg:.3f})\t' 'Acc@5 {acc5.val:.3f} ({acc5.avg:.3f})'.format( epoch, batch_idx, len(train_loader), batch_time=meters['batch_time'], data_time=meters['data_time'], loss=meters['loss'], acc1=meters['acc1'], acc5=meters['acc5'])) sys.stdout.flush() logger.log_meters('train', n=epoch)