From 2d9851f9b0ea64e92cfef332d810e865f143beec Mon Sep 17 00:00:00 2001 From: Abdullah Selek Date: Fri, 28 Jun 2019 13:13:52 +0100 Subject: [PATCH] Create an internal undeprecated function to check checkpoint exists. --- .../python/training/checkpoint_management.py | 33 ++++++++++++++----- tensorflow/python/training/saver.py | 2 +- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/tensorflow/python/training/checkpoint_management.py b/tensorflow/python/training/checkpoint_management.py index 32b9c023aae..c18e1395b45 100644 --- a/tensorflow/python/training/checkpoint_management.py +++ b/tensorflow/python/training/checkpoint_management.py @@ -347,6 +347,30 @@ def latest_checkpoint(checkpoint_dir, latest_filename=None): return None +def checkpoint_exists_internal(checkpoint_prefix): + """Checks whether a V1 or V2 checkpoint exists with the specified prefix. + + This is an internal function to check if a checkpoint exists, + since it takes into account the naming difference between V1 and V2 formats. + + Args: + checkpoint_prefix: the prefix of a V1 or V2 checkpoint, with V2 taking + priority. Typically the result of `Saver.save()` or that of + `tf.train.latest_checkpoint()`, regardless of sharded/non-sharded or + V1/V2. + Returns: + A bool, true iff a checkpoint referred to by `checkpoint_prefix` exists. + """ + pathname = _prefix_to_checkpoint_path(checkpoint_prefix, + saver_pb2.SaverDef.V2) + if file_io.get_matching_files(pathname): + return True + elif file_io.get_matching_files(checkpoint_prefix): + return True + else: + return False + + @deprecation.deprecated( date=None, instructions="Use standard file APIs to check for files with this prefix.") @@ -365,14 +389,7 @@ def checkpoint_exists(checkpoint_prefix): Returns: A bool, true iff a checkpoint referred to by `checkpoint_prefix` exists. """ - pathname = _prefix_to_checkpoint_path(checkpoint_prefix, - saver_pb2.SaverDef.V2) - if file_io.get_matching_files(pathname): - return True - elif file_io.get_matching_files(checkpoint_prefix): - return True - else: - return False + return checkpoint_exists_internal(checkpoint_exists) @deprecation.deprecated( diff --git a/tensorflow/python/training/saver.py b/tensorflow/python/training/saver.py index 7b502bffa38..4a30e667be9 100644 --- a/tensorflow/python/training/saver.py +++ b/tensorflow/python/training/saver.py @@ -1276,7 +1276,7 @@ class Saver(object): if save_path is None: raise ValueError("Can't load save_path when it is None.") - if not checkpoint_management.checkpoint_exists(compat.as_text(save_path)): + if not checkpoint_management.checkpoint_exists_internal(compat.as_text(save_path)): raise ValueError("The passed save_path is not a valid checkpoint: " + compat.as_text(save_path))