TensorFlow-GPU环境下加载.h5预训练模型失败求助
Hey there, let's break down how to fix your .h5 model loading problem. I've dealt with similar version mismatch and compatibility headaches before, so here are actionable steps to try:
1. Fix Keras-TensorFlow Compatibility Conflicts
Your setup uses Keras 2.4.3 alongside TensorFlow-GPU 2.3.1—this is likely the root issue. TensorFlow 2.x bundles its own tensorflow.keras module, and using a standalone Keras version can create conflicts (since standalone Keras 2.4.3 is designed for newer TensorFlow versions like 2.4+).
- Solution: Uninstall the standalone Keras package and rely solely on TensorFlow's built-in Keras:
Then, make sure all your imports usepip uninstall keras -ytensorflow.kerasinstead of plainkeras(e.g.,from tensorflow.keras.models import load_model).
2. Handle Model Version Mismatches
If the .h5 model was saved with a newer version of TensorFlow/Keras than your 2.3.1 environment, loading it directly will fail. Here's how to work around this:
- Option 1: Convert the model to SavedModel format first (using the environment it was saved in):
Then load the SavedModel in your 2.3.1 environment:from tensorflow.keras.models import load_model model = load_model('original_model.h5') model.save('saved_model_dir', save_format='tf')from tensorflow.keras.models import load_model model = load_model('saved_model_dir') - Option 2: Check for custom layers. If the model uses custom layers (e.g., custom attention layers, custom loss functions), you need to pass them to
custom_objectswhen loading:# Replace CustomLayer with your actual custom layer class model = tf.keras.models.load_model('your_model.h5', custom_objects={'CustomLayer': CustomLayer})
3. Verify File Integrity & Path
Sometimes the issue is simpler than you think:
- Check file corruption: Use
h5pyto test if the file can be read properly:
If this throws an error, re-download the model file to fix corruption.import h5py try: with h5py.File('your_model.h5', 'r') as f: print("File is readable. Keys:", list(f.keys())) except Exception as e: print(f"File is corrupted: {e}") - Double-check file paths: On Windows, make sure your path uses forward slashes (
C:/models/model.h5) or raw strings (r'C:\models\model.h5') to avoid escape character issues. Also, ensure your IDE has read permissions for the file location.
4. Load Weights Separately (If All Else Fails)
If load_model still won't work, try reconstructing the model architecture first, then loading just the weights:
- Get the model architecture code (either from the source of the pre-trained model, or by using
model.to_json()if you can load it in a compatible environment). - Recreate the model in your 2.3.1 environment:
from tensorflow.keras.models import model_from_json with open('model_architecture.json', 'r') as f: model = model_from_json(f.read()) - Load the weights:
model.load_weights('your_model.h5')
Give these steps a try—start with the compatibility fix first, since that's the most common issue with your version combo. Let me know if you hit any specific errors while testing!
内容的提问来源于stack exchange,提问作者Friedrich_Cheng94

