def main(unused_args):
    # Load model configuration
    cu = CommonUtiler()
    config_path = os.path.join('./model_conf', FLAGS.model_name + '.py')
    config = cu.load_config(config_path)

    # Evaluate trained models on val
    decoder = mRNNDecoder(config,
                          FLAGS.model_name,
                          FLAGS.vocab_path,
                          gpu_memory_fraction=FLAGS.gpu_memory_fraction)
    for i in xrange(*[int(x) for x in FLAGS.eval_stat.split()]):
        model_path = os.path.join(FLAGS.model_root, FLAGS.model_name,
                                  'variables', 'model_%d.ckpt' % i)
        while not os.path.exists(model_path):
            logger.warn('Cannot load model file, sleep 1 hour to retry')
            time.sleep(3600)

        decoder.load_model(model_path)

        num_decode = 0
        pred_sentences = []
        for anno_file_path in FLAGS.anno_files_path.split(':'):
            annos = np.load(anno_file_path).tolist()
            for anno in annos:
                feat_path = os.path.join(
                    FLAGS.vf_dir, anno['file_path'],
                    anno['file_name'].split('.')[0] + '.txt')
                visual_features = np.loadtxt(feat_path)
                sentences = decoder.decode(visual_features, FLAGS.beam_size)

                sentence_coco = {}
                sentence_coco['image_id'] = anno['id']
                sentence_coco['caption'] = ' '.join(sentences[0]['words'])
                pred_sentences.append(sentence_coco)
                num_decode += 1

                if num_decode % 100 == 0:
                    logger.info('%d images are decoded' % num_decode)

        pred_path = os.path.join(FLAGS.model_root, FLAGS.model_name,
                                 'decode_val_result', 'generated_%d.json' % i)
        result_path = os.path.join(FLAGS.model_root, FLAGS.model_name,
                                   'decode_val_result', 'result_%d.txt' % i)
        cu.create_dir_if_not_exists(os.path.dirname(pred_path))
        with open(pred_path, 'w') as fout:
            json.dump(pred_sentences, fout)
        cu.coco_val_eval(pred_path, result_path)
def main(unused_args):
  # Load model configuration
  cu = CommonUtiler()
  config_path = os.path.join('./model_conf', FLAGS.model_name + '.py')
  config = cu.load_config(config_path)
      
  # Evaluate trained models on val
  decoder = mRNNDecoder(config, FLAGS.model_name, FLAGS.vocab_path,
      gpu_memory_fraction=FLAGS.gpu_memory_fraction)
  for i in xrange(*[int(x) for x in FLAGS.eval_stat.split()]):
    model_path = os.path.join(FLAGS.model_root, FLAGS.model_name, 
        'variables', 'model_%d.ckpt' % i)
    while not os.path.exists(model_path):
      logger.warn('Cannot load model file, sleep 1 hour to retry')
      time.sleep(3600)
    
    decoder.load_model(model_path)
    
    num_decode = 0
    pred_sentences = []
    for anno_file_path in FLAGS.anno_files_path.split(':'):
      annos = np.load(anno_file_path).tolist()
      for anno in annos:
        feat_path = os.path.join(FLAGS.vf_dir, anno['file_path'],
            anno['file_name'].split('.')[0] + '.txt')
        visual_features = np.loadtxt(feat_path)
        sentences = decoder.decode(visual_features, FLAGS.beam_size)
        
        sentence_coco = {}
        sentence_coco['image_id'] = anno['id']
        sentence_coco['caption'] = ' '.join(sentences[0]['words'])
        pred_sentences.append(sentence_coco)
        num_decode += 1
        
        if num_decode % 100 == 0:
          logger.info('%d images are decoded' % num_decode)
          
    pred_path = os.path.join(FLAGS.model_root, FLAGS.model_name, 
        'decode_val_result', 'generated_%d.json' % i)
    result_path = os.path.join(FLAGS.model_root, FLAGS.model_name, 
        'decode_val_result', 'result_%d.txt' % i)
    cu.create_dir_if_not_exists(os.path.dirname(pred_path))
    with open(pred_path, 'w') as fout:
      json.dump(pred_sentences, fout)
    cu.coco_val_eval(pred_path, result_path)