Flask加载Keras(TensorFlow).h5模型触发ValueError错误求助
加载本地Keras .h5模型时出现ValueError: bad marshal data错误
我是一名Web前端开发者,缺乏Python开发经验。在运行Flask应用加载本地.h5格式的Keras(TensorFlow)模型时,出现如下错误:
python app.py 2023-09-03 22:09:58.412966: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: SSE SSE2 SSE3 SSE4.1 SSE4.2 AVX, in other operations, rebuild TensorFlow with the appropriate compiler flags. Traceback (most recent call last): File "C:\Users\Osama\python enviroment\app.py", line 95, in <module> model = load_local_model() ^^^^^^^^^^^^^^^^^^ File "C:\Users\Osama\python enviroment\app.py", line 29, in load_local_model model = load_model(model_path) ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Osama\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\saving\saving_api.py", line 238, in load_model return legacy_sm_saving_lib.load_model( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Osama\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\utils\traceback_utils.py", line 70, in error_handler raise e.with_traceback(filtered_tb) from None File "C:\Users\Osama\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\utils\generic_utils.py", line 102, in func_load code = marshal.loads(raw_code) ^^^^^^^^^^^^^^^^^^^^^^^ ValueError: bad marshal data (unknown type code)
以下是完整代码:
import os import uuid import requests from PIL import Image import tensorflow as tf from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing.image import load_img, img_to_array from flask import Flask, request, jsonify from werkzeug.utils import secure_filename from dotenv import load_dotenv import json load_dotenv() app = Flask(__name__) BASE_DIR = os.path.dirname(os.path.abspath(__file__)) model = None ALLOWED_EXT = {'jpg', 'jpeg', 'png', 'jfif'} classes = ['Meningioma', 'Glioma', 'Pituitary'] def allowed_file(filename: str) -> bool: return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXT def load_local_model() -> tf.keras.Model: model_path = os.path.join(BASE_DIR, 'model.h5') model = load_model(model_path) return model def predict(filename: str, model) -> tuple[list[str], list[float]]: img = load_img(filename, target_size=(256, 256)) img = img_to_array(img) img = img.reshape(1, 256, 256, 3) img = img.astype('float32') img = img / 255.0 result = model.predict(img) dict_result = {} for i in range(len(classes)): dict_result[result[0][i]] = classes[i] res = result[0] res.sort() res = res[::-1] prob = res[:3] prob_result = [] class_result = [] for i in range(len(prob)): prob_result.append(round(prob[i] * 100, 3)) class_result.append(dict_result[prob[i]]) return class_result, prob_result @app.route('/predict', methods=['POST']) def predict_image(): if 'file' not in request.files: return jsonify({'error': 'No file found in the request'}) file = request.files['file'] if file.filename == '': return jsonify({'error': 'No file selected'}) if file and allowed_file(file.filename): filename = secure_filename(file.filename) unique_filename = str(uuid.uuid4()) img_path = os.path.join( BASE_DIR, 'static/images', unique_filename + '.jpg') file.save(img_path) class_result, prob_result = predict(img_path, model) predictions = { "class1": class_result[0], "class2": class_result[1], "class3": class_result[1], "prob1": prob_result[0], "prob2": prob_result[1], "prob3": prob_result[2], } return jsonify(predictions) return jsonify({'message': 'serve is working'}) else: return jsonify({'error': 'Invalid file format'}) if __name__ == "__main__": # Load model locally model = load_local_model() print("Model loaded.") app.run(host='0.0.0.0', port=int(os.getenv('PORT', 5000)), debug=False)
已尝试过ChatGPT、Bing和StackOverflow的方案,但仍未解决问题,希望得到帮助。
内容的提问来源于stack exchange,提问作者Osama Hima
相关产品推荐
相关产品推荐

