質問するログイン新規登録
Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

2回答

194閲覧

Keras転移学習モデルの前処理部分を取得したい

meg_

総合スコア10995

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2025/07/14 15:26

編集2025/07/14 15:40

0

0

実現したいこと

書籍(Deep Learning with Python, Third Edition(MEAP))に記載の転移学習に関するコードをエラーなく実行したいです。

発生している問題・分からないこと

エラーが発生しました。

エラーメッセージ

error

1--------------------------------------------------------------------------- 2ImportError Traceback (most recent call last) 3File c:\Users\XXX\anaconda3\envs\keras3\Lib\site-packages\keras\src\ops\operation.py:234, in Operation.from_config(cls, config) 4 233 try: 5--> 234 return cls(**config) 6 235 except Exception as e: 7 8File c:\Users\XXX\anaconda3\envs\keras3\Lib\site-packages\keras_hub\src\layers\preprocessing\image_converter.py:216, in ImageConverter.__init__(self, image_size, scale, offset, crop_to_aspect_ratio, pad_to_aspect_ratio, interpolation, antialias, bounding_box_format, data_format, **kwargs) 9 214 offset = [-m / s for m, s in zip(kwargs.pop("mean"), std)] 10--> 216 super().__init__(**kwargs) 11 218 if crop_to_aspect_ratio and pad_to_aspect_ratio: 12 13File c:\Users\XXX\anaconda3\envs\keras3\Lib\site-packages\keras_hub\src\layers\preprocessing\preprocessing_layer.py:10, in PreprocessingLayer.__init__(self, **kwargs) 14 9 def __init__(self, **kwargs): 15---> 10 assert_tf_libs_installed(self.__class__.__name__) 16 11 super().__init__(**kwargs) 17 18File c:\Users\XXX\anaconda3\envs\keras3\Lib\site-packages\keras_hub\src\utils\tensor_utils.py:262, in assert_tf_libs_installed(symbol_name) 19 261 if tf_text is None or tf is None: 20--> 262 raise ImportError( 21 263 f"{symbol_name} requires `tensorflow` and `tensorflow-text` for " 22 264 "text processing. Run `pip install tensorflow-text` to install " 23 265 "both packages or visit https://www.tensorflow.org/install\n\n" 24 266 "If `tensorflow-text` is already installed, try importing it " 25 267 "in a clean python session. Your installation may have errors.\n\n" 26 268 "KerasHub uses `tf.data` and `tensorflow-text` to preprocess text " 27 269 "on all Keras backends. If you are running on Jax or Torch, this " 28 270 "installation does not need GPU support." 29 271 ) 30 31ImportError: ImageConverter requires `tensorflow` and `tensorflow-text` for text processing. Run `pip install tensorflow-text` to install both packages or visit https://www.tensorflow.org/install 32 33If `tensorflow-text` is already installed, try importing it in a clean python session. Your installation may have errors. 34 35KerasHub uses `tf.data` and `tensorflow-text` to preprocess text on all Keras backends. If you are running on Jax or Torch, this installation does not need GPU support. 36 37During handling of the above exception, another exception occurred: 38 39TypeError Traceback (most recent call last) 40File c:\Users\XXX\anaconda3\envs\keras3\Lib\site-packages\keras\src\saving\serialization_lib.py:718, in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs) 41 717 try: 42--> 718 instance = cls.from_config(inner_config) 43 719 except TypeError as e: 44 45File c:\Users\XXX\anaconda3\envs\keras3\Lib\site-packages\keras\src\ops\operation.py:236, in Operation.from_config(cls, config) 46 235 except Exception as e: 47--> 236 raise TypeError( 48 237 f"Error when deserializing class '{cls.__name__}' using " 49 238 f"config={config}.\n\nException encountered: {e}" 50 239 ) 51 52TypeError: Error when deserializing class 'ImageConverter' using config={'name': 'image_converter', 'trainable': True, 'dtype': 'float32', 'image_size': (180, 180), 'scale': 0.00784313725490196, 'offset': -1.0, 'interpolation': 'bilinear', 'antialias': False, 'crop_to_aspect_ratio': True, 'pad_to_aspect_ratio': False, 'bounding_box_format': 'yxyx'}. 53 54Exception encountered: ImageConverter requires `tensorflow` and `tensorflow-text` for text processing. Run `pip install tensorflow-text` to install both packages or visit https://www.tensorflow.org/install 55 56If `tensorflow-text` is already installed, try importing it in a clean python session. Your installation may have errors. 57 58KerasHub uses `tf.data` and `tensorflow-text` to preprocess text on all Keras backends. If you are running on Jax or Torch, this installation does not need GPU support. 59 60During handling of the above exception, another exception occurred: 61 62TypeError Traceback (most recent call last) 63Cell In[27], line 1 64----> 1 preprocessor = keras_hub.layers.ImageConverter.from_preset('xception_41_imagenet', image_size=(180, 180)) 65 66File c:\Users\XXX\anaconda3\envs\keras3\Lib\site-packages\keras_hub\src\layers\preprocessing\image_converter.py:386, in ImageConverter.from_preset(cls, preset, **kwargs) 67 384 if cls.backbone_cls != backbone_cls: 68 385 cls = find_subclass(preset, cls, backbone_cls) 69--> 386 return loader.load_image_converter(cls, **kwargs) 70 71File c:\Users\XXX\anaconda3\envs\keras3\Lib\site-packages\keras_hub\src\utils\preset_utils.py:669, in KerasPresetLoader.load_image_converter(self, cls, **kwargs) 72 667 def load_image_converter(self, cls, **kwargs): 73 668 converter_config = load_json(self.preset, IMAGE_CONVERTER_CONFIG_FILE) 74--> 669 return self._load_serialized_object(converter_config, **kwargs) 75 76File c:\Users\XXX\anaconda3\envs\keras3\Lib\site-packages\keras_hub\src\utils\preset_utils.py:729, in KerasPresetLoader._load_serialized_object(self, config, **kwargs) 77 726 config = set_dtype_in_config(config, dtype) 78 728 config["config"] = {**config["config"], **kwargs} 79--> 729 return keras.saving.deserialize_keras_object(config) 80 81File c:\Users\XXX\anaconda3\envs\keras3\Lib\site-packages\keras\src\saving\serialization_lib.py:720, in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs) 82 718 instance = cls.from_config(inner_config) 83 719 except TypeError as e: 84--> 720 raise TypeError( 85 721 f"{cls} could not be deserialized properly. Please" 86 722 " ensure that components that are Python object" 87 723 " instances (layers, models, etc.) returned by" 88 724 " `get_config()` are explicitly deserialized in the" 89 725 " model's `from_config()` method." 90 726 f"\n\nconfig={config}.\n\nException encountered: {e}" 91 727 ) 92 728 build_config = config.get("build_config", None) 93 729 if build_config and not instance.built: 94 95TypeError: <class 'keras_hub.src.layers.preprocessing.image_converter.ImageConverter'> could not be deserialized properly. Please ensure that components that are Python object instances (layers, models, etc.) returned by `get_config()` are explicitly deserialized in the model's `from_config()` method. 96 97config={'module': 'keras_hub.src.layers.preprocessing.image_converter', 'class_name': 'ImageConverter', 'config': {'name': 'image_converter', 'trainable': True, 'dtype': {'module': 'keras', 'class_name': 'DTypePolicy', 'config': {'name': 'float32'}, 'registered_name': None}, 'image_size': (180, 180), 'scale': 0.00784313725490196, 'offset': -1.0, 'interpolation': 'bilinear', 'antialias': False, 'crop_to_aspect_ratio': True, 'pad_to_aspect_ratio': False, 'bounding_box_format': 'yxyx'}, 'registered_name': 'keras_hub>ImageConverter'}. 98 99Exception encountered: Error when deserializing class 'ImageConverter' using config={'name': 'image_converter', 'trainable': True, 'dtype': 'float32', 'image_size': (180, 180), 'scale': 0.00784313725490196, 'offset': -1.0, 'interpolation': 'bilinear', 'antialias': False, 'crop_to_aspect_ratio': True, 'pad_to_aspect_ratio': False, 'bounding_box_format': 'yxyx'}. 100 101Exception encountered: ImageConverter requires `tensorflow` and `tensorflow-text` for text processing. Run `pip install tensorflow-text` to install both packages or visit https://www.tensorflow.org/install 102 103If `tensorflow-text` is already installed, try importing it in a clean python session. Your installation may have errors. 104 105KerasHub uses `tf.data` and `tensorflow-text` to preprocess text on all Keras backends. If you are running on Jax or Torch, this installation does not need GPU support.

該当のソースコード

Python

1preprocessor = keras_hub.layers.ImageConverter.from_preset('xception_41_imagenet', image_size=(180, 180))

試したこと・調べたこと

  • teratailやGoogle等で検索した
  • ソースコードを自分なりに変更した
  • 知人に聞いた
  • その他
上記の詳細・結果

pip install tensorflow-text
上記を実行すると下記エラーとなりインストールできませんでした。
ERROR: Could not find a version that satisfies the requirement tensorflow-text (from versions: none)
ERROR: No matching distribution found for tensorflow-text

ネット検索の結果、Version 2.10 や 2.5 が良いとあったので試してみたが同じエラーが出ました。(最新バージョンは2.19.0)

補足

Windows 11
Python 3.12.8
keras 3.7.0
keras-hub 0.21.1
tensorflow 2.18.0

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

おそらくインストールできるバージョンが無いのだと思います。
tensorflowのバージョンが2.18.0なので、tensorflow-textも2.18.0をインストールするような挙動になると思いますが、2.18.0はこちらを確認したところ、Python3.12には対応していないみたいです。
従って、tensorflowのバージョンを上げるか(この場合2.19.0になる)、Pythonのバージョンを下げるかのどちらかをすればインストール出来るようになると思います。

投稿2025/07/14 23:31

fiveHundred

総合スコア10466

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

meg_

2025/07/15 00:01

確かにバージョンを合わせる必要があるようですね。 https://github.com/tensorflow/text どう合わせるか考えます。
meg_

2025/07/15 01:37

下記組み合わせで試したのですが同じエラーが発生してしまいました。 Python 3.11.13 tensorflow 2.18.0 pip install tensorflow-text==2.18.0
fiveHundred

2025/07/15 02:41

すみません、色々試したところ、 Python 3.10.18 tensorflow 2.10.1 tensorflow-text 2.10.0 という組み合わせなら、インストールできました。 新しいバージョンで上手くいかない点は不明ですが、以下のページに、2.10でWindowsネイティブのGPUサポートを打ち切った旨が書かれていたので、その影響があるのかもしれません。 https://www.tensorflow.org/install/pip?hl=ja#windows-native (とはいえ、GPU使わない場合でも上手くいかないのは謎ですが)
meg_

2025/07/15 03:16

Windows11では難しいことが分かりました。Keras3はTensorFlow 2.15以降が必要なようです。(デフォルトではおそらくTensorFlow 2.16) https://keras.io/getting_started/ WSLかGoogleColabなどの使用を検討します。 色々と試していただきありがとうございました。大変参考になりました。
guest

0

GoogleColaboratoryで下記バージョンの組み合わせにすることでエラーなく該当のコードを実行することができました。デフォルトの組み合わせでは上手く行きませんでした。('xception_41_imagenet'が見つからないとのエラーが出ました)

【実行できた組み合わせ】
Python 3.11.13(デフォルト)
tensorflow-text 2.18.1(デフォルト)
keras 3.7.0
tensorflow 2.18.0
keras-nlp 0.21.1
keras-hub 0.21.1

投稿2025/07/15 05:21

編集2025/07/15 05:29
meg_

総合スコア10995

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.30%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問