Skip to content

Commit

Permalink
Skip zero-length audio in embedding colab notebook.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 676521874
  • Loading branch information
sdenton4 authored and copybara-github committed Sep 19, 2024
1 parent 275e680 commit 6bb56fa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 10 additions & 5 deletions chirp/inference/embed_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ def _log_exception(self, source_info, exception, counter_name):
exception,
)

def validate_audio(self, source_info: SourceInfo, audio: np.ndarray) -> bool:
if audio is None or audio.shape[0] == 0:
self._log_exception(source_info, 'no_exception', 'audio_empty')
return False
if audio.shape[0] < self.min_audio_s * self.target_sample_rate:
self._log_exception(source_info, 'no_exception', 'audio_too_short')
return False
return True

def audio_to_example(
self, file_id: str, timestamp_offset_s: float, audio: np.ndarray
) -> tf.train.Example:
Expand Down Expand Up @@ -345,11 +354,7 @@ def process(self, source_info: SourceInfo, crop_s: float = -1.0):
self._log_exception(source_info, inst, 'audio_runtime_error')
return

if audio is None or audio.shape[0] == 0:
self._log_exception(source_info, 'no_exception', 'audio_empty')
return
if audio.shape[0] < self.min_audio_s * self.target_sample_rate:
self._log_exception(source_info, 'no_exception', 'audio_too_short')
if not self.validate_audio(source_info, audio):
return

logging.info(
Expand Down
2 changes: 2 additions & 0 deletions embed_audio.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@
" output_dir=output_dir, num_files=config.get('tf_record_shards', 1)) as file_writer:\n",
" for source_info, audio in tqdm.tqdm(\n",
" zip(new_source_infos, audio_iterator), total=len(new_source_infos)):\n",
" if not embed_fn.validate_audio(source_info, audio):\n",
" continue\n",
" file_id = source_info.file_id(config.embed_fn_config.file_id_depth)\n",
" offset_s = source_info.shard_num * source_info.shard_len_s\n",
" example = embed_fn.audio_to_example(file_id, offset_s, audio)\n",
Expand Down

0 comments on commit 6bb56fa

Please sign in to comment.