Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

tfds.features.Audio eagerly imports TensorFlow in _AudioDecoder.__init__, breaking TensorFlow-less usage

オープン
#11,233 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
76/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python

調査の方向性

tensorflow_datasets/core/features/audio_feature.py の _AudioDecoder.init から始め、_LazyDecoder.decode_audio が _dtype をどのように使用しているかを追跡します。TensorFlow なしで tfds.features.Audio と tfds.builder("librispeech") を構築できることを確認し、TensorFlow が利用可能な場合には音声デコードでも dtype が引き続き処理されることを確認します。

索引モデルが issue の本文から書いたものです。

説明

Description

TFDS supports TensorFlow-less dataset reading (e.g. via tfds.data_source with JAX, PyTorch, or Grain). However, constructing any dataset containing an audio feature (such as librispeech) or directly instantiating tfds.features.Audio fails with ModuleNotFoundError: No module named 'tensorflow' in an environment without TensorFlow.

Root Cause

In tensorflow_datasets/core/features/audio_feature.py, _AudioDecoder.__init__ eagerly evaluates tf.dtypes.as_dtype:

class _AudioDecoder(abc.ABC):
  def __init__(self, file_format: Optional[str], np_dtype: np.dtype, shape: utils.Shape):
    self._file_format = file_format
    self._np_dtype = np_dtype
    self._dtype = tf.dtypes.as_dtype(self._np_dtype)  # <--- Forces import of lazy tf module
    self._shape = shape
    self._channels = shape[1] if len(shape) > 1 else 1

Because tf is lazily imported via etils.epy.lazy_imports, accessing tf.dtypes triggers import tensorflow immediately during feature specification construction in _info(), even when only reading NumPy arrays.

Minimal Reproduction

In a virtual environment with only tensorflow-datasets installed (no tensorflow):

import tensorflow_datasets as tfds

# Fails with ModuleNotFoundError: No module named 'tensorflow'
tfds.features.Audio(sample_rate=16000)
# Or:
tfds.builder("librispeech")
Minimal Workaround

Monkeypatch _AudioDecoder.__init__ before importing tfds:

import tensorflow_datasets.core.features.audio_feature as af

def _patched_init(self, file_format, np_dtype, shape):
  self._file_format = file_format
  self._np_dtype = np_dtype
  self._dtype = None
  self._shape = shape
  self._channels = shape[1] if len(shape) > 1 else 1

af._AudioDecoder.__init__ = _patched_init

import tensorflow_datasets as tfds
# Now succeeds without TensorFlow installed:
ds = tfds.data_source("librispeech", split="train_clean100")
Suggested Fix

Make _dtype a lazy property or defer tf.dtypes.as_dtype to _LazyDecoder.decode_audio, where TensorFlow tensors are actually consumed:

@property
def _dtype(self):
  return tf.dtypes.as_dtype(self._np_dtype)
主要言語
Python
スター
4.6k
フォーク
1.6k
平均マージ
5時間 47分
マージ済み PR(30日)
2

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

tensorflow/datasets のほかの issue

tensorflow/datasets の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。