def validate(args, epoch, model, criterion, test_loader): global global_step logger.info('Test {}'.format(epoch)) loss_meter = AverageMeter() angle_error_meter = AverageMeter() start = time.time() for step, (images, gazes) in enumerate(test_loader): if args.use_cuda: images = images.cuda() gazes = gazes.cuda() with torch.no_grad(): outputs = model(images) loss = criterion(outputs, gazes) angle_error = compute_angle_error(outputs, gazes).mean() num = images.size(0) loss_meter.update(loss.item(), num) angle_error_meter.update(angle_error.item(), num) logger.info('Epoch {} Loss {:.4f} AngleError {:.2f}'.format( epoch, loss_meter.avg, angle_error_meter.avg)) elapsed = time.time() - start logger.info('Elapsed {:.2f}'.format(elapsed)) return angle_error_meter.avg
def test(val_loader, model, criterion, epoch): model.eval() # gaze_model.eval() batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() angle_error_Loss = AverageMeter() # switch to evaluate mode end = time.time() for i, (imFace, gaze) in enumerate(val_loader): # measure data loading time data_time.update(time.time() - end) imFace = imFace.cuda(async=True) gaze = gaze.cuda(async=True) imFace = torch.autograd.Variable(imFace) gaze = torch.autograd.Variable(gaze) with torch.no_grad(): # compute output output = model(imFace) # output = gaze_model(feat) loss = criterion(output, gaze) angle_error = compute_angle_error(gaze, output).mean() losses.update(loss.item(), imFace.size(0)) angle_error_Loss.update(angle_error.item(), imFace.size(0)) # measure elapsed time batch_time.update(time.time() - end) end = time.time() print( 'Epoch (val): [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' 'angle_error_Loss {angle_error_Loss.val: .4f} ({angle_error_Loss.avg: .4f})' .format(epoch, i, len(val_loader), batch_time=batch_time, loss=losses, angle_error_Loss=angle_error_Loss)) return angle_error_Loss.avg
def train(args, epoch, model, optimizer, criterion, train_loader): global global_step logger.info('Train {}'.format(epoch)) model.train() loss_meter = AverageMeter() angle_error_meter = AverageMeter() start = time.time() for step, (images, gazes) in enumerate(train_loader): global_step += 1 if args.use_cuda: images = images.cuda() gazes = gazes.cuda() # zero optimizer's gradient optimizer.zero_grad() outputs = model(images) loss = criterion(outputs, gazes) loss.backward() optimizer.step() angle_error = compute_angle_error(outputs, gazes).mean() num = images.size(0) loss_meter.update(loss.item(), num) angle_error_meter.update(angle_error.item(), num) print("Epoch {} Step {}/{} Loss {:.4f} Angle Error {:.2f}".format( epoch, step, len(train_loader), loss_meter.avg, angle_error_meter.avg)) if step % 10 == 0: logger.info('Epoch {} Step {}/{}\t' 'Loss {:.4f} ({:.4f})\t' 'Angle Error {:.2f} ({:.2f})'.format( epoch, step, len(train_loader), loss_meter.val, loss_meter.avg, angle_error_meter.val, angle_error_meter.avg, )) print("Gaze[0:2]: ", gazes[0:2]) print("Output[0:2]: ", outputs[0:2]) elapsed = time.time() - start logger.info('Elapsed {:.2f}'.format(elapsed))
def train(train_loader, model, criterion, optimizer, epoch, k): batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() angle_error_meter = AverageMeter() # switch to train mode model.train() # gaze_model.train() end = time.time() for i, (imFace, gaze) in enumerate(train_loader): data_time.update(time.time() - end) imFace = imFace.cuda(async=True) gaze = gaze.cuda(async=True) imFace = torch.autograd.Variable(imFace, requires_grad=True) gaze = torch.autograd.Variable(gaze, requires_grad=True) # compute output model.zero_grad() # gaze_model.zero_grad() #Pass the image to VGG-Face and Gaze output model # features = feature_extractor(imFace) output = model(imFace) #compute loss loss = criterion(output, gaze) #computer angle error angle_error = compute_angle_error(gaze, output).mean() losses.update(loss.item(), imFace.size(0)) angle_error_meter.update(angle_error.item(), imFace.size(0)) # compute gradient and do SGD step loss.backward() optimizer.step() # measure elapsed time batch_time.update(time.time() - end) end = time.time() print( 'Subject %d' % (k), 'Epoch (train): [{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' 'Angle_Loss {angle_error_meter.val: .4f} ({angle_error_meter.avg:.4f})' .format(epoch, i, len(train_loader), batch_time=batch_time, data_time=data_time, loss=losses, angle_error_meter=angle_error_meter)) return
prediction = model(input) mse = criterion(prediction, target) predictions[i*test_batch_size:(i+1)*test_batch_size, :] = prediction.cpu().numpy() targets[i*test_batch_size:(i+1)*test_batch_size, :] = target.cpu().numpy() avg_error += mse.item() print("Distance error: {:.4f}".format(avg_error / len(testing_data_loader))) return mse, predictions, targets # run test and visualize error, predictions, targets = validate() angle_errors = np.zeros((len(dataset), 2)) # horizontal and vertical error for i,(predict, target) in enumerate(zip(predictions, targets)): angle_errors[i] = compute_angle_error(predict, target) print('Angle error: ', np.mean(angle_errors)) print('Horizontal angle error: ', np.mean(angle_errors[:,0])) print('Vertical angle error: ', np.mean(angle_errors[:,1])) num_spot = 4 visualize_2d(predictions, targets, num_spot) #visualize_3d(predictions, targets) #draw_error(targets, np.mean(angle_errors, 1)) # def checkpoint(epoch, error): # state = OrderedDict([ # ('state_dict', model.state_dict()), # ('optimizer', optimizer.state_dict()), # ('epoch', epoch), # ('error', error), # ])
time.strftime("%a, %d %b %Y %H:%M:%S ", time.gmtime()))) for epoch in range(args.epochs): for batch_idx, (imgs, gt) in enumerate(train_loader): imgs = imgs.float().cuda() gt = gt.cuda() optimizer.zero_grad() outputs = model(imgs) loss = loss_fn(outputs, gt) loss.backward() optimizer.step() angle_error = utils.compute_angle_error(outputs, gt).mean() if batch_idx % 100 == 0: s = ('Epoch {} Step {}/{} ' 'Loss: {:.4f} ' 'AngleError: {:.2f}'.format( epoch, batch_idx, len(train_loader), loss.item(), angle_error.item(), )) print(s) with open(args.log_path, 'a') as f: f.write('{}\n'.format(s))