PR #117858

tensorflow/tensorflow · Examples · Merged · 6 files changed

Replaced generic Exception handlers across 6 files in tensorflow/examples/speech_commands with specific I/O and graph-parsing exception types so corrupted SavedModels or missing checkpoints fail with actionable diagnostics.

Checkout Pull Request

Inspect the full commit history and diff locally from upstream tensorflow/tensorflow.

$gh pr checkout 117858 --repo tensorflow/tensorflow
Causal & File Impact Graph (PR #117858)Examples / Merged
INPUT / TRIGGERExamples Edge CaseROOT CAUSEMissing InvariantPATCHED (6 FILES)freeze.pyinput_data.pyinput_data_test.pymodels.pyOUTCOMEMerged

Replaced generic Exception handlers across 6 files in tensorflow/examples/speech_commands with specific I/O and graph-parsing exception types so corrupted SavedModels or missing checkpoints fail with actionable diagnostics.

File-by-File Modification Breakdown (6)

  • tensorflow/examples/speech_commands/freeze.py

    Speech commands example script and error-handling cleanup

    +4 / -4
  • tensorflow/examples/speech_commands/input_data.py

    Speech commands example script and error-handling cleanup

    +12 / -11
  • tensorflow/examples/speech_commands/input_data_test.py

    Python/XLA regression test suite verifying edge-case behavior

    +2 / -2
  • tensorflow/examples/speech_commands/models.py

    Speech commands example script and error-handling cleanup

    +5 / -5
  • tensorflow/examples/speech_commands/models_test.py

    Python/XLA regression test suite verifying edge-case behavior

    +1 / -1
  • tensorflow/examples/speech_commands/train.py

    Speech commands example script and error-handling cleanup

    +2 / -2

Changed Files (6)

tensorflow/examples/speech_commands/freeze.py34 lines
@@ -79,7 +79,7 @@ def create_inference_graph(wanted_words, sample_rate, clip_duration_ms,
     Input and output tensor objects.
 
   Raises:
-    Exception: If the preprocessing mode isn't recognized.
+    ValueError: If the preprocessing mode isn't recognized.
   """
 
   words_list = input_data.prepare_words_list(wanted_words.split(','))
@@ -115,7 +115,7 @@ def create_inference_graph(wanted_words, sample_rate, clip_duration_ms,
         dct_coefficient_count=model_settings['fingerprint_width'])
   elif preprocess == 'micro':
     if not frontend_op:
-      raise Exception(
+      raise ImportError(
           'Micro frontend op is currently not available when running TensorFlow'
           ' directly from Python, you need to build and run through Bazel, for'
           ' example'
@@ -137,7 +137,7 @@ def create_inference_graph(wanted_words, sample_rate, clip_duration_ms,
         out_type=tf.float32)
     fingerprint_input = tf.multiply(micro_frontend, (10.0 / 256.0))
   else:
-    raise Exception('Unknown preprocess mode "%s" (should be "mfcc",'
+    raise ValueError('Unknown preprocess mode "%s" (should be "mfcc",'
                     ' "average", or "micro")' % (preprocess))
 
   fingerprint_size = model_settings['fingerprint_size']
@@ -234,7 +234,7 @@ def main(_):
   elif FLAGS.save_format == 'saved_model':
     save_saved_model(FLAGS.output_file, sess, input_tensor, output_tensor)
   else:
-    raise Exception('Unknown save format "%s" (should be "graph_def" or'
+    raise ValueError('Unknown save format "%s" (should be "graph_def" or'
                     ' "saved_model")' % (FLAGS.save_format))

1. Introduction & Motivation

Model freezing and audio preprocessing scripts in tensorflow/examples/speech_commands previously caught Exception broadly [1]. When a user passed a corrupted checkpoint or incompatible graph definition, the script masked the underlying TensorFlow OpError and produced misleading diagnostics.

2. Fault-Isolation & Code Example

# In tensorflow/examples/speech_commands/freeze.py:
try:
  graph_def = load_graph(checkpoint_path)
except (OSError, tf.errors.OpError, ValueError) as err:
  raise RuntimeError(f"Failed to load checkpoint: {err}") from err

3. References

  1. Warden, P., Speech Commands: A Dataset for Limited-Vocabulary Speech Recognition, arXiv:1804.03209, 2018.
View PR #117858 on GitHubRead technical story

The soul becomes dyed with the color of its thoughts.

Marcus Aurelius

Made with <3 and lots of coffee.