Eine HDF5 -Modelldatei kann nicht in TensorFlow / Keras geladen werdenPython

Python-Programme
Anonymous
 Eine HDF5 -Modelldatei kann nicht in TensorFlow / Keras geladen werden

Post by Anonymous »

Ich erhielt eine HDF5 -Modelldatei, die mit TensorFlow / Keras erstellt wurde. Trainingsdaten sind nicht mehr verfügbar.# python3
Python 3.9.23 (main, Sep 30 2025, 00:43:48)
[GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
< /code>
Kehren wir zu dem Problem zurück: Ich kann diese Modelldatei nicht ohne Fehler laden (vergessen Sie die CUDA-bezogenen Nachrichten; Ich habe keine Grafik-Chips auf dem Computer, auf dem ich gerade den Code ausführe): < /p>
< /P.from tensorflow import keras
2025-10-02 19:55:29.871714: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
2025-10-02 19:55:29.872035: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2025-10-02 19:55:29.905906: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2025-10-02 19:55:30.901154: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2025-10-02 19:55:30.901515: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.

import tensorflow as tf
tf.version.VERSION
'2.20.0'
keras.__version__
'3.10.0'

m = tf.keras.models.load_model('/app/my_model.h5')
2025-10-02 19:55:49.998432: E external/local_xla/xla/stream_executor/cuda/cuda_platform.cc:51] failed call to cuInit: INTERNAL: CUDA error: Failed call to cuInit: UNKNOWN ERROR (303)
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/keras/src/ops/operation.py", line 256, in from_config
return cls(**config)
File "/usr/local/lib/python3.9/site-packages/keras/src/layers/convolutional/conv2d_transpose.py", line 115, in __init__
super().__init__(
File "/usr/local/lib/python3.9/site-packages/keras/src/layers/convolutional/base_conv_transpose.py", line 94, in __init__
super().__init__(
File "/usr/local/lib/python3.9/site-packages/keras/src/layers/layer.py", line 291, in __init__
raise ValueError(
ValueError: Unrecognized keyword arguments passed to Conv2DTranspose: {'groups': 1}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.9/site-packages/keras/src/saving/saving_api.py", line 196, in load_model
return legacy_h5_format.load_model_from_hdf5(
File "/usr/local/lib/python3.9/site-packages/keras/src/legacy/saving/legacy_h5_format.py", line 133, in load_model_from_hdf5
model = saving_utils.model_from_config(
File "/usr/local/lib/python3.9/site-packages/keras/src/legacy/saving/saving_utils.py", line 88, in model_from_config
return serialization.deserialize_keras_object(
File "/usr/local/lib/python3.9/site-packages/keras/src/legacy/saving/serialization.py", line 495, in deserialize_keras_object
deserialized_obj = cls.from_config(
File "/usr/local/lib/python3.9/site-packages/keras/src/models/model.py", line 651, in from_config
return functional_from_config(
File "/usr/local/lib/python3.9/site-packages/keras/src/models/functional.py", line 560, in functional_from_config
process_layer(layer_data)
File "/usr/local/lib/python3.9/site-packages/keras/src/models/functional.py", line 523, in process_layer
layer = saving_utils.model_from_config(
File "/usr/local/lib/python3.9/site-packages/keras/src/legacy/saving/saving_utils.py", line 88, in model_from_config
return serialization.deserialize_keras_object(
File "/usr/local/lib/python3.9/site-packages/keras/src/legacy/saving/serialization.py", line 504, in deserialize_keras_object
deserialized_obj = cls.from_config(cls_config)
File "/usr/local/lib/python3.9/site-packages/keras/src/ops/operation.py", line 258, in from_config
raise TypeError(
TypeError: Error when deserializing class 'Conv2DTranspose' using config={'name': 'conv2d_transpose', 'trainable': True, 'dtype': 'float32', 'filters': 512, 'kernel_size': [3, 3], 'strides': [2, 2], 'padding': 'same', 'data_format': 'channels_last', 'dilation_rate': [1, 1], 'groups': 1, 'activation': 'linear', 'use_bias': True, 'kernel_initializer': {'class_name': 'GlorotUniform', 'config': {'seed': None}}, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None, 'output_padding': None}.

Exception encountered: Unrecognized keyword arguments passed to Conv2DTranspose: {'groups': 1}
< /code>
Durch einige Zeit im Web konnte ich herausfinden, dass diese Modelldatei tatsächlich mit Keras 2.6.0 erstellt wurde: < /p>
import h5py
def get_keras_version(h5_file_path):
with h5py.File(h5_file_path, 'r') as f:
if 'keras_version' in f.attrs and 'backend' in f.attrs:
keras_version = f.attrs['keras_version']
backend = f.attrs['backend']
print(f"Keras version: {keras_version}")
print(f"Backend: {backend}")
else:
print("Metadata for Keras version or Backend not found in the file.")

get_keras_version('/app/my_model.h5')
Keras version: 2.6.0
Backend: tensorflow
< /code>
Ich habe also natürlich versucht, diese Version zu installieren, aber sie zeigt einen Fehler an: < /p>
# pip install keras==2.6.0
Collecting keras==2.6.0
Using cached keras-2.6.0-py2.py3-none-any.whl (1.3 MB)
Installing collected packages: keras
Attempting uninstall: keras
Found existing installation: keras 3.10.0
Uninstalling keras-3.10.0:
Successfully uninstalled keras-3.10.0
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tensorflow 2.20.0 requires keras>=3.10.0, but you have keras 2.6.0 which is incompatible.
Successfully installed keras-2.6.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
< /code>
Und ich kann TensorFlow nicht richtig laden: < /p>
import tensorflow as tf
2025-10-02 20:10:37.548923: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
2025-10-02 20:10:37.549234: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2025-10-02 20:10:37.583415: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2025-10-02 20:10:38.559104: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2025-10-02 20:10:38.559392: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.9/site-packages/tensorflow/__init__.py", line 468, in
importlib.import_module("keras.src.optimizers")
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/usr/local/lib/python3.9/site-packages/keras/__init__.py", line 25, in
from keras import models
File "/usr/local/lib/python3.9/site-packages/keras/models.py", line 20, in
from keras import metrics as metrics_module
File "/usr/local/lib/python3.9/site-packages/keras/metrics.py", line 26, in
from keras import activations
File "/usr/local/lib/python3.9/site-packages/keras/activations.py", line 20, in
from keras.layers import advanced_activations
File "/usr/local/lib/python3.9/site-packages/keras/layers/__init__.py", line 24, in
from keras.engine.input_layer import Input
File "/usr/local/lib/python3.9/site-packages/keras/engine/input_layer.py", line 21, in
from keras.engine import base_layer
File "/usr/local/lib/python3.9/site-packages/keras/engine/base_layer.py", line 40, in
from keras.mixed_precision import loss_scale_optimizer
File "/usr/local/lib/python3.9/site-packages/keras/mixed_precision/loss_scale_optimizer.py", line 18, in
from keras import optimizers
File "/usr/local/lib/python3.9/site-packages/keras/optimizers.py", line 26, in
from keras.optimizer_v2 import adadelta as adadelta_v2
File "/usr/local/lib/python3.9/site-packages/keras/optimizer_v2/adadelta.py", line 22, in
from keras.optimizer_v2 import optimizer_v2
File "/usr/local/lib/python3.9/site-packages/keras/optimizer_v2/optimizer_v2.py", line 95, in
@keras_export("keras.optimizers.Optimizer", metaclass=abc.ABCMeta)
TypeError: __init__() got an unexpected keyword argument 'metaclass'
< /code>
Daher habe ich mich entschlossen, den Tensorflow selbst auf 2.6.0 herabzustufen, aber auch hier habe ich einige Fehler getroffen: < /p>
# pip install tensorflow==2.6.0
Collecting tensorflow==2.6.0
Using cached tensorflow-2.6.0-cp39-cp39-manylinux2010_x86_64.whl (458.4 MB)
Requirement already satisfied: six~=1.15.0 in /usr/local/lib/python3.9/site-packages (from tensorflow==2.6.0) (1.15.0)
Requirement already satisfied: wrapt~=1.12.1 in /usr/local/lib/python3.9/site-packages (from tensorflow==2.6.0) (1.12.1)
Requirement already satisfied: wheel~=0.35 in /usr/local/lib/python3.9/site-packages (from tensorflow==2.6.0) (0.45.1)
Requirement already satisfied: astunparse~=1.6.3 in /usr/local/lib/python3.9/site-packages (from tensorflow==2.6.0) (1.6.3)
Requirement already satisfied: google-pasta~=0.2 in /usr/local/lib/python3.9/site-packages (from tensorflow==2.6.0) (0.2.0)
Requirement already satisfied: grpcio=1.37.0 in /usr/local/lib/python3.9/site-packages (from tensorflow==2.6.0) (1.74.0)
Requirement already satisfied: keras-preprocessing~=1.1.2 in /usr/local/lib/python3.9/site-packages (from tensorflow==2.6.0) (1.1.2)
Collecting h5py~=3.1.0
Using cached h5py-3.1.0-cp39-cp39-manylinux1_x86_64.whl (4.4 MB)
Requirement already satisfied: opt-einsum~=3.3.0 in /usr/local/lib/python3.9/site-packages (from tensorflow==2.6.0) (3.3.0)
Collecting typing-extensions~=3.7.4
Using cached typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Collecting absl-py~=0.10
Using cached absl_py-0.15.0-py3-none-any.whl (132 kB)
Requirement already satisfied: termcolor~=1.1.0 in /usr/local/lib/python3.9/site-packages (from tensorflow==2.6.0) (1.1.0)
Requirement already satisfied: protobuf>=3.9.2 in /usr/local/lib/python3.9/site-packages (from tensorflow==2.6.0) (6.32.1)
Requirement already satisfied: keras~=2.6 in /usr/local/lib/python3.9/site-packages (from tensorflow==2.6.0) (2.6.0)
Requirement already satisfied: tensorboard~=2.6 in /usr/local/lib/python3.9/site-packages (from tensorflow==2.6.0) (2.20.0)
Requirement already satisfied: gast==0.4.0 in /usr/local/lib/python3.9/site-packages (from tensorflow==2.6.0) (0.4.0)
Requirement already satisfied: clang~=5.0 in /usr/local/lib/python3.9/site-packages (from tensorflow==2.6.0) (5.0)
Collecting flatbuffers~=1.12.0
Using cached flatbuffers-1.12-py2.py3-none-any.whl (15 kB)
Requirement already satisfied: tensorflow-estimator~=2.6 in /usr/local/lib/python3.9/site-packages (from tensorflow==2.6.0) (2.15.0)
Collecting numpy~=1.19.2
Using cached numpy-1.19.5-cp39-cp39-manylinux2010_x86_64.whl (14.9 MB)
Requirement already satisfied: setuptools>=41.0.0 in /usr/local/lib/python3.9/site-packages (from tensorboard~=2.6->tensorflow==2.6.0) (80.9.0)
Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.9/site-packages (from tensorboard~=2.6->tensorflow==2.6.0) (3.9)
Requirement already satisfied: werkzeug>=1.0.1 in /usr/local/lib/python3.9/site-packages (from tensorboard~=2.6->tensorflow==2.6.0) (3.1.3)
Requirement already satisfied: pillow in /usr/local/lib/python3.9/site-packages (from tensorboard~=2.6->tensorflow==2.6.0) (11.3.0)
Requirement already satisfied: tensorboard-data-server=0.7.0 in /usr/local/lib/python3.9/site-packages (from tensorboard~=2.6->tensorflow==2.6.0) (0.7.2)
Requirement already satisfied: packaging in /usr/local/lib/python3.9/site-packages (from tensorboard~=2.6->tensorflow==2.6.0) (25.0)
Requirement already satisfied: importlib-metadata>=4.4 in /usr/local/lib/python3.9/site-packages (from markdown>=2.6.8->tensorboard~=2.6->tensorflow==2.6.0) (8.7.0)
Requirement already satisfied: MarkupSafe>=2.1.1 in /usr/local/lib/python3.9/site-packages (from werkzeug>=1.0.1->tensorboard~=2.6->tensorflow==2.6.0) (3.0.3)
Requirement already satisfied: zipp>=3.20 in /usr/local/lib/python3.9/site-packages (from importlib-metadata>=4.4->markdown>=2.6.8->tensorboard~=2.6->tensorflow==2.6.0) (3.23.0)
Installing collected packages: typing-extensions, flatbuffers, numpy, absl-py, h5py, tensorflow
Attempting uninstall: typing-extensions
Found existing installation: typing_extensions 4.15.0
Uninstalling typing_extensions-4.15.0:
Successfully uninstalled typing_extensions-4.15.0
Attempting uninstall: flatbuffers
Found existing installation: flatbuffers 25.9.23
Uninstalling flatbuffers-25.9.23:
Successfully uninstalled flatbuffers-25.9.23
Attempting uninstall: numpy
Found existing installation: numpy 2.0.2
Uninstalling numpy-2.0.2:
Successfully uninstalled numpy-2.0.2
Attempting uninstall: absl-py
Found existing installation: absl-py 2.3.1
Uninstalling absl-py-2.3.1:
Successfully uninstalled absl-py-2.3.1
Attempting uninstall: h5py
Found existing installation: h5py 3.14.0
Uninstalling h5py-3.14.0:
Successfully uninstalled h5py-3.14.0
Attempting uninstall: tensorflow
Found existing installation: tensorflow 2.20.0
Uninstalling tensorflow-2.20.0:
Successfully uninstalled tensorflow-2.20.0
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
optree 0.17.0 requires typing-extensions>=4.6.0, but you have typing-extensions 3.7.4.3 which is incompatible.
ml-dtypes 0.5.3 requires numpy>=1.21, but you have numpy 1.19.5 which is incompatible.
Successfully installed absl-py-0.15.0 flatbuffers-1.12 h5py-3.1.0 numpy-1.19.5 tensorflow-2.6.0 typing-extensions-3.7.4.3
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
< /code>
Wie auch immer, ich habe es versuchen: < /p>
import tensorflow as tf
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.9/site-packages/tensorflow/__init__.py", line 41, in
from tensorflow.python.tools import module_util as _module_util
File "/usr/local/lib/python3.9/site-packages/tensorflow/python/__init__.py", line 40, in
from tensorflow.python.eager import context
File "/usr/local/lib/python3.9/site-packages/tensorflow/python/eager/context.py", line 32, in
from tensorflow.core.framework import function_pb2
File "/usr/local/lib/python3.9/site-packages/tensorflow/core/framework/function_pb2.py", line 16, in
from tensorflow.core.framework import attr_value_pb2 as tensorflow_dot_core_dot_framework_dot_attr__value__pb2
File "/usr/local/lib/python3.9/site-packages/tensorflow/core/framework/attr_value_pb2.py", line 16, in
from tensorflow.core.framework import tensor_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__pb2
File "/usr/local/lib/python3.9/site-packages/tensorflow/core/framework/tensor_pb2.py", line 16, in
from tensorflow.core.framework import resource_handle_pb2 as tensorflow_dot_core_dot_framework_dot_resource__handle__pb2
File "/usr/local/lib/python3.9/site-packages/tensorflow/core/framework/resource_handle_pb2.py", line 16, in
from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__shape__pb2
File "/usr/local/lib/python3.9/site-packages/tensorflow/core/framework/tensor_shape_pb2.py", line 36, in
_descriptor.FieldDescriptor(
File "/usr/local/lib/python3.9/site-packages/google/protobuf/descriptor.py", line 675, in __new__
_message.Message._CheckCalledFromGeneratedFile()
TypeError: Descriptors cannot be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
1. Downgrade the protobuf package to 3.20.x or lower.
2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).

More information: https://developers.google.com/protocol- ... on-updates
< /code>
Ich muss Protobuf herabstufen: < /p>
# pip install --upgrade "protobuf

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post