text: # -----------------------------------------------------------
  # Stacked Cross Attention Network implementation based on 
  # https://arxiv.org/abs/1803.08024.
  # "Stacked Cross Attention for Image-Text Matching"
  # Kuang-Huei Lee, Xi Chen, Gang Hua, Houdong Hu, Xiaodong He
  #
  # Writen by Kuang-Huei Lee, 2018
  # ---------------------------------------------------------------
  """Training script"""
  
  import os
  import time
  import shutil
  
  import torch
  import numpy
  
  import data
  from vocab import Vocabulary, deserialize_vocab
  from model import SCAN
  from evaluation import i2t, t2i, AverageMeter, LogCollector, encode_data, shard_xattn_t2i, shard_xattn_i2t
  from torch.autograd import Variable
  
  import logging
  import tensorboard_logger as tb_logger
  
  import argparse
  
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  
  
  def main():
      # Hyper Parameters
      parser = argparse.ArgumentParser()
      dataset = 'cub'  # deep_fashion, cub 'f30k' or 'coco'
      exp_type = 'mlf'  # scan or mlf
      parser.add_argument('--data_path', default='/ivi/ilps/personal/mbiriuk/repro/data',
                          help='path to datasets')
      parser.add_argument('--data_name', default=f'{dataset}_{exp_type}_precomp',
                          help='{coco,f30k, cub}_precomp')
      parser.add_argument('--vocab_path', default='./vocab/',
                          help='Path to saved vocabulary json files.')
      parser.add_argument('--margin', default=0.2, type=float,
                          help='Rank loss margin.')
      parser.add_argument('--num_epochs', default=15, type=int,
                          help='Number of training epochs.')
      parser.add_argument('--batch_size', default=128, type=int,
                          help='Size of a training mini-batch.')
      parser.add_argument('--word_dim', default=300, type=int,
                          help='Dimensionality of the word embedding.')
      parser.add_argument('--embed_size', default=1024, type=int,
                          help='Dimensionality of the joint embedding.')
      parser.add_argument('--grad_clip', default=2., type=float,
                          help='Gradient clipping threshold.')
      parser.add_argument('--num_layers', default=1, type=int,
                          help='Number of GRU layers.')
      parser.add_argument('--learning_rate', default=.0002, type=float,
                          help='Initial learning rate.')
      parser.add_argument('--lr_update', default=15, type=int,
                          help='Number of epochs to update the learning rate.')
      parser.add_argument('--workers', default=10, type=int,
                          help='Number of data loader workers.')
      parser.add_argument('--log_step', default=10, type=int,
                          help='Number of steps to print and record the log.')
      parser.add_argument('--val_step', default=500, type=int,
                          help='Number of steps to run validation.')
      parser.add_argument('--logger_name', default=f'./runs/{dataset}_{exp_type}/log',
                          help='Path to save Tensorboard log.')
      parser.add_argument('--model_name', default=f'./runs/{dataset}_{exp_type}/log',
                          help='Path to save the model.')
      parser.add_argument('--resume'
                          , default=''
                          # , default='/Users/mhendriksen/Desktop/repositories/SCAN/runs/f30k_scan/checkpoint_9.pth.tar'
                          , type=str, metavar='PATH',
                          help='path to latest checkpoint (default: none)')
      parser.add_argument('--max_violation', action='store_true', default='bi_gru',
                          help='Use max instead of sum in the rank loss.')
      parser.add_argument('--img_dim', default=2048, type=int,
                          help='Dimensionality of the image embedding.')
      parser.add_argument('--no_imgnorm', action='store_true',
                          help='Do not normalize the image embeddings.')
      parser.add_argument('--no_txtnorm', action='store_true',
                          help='Do not normalize the text embeddings.')
      parser.add_argument('--raw_feature_norm', default="clipped_l2norm",
                          help='clipped_l2norm|l2norm|clipped_l1norm|l1norm|no_norm|softmax')
      parser.add_argument('--agg_func', default="LogSumExp",
                          help='LogSumExp|Mean|Max|Sum')
      parser.add_argument('--cross_attn', default="t2i",
                          help='t2i|i2t')
      parser.add_argument('--precomp_enc_type', default="basic",
                          help='basic|weight_norm')
      parser.add_argument('--bi_gru', action='store_true',
                          help='Use bidirectional GRU.')
      parser.add_argument('--lambda_lse', default=6., type=float,
                          help='LogSumExp temp.')
      parser.add_argument('--lambda_softmax', default=9., type=float,
                          help='Attention softmax temperature.')
      opt = parser.parse_args()
      logging.info(opt)
  
      logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
      tb_logger.configure(opt.logger_name, flush_secs=5)
  
      # Load Vocabulary Wrapper
      vocab = deserialize_vocab(os.path.join(opt.vocab_path, '%s_vocab.json' % opt.data_name))
      opt.vocab_size = len(vocab)
  
      # Load data loaders
      # Load data loaders
      train_loader, val_loader = data.get_loaders(
          opt.data_name, vocab, opt.batch_size, opt.workers, opt)
  
      # Construct the model
      model = SCAN(opt)
  
      best_rsum = 0
      start_epoch = 0
      # optionally resume from a checkpoint
      if opt.resume:
          if os.path.isfile(opt.resume):
              print("=> loading checkpoint '{}'".format(opt.resume))
              checkpoint = torch.load(opt.resume, map_location=device)
              start_epoch = checkpoint['epoch'] + 1
              best_rsum = checkpoint['best_rsum']
              model.load_state_dict(checkpoint['model'])
              # Eiters is used to show logs as the continuation of another
              # training
              model.Eiters = checkpoint['Eiters']
              print("=> loaded checkpoint '{}' (epoch {}, best_rsum {})"
                    .format(opt.resume, start_epoch, best_rsum))
              validate(opt, val_loader, model)
          else:
              print("=> no checkpoint found at '{}'".format(opt.resume))
  
      # Train the Model
      for epoch in range(start_epoch, opt.num_epochs):
          print(opt.logger_name)
          print(opt.model_name)
  
          adjust_learning_rate(opt, model.optimizer, epoch)
  
          # train for one epoch
          train(opt, train_loader, model, epoch, val_loader)
  
          # evaluate on validation set
          rsum = validate(opt, val_loader, model)
  
          # remember best R@ sum and save checkpoint
          is_best = rsum > best_rsum
          best_rsum = max(rsum, best_rsum)
          if not os.path.exists(opt.model_name):
              os.mkdir(opt.model_name)
          save_checkpoint({
              'epoch': epoch,
              'model': model.state_dict(),
              'best_rsum': best_rsum,
              'opt': opt,
              'Eiters': model.Eiters,
          }, is_best, filename='checkpoint_{}.pth.tar'.format(epoch), prefix=opt.model_name + '/')
  
  
  def train(opt, train_loader, model, epoch, val_loader):
      # average meters to record the training statistics
      batch_time = AverageMeter()
      data_time = AverageMeter()
      train_logger = LogCollector()
  
      end = time.time()
      for i, train_data in enumerate(train_loader):
          # switch to train mode
          model.train_start()
  
          # measure data loading time
          data_time.update(time.time() - end)
  
          # make sure train logger is used
          model.logger = train_logger
  
          # Update the model
          model.train_emb(*train_data)
  
          # measure elapsed time
          batch_time.update(time.time() - end)
          end = time.time()
  
          # Print log info
          if model.Eiters % opt.log_step == 0:
              logging.info(
                  'Epoch: [{0}][{1}/{2}]\t'
                  '{e_log}\t'
                  'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                  'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
                      .format(
                      epoch, i, len(train_loader), batch_time=batch_time,
                      data_time=data_time, e_log=str(model.logger)))
  
          # Record logs in tensorboard
          tb_logger.log_value('epoch', epoch, step=model.Eiters)
          tb_logger.log_value('step', i, step=model.Eiters)
          tb_logger.log_value('batch_time', batch_time.val, step=model.Eiters)
          tb_logger.log_value('data_time', data_time.val, step=model.Eiters)
          model.logger.tb_log(tb_logger, step=model.Eiters)
  
          # validate at every val_step
          if model.Eiters % opt.val_step == 0:
              validate(opt, val_loader, model)
  
  
  def validate(opt, val_loader, model):
      # compute the encoding for all the validation images and captions
      img_embs, cap_embs, cap_lens = encode_data(
          model, val_loader, opt.log_step, logging.info)
  
      img_embs = numpy.array([img_embs[i] for i in range(0, len(img_embs), 5)])
  
      start = time.time()
      if opt.cross_attn == 't2i':
          sims = shard_xattn_t2i(img_embs, cap_embs, cap_lens, opt, shard_size=128)
      elif opt.cross_attn == 'i2t':
          sims = shard_xattn_i2t(img_embs, cap_embs, cap_lens, opt, shard_size=128)
      else:
          raise NotImplementedError
      end = time.time()
      print("calculate similarity time:", end - start)
  
      # caption retrieval
      (r1, r5, r10, medr, meanr) = i2t(img_embs, cap_embs, cap_lens, sims)
      logging.info("Image to text: %.1f, %.1f, %.1f, %.1f, %.1f" %
                   (r1, r5, r10, medr, meanr))
      # image retrieval
      (r1i, r5i, r10i, medri, meanr) = t2i(
          img_embs, cap_embs, cap_lens, sims)
      logging.info("Text to image: %.1f, %.1f, %.1f, %.1f, %.1f" %
                   (r1i, r5i, r10i, medri, meanr))
      # sum of recalls to be used for early stopping
      currscore = r1 + r5 + r10 + r1i + r5i + r10i
  
      # record metrics in tensorboard
      tb_logger.log_value('r1', r1, step=model.Eiters)
      tb_logger.log_value('r5', r5, step=model.Eiters)
      tb_logger.log_value('r10', r10, step=model.Eiters)
      tb_logger.log_value('medr', medr, step=model.Eiters)
      tb_logger.log_value('meanr', meanr, step=model.Eiters)
      tb_logger.log_value('r1i', r1i, step=model.Eiters)
      tb_logger.log_value('r5i', r5i, step=model.Eiters)
      tb_logger.log_value('r10i', r10i, step=model.Eiters)
      tb_logger.log_value('medri', medri, step=model.Eiters)
      tb_logger.log_value('meanr', meanr, step=model.Eiters)
      tb_logger.log_value('rsum', currscore, step=model.Eiters)
  
      return currscore
  
  
  def save_checkpoint(state, is_best, filename='checkpoint.pth.tar', prefix=''):
      tries = 15
      error = None
  
      # deal with unstable I/O. Usually not necessary.
      while tries:
          try:
              torch.save(state, prefix + filename)
              if is_best:
                  shutil.copyfile(prefix + filename, prefix + 'model_best.pth.tar')
          except IOError as e:
              error = e
              tries -= 1
          else:
              break
          print('model save {} failed, remaining {} trials'.format(filename, tries))
          if not tries:
              raise error
  
  
  def adjust_learning_rate(opt, optimizer, epoch):
      """Sets the learning rate to the initial LR
         decayed by 10 every 30 epochs"""
      lr = opt.learning_rate * (0.1 ** (epoch // opt.lr_update))
      for param_group in optimizer.param_groups:
          param_group['lr'] = lr
  
  
  def accuracy(output, target, topk=(1,)):
      """Computes the precision@k for the specified values of k"""
      maxk = max(topk)
      batch_size = target.size(0)
  
      _, pred = output.topk(maxk, 1, True, True)
      pred = pred.t()
      correct = pred.eq(target.view(1, -1).expand_as(pred))
  
      res = []
      for k in topk:
          correct_k = correct[:k].view(-1).float().sum(0)
          res.append(correct_k.mul_(100.0 / batch_size))
      return res
  
  
  if __name__ == '__main__':
      main()
  