def decode_from_dataset(estimator,
                        problem_name,
                        hparams,
                        decode_hp,
                        decode_to_file=None,
                        dataset_split=None,
                        checkpoint_path=None):
  """Perform decoding from dataset."""
  tf.logging.info("Performing local inference from dataset for %s.",
                  str(problem_name))
  # We assume that worker_id corresponds to shard number.
  shard = decode_hp.shard_id if decode_hp.shards > 1 else None

  # Setup output directory for any artifacts that may be written out.
  output_dir = os.path.join(estimator.model_dir, "decode")
  tf.gfile.MakeDirs(output_dir)

  # If decode_hp.batch_size is specified, use a fixed batch size
  if decode_hp.batch_size:
    hparams.batch_size = decode_hp.batch_size
    hparams.use_fixed_batch_size = True

  dataset_kwargs = {
      "shard": shard,
      "dataset_split": dataset_split,
      "max_records": decode_hp.num_samples
  }

  # Build the inference input function
  problem = hparams.problem
  infer_input_fn = problem.make_estimator_input_fn(
      tf.estimator.ModeKeys.PREDICT, hparams, dataset_kwargs=dataset_kwargs)

  predictions, output_dirs = [], []
  for decode_id in range(decode_hp.num_decodes):
    tf.logging.info("Decoding {}".format(decode_id))

    # Create decode directory if not in-memory decoding.
    if not decode_hp.decode_in_memory:
      output_dir = os.path.join(estimator.model_dir, "decode_%05d" % decode_id)
      tf.gfile.MakeDirs(output_dir)
      output_dirs.append(output_dir)

    result = decode_once(estimator,
                         problem_name,
                         hparams,
                         infer_input_fn,
                         decode_hp,
                         decode_to_file,
                         output_dir,
                         log_results=decode_hp.log_results,
                         checkpoint_path=checkpoint_path)

    if decode_hp.decode_in_memory:
      output_dirs = [output_dir]
      predictions.append(result)

  if decode_hp.decode_to_file:
    decode_hp.decode_to_file = _decode_filename(
        decode_hp.decode_to_file, problem_name, decode_hp)

  run_postdecode_hooks(DecodeHookArgs(
      estimator=estimator,
      problem=problem,
      output_dirs=output_dirs,
      hparams=hparams,
      decode_hparams=decode_hp,
      predictions=predictions
  ), dataset_split)
  return predictions
def decode_from_dataset ( estimator , problem_name , hparams , decode_hp , decode_to_file = None , dataset_split = None , checkpoint_path = None ) : tf . logging . info ( "Performing local inference from dataset for %s." , str ( problem_name ) ) shard = decode_hp . shard_id if decode_hp . shards > 1 else None output_dir = os . path . join ( estimator . model_dir , "decode" ) tf . gfile . MakeDirs ( output_dir ) if decode_hp . batch_size : hparams . batch_size = decode_hp . batch_size hparams . use_fixed_batch_size = True dataset_kwargs = { "shard" : shard , "dataset_split" : dataset_split , "max_records" : decode_hp . num_samples } problem = hparams . problem infer_input_fn = problem . make_estimator_input_fn ( tf . estimator . ModeKeys . PREDICT , hparams , dataset_kwargs = dataset_kwargs ) predictions , output_dirs = [ ] , [ ] for decode_id in range ( decode_hp . num_decodes ) : tf . logging . info ( "Decoding {}" . format ( decode_id ) ) if not decode_hp . decode_in_memory : output_dir = os . path . join ( estimator . model_dir , "decode_%05d" % decode_id ) tf . gfile . MakeDirs ( output_dir ) output_dirs . append ( output_dir ) result = decode_once ( estimator , problem_name , hparams , infer_input_fn , decode_hp , decode_to_file , output_dir , log_results = decode_hp . log_results , checkpoint_path = checkpoint_path ) if decode_hp . decode_in_memory : output_dirs = [ output_dir ] predictions . append ( result ) if decode_hp . decode_to_file : decode_hp . decode_to_file = _decode_filename ( decode_hp . decode_to_file , problem_name , decode_hp ) run_postdecode_hooks ( DecodeHookArgs ( estimator = estimator , problem = problem , output_dirs = output_dirs , hparams = hparams , decode_hparams = decode_hp , predictions = predictions ) , dataset_split ) return predictions
Perform decoding from dataset.