You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决TensorFlow表情识别模型保存h5时的h5py依赖错误

解决Keras保存h5权重时提示h5py未安装的问题

问题描述

编写了基于TensorFlow的面部表情识别模型训练代码,最初训练数小时后h5格式权重文件未保存,遂设置10秒训练时长测试保存功能,但每次触发错误:

ImportError: save_weights requires h5py when saving in hdf5, but h5py is not available. Try installing h5py package.

训练代码如下:

# Import required packages
import cv2
import time
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Flatten
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from tqdm import tqdm

# Initialize image data generator with rescaling
train_data_gen = ImageDataGenerator(rescale=1./255)
validation_data_gen = ImageDataGenerator(rescale=1./255)

# Preprocess all test images
train_generator = train_data_gen.flow_from_directory(
        r'A:\OneDrive - The British University in Egypt\FER RP\Datasets\Fer2013\train',
        target_size=(48, 48),
        batch_size=64,
        color_mode="grayscale",
        class_mode='categorical')

# Preprocess all train images
validation_generator = validation_data_gen.flow_from_directory(
        r'A:\OneDrive - The British University in Egypt\FER RP\Datasets\Fer2013\test',
        target_size=(48, 48),
        batch_size=64,
        color_mode="grayscale",
        class_mode='categorical')

# Create model structure
emotion_model = Sequential()

emotion_model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(48, 48, 1)))
emotion_model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Dropout(0.25))

emotion_model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Dropout(0.25))

emotion_model.add(Flatten())
emotion_model.add(Dense(1024, activation='relu'))
emotion_model.add(Dropout(0.5))
emotion_model.add(Dense(7, activation='softmax'))

cv2.ocl.setUseOpenCL(False)

emotion_model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.0001), metrics=['accuracy'])

# Train the neural network/model with tqdm progress bar
max_training_time = 10# in seconds (adjust as needed)
training_start_time = time.time()

for epoch in range(50):
    print(f"Epoch {epoch + 1}/{50}")
    
    for _ in tqdm(range(28709 // 64)):
        emotion_model.train_on_batch(*next(train_generator))
        
        # Check elapsed time and interrupt if it exceeds the maximum training time
        if time.time() - training_start_time > max_training_time:
            break
    
    emotion_model_info = emotion_model.evaluate_generator(
        validation_generator, steps=7178 // 64)
    print(f"Validation Accuracy: {emotion_model_info[1] * 100:.2f}%")
    
    # Save model structure in json file
    model_json = emotion_model.to_json()
    with open("emotion_model.json", "w") as json_file:
        json_file.write(model_json)

    # Save trained model weight in .h5 file
    emotion_model.save_weights('emotion_model.h5')

    # Reset training start time for the next epoch
    training_start_time = time.time()

已通过Anaconda安装h5py库,且多次重装,但问题仍未解决。

解决方案

  • 检查当前Python环境的h5py状态
    打开Anaconda Prompt,激活你的训练环境(如果使用虚拟环境),输入python进入交互模式,执行:

    import h5py
    print(h5py.__version__)
    

    若报错,说明当前环境未正确安装h5py;若能输出版本号,说明环境没问题,需检查IDE的解释器配置。

  • 确认IDE使用的是Anaconda环境

    • VS Code:按Ctrl+Shift+P打开命令面板,输入Python: Select Interpreter,选择安装了h5py的Anaconda环境。
    • PyCharm:进入File > Settings > Project: xxx > Python Interpreter,选择对应Anaconda环境。
  • 重新在目标环境安装h5py
    激活环境后执行:

    conda install h5py
    

    若conda安装失败,尝试用pip:

    pip install h5py
    
  • 检查版本兼容性
    不同版本的TensorFlow/Keras对h5py版本有要求,执行以下命令查看版本:

    import tensorflow as tf
    print(tf.__version__)
    import keras
    print(keras.__version__)
    

    若版本不兼容,执行升级命令:

    conda update tensorflow keras h5py
    
  • 临时替换保存格式
    若上述方法无效,可改用TensorFlow原生SavedModel格式保存权重(无需h5py):

    # 保存
    emotion_model.save_weights('emotion_model_weights')
    # 加载
    emotion_model.load_weights('emotion_model_weights')
    

内容的提问来源于stack exchange,提问作者omarhatim79

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 21:54:55